|
1 | 1 | # Python SDK for Phase |
2 | 2 |
|
3 | | -SDK to integrate Phase in server-side applications running Python |
| 3 | +SDK to integrate Phase in server-side applications running Python. This SDK allows you to manage secrets securely using the Phase platform. |
4 | 4 |
|
5 | 5 | ## Install |
6 | 6 |
|
7 | | -`pip install phase-dev` |
| 7 | +``` |
| 8 | +pip install phase-dev |
| 9 | +``` |
8 | 10 |
|
9 | 11 | ## Import |
10 | 12 |
|
11 | 13 | ```python |
12 | | -from phase import Phase; |
| 14 | +from phase import Phase, CreateSecretsOptions, GetAllSecretsOptions, GetSecretOptions, UpdateSecretOptions, DeleteSecretOptions |
13 | 15 | ``` |
14 | 16 |
|
15 | 17 | ## Initialize |
16 | 18 |
|
17 | | -Initialize the SDK with your `APP_ID` and `APP_SECRET`: |
| 19 | +Initialize the SDK with your host and token: |
18 | 20 |
|
19 | 21 | ```python |
20 | | -phase = Phase(APP_ID, APP_SECRET) |
| 22 | +phase = Phase( |
| 23 | + init=False, |
| 24 | + host='https://your-phase-host.com', |
| 25 | + pss=PHASE_SERVICE_TOKEN |
| 26 | + |
| 27 | +) |
21 | 28 | ``` |
22 | 29 |
|
23 | 30 | ## Usage |
24 | 31 |
|
25 | | -### Encrypt |
| 32 | +### Create Secrets |
| 33 | + |
| 34 | +Create one or more secrets in a specified application and environment: |
| 35 | + |
| 36 | +```python |
| 37 | +create_options = CreateSecretsOptions( |
| 38 | + env_name="Development", |
| 39 | + app_name="Your App Name", |
| 40 | + key_value_pairs=[ |
| 41 | + {"API_KEY": "your-api-key"}, |
| 42 | + {"DB_PASSWORD": "your-db-password"} |
| 43 | + ], |
| 44 | + secret_path="/api" |
| 45 | +) |
| 46 | +result = phase.create_secrets(create_options) |
| 47 | +print(f"Create secrets result: {result}") |
| 48 | +``` |
| 49 | + |
| 50 | +### Get Secrets |
| 51 | + |
| 52 | +Fetch one or more secrets from a specified application and environment: |
| 53 | + |
| 54 | +```python |
| 55 | +get_options = GetAllSecretsOptions( |
| 56 | + env_name="Development", |
| 57 | + app_name="Your App Name", |
| 58 | + tag="api", # Optional: filter by tag |
| 59 | + secret_path="/api" # Optional: specify path |
| 60 | +) |
| 61 | +secrets = phase.get_all_secrets(get_options) |
| 62 | +for secret in secrets: |
| 63 | + print(f"Key: {secret.key}, Value: {secret.value}") |
| 64 | +``` |
| 65 | + |
| 66 | +To get a specific secret: |
26 | 67 |
|
27 | 68 | ```python |
28 | | -ciphertext = phase.encrypt("hello world"); |
| 69 | +get_options = GetSecretOptions( |
| 70 | + env_name="Development", |
| 71 | + app_name="Your App Name", |
| 72 | + key_to_find="API_KEY", |
| 73 | + secret_path="/api" |
| 74 | +) |
| 75 | +secret = phase.get_secret(get_options) |
| 76 | +if secret: |
| 77 | + print(f"Key: {secret.key}, Value: {secret.value}") |
29 | 78 | ``` |
30 | 79 |
|
31 | | -### Decrypt |
| 80 | +### Update Secrets |
| 81 | + |
| 82 | +Update an existing secret in a specified application and environment: |
32 | 83 |
|
33 | 84 | ```python |
34 | | -plaintext = phase.decrypt(ciphertext); |
| 85 | +update_options = UpdateSecretOptions( |
| 86 | + env_name="Development", |
| 87 | + app_name="Your App Name", |
| 88 | + key="API_KEY", |
| 89 | + value="new-api-key-value", |
| 90 | + secret_path="/api", |
| 91 | + destination_path="/new-api", # Optional: move secret to a new path |
| 92 | + override=False, # Optional: create a personal override |
| 93 | + toggle_override=False # Optional: toggle personal override |
| 94 | +) |
| 95 | +result = phase.update_secret(update_options) |
| 96 | +print(f"Update result: {result}") |
35 | 97 | ``` |
| 98 | + |
| 99 | +### Delete Secrets |
| 100 | + |
| 101 | +Delete a secret from a specified application and environment: |
| 102 | + |
| 103 | +```python |
| 104 | +delete_options = DeleteSecretOptions( |
| 105 | + env_name="Development", |
| 106 | + app_name="Your App Name", |
| 107 | + key_to_delete="API_KEY", |
| 108 | + secret_path="/api" |
| 109 | +) |
| 110 | +result = phase.delete_secret(delete_options) |
| 111 | +print(f"Delete result: {result}") |
| 112 | +``` |
| 113 | + |
| 114 | +### Resolve Secret References |
| 115 | + |
| 116 | +Resolve references in secret values: |
| 117 | + |
| 118 | +```python |
| 119 | +get_options = GetAllSecretsOptions( |
| 120 | + env_name="Development", |
| 121 | + app_name="Your App Name" |
| 122 | +) |
| 123 | +secrets = phase.get_all_secrets(get_options) |
| 124 | +resolved_secrets = phase.resolve_references(secrets, "Development", "Your App Name") |
| 125 | +for secret in resolved_secrets: |
| 126 | + print(f"Key: {secret.key}, Resolved Value: {secret.value}") |
| 127 | +``` |
| 128 | + |
| 129 | +## Error Handling |
| 130 | + |
| 131 | +The SDK methods may raise exceptions for various error conditions. It's recommended to wrap SDK calls in try-except blocks to handle potential errors: |
| 132 | + |
| 133 | +```python |
| 134 | +try: |
| 135 | + get_options = GetAllSecretsOptions(env_name="Development", app_name="Your App Name") |
| 136 | + secrets = phase.get_all_secrets(get_options) |
| 137 | +except ValueError as e: |
| 138 | + print(f"An error occurred: {e}") |
| 139 | +``` |
| 140 | + |
| 141 | +## Note on Security |
| 142 | + |
| 143 | +Never hard-code sensitive information like tokens or secrets directly in your code. Always use environment variables or secure configuration management to provide these values to your application. |
0 commit comments