Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
import psycopg2
# PostgreSQL with SSL
conn = psycopg2.connect(
host="db.example.com",
database="mydb",
user="dbuser",
password="password",
sslmode="require", # or "verify-full" for cert validation
sslrootcert="/path/to/ca.crt",
sslcert="/path/to/client.crt",
sslkey="/path/to/client.key"
)
Key Management
AWS KMS
import boto3
import base64
kms = boto3.client('kms')
# Create master key
response = kms.create_key(
Description='Master encryption key',
KeyUsage='ENCRYPT_DECRYPT',
Origin='AWS_KMS'
)
key_id = response['KeyMetadata']['KeyId']
# Create alias
kms.create_alias(
AliasName='alias/my-master-key',
TargetKeyId=key_id
)
# Encrypt data
plaintext = b'sensitive data'
response = kms.encrypt(
KeyId='alias/my-master-key',
Plaintext=plaintext
)
ciphertext = response['CiphertextBlob']
# Decrypt data
response = kms.decrypt(
CiphertextBlob=ciphertext
)
decrypted = response['Plaintext']
# Generate data key (envelope encryption)
response = kms.generate_data_key(
KeyId='alias/my-master-key',
KeySpec='AES_256'
)
plaintext_key = response['Plaintext'] # Use this to encrypt data
encrypted_key = response['CiphertextBlob'] # Store this with data
Envelope Encryption
1. Generate data encryption key (DEK) from KMS
2. Encrypt data with DEK
3. Encrypt DEK with master key (from KMS)
4. Store encrypted data + encrypted DEK together
5. To decrypt: decrypt DEK with KMS, then decrypt data with DEK
Benefits:
- Fast (bulk encryption with DEK, not KMS API)
- Secure (DEK never stored plaintext)
- Key rotation (re-encrypt DEK, not all data)
defenvelope_encrypt(data, kms_key_id):
"""Encrypt data using envelope encryption"""# Generate data key
response = kms.generate_data_key(
KeyId=kms_key_id,
KeySpec='AES_256'
)
plaintext_key = response['Plaintext']
encrypted_key = response['CiphertextBlob']
# Encrypt data with data key
cipher = AESGCM(plaintext_key)
nonce = os.urandom(12)
ciphertext = cipher.encrypt(nonce, data, None)
return {
'ciphertext': ciphertext,
'encrypted_key': encrypted_key,
'nonce': nonce
}
defenvelope_decrypt(encrypted_data):
"""Decrypt data using envelope encryption"""# Decrypt data key with KMS
response = kms.decrypt(
CiphertextBlob=encrypted_data['encrypted_key']
)
plaintext_key = response['Plaintext']
# Decrypt data with data key
cipher = AESGCM(plaintext_key)
plaintext = cipher.decrypt(
encrypted_data['nonce'],
encrypted_data['ciphertext'],
None
)
return plaintext
Key Rotation
Automatic Rotation (AWS KMS)
# Enable automatic rotation (every year)
kms.enable_key_rotation(KeyId=key_id)
# Check rotation status
response = kms.get_key_rotation_status(KeyId=key_id)
print(f"Rotation enabled: {response['KeyRotationEnabled']}")
# Note: Old ciphertexts still decryptable with old key versions
Manual Rotation
defrotate_encryption_key():
"""Rotate application-level encryption key"""# 1. Generate new key
new_key = Fernet.generate_key()
# 2. Store new key with version
store_key(new_key, version=2)
# 3. Re-encrypt all datafor record in db.execute("SELECT id, encrypted_field FROM sensitive_data"):
# Decrypt with old key
old_cipher = Fernet(get_key(version=1))
plaintext = old_cipher.decrypt(record['encrypted_field'])
# Encrypt with new key
new_cipher = Fernet(new_key)
new_ciphertext = new_cipher.encrypt(plaintext)
# Update record
db.execute(
"UPDATE sensitive_data SET encrypted_field = ?, key_version = 2 WHERE id = ?",
(new_ciphertext, record['id'])
)
# 4. Mark old key as deprecated
deprecate_key(version=1)