I recently dealt with a problem that was fairly similar at work.
The first thing you need to understand is when and why Vault creates entities. Every time there is any login to Vault, using any auth method, Vault looks up the pair of (auth method, “username”) in the identity store, and creates an entity if there isn’t one there already.
Exactly what “username” means, varies depending on the auth method. As @jbayer points out, the Kubernetes auth method is unusual in that it gives you a choice of two options for what is used as the “username” in this context. If the reason you have so many entities, is because your Kubernetes service accounts are frequently being deleted and recreated with the same name, this option should slow the growth (after an initial peak, as changing it would cause all active identities to create a new identity on next login).
However, you need a way of cleaning up entities that are no longer relevant, and unfortunately this is one of the things that Vault provides no out of the box support for, and requires a Vault admin to write their own script using the API.
If you are using Vault Enterprise: Bear in mind that “number of entities that have performed operations against the Vault API in the calendar month” is the licensed quantity, so effectively re-using entities where appropriate, and not deleting them unnecessarily, has financial implications.
When it comes to cleaning up Kubernetes entities, the approach I used was to query the Kubernetes API server for a list of all service accounts in all namespaces, extract all the UIDs from the response, and use this as a filter to determine what to delete from Vault. I think this is better than TTL based approaches, as Kubernetes service accounts do not have an expiry, and Vault entities don’t track their last use time.
This is clearly complex enough to require writing a script - for implementation language, Python is a good option if you have any previous experience with it, as it has both a library for accessing the Kubernetes API, abstracting away its rather complex authentication peculiarities, and the lovely requests HTTP library that makes talking to Vault easy.
The general approach is:
- Request all the service accounts from Kubernetes
- Request all the entities from Vault
- Loop over them all issuing deletes where appropriate
When it comes to getting all the entities from Vault, it is handy to know that the Vault GET /v1/identity/entity/id/?list=true endpoint returns more than a mere list - it additionally returns summary information for all entries in the list. Here’s an example response from my test Vault (irrelevant fields elided):
{
"data": {
"key_info": {
"c128cebf-97f6-9654-35b8-8181a1ab4885": {
"aliases": [
{
"id": "1247f01b-afb2-dd22-cace-fe3f57827baf",
"mount_accessor": "auth_userpass_10f3c534",
"mount_path": "auth/userpass/",
"mount_type": "userpass",
"name": "admin"
}
],
"name": "admin"
}
},
"keys": [
"c128cebf-97f6-9654-35b8-8181a1ab4885"
]
}
}
Do note that for your test Vault this response is going to be gigantic, and there is a risk of it timing out completely, which would be a problem, as Vault APIs don’t support pagination
.
The above example is for the userpass auth method because I don’t have Kubernetes set up in this environment. In particular, pbserve that entity-alias information has been provided in-line with the entity information:
mount_type will let you easily filter kubernetes entities and ignore others
mount_path will let you distinguish between multiple Kubernetes auth methods, if relevant
name (within aliases) will be the Kubernetes service account UID
Technically, one entity can have multiple entity-aliases, but this is never the case for auto-created entities - only ones administratively configured via the Vault API.
Hopefully that sets you on the path to constructing a cleanup script for your environment.
Lastly, a correction to incorrect information earlier in this topic:
As detailed earlier in my response, all auth methods create entities.
A multitude of different Kubernetes service accounts using a Kubernetes auth method is very much a concern for Enterprise users’ licensing - as I know from monitoring usage and planning to ensure we remain within our license count at my place of work.