Skip to content

Commit

Permalink
Alpha7 (#142)
Browse files Browse the repository at this point in the history
* Bump version

* Update HISTORY.md

* Update credentials.json parent dir name

* Update User-Agent package name

* Add developer_token_provider support

* Add python2 support for urlparse
  • Loading branch information
sserrata authored Mar 23, 2020
1 parent 8a39af3 commit 2aa5c5d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# History

## 2.0.0-alpha7 (2020-03-23)

- Updated SDK name in HTTPClient default headers
- Added support for specifying Developer Token provider using envar or kwarg
- Updated `credentials.json` parent dir name

## 2.0.0-alpha6 (2020-03-04)

- Changed `iter_job_results()` method to allow overriding `enforce_json`
Expand Down
2 changes: 1 addition & 1 deletion pan_cortex_data_lake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Main package for cortex."""

__author__ = "Palo Alto Networks"
__version__ = "2.0.0-a6"
__version__ = "2.0.0-a7"

from .exceptions import ( # noqa: F401
CortexError,
Expand Down
5 changes: 4 additions & 1 deletion pan_cortex_data_lake/adapters/tinydb_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def init_store(self):
dbfile = os.getenv("PAN_CREDENTIALS_DBFILE")
else:
dbfile = os.path.join(
os.path.expanduser("~"), ".config", "pancloud", "credentials.json"
os.path.expanduser("~"),
".config",
"pan_cortex_data_lake",
"credentials.json",
)
if not os.path.exists(os.path.dirname(dbfile)):
try:
Expand Down
36 changes: 30 additions & 6 deletions pan_cortex_data_lake/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from collections import namedtuple
from threading import Lock

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

from requests import Request
from time import time
from base64 import b64decode
Expand All @@ -20,7 +25,7 @@
# Constants
API_BASE_URL = "https://api.paloaltonetworks.com"
AUTH_BASE_URL = "https://identity.paloaltonetworks.com/as/authorization.oauth2"
DEVELOPER_TOKEN_URL = "https://app.apiexplorer.rocks"
DEVELOPER_TOKEN_PROVIDER = "https://app.apiexplorer.rocks/request_token"

ReadOnlyCredentials = namedtuple(
"ReadOnlyCredentials",
Expand All @@ -39,7 +44,7 @@ def __init__(
client_id=None,
client_secret=None,
developer_token=None,
developer_token_url=None,
developer_token_provider=None,
instance_id=None,
profile=None,
redirect_uri=None,
Expand Down Expand Up @@ -71,7 +76,7 @@ def __init__(
client_id (str): OAuth2 client ID. Defaults to ``None``.
client_secret (str): OAuth2 client secret. Defaults to ``None``.
developer_token (str): Developer Token. Defaults to ``None``.
developer_token_url (str): Developer Token URL. Defaults to ``None``.
developer_token_provider (str): Developer Token Provider URL. Defaults to ``None``.
instance_id (str): Instance ID. Defaults to ``None``.
profile (str): Credentials profile. Defaults to ``'default'``.
redirect_uri (str): Redirect URI. Defaults to ``None``.
Expand All @@ -90,7 +95,7 @@ def __init__(
self.client_id_ = client_id
self.client_secret_ = client_secret
self.developer_token_ = developer_token
self.developer_token_url = developer_token_url or DEVELOPER_TOKEN_URL
self.developer_token_provider_ = developer_token_provider
self.instance_id = instance_id
self.jwt_exp_ = None
self.profile = profile or "default"
Expand Down Expand Up @@ -178,6 +183,20 @@ def developer_token(self, developer_token):
"""Set developer token."""
self.developer_token_ = developer_token

@property
def developer_token_provider(self):
"""Get developer token provider."""
return (
self.developer_token_provider_
or os.getenv("PAN_DEVELOPER_TOKEN_PROVIDER")
or DEVELOPER_TOKEN_PROVIDER
)

@developer_token_provider.setter
def developer_token_provider(self, developer_token_provider):
"""Set developer token provider."""
self.developer_token_provider_ = developer_token_provider

@property
def jwt_exp(self):
"""Get JWT exp."""
Expand Down Expand Up @@ -459,10 +478,15 @@ def refresh(self, access_token=None, **kwargs):
with self.token_lock:
if access_token == self.access_token or access_token is None:
if self.developer_token is not None:
parsed_provider = urlparse(self.developer_token_provider)
url = "{}://{}".format(
parsed_provider.scheme, parsed_provider.netloc
)
endpoint = parsed_provider.path
r = self._httpclient.request(
method="POST",
url=self.developer_token_url,
endpoint="/request_token",
url=url,
endpoint=endpoint,
headers={
"Authorization": "Bearer {}".format(
self.developer_token
Expand Down
2 changes: 1 addition & 1 deletion pan_cortex_data_lake/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _default_headers(self):
self.session.headers.update(
{
"Accept": "application/json",
"User-Agent": "%s/%s" % ("cortex-sdk-python", __version__),
"User-Agent": "%s/%s" % ("cortex-data-lake-python", __version__),
}
)
logger.debug("Default headers applied: %r" % self.session.headers)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 2.0.0a6
current_version = 2.0.0a7
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setup(
name="pan-cortex-data-lake",
version="2.0.0-a6",
version="2.0.0-a7",
description="Python idiomatic SDK for Cortex™ Data Lake.",
long_description=readme + "\n\n" + history,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 2aa5c5d

Please sign in to comment.