|
| 1 | +from infisical_sdk import InfisicalSDKClient |
| 2 | +import time |
| 3 | +import os |
| 4 | +import random |
| 5 | +import string |
| 6 | + |
| 7 | + |
| 8 | +def loadEnvVarsFromFileIntoEnv(): |
| 9 | + d = dict() |
| 10 | + with open("./.env", "r") as fp: |
| 11 | + for line in fp: |
| 12 | + line = line.strip() |
| 13 | + if line and not line.startswith("#"): |
| 14 | + line = line.split("=", 1) |
| 15 | + d[line[0]] = line[1] |
| 16 | + |
| 17 | + for key, value in d.items(): |
| 18 | + os.environ[key] = value |
| 19 | + |
| 20 | +loadEnvVarsFromFileIntoEnv() |
| 21 | + |
| 22 | +SECRETS_PROJECT_ID = os.getenv("SECRETS_PROJECT_ID") |
| 23 | +SECRETS_ENVIRONMENT_SLUG = os.getenv("SECRETS_ENVIRONMENT_SLUG") |
| 24 | + |
| 25 | +MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_ID = os.getenv("MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_ID") |
| 26 | +MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_SECRET = os.getenv("MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_SECRET") |
| 27 | +SITE_URL = os.getenv("SITE_URL") |
| 28 | + |
| 29 | +cache_enabled_client = InfisicalSDKClient(host=SITE_URL, cache_ttl=10) |
| 30 | +cache_enabled_client.auth.universal_auth.login(MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_ID, MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_SECRET) |
| 31 | + |
| 32 | + |
| 33 | +time_start_cache_disabled = time.time() |
| 34 | + |
| 35 | +def randomStringNoSpecialChars(length: int = 10) -> str: |
| 36 | + return ''.join(random.choices(string.ascii_letters + string.digits, k=length)) |
| 37 | + |
| 38 | +created_sec = cache_enabled_client.secrets.create_secret_by_name( |
| 39 | + secret_name=f"TEST_{randomStringNoSpecialChars()}", |
| 40 | + project_id=SECRETS_PROJECT_ID, |
| 41 | + environment_slug=SECRETS_ENVIRONMENT_SLUG, |
| 42 | + secret_path="/", |
| 43 | + secret_value=f"secret_value_{randomStringNoSpecialChars()}", |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +single_secret_cached = cache_enabled_client.secrets.get_secret_by_name( |
| 48 | + secret_name=created_sec.secretKey, |
| 49 | + project_id=SECRETS_PROJECT_ID, |
| 50 | + environment_slug=SECRETS_ENVIRONMENT_SLUG, |
| 51 | + secret_path="/", |
| 52 | + expand_secret_references=True, |
| 53 | + include_imports=True) |
| 54 | + |
| 55 | +print(single_secret_cached) |
| 56 | + |
| 57 | + |
| 58 | +deleted_secret = cache_enabled_client.secrets.delete_secret_by_name( |
| 59 | + secret_name=created_sec.secretKey, |
| 60 | + project_id=SECRETS_PROJECT_ID, |
| 61 | + environment_slug=SECRETS_ENVIRONMENT_SLUG, |
| 62 | + secret_path="/", |
| 63 | +) |
| 64 | + |
| 65 | +print(deleted_secret) |
| 66 | + |
| 67 | +# Should error |
| 68 | +try: |
| 69 | + single_secret_cached = cache_enabled_client.secrets.get_secret_by_name( |
| 70 | + secret_name=created_sec.secretKey, |
| 71 | + project_id=SECRETS_PROJECT_ID, |
| 72 | + environment_slug=SECRETS_ENVIRONMENT_SLUG, |
| 73 | + secret_path="/", |
| 74 | + expand_secret_references=True, |
| 75 | + include_imports=True) |
| 76 | +except Exception as e: |
| 77 | + print(e) |
| 78 | + print("Good, we errored as expected!") |
0 commit comments