Skip to content

Commit f66bc94

Browse files
dpeacheyDan Peacheyroaldnefs
authored
fix: add argument to disable IP whitelisting (#47) (#47)
Add `global_key` argument to TransIP client `__init()__` method to allow disabling of IP whitelisting for access token Fixes #46 Co-authored-by: Dan Peachey <[email protected]> Co-authored-by: Roald Nefs <[email protected]>
1 parent 9ddd2d9 commit f66bc94

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes in **python-transip** are documented below.
66
- The option to replace all existing nameservers of a single domain at once from the `transip.v6.objects.Domain.nameservers` service.
77
- The option to list all colocations from the `transip.TransIP.colocations` service ([#24](https://github.com/roaldnefs/python-transip/issues/24)).
88
- The option to retrieve a single colocation by name from the `transip.TransIP.colocations` service ([#24](https://github.com/roaldnefs/python-transip/issues/24)).
9+
- The option to allow the access token to be used from all IP-addresses instead of only the whitelisted ones ([#46](https://github.com/roaldnefs/python-transip/issues/46)).
910

1011
## [0.4.0] (2021-01-24)
1112
### Added

transip/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class TransIP:
5454
TransIP API
5555
private_key_file (str): Path to the private key for accessing the
5656
TransIP API
57+
global_key (bool): Allow the access token to be used from all
58+
IP-addresses instead of only the whitelisted ones.
5759
"""
5860

5961
def __init__(
@@ -63,6 +65,7 @@ def __init__(
6365
access_token: Optional[str] = None,
6466
private_key: Optional[str] = None,
6567
private_key_file: Optional[str] = None,
68+
global_key: bool = False,
6669
) -> None:
6770
self._api_version: str = api_version
6871
self._url: str = f"https://api.transip.nl/v{api_version}"
@@ -80,6 +83,7 @@ def __init__(
8083
self._access_token: Optional[str] = access_token
8184
self._private_key: Optional[str] = private_key
8285
self._private_key_file: Optional[str] = private_key_file
86+
self._global_key: Optional[bool] = global_key
8387
self._set_auth_info()
8488

8589
# Dynamically import the services for the specified API version
@@ -154,9 +158,7 @@ def _request_access_token(self) -> str:
154158
# TODO(roaldnefs): Allow a custom label to be specified when
155159
# generating a new access token
156160
# "label": "python-transip",
157-
# TODO(roaldnefs): Allow the access token to only be use from
158-
# whitelisted IP-addresses
159-
"global_key": False
161+
"global_key": self._global_key
160162
}
161163

162164
headers: Dict[str, str] = self.headers.copy()

transip/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ def load_rsa_private_key(key: Union[bytes, str]) -> RSAPrivateKey:
4141
if isinstance(key, str):
4242
key = key.encode()
4343

44-
return serialization.load_pem_private_key(
44+
private_key = serialization.load_pem_private_key(
4545
key, password=None, backend=default_backend()
4646
)
4747

48+
# Check type of the loaded private key
49+
if not isinstance(private_key, RSAPrivateKey):
50+
raise ValueError('The supplied key must be a RSA private key')
51+
52+
return private_key
53+
4854

4955
def generate_message_signature(
5056
message: Union[str, bytes],
@@ -63,11 +69,11 @@ def generate_message_signature(
6369
if isinstance(message, str):
6470
message = message.encode()
6571

66-
# Convert the private key content to a RSAPrivateKey object
72+
# Convert the private key content to an asymmetric private key type
6773
if isinstance(private_key, str):
6874
private_key = load_rsa_private_key(private_key)
6975

70-
# Sign the message using the RSAPrivateKey object
76+
# Sign the message using the private key
7177
signature: bytes = private_key.sign(message, PKCS1v15(), SHA512())
7278

7379
# Return the BASE64 encoded SHA512 signature

0 commit comments

Comments
 (0)