Skip to content

Instantly share code, notes, and snippets.

@conor-mullen
Last active January 5, 2023 14:17
Show Gist options
  • Save conor-mullen/4dbcf0ce6d7135c847c7ff2a0cb72db0 to your computer and use it in GitHub Desktop.
Save conor-mullen/4dbcf0ce6d7135c847c7ff2a0cb72db0 to your computer and use it in GitHub Desktop.
Revoke all secret IDs for a given Vault Application Role
# THIS SCRIPT WILL REVOKE ALL SECRET IDS FOR A GIVEN ROLE. USE WITH CAUTION
# Log in to vault using a Github personal access token
vault login -method github > /dev/null
# Change VAULT_ROLE_NAME to the Vault Role that you want to revoke secrets for. This is normally your team name followed by a system code
export VAULT_ROLE_NAME=
# If you have already generate a new secret that you don't want to revoke, put the Secret Accessor of the Secret below and it will be skipped. If you want to keep multiple Accessors, this script isn't for you
export SECRET_ACCESSOR_TO_KEEP=
# Get the active Secret Accessors for this role
vault list auth/approle/role/$VAULT_ROLE_NAME/secret-id |
while read -r line;
do
# Skip the first two lines of the response or the Secret Accessor we want to keep
if [ "$line" = "Keys" ] || [ "$line" = "----" ] || [ "$line" = "$SECRET_ACCESSOR_TO_KEEP" ]
then
continue
fi
# Revoke the Secret attached to this Secret Accessor
vault write auth/approle/role/$VAULT_ROLE_NAME/secret-id-accessor/destroy secret_id_accessor=$line
done
# Print the remaining secrets. You should either see one key which is SECRET_ACCESSOR_TO_KEEP or "No value found"
vault list auth/approle/role/$VAULT_ROLE_NAME/secret-id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment