Skip to content

Commit 5986751

Browse files
Merge pull request #55 from akamai/release/1.2.0
Release/1.2.0
2 parents 62cd91e + e07b6e8 commit 5986751

File tree

9 files changed

+204
-89
lines changed

9 files changed

+204
-89
lines changed

HISTORY.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
History
44
-------
55

6+
1.2.0 (2021-08-10)
7+
++++++++++++++++++
8+
9+
* Bug fixes
10+
- `GH#48 <https://github.com/akamai/AkamaiOPEN-edgegrid-python/issues/48>`_ and `GH#50 <https://github.com/akamai/AkamaiOPEN-edgegrid-python/issues/50>`_ issues: recognize the `~` tilde character as home directory alias
11+
- `GH#36 <https://github.com/akamai/AkamaiOPEN-edgegrid-python/issues/36>`_, `GH#44 <https://github.com/akamai/AkamaiOPEN-edgegrid-python/issues/44>`_ and `GH#53 <https://github.com/akamai/AkamaiOPEN-edgegrid-python/issues/53>`_ issues: add missing test resource files to PyPI package
12+
- `GH#41 <https://github.com/akamai/AkamaiOPEN-edgegrid-python/issues/41>`_: require PyOpenSSL >= v19.0.0 to avoid old OS packages
13+
14+
* Improvements
15+
- better Python 2 and Python 3 documentation and related setup.py tags
16+
617
1.1.0 (2017-09-11)
718
++++++++++++++++++
819

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include README.rst LICENSE requirements.txt akamai/edgegrid/test/testdata.json
1+
include README.rst LICENSE requirements.txt akamai/edgegrid/test/testdata.json akamai/edgegrid/test/sample_edgerc akamai/edgegrid/test/edgerc_that_doesnt_parse

README.rst

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ visit the `Akamai {OPEN} Developer Community`_.
99
1010
>>> import requests
1111
>>> from akamai.edgegrid import EdgeGridAuth
12-
>>> from urlparse import urljoin
12+
>>> from urllib.parse import urljoin
1313
>>> baseurl = 'https://akaa-WWWWWWWWWWWW.luna.akamaiapis.net/'
1414
>>> s = requests.Session()
1515
>>> s.auth = EdgeGridAuth(
@@ -31,7 +31,7 @@ Alternatively, your program can read the credentials from an .edgerc file.
3131
3232
>>> import requests
3333
>>> from akamai.edgegrid import EdgeGridAuth, EdgeRc
34-
>>> from urlparse import urljoin
34+
>>> from urllib.parse import urljoin
3535
3636
>>> edgerc = EdgeRc('~/.edgerc')
3737
>>> section = 'default'
@@ -47,34 +47,50 @@ Alternatively, your program can read the credentials from an .edgerc file.
4747
Oakbrook, IL, United States
4848
...
4949
50+
If you intend to run the above examples with Python 2.7, remember that urljoin is contained in a different package:
51+
52+
.. code-block:: pycon
53+
54+
>>> from urlparse import urljoin
55+
5056
.. _`requests`: http://docs.python-requests.org
5157
.. _`Akamai {OPEN} Edgegrid authentication`: https://developer.akamai.com/introduction/Client_Auth.html
5258
.. _`Akamai {OPEN} Developer Community`: https://developer.akamai.com
5359

5460
Installation
5561
------------
5662

57-
Pre-requisite
58-
-------------
59-
If you are installing on a Linux based distribution, you will need to install the developer libraries for python, ssl and a ffi. On Ubuntu based systems, you will need to do the following:
63+
**Prerequisite**
64+
65+
For Linux-based distribution, install the developer libraries for Python, SSL and FFI. For example, on Debian-based systems, run:
66+
67+
.. code-block:: bash
68+
69+
$ sudo apt-get install ibssl-dev libffi-dev python-dev
70+
71+
**To install from pip**
72+
73+
If you are planning to use Python 2, upgrade pip to version 20.3.4, which is the most recent and last version that supports Python 2. Run:"
6074

6175
.. code-block:: bash
62-
$ sudo apt-get install ibssl-dev libffi-dev python-dev
6376
77+
$ pip install --upgrade 'pip<21.0'
6478
65-
To install from pip:
79+
To proceed with the installation:
6680

6781
.. code-block:: bash
6882
6983
$ pip install edgegrid-python
7084
71-
To install from sources:
85+
**To install from sources**
7286

7387
.. code-block:: bash
7488
7589
$ python setup.py install
7690
77-
To run tests:
91+
**To run tests**
92+
93+
Both Python 2 and Python 3 are supported. This example uses Python 2.7. Run:
7894

7995
.. code-block:: bash
8096
@@ -83,6 +99,23 @@ To run tests:
8399
$ pip install -r requirements.txt
84100
$ python -m unittest discover
85101
102+
For Python 3.3 or newer, replace the `virtualenv` module with `venv`. Run:
103+
104+
.. code-block:: bash
105+
106+
$ python3 -m venv venv
107+
$ . venv/bin/activate
108+
$ pip install -r requirements.txt
109+
$ python -m unittest discover
110+
111+
Creating your own .edgerc
112+
----------
113+
114+
#. Copy the `akamai/edgegrid/test/sample_edgerc` file to your home directory and rename as `.edgerc`.
115+
#. Edit the copied file and provide your own credentials. For more information on creating an `.edgerc` file, see `Get started with APIs`_.
116+
117+
.. _`Get started with APIs`: https://developer.akamai.com/api/getting-started#edgercfile
118+
86119
Contribute
87120
----------
88121

@@ -100,7 +133,7 @@ Jonathan Landis
100133
License
101134
-------
102135

103-
Copyright 2015 Akamai Technologies, Inc. All rights reserved.
136+
Copyright 2021 Akamai Technologies, Inc. All rights reserved.
104137

105138
Licensed under the Apache License, Version 2.0 (the "License");
106139
you may not use this file except in compliance with the License.

akamai/edgegrid/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
akamai.edgegrid
33
~~~~~~~~~~~~~~~
44
5-
This library provides an authentication handler for Requests that implements the
6-
Akamai {OPEN} EdgeGrid client authentication protocol as
5+
This library provides an authentication handler for Requests that implements the
6+
Akamai {OPEN} EdgeGrid client authentication protocol as
77
specified by https://developer.akamai.com/introduction/Client_Auth.html.
88
For more information visit https://developer.akamai.com.
99
@@ -32,16 +32,17 @@
3232

3333
from .edgegrid import EdgeGridAuth
3434
from .edgerc import EdgeRc
35-
__all__=['EdgeGridAuth', 'EdgeRc']
35+
__all__ = ['EdgeGridAuth', 'EdgeRc']
3636

3737
__title__ = 'edgegrid-python'
3838
__version__ = '1.1'
3939
__author__ = 'Jonathan Landis <[email protected]>'
40+
__maintainer__ = 'Akamai Developer Experience team <[email protected]>'
4041
__license__ = 'Apache 2.0'
41-
__copyright__ = 'Copyright 2014 Akamai Technologies'
42+
__copyright__ = 'Copyright 2021 Akamai Technologies'
4243

43-
# Copyright 2014 Akamai Technologies, Inc. All Rights Reserved
44-
#
44+
# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved
45+
#
4546
# Licensed under the Apache License, Version 2.0 (the "License");
4647
# you may not use this file except in compliance with the License.
4748
# You may obtain a copy of the License at

akamai/edgegrid/edgegrid.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# EdgeGrid requests Auth handler
44
#
55
# Original author: Jonathan Landis <[email protected]>
6+
# Package maintainer: Akamai Developer Experience team <[email protected]>
67
#
78
# For more information visit https://developer.akamai.com
89

9-
# Copyright 2014 Akamai Technologies, Inc. All Rights Reserved
10-
#
10+
# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved
11+
#
1112
# Licensed under the Apache License, Version 2.0 (the "License");
1213
# you may not use this file except in compliance with the License.
1314
# You may obtain a copy of the License at
@@ -20,7 +21,6 @@
2021
# See the License for the specific language governing permissions and
2122
# limitations under the License.
2223

23-
import requests
2424
import logging
2525
import uuid
2626
import hashlib
@@ -34,33 +34,41 @@
3434

3535
if sys.version_info[0] >= 3:
3636
# python3
37-
from urllib.parse import urlparse, parse_qsl, urlunparse
37+
from urllib.parse import urlparse
3838
else:
3939
# python2.7
40-
from urlparse import urlparse, parse_qsl, urlunparse
40+
from urlparse import urlparse
4141
import urllib3.contrib.pyopenssl
4242
urllib3.contrib.pyopenssl.inject_into_urllib3()
4343

4444
logger = logging.getLogger(__name__)
4545

46-
__all__=['EdgeGridAuth']
46+
__all__ = ['EdgeGridAuth']
47+
4748

4849
def eg_timestamp():
4950
return strftime('%Y%m%dT%H:%M:%S+0000', gmtime())
5051

52+
5153
def new_nonce():
5254
return uuid.uuid4()
5355

56+
5457
def base64_hmac_sha256(data, key):
5558
return base64.b64encode(
56-
hmac.new(key.encode('utf8'), data.encode('utf8'), hashlib.sha256).digest()
59+
hmac.new(
60+
key.encode('utf8'),
61+
data.encode('utf8'),
62+
hashlib.sha256).digest()
5763
).decode('utf8')
5864

65+
5966
def base64_sha256(data):
6067
if isinstance(data, str):
6168
data = data.encode('utf8')
6269
return base64.b64encode(hashlib.sha256(data).digest()).decode('utf8')
6370

71+
6472
class EdgeGridAuth(AuthBase):
6573
"""A Requests authentication handler that provides Akamai {OPEN} EdgeGrid support.
6674
@@ -76,15 +84,15 @@ class EdgeGridAuth(AuthBase):
7684
7785
"""
7886

79-
def __init__(self, client_token, client_secret, access_token,
87+
def __init__(self, client_token, client_secret, access_token,
8088
headers_to_sign=None, max_body=131072):
81-
"""Initialize authentication using the given parameters from the Luna Manage APIs
89+
"""Initialize authentication using the given parameters from the Akamai OPEN APIs
8290
Interface:
8391
8492
:param client_token: Client token provided by "Credentials" ui
8593
:param client_secret: Client secret provided by "Credentials" ui
8694
:param access_token: Access token provided by "Authorizations" ui
87-
:param headers_to_sign: An ordered list header names that will be included in
95+
:param headers_to_sign: An ordered list header names that will be included in
8896
the signature. This will be provided by specific APIs. (default [])
8997
:param max_body: Maximum content body size for POST requests. This will be provided by
9098
specific APIs. (default 131072)
@@ -94,7 +102,7 @@ def __init__(self, client_token, client_secret, access_token,
94102
self.client_secret = client_secret
95103
self.access_token = access_token
96104
if headers_to_sign:
97-
self.headers_to_sign = [ h.lower() for h in headers_to_sign ]
105+
self.headers_to_sign = [h.lower() for h in headers_to_sign]
98106
else:
99107
self.headers_to_sign = []
100108
self.max_body = max_body
@@ -103,15 +111,15 @@ def __init__(self, client_token, client_secret, access_token,
103111

104112
@staticmethod
105113
def from_edgerc(rcinput, section='default'):
106-
"""Returns an EdgeGridAuth object from the configuration from the given section of the
114+
"""Returns an EdgeGridAuth object from the configuration from the given section of the
107115
given edgerc file.
108116
109-
:param filename: path to the edgerc file
110-
:param section: the section to use (this is the [bracketed] part of the edgerc,
117+
:param rcinput: EdgeRc instance or path to the edgerc file
118+
:param section: the section to use (this is the [bracketed] part of the edgerc,
111119
default is 'default')
112120
113121
"""
114-
from .edgerc import EdgeRc
122+
from .edgerc import EdgeRc
115123
if isinstance(rcinput, EdgeRc):
116124
rc = rcinput
117125
else:
@@ -149,11 +157,13 @@ def make_content_hash(self, r):
149157
logger.debug("signing content: %s", prepared_body)
150158
if len(prepared_body) > self.max_body:
151159
logger.debug(
152-
"data length %d is larger than maximum %d",
160+
"data length %d is larger than maximum %d",
153161
len(prepared_body), self.max_body
154162
)
155163
prepared_body = prepared_body[0:self.max_body]
156-
logger.debug("data truncated to %d for computing the hash", len(prepared_body))
164+
logger.debug(
165+
"data truncated to %d for computing the hash",
166+
len(prepared_body))
157167

158168
content_hash = base64_sha256(prepared_body)
159169

@@ -173,7 +183,8 @@ def get_header_versions(self, header=None):
173183
akamai_cli_command = os.getenv('AKAMAI_CLI_COMMAND')
174184
akamai_cli_command_version = os.getenv('AKAMAI_CLI_COMMAND_VERSION')
175185
if akamai_cli_command and akamai_cli_command_version:
176-
version_header += " AkamaiCLI-" + akamai_cli_command + "/" + akamai_cli_command_version
186+
version_header += " AkamaiCLI-" + akamai_cli_command + \
187+
"/" + akamai_cli_command_version
177188

178189
if version_header != '':
179190
if 'User-Agent' not in header:
@@ -186,7 +197,7 @@ def get_header_versions(self, header=None):
186197
def make_data_to_sign(self, r, auth_header):
187198
parsed_url = urlparse(r.url)
188199

189-
if (r.headers.get('Host', False)):
200+
if r.headers.get('Host', False):
190201
netloc = r.headers['Host']
191202
else:
192203
netloc = parsed_url.netloc
@@ -197,8 +208,10 @@ def make_data_to_sign(self, r, auth_header):
197208
r.method,
198209
parsed_url.scheme,
199210
netloc,
200-
# Note: relative URL constraints are handled by requests when it sets up 'r'
201-
parsed_url.path + ('?' + parsed_url.query if parsed_url.query else ""),
211+
# Note: relative URL constraints are handled by requests when it
212+
# sets up 'r'
213+
parsed_url.path + \
214+
('?' + parsed_url.query if parsed_url.query else ""),
202215
self.canonicalize_headers(r),
203216
self.make_content_hash(r),
204217
auth_header
@@ -208,7 +221,7 @@ def make_data_to_sign(self, r, auth_header):
208221

209222
def sign_request(self, r, timestamp, auth_header):
210223
return base64_hmac_sha256(
211-
self.make_data_to_sign(r, auth_header),
224+
self.make_data_to_sign(r, auth_header),
212225
self.make_signing_key(timestamp)
213226
)
214227

@@ -219,7 +232,8 @@ def make_auth_header(self, r, timestamp, nonce):
219232
('timestamp', timestamp),
220233
('nonce', nonce),
221234
]
222-
auth_header = "EG1-HMAC-SHA256 " + ';'.join([ "%s=%s" % kvp for kvp in kvps ]) + ';'
235+
auth_header = "EG1-HMAC-SHA256 " + \
236+
';'.join(["%s=%s" % kvp for kvp in kvps]) + ';'
223237
logger.debug('unsigned authorization header: %s', auth_header)
224238

225239
signed_auth_header = auth_header + \

akamai/edgegrid/edgerc.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#
33
# support for .edgerc file format
44
#
5-
# Copyright 2014 Akamai Technologies, Inc. All Rights Reserved
6-
#
5+
# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved
6+
#
77
# Licensed under the Apache License, Version 2.0 (the "License");
88
# you may not use this file except in compliance with the License.
99
# You may obtain a copy of the License at
@@ -18,6 +18,7 @@
1818

1919
import logging
2020
import sys
21+
from os.path import expanduser
2122

2223
if sys.version_info[0] >= 3:
2324
# python3
@@ -29,12 +30,19 @@
2930

3031
logger = logging.getLogger(__name__)
3132

33+
3234
class EdgeRc(ConfigParser):
3335
def __init__(self, filename):
34-
ConfigParser.__init__(self, {'client_token': '', 'client_secret':'', 'host':'', 'access_token':'','max_body': '131072', 'headers_to_sign': 'None'})
36+
ConfigParser.__init__(self,
37+
{'client_token': '',
38+
'client_secret': '',
39+
'host': '',
40+
'access_token': '',
41+
'max_body': '131072',
42+
'headers_to_sign': 'None'})
3543
logger.debug("loading edgerc from %s", filename)
3644

37-
self.read(filename)
45+
self.read(expanduser(filename))
3846

3947
logger.debug("successfully loaded edgerc")
4048

0 commit comments

Comments
 (0)