Skip to main content

Key hygiene

Virtual keys pile up. Keys created for a test, a contractor or a retired service keep working long after anyone needs them, and every one of them is a credential that can leak. The key hygiene report lists the keys worth a second look, and an optional policy gives unused keys an expiry date so they stop working on their own. This is an enterprise feature that needs a license granting key_hygiene (see feature names).

When you would use this​

Before an audit the platform team wants to show that no forgotten key can still reach the models. They open the report, find 14 keys nobody has used in 90 days and 31 keys with no budget, block the unused ones in one go, give the budgetless ones a budget, and turn on auto-expiry so that from now on an unused key gets a seven day notice and then expires by itself.

Before you start​

The license has to include key_hygiene. The report reads each key's last_active time, which the gateway updates when the key passes authentication, at most once an hour per key and worker, and the spend writer updates it too. Keys used only before this tracking existed and that never recorded spend show as never used, measured from their creation date, so give the gateway at least unused_days of running time on this release before you turn on auto-expiry.

Step by step​

  1. Set the policy in the Admin UI or under general_settings. In the Admin UI, open Virtual Keys, click Key hygiene and edit the Policy card, then click Save policy (proxy admins only, see the Key Hygiene page). In the config every field is optional.

    general_settings:
    key_hygiene:
    unused_days: 90
    rotation_max_age_days: 180
    exempt_team_ids: ["break-glass"]
    auto_expire:
    mode: unused
    grace_days: 7
    max_lifetime_days: 365
    expiry_notice_days: [30, 1]

    unused_days defaults to 90. rotation_max_age_days is unset by default, which turns the rotation_overdue finding off. auto_expire defaults to {mode: off}. max_lifetime_days is unset by default and expiry_notice_days defaults to [30, 1], both are described on leaked keys and key expiry. Put break-glass and long-lived service keys in a team listed in exempt_team_ids. Moving a key between teams needs team admin rights, so a key owner cannot exempt their own key.

  2. Read the summary. In the Admin UI open Virtual Keys and click the Key hygiene link in the header. Four tiles, Unused, No budget, No expiry and Rotation overdue, count the keys with each finding, and a note under them sums up the policy. Over the API call GET /key/hygiene/summary.

    curl http://localhost:4000/key/hygiene/summary -H "Authorization: Bearer $SPANWAY_API_KEY"
    {
    "unused": 14,
    "no_budget": 31,
    "no_expiry": 52,
    "rotation_overdue": null,
    "would_auto_expire": null,
    "policy": {
    "unused_days": 90,
    "rotation_max_age_days": null,
    "exempt_team_ids": [],
    "auto_expire": {"mode": "off"},
    "max_lifetime_days": null,
    "expiry_notice_days": [30, 1]
    }
    }

    rotation_overdue and would_auto_expire are null when the policy does not use them.

  3. List the keys with one finding. Click a tile or one of the Unused, No budget, No expiry and Rotation overdue buttons to fill the table. Over the API call GET /key/hygiene with finding, and pass the returned next_cursor as cursor for the next page. page_size is at most 200.

    curl "http://localhost:4000/key/hygiene?finding=unused&page_size=50" -H "Authorization: Bearer $SPANWAY_API_KEY"
  4. Act on the keys. Tick rows in the table and use Block, Set expiry with the Expire in choice of 7d, 30d or 90d, or Delete, and confirm. The report never changes a key itself. The actions use the normal /key/block, /key/update and /key/delete routes, which apply their own permission checks and audit rows, so over the API call those routes with the token from each row.

    curl -X POST http://localhost:4000/key/update \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"key": "sk-9f3a2c7e41b8d605", "duration": "7d"}'
  5. Turn on auto-expiry. Switch on Give unused keys an expiry date on the Policy card and click Save policy, or set auto_expire: {mode: unused, grace_days: 7} through POST /config/field/update. Either way the next hourly run uses it, no restart needed. A change in the config file needs a restart or a config reload. A job then runs every hour on one replica at a time and gives every key the report flags as unused an expiry date grace_days from now. The summary's would_auto_expire count and the policy note on the page tell you how many keys the next run will touch before you switch it on.

