Praktiken zur Geheimnis-Rotation
Warum Geheimnisse rotieren
Regelmäßige Geheimnis-Rotation hilft, Risiken zu minimieren:
| Risiko | Wie Rotation hilft |
|---|---|
| Passwort-Leak | Ein kompromittiertes Passwort wird schnell ungültig |
| Langlebige Token | Kürzere Laufzeiten reduzieren das Angriffsfenster |
| Insider-Bedrohungen | Ehemalige Mitarbeiter verlieren nach der Rotation den Zugriff |
| Compliance-Anforderungen | Standards wie PCI DSS und SOC 2 erfordern regelmäßige Passwortwechsel |
Rotations-Workflow
┌─────────────────────────────────────────────────────────────┐
│ 1. Generate 2. Target system 3. Passwork │
│ new password (update) (save) │
│ │
│ openssl rand ──► ALTER ROLE ... ──────► passwork-cli update│
└─────────────────────────────────────────────────────────────┘
Wichtig: Reihenfolge der Operationen
Aktualisieren Sie zuerst das Passwort im Zielsystem (Datenbank, Dienst) und speichern Sie es dann in Passwork. Andernfalls entsteht eine Diskrepanz: Passwork enthält das neue Passwort, während das System noch das alte verwendet.
Rotation über CLI
PostgreSQL
#!/bin/bash
set -e
ITEM_ID="<item-id>"
DB_USER="order_service"
# 1. Generate new password
NEW_PASS=$(openssl rand -base64 32)
# 2. Apply to PostgreSQL
psql -h pg.prod.internal -U postgres -d postgres -c \
"ALTER ROLE ${DB_USER} WITH PASSWORD '${NEW_PASS}';"
# 3. Store in Passwork
passwork-cli update --password-id "${ITEM_ID}" --password "${NEW_PASS}"
echo "Password rotated for ${DB_USER}"
MySQL
#!/bin/bash
set -e
ITEM_ID="<item-id>"
DB_USER="inventory_svc"
NEW_PASS=$(openssl rand -base64 32)
mysql -h mysql.prod.internal -u root -p"${MYSQL_ROOT_PASSWORD}" -e \
"ALTER USER '${DB_USER}'@'%' IDENTIFIED BY '${NEW_PASS}';"
passwork-cli update --password-id "${ITEM_ID}" --password "${NEW_PASS}"
echo "Password rotated for ${DB_USER}"
API-Schlüssel
#!/bin/bash
set -e
ITEM_ID="<item-id>"
# 1. Request a new key from the external service
NEW_KEY=$(curl -s -X POST https://api.service.com/keys/rotate \
-H "Authorization: Bearer ${OLD_KEY}" | jq -r '.new_key')
# 2. Store in Passwork
passwork-cli update --password-id "${ITEM_ID}" --password "${NEW_KEY}"
echo "API key rotated"
Rotation über Python SDK
Vollständiges Beispiel mit Fehlerbehandlung:
#!/usr/bin/env python3
"""PostgreSQL password rotation script."""
import os
import secrets
import sys
import psycopg2
from passwork import Client
def rotate_password(item_id: str, db_user: str) -> bool:
"""
Rotate the password for a PostgreSQL user.
Returns:
True on success, False on failure.
"""
client = Client(
url=os.environ["PASSWORK_HOST"],
token=os.environ["PASSWORK_TOKEN"],
)
# Fetch admin credentials from Passwork
admin_item = client.get_password(
item_id=os.environ["PG_ADMIN_ITEM_ID"]
)
# Create new password
new_password = secrets.token_urlsafe(32)
try:
# Connect to PostgreSQL
conn = psycopg2.connect(
host=admin_item.fields["PG_HOST"],
dbname="postgres",
user=admin_item.login,
password=admin_item.password,
)
conn.autocommit = True
# Apply new password
with conn.cursor() as cur:
cur.execute(
"ALTER ROLE %s WITH PASSWORD %s",
(db_user, new_password)
)
conn.close()
# Persist to Passwork
item = client.get_password(item_id=item_id)
item.password = new_password
client.update_password(item)
print(f"Password rotated for {db_user}")
return True
except psycopg2.Error as e:
print(f"Database error: {e}", file=sys.stderr)
return False
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return False
if __name__ == "__main__":
success = rotate_password(
item_id=os.environ["TARGET_ITEM_ID"],
db_user=os.environ["PG_USER"],
)
sys.exit(0 if success else 1)
Rotation planen
Cron-Job
# /etc/cron.d/passwork-rotation
# Rotate DB passwords every Sunday at 3:00 AM
0 3 * * 0 passwork /opt/scripts/rotate-db-passwords.sh >> /var/log/rotation.log 2>&1
Empfehlungen
Rotationshäufigkeit
| Geheimnistyp | Empfohlene Häufigkeit |
|---|---|
| Produktions-DB-Passwörter | 30–90 Tage |
| Externe Service-API-Schlüssel | 90 Tage oder gemäß Anbieterrichtlinie |
| Service-Token | 7–30 Tage |
| SSH-Schlüssel | 6–12 Monate |