1- import requests
2- from typing import Tuple
3- from typing import List , Dict
41from 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
512from .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
1320)
14- from nacl .bindings import (
15- crypto_kx_server_session_keys ,
16- )
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
@@ -85,8 +85,8 @@ def auth(self):
8585
8686 return "Success"
8787
88- except ValueError as err :
89- raise ValueError (f "Invalid Phase credentials" )
88+ except ValueError :
89+ raise ValueError ("Invalid Phase credentials" )
9090
9191
9292 def init (self ):
@@ -95,12 +95,12 @@ def init(self):
9595 # Ensure the response is OK
9696 if response .status_code != 200 :
9797 raise ValueError (f"Request failed with status code { response .status_code } : { response .text } " )
98-
98+
9999 # Parse and return the JSON content
100100 return response .json ()
101101
102102
103- def create (self , key_value_pairs : List [Tuple [str , str ]], env_name : str , app_name : str = None , app_id : str = None , path : str = '/' , override_value : str = None ) -> requests .Response :
103+ def create (self , key_value_pairs : List [Tuple [str , str ]], env_name : str , app_name : Optional [ str ] = None , app_id : Optional [ str ] = None , path : str = '/' , override_value : Optional [ str ] = None ) -> requests .Response :
104104 """
105105 Create secrets in Phase KMS with support for specifying a path and overrides.
106106
@@ -156,7 +156,7 @@ def create(self, key_value_pairs: List[Tuple[str, str]], env_name: str, app_name
156156 return create_phase_secrets (self ._token_type , self ._app_secret .app_token , env_id , secrets , self ._api_host )
157157
158158
159- def get (self , env_name : str , keys : List [str ] = None , app_name : str = None , app_id : str = None , tag : str = None , path : str = '' ) -> List [Dict ]:
159+ def get (self , env_name : str , keys : List [str ] = None , app_name : Optional [ str ] = None , app_id : Optional [ str ] = None , tag : Optional [ str ] = None , path : str = '' ) -> List [Dict ]:
160160 """
161161 Get secrets from Phase KMS based on key and environment, with support for personal overrides,
162162 optional tag matching, decrypting comments, and now including path support and key digest optimization.
@@ -172,7 +172,7 @@ def get(self, env_name: str, keys: List[str] = None, app_name: str = None, app_i
172172 Returns:
173173 List[Dict]: A list of dictionaries for all secrets in the environment that match the criteria, including their paths.
174174 """
175-
175+
176176 user_response = fetch_phase_user (self ._token_type , self ._app_secret .app_token , self ._api_host )
177177 if user_response .status_code != 200 :
178178 raise ValueError (f"Request failed with status code { user_response .status_code } : { user_response .text } " )
@@ -228,7 +228,7 @@ def get(self, env_name: str, keys: List[str] = None, app_name: str = None, app_i
228228 "comment" : decrypted_comment ,
229229 "path" : secret .get ("path" , "/" ),
230230 "application" : app_name ,
231- "environment" : env_name
231+ "environment" : env_name
232232 }
233233
234234 # Only add the secret to results if the requested keys are not specified or the decrypted key is one of the requested keys.
@@ -238,10 +238,10 @@ def get(self, env_name: str, keys: List[str] = None, app_name: str = None, app_i
238238 return results
239239
240240
241- def update (self , env_name : str , key : str , value : str = None , app_name : str = None , app_id : str = None , source_path : str = '' , destination_path : str = None , override : bool = False , toggle_override : bool = False ) -> str :
241+ def update (self , env_name : str , key : str , value : Optional [ str ] = None , app_name : Optional [ str ] = None , app_id : Optional [ str ] = None , source_path : str = '' , destination_path : Optional [ str ] = None , override : bool = False , toggle_override : bool = False ) -> str :
242242 """
243243 Update a secret in Phase KMS based on key and environment, with support for source and destination paths.
244-
244+
245245 Args:
246246 env_name (str): The name (or partial name) of the desired environment.
247247 key (str): The key for which to update the secret value.
@@ -251,12 +251,12 @@ def update(self, env_name: str, key: str, value: str = None, app_name: str = Non
251251 source_path (str, optional): The current path of the secret. Defaults to root path '/'.
252252 destination_path (str, optional): The new path for the secret, if changing its location.
253253 override (bool, optional): Whether to update an overridden secret value. Defaults to False.
254- toggle_override (bool, optional): Whether to toggle the override state. Defaults to False.
255-
254+ toggle_override (bool, optional): Whether to toggle the override state between active and inactive . Defaults to False.
255+
256256 Returns:
257257 str: A message indicating the outcome of the update operation.
258258 """
259-
259+
260260 user_response = fetch_phase_user (self ._token_type , self ._app_secret .app_token , self ._api_host )
261261 if user_response .status_code != 200 :
262262 raise ValueError (f"Request failed with status code { user_response .status_code } : { user_response .text } " )
@@ -309,10 +309,10 @@ def update(self, env_name: str, key: str, value: str = None, app_name: str = Non
309309 # This prevents toggling an override on a secret that doesn't have one.
310310 if "override" not in matching_secret or matching_secret ["override" ] is None :
311311 raise OverrideNotFoundException (key )
312-
312+
313313 # Retrieve the current override state. If the override is not active, it defaults to False.
314314 current_override_state = matching_secret ["override" ].get ("is_active" , False )
315-
315+
316316 # Prepare the payload to update the override status. The value of the override remains unchanged,
317317 # but the isActive status is toggled.
318318 secret_update_payload ["override" ] = {
@@ -346,21 +346,21 @@ def update(self, env_name: str, key: str, value: str = None, app_name: str = Non
346346 return f"Error: Failed to update secret. HTTP Status Code: { response .status_code } "
347347
348348
349- def delete (self , env_name : str , keys_to_delete : List [str ], app_name : str = None , app_id : str = None , path : str = None ) -> List [str ]:
349+ def delete (self , env_name : str , keys_to_delete : List [str ], app_name : Optional [ str ] = None , app_id : Optional [ str ] = None , path : Optional [ str ] = None ) -> List [str ]:
350350 """
351351 Delete secrets in Phase KMS based on keys and environment, with optional path support.
352-
352+
353353 Args:
354354 env_name (str): The name (or partial name) of the desired environment.
355355 keys_to_delete (List[str]): The keys for which to delete the secrets.
356356 app_name (str, optional): The name of the desired application.
357357 app_id (str, optional): The ID of the application. Takes precedence over app_name if both are provided.
358358 path (str, optional): The path within which to delete the secrets. If specified, only deletes secrets within this path.
359-
359+
360360 Returns:
361361 List[str]: A list of keys that were not found and could not be deleted.
362362 """
363-
363+
364364 user_response = fetch_phase_user (self ._token_type , self ._app_secret .app_token , self ._api_host )
365365 if user_response .status_code != 200 :
366366 raise ValueError (f"Request failed with status code { user_response .status_code } : { user_response .text } " )
@@ -381,7 +381,7 @@ def delete(self, env_name: str, keys_to_delete: List[str], app_name: str = None,
381381 keys_not_found = []
382382 secrets_response = fetch_phase_secrets (self ._token_type , self ._app_secret .app_token , env_id , self ._api_host , path = path )
383383 secrets_data = secrets_response .json ()
384-
384+
385385 for key in keys_to_delete :
386386 found = False
387387 for secret in secrets_data :
@@ -400,7 +400,7 @@ def delete(self, env_name: str, keys_to_delete: List[str], app_name: str = None,
400400 if delete_response .status_code != 200 :
401401 raise ValueError (f"Failed to delete secrets. Status code: { delete_response .status_code } " )
402402 return keys_not_found
403-
403+
404404
405405 def decrypt (self , phase_ciphertext ) -> str | None :
406406 """
0 commit comments