What you should see​

The page shows the four tiles, the policy note, for example "Unused after 90 days, rotation age is not checked, auto-expiry is off, key lifetime is not capped, owners are emailed 30 and 1 days before a key expires.", and a table with the key alias, team, creation date, last use, spend, expiry and a badge per finding. A blocked key disappears from the report on the next load, because blocked keys are left out, and a key given an expiry moves from No expiry to the other findings it still has. With auto-expiry on, each run writes an audit row per key with spanway_changed_by set to key_hygiene_policy and the before and after values of expires, the owners get the expiry email described on leaked keys and key expiry, and using a key during the grace period does not cancel its expiry.

What the report flags​

FindingWhen a key is flagged
unusedIts last use is more than unused_days ago. A key that was never used is measured from its creation date, so a new key is not flagged on its first day.
no_budgetIt has neither a max_budget nor a budget attached. Team and organization budgets still apply to it, but the key itself has no limit.
no_expiryIt has no expiry date.
rotation_overduerotation_max_age_days is set, the key is not on auto_rotate, and its last rotation, or its creation if it was never rotated, is more than that many days ago.

Blocked keys, keys that have already expired, Admin UI session keys and keys of the teams listed in exempt_team_ids are left out. A key can carry several findings at once. Proxy admins and admin viewers see every key. Organization admins see the keys of their organizations and of the teams in them, team admins see the keys of the teams they administer, and anyone else gets 403. Counting runs in the database, so the report does not load every key into memory.

The same hourly job applies the maximum key lifetime and sends the expiry emails. Both are described on leaked keys and key expiry, together with the public endpoint that blocks a key someone reports as leaked.

MethodPathSummary
GET/key/hygiene/summaryCount the keys each hygiene finding flags
GET/key/hygieneList the keys with one hygiene finding

Errors​

ErrorCauseFix
403 naming the key_hygiene featureThe license does not include key hygiene.Add the feature to the license. The page says so and names the feature.
403 Only proxy admins, admin viewers, org admins and team admins can read the key hygiene reportThe caller holds none of those roles.Use an admin, organization admin or team admin session.
400 Invalid key_hygiene: <field>: <rule> on Save policy or POST /config/field/updateThe policy you saved has a value out of range, for example max_lifetime_days: 0.Fix the value the message names and save again. A saved policy applies at once without a restart.
500 with a policy messageA value under general_settings.key_hygiene in config.yaml does not validate, for example a negative unused_days.Fix the value in the config file and restart, or save a valid policy from the Policy card, which applies at once. The auto-expiry job also logs an error and skips its run until then.
500 No database connectedThe gateway runs without a database, so there are no keys to report on.Set DATABASE_URL.
422 on finding or page_sizefinding is not one of the four names, or page_size is above 200.Use unused, no_budget, no_expiry or rotation_overdue and a smaller page.
A key in active use shows as never usedIt was last used before last-use tracking existed and never recorded spend.Wait unused_days after the upgrade before trusting the unused finding or turning on auto-expiry.
Bulk action fails for some keysThose keys are outside your team or organization scope.Block and Set expiry change every key they may and say how many were not changed, so the others stay changed. Delete is all or nothing for anyone but a proxy admin: if one selected key is out of scope, nothing is deleted. Ask a proxy admin for the rest.
Auto-expiry never runsauto_expire.mode is off, or another replica holds the scheduled job lock.Set mode: unused and check the gateway log on each replica.

Logs​

LevelLineMeaningAction
INFOkey hygiene: set an expiry on N unused keysThe hourly auto-expiry run gave N unused keys an expiry date. Each one also has an audit row.None. Check the audit log if an owner asks why their key expires.
ERRORkey hygiene job skipped: general_settings.key_hygiene is invalid: ...The policy does not validate, so no auto-expiry, lifetime cap or expiry email runs.Fix the value named in the message.
DEBUGKey hygiene auto-expiry is an Enterprise Feature. Skipping...The gateway has no enterprise license, so the hourly job is not scheduled.Add a license that includes key_hygiene.

The lifetime cap and expiry email lines are on leaked keys and key expiry, and every line is in the log reference.