Skip to content

Commit 74d3ad0

Browse files
authored
Merge pull request #9 from hactar-is/typing-fix
Fixes uses of type :str = None to be :Optional[str] = None
2 parents 685f090 + a7e0e27 commit 74d3ad0

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/phase/utils/phase_io.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import requests
2-
from typing import Tuple
3-
from typing import List, Dict
41
from dataclasses import dataclass
2+
from typing import Dict, List, Optional, Tuple
3+
4+
import requests
5+
from nacl.bindings import (
6+
crypto_kx_server_session_keys,
7+
)
8+
9+
from .const import pss_service_pattern, pss_user_pattern
10+
from .crypto import CryptoUtils
11+
from .misc import phase_get_context, tag_matches
512
from .network import (
6-
fetch_phase_user,
13+
create_phase_secrets,
14+
delete_phase_secrets,
715
fetch_app_key,
8-
fetch_wrapped_key_share,
916
fetch_phase_secrets,
10-
create_phase_secrets,
17+
fetch_phase_user,
18+
fetch_wrapped_key_share,
1119
update_phase_secrets,
12-
delete_phase_secrets
13-
)
14-
from nacl.bindings import (
15-
crypto_kx_server_session_keys,
1620
)
17-
from .crypto import CryptoUtils
18-
from .const import __ph_version__, pss_user_pattern, pss_service_pattern
19-
from .misc import phase_get_context, normalize_tag, tag_matches
20-
from .secret_referencing import resolve_all_secrets
2121

2222

2323
@dataclass
@@ -80,8 +80,8 @@ def auth(self):
8080

8181
return "Success"
8282

83-
except ValueError as err:
84-
raise ValueError(f"Invalid Phase credentials")
83+
except ValueError:
84+
raise ValueError("Invalid Phase credentials")
8585

8686

8787
def init(self):
@@ -90,12 +90,12 @@ def init(self):
9090
# Ensure the response is OK
9191
if response.status_code != 200:
9292
raise ValueError(f"Request failed with status code {response.status_code}: {response.text}")
93-
93+
9494
# Parse and return the JSON content
9595
return response.json()
9696

9797

98-
def create(self, key_value_pairs: List[Tuple[str, str]], env_name: str, app_name: str, path: str = '/', override_value: str = None) -> requests.Response:
98+
def create(self, key_value_pairs: List[Tuple[str, str]], env_name: str, app_name: str, path: str = '/', override_value: Optional[str] = None) -> requests.Response:
9999
"""
100100
Create secrets in Phase KMS with support for specifying a path and overrides.
101101
@@ -150,7 +150,7 @@ def create(self, key_value_pairs: List[Tuple[str, str]], env_name: str, app_name
150150
return create_phase_secrets(self._token_type, self._app_secret.app_token, env_id, secrets, self._api_host)
151151

152152

153-
def get(self, env_name: str, keys: List[str] = None, app_name: str = None, tag: str = None, path: str = '') -> List[Dict]:
153+
def get(self, env_name: str, keys: List[str] = None, app_name: Optional[str] = None, tag: Optional[str] = None, path: str = '') -> List[Dict]:
154154
"""
155155
Get secrets from Phase KMS based on key and environment, with support for personal overrides,
156156
optional tag matching, decrypting comments, and now including path support and key digest optimization.
@@ -165,7 +165,7 @@ def get(self, env_name: str, keys: List[str] = None, app_name: str = None, tag:
165165
Returns:
166166
List[Dict]: A list of dictionaries for all secrets in the environment that match the criteria, including their paths.
167167
"""
168-
168+
169169
user_response = fetch_phase_user(self._token_type, self._app_secret.app_token, self._api_host)
170170
if user_response.status_code != 200:
171171
raise ValueError(f"Request failed with status code {user_response.status_code}: {user_response.text}")
@@ -221,7 +221,7 @@ def get(self, env_name: str, keys: List[str] = None, app_name: str = None, tag:
221221
"comment": decrypted_comment,
222222
"path": secret.get("path", "/"),
223223
"application": app_name,
224-
"environment": env_name
224+
"environment": env_name
225225
}
226226

227227
# Only add the secret to results if the requested keys are not specified or the decrypted key is one of the requested keys.
@@ -231,10 +231,10 @@ def get(self, env_name: str, keys: List[str] = None, app_name: str = None, tag:
231231
return results
232232

233233

234-
def update(self, env_name: str, key: str, value: str = None, app_name: str = None, source_path: str = '', destination_path: str = None, override: bool = False, toggle_override: bool = False) -> str:
234+
def update(self, env_name: str, key: str, value: Optional[str] = None, app_name: Optional[str] = None, source_path: str = '', destination_path: Optional[str] = None, override: bool = False, toggle_override: bool = False) -> str:
235235
"""
236236
Update a secret in Phase KMS based on key and environment, with support for source and destination paths.
237-
237+
238238
Args:
239239
env_name (str): The name (or partial name) of the desired environment.
240240
key (str): The key for which to update the secret value.
@@ -244,11 +244,11 @@ def update(self, env_name: str, key: str, value: str = None, app_name: str = Non
244244
destination_path (str, optional): The new path for the secret, if changing its location. If not provided, the path is not updated.
245245
override (bool, optional): Whether to update an overridden secret value. Defaults to False.
246246
toggle_override (bool, optional): Whether to toggle the override state between active and inactive. Defaults to False.
247-
247+
248248
Returns:
249249
str: A message indicating the outcome of the update operation.
250250
"""
251-
251+
252252
user_response = fetch_phase_user(self._token_type, self._app_secret.app_token, self._api_host)
253253
if user_response.status_code != 200:
254254
raise ValueError(f"Request failed with status code {user_response.status_code}: {user_response.text}")
@@ -301,10 +301,10 @@ def update(self, env_name: str, key: str, value: str = None, app_name: str = Non
301301
# This prevents toggling an override on a secret that doesn't have one.
302302
if "override" not in matching_secret or matching_secret["override"] is None:
303303
raise OverrideNotFoundException(key)
304-
304+
305305
# Retrieve the current override state. If the override is not active, it defaults to False.
306306
current_override_state = matching_secret["override"].get("is_active", False)
307-
307+
308308
# Prepare the payload to update the override status. The value of the override remains unchanged,
309309
# but the isActive status is toggled.
310310
secret_update_payload["override"] = {
@@ -338,20 +338,20 @@ def update(self, env_name: str, key: str, value: str = None, app_name: str = Non
338338
return f"Error: Failed to update secret. HTTP Status Code: {response.status_code}"
339339

340340

341-
def delete(self, env_name: str, keys_to_delete: List[str], app_name: str = None, path: str = None) -> List[str]:
341+
def delete(self, env_name: str, keys_to_delete: List[str], app_name: Optional[str] = None, path: Optional[str] = None) -> List[str]:
342342
"""
343343
Delete secrets in Phase KMS based on keys and environment, with optional path support.
344-
344+
345345
Args:
346346
env_name (str): The name (or partial name) of the desired environment.
347347
keys_to_delete (List[str]): The keys for which to delete the secrets.
348348
app_name (str, optional): The name of the desired application.
349349
path (str, optional): The path within which to delete the secrets. If specified, only deletes secrets within this path.
350-
350+
351351
Returns:
352352
List[str]: A list of keys that were not found and could not be deleted.
353353
"""
354-
354+
355355
user_response = fetch_phase_user(self._token_type, self._app_secret.app_token, self._api_host)
356356
if user_response.status_code != 200:
357357
raise ValueError(f"Request failed with status code {user_response.status_code}: {user_response.text}")
@@ -372,7 +372,7 @@ def delete(self, env_name: str, keys_to_delete: List[str], app_name: str = None,
372372
keys_not_found = []
373373
secrets_response = fetch_phase_secrets(self._token_type, self._app_secret.app_token, env_id, self._api_host, path=path)
374374
secrets_data = secrets_response.json()
375-
375+
376376
for key in keys_to_delete:
377377
found = False
378378
for secret in secrets_data:
@@ -388,9 +388,9 @@ def delete(self, env_name: str, keys_to_delete: List[str], app_name: str = None,
388388

389389
if secret_ids_to_delete:
390390
delete_phase_secrets(self._token_type, self._app_secret.app_token, env_id, secret_ids_to_delete, self._api_host)
391-
391+
392392
return keys_not_found
393-
393+
394394

395395
def decrypt(self, phase_ciphertext) -> str | None:
396396
"""

0 commit comments

Comments
 (0)