The Kubiya Secrets service provides a secure interface for managing sensitive data and credentials through the Kubiya platform.
It enables you to create, retrieve, update, and delete secrets with robust validation and security features.
For larger secrets or when reading from configuration files:
Copy
Ask AI
import tempfilefrom pathlib import Path# Create a temporary file with secret contenttemp_file = Path(tempfile.mktemp())temp_file.write_text("my-secret-api-key-from-file")try: # Create secret from file create_result = client.secrets.create( name="api-key-from-file", description="API key loaded from file", from_file=str(temp_file) ) print(f"Secret created from file: {create_result}") # Update secret from another file temp_file.write_text("updated-api-key-from-file") update_result = client.secrets.update( name="api-key-from-file", description="Updated API key loaded from file", from_file=str(temp_file) ) print(f"Secret updated from file: {update_result}")finally: # Clean up if temp_file.exists(): temp_file.unlink()
try: # This will fail - empty secret name client.secrets.create(name="", value="some-value")except SecretValidationError as e: print(f"Validation failed: {e}") # Handle validation errors - usually user input issuestry: # This will fail - no value provided client.secrets.create(name="my-secret")except SecretValidationError as e: print(f"Validation failed: {e}") # Secret value must be provided via value or from_filetry: # This will fail - both value and from_file provided client.secrets.create( name="my-secret", value="direct-value", from_file="/path/to/file" )except SecretValidationError as e: print(f"Validation failed: {e}") # Cannot use both value and from_file
try: # This will fail if file doesn't exist client.secrets.create( name="my-secret", from_file="/nonexistent/file.txt" )except SecretError as e: print(f"Secret operation failed: {e}") print(f"Secret name: {e.details.get('secret_name') if e.details else 'Unknown'}") # Handle file not found or other operational errors if "File not found" in str(e): print("Check that the specified file exists and is readable")try: # This will fail if secret doesn't exist secret_value = client.secrets.value("nonexistent-secret")except SecretError as e: print(f"Secret retrieval failed: {e}") # Handle secret not found errors
# Good practice: Don't log actual secret valuessecret_value = client.secrets.value("my-secret")print(f"Retrieved secret of length: {len(secret_value)}")# Bad practice: Don't do this!# print(f"Secret value: {secret_value}") # Never log secrets!
from pathlib import Pathdef create_secret_from_file_safely(name: str, file_path: str, description: str = None): """Safely create a secret from a file with proper error handling""" path = Path(file_path) if not path.exists(): raise FileNotFoundError(f"Secret file not found: {file_path}") if not path.is_file(): raise ValueError(f"Path is not a file: {file_path}") try: return client.secrets.create( name=name, description=description, from_file=str(path) ) except SecretError as e: print(f"Failed to create secret from file: {e}") raise
def get_secret_safely(secret_name: str) -> str: """Get a secret value with proper error handling""" try: # Validate the secret name format if not secret_name or not isinstance(secret_name, str): raise SecretValidationError("Secret name must be a non-empty string") # Retrieve the secret value value = client.secrets.value(secret_name) if not value: raise SecretError(f"Secret '{secret_name}' returned empty value") return value except SecretValidationError as e: print(f"Invalid secret name: {e}") raise except SecretError as e: print(f"Failed to retrieve secret '{secret_name}': {e}") raise