Stripe DevOps code question: How would you design a Python remediation script that can disable stale CI deploy keys across hundreds of repositories while supporting dry-run, approvals, audit logs, and safe rollback?
Quick Answer
Design the script as an idempotent workflow: discover keys, classify stale keys, produce a dry-run plan, require approval for destructive actions, execute with rate-limit-aware retries, and write append-only audit logs. Rollback should be based on captured key metadata and should be tested on a small allowlisted repository set before fleet-wide execution.
Detailed Answer
A remediation script is closer to a surgical checklist than a one-off script. The dangerous part is not the API call that deletes a key; it is deleting the wrong key quickly across hundreds of repositories. Interviewers ask this to see whether you understand automation blast radius, approval gates, and auditability.
The workflow should have clear phases. Discovery reads repository keys and last-used signals where the provider exposes them. Classification marks keys stale only when they match policy, such as no usage for 90 days, not owned by a protected integration, and not tagged as break-glass. Planning writes a human-readable and machine-readable diff. Approval requires a ticket, reviewer, or signed change request. Execution deletes or disables keys in small batches. Verification confirms the key is gone and that known deployment pipelines still authenticate.
In Python, design the code around pure functions and side-effect boundaries. A classify_key function should be easy to unit test with fake key metadata. A provider client should hide GitHub, GitLab, or Bitbucket API differences. The execution layer should handle 429 rate limits, 5xx retries, and permanent 403 permission errors differently. The script should accept --dry-run by default and require --execute plus a change ID for mutation.
At scale, rollback is not magic. If the API supports disabling instead of deleting, prefer disable first. If deletion is required, capture enough metadata to recreate the key: title, public key, repository, permissions, and owner. Never log private keys because they are not retrievable and should not exist in logs. Use append-only audit storage so security teams can answer who changed what and when.
The non-obvious gotcha is ownership. A key may look stale because usage telemetry is incomplete or because a rarely used disaster recovery pipeline only runs quarterly. Senior engineers define exception handling and communication before the deletion run, not after the outage.