Files
python-sdk/sink/cache_expire_test.py
zeekay 23138100de refactor: rebrand Infisical Python SDK -> Hanzo KMS Python SDK
- Rename package: infisical_sdk -> hanzo_kms (pip: hanzokms)
- Rename classes: InfisicalSDKClient -> KMSClient, InfisicalRequests -> KMSRequests, InfisicalError -> KMSError
- Default API host: https://kms.hanzo.ai
- Update all imports, docstrings, and user-facing strings
- Copyright: Hanzo AI Inc.
- Backward-compatible aliases: InfisicalSDKClient, InfisicalError
- Env var: INFISICAL_AWS_IAM_AUTH_IDENTITY_ID -> HANZO_KMS_AWS_IAM_AUTH_IDENTITY_ID
- Remove obsolete OpenAPI generator files (.travis.yml, .gitlab-ci.yml, etc.)
2026-02-24 02:00:18 -08:00

73 lines
2.3 KiB
Python

from hanzo_kms import KMSClient
import time
import os
def loadEnvVarsFromFileIntoEnv():
d = dict()
with open("./.env", "r") as fp:
for line in fp:
line = line.strip()
if line and not line.startswith("#"):
line = line.split("=", 1)
d[line[0]] = line[1]
for key, value in d.items():
os.environ[key] = value
loadEnvVarsFromFileIntoEnv()
SECRETS_PROJECT_ID = os.getenv("SECRETS_PROJECT_ID")
SECRETS_ENVIRONMENT_SLUG = os.getenv("SECRETS_ENVIRONMENT_SLUG")
MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_ID = os.getenv("MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_ID")
MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_SECRET = os.getenv("MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_SECRET")
SITE_URL = os.getenv("SITE_URL")
cache_enabled_client = KMSClient(host=SITE_URL, cache_ttl=10)
cache_enabled_client.auth.universal_auth.login(MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_ID, MACHINE_IDENTITY_UNIVERSAL_AUTH_CLIENT_SECRET)
single_secret_cached = cache_enabled_client.secrets.get_secret_by_name(
secret_name="TEST",
project_id=SECRETS_PROJECT_ID,
environment_slug=SECRETS_ENVIRONMENT_SLUG,
secret_path="/",
expand_secret_references=True,
include_imports=True)
time_start_cache_enabled = time.time()
# Running in loop 10 times or the time is so small that python messes up the print (which is a great sign for us!)
for i in range(10):
single_secret_cached = cache_enabled_client.secrets.get_secret_by_name(
secret_name="TEST",
project_id=SECRETS_PROJECT_ID,
environment_slug=SECRETS_ENVIRONMENT_SLUG,
secret_path="/",
expand_secret_references=True,
include_imports=True)
time_end_cache_enabled = time.time()
print(f"[CACHE ENABLED] Time taken: {time_end_cache_enabled - time_start_cache_enabled} seconds")
print("Sleeping for 10 seconds")
time.sleep(10)
print("Getting secret again")
time_start_cache_enabled = time.time()
single_secret_cached = cache_enabled_client.secrets.get_secret_by_name(
secret_name="TEST",
project_id=SECRETS_PROJECT_ID,
environment_slug=SECRETS_ENVIRONMENT_SLUG,
secret_path="/",
expand_secret_references=True,
include_imports=True)
time_end_cache_enabled = time.time()
print(f"[CACHE EXPIRED] Time taken: {time_end_cache_enabled - time_start_cache_enabled} seconds")