Skip to content

Instantly share code, notes, and snippets.

@ibrezm1
Last active February 10, 2022 04:35
Show Gist options
  • Save ibrezm1/5e981e6be518b63d52f7853eb528b3ba to your computer and use it in GitHub Desktop.
Save ibrezm1/5e981e6be518b63d52f7853eb528b3ba to your computer and use it in GitHub Desktop.
Rotate GCP google cloud serviceaccount keys
# From https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-gcloud
# Enable IAM API https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-gcloud
# following script will rotate all json keys present in the current folder
# Need the permission to roate keys to the current service accout key admin
mkdir -p backup
echo Starting key rotation
for filename in *.json; do
echo Starting $filename
cp $filename backup/$filename
#filname=zeta-yen-319702-12df01c0a305.json
saemail=$(jq -r ".client_email" $filename)
sakey=$(jq -r ".private_key_id" $filename)
echo Starting to rotate $saemail $saemail
gcloud auth activate-service-account --key-file $filename
gcloud iam service-accounts keys create newfile.json --iam-account $saemail
gcloud iam service-accounts keys delete $sakey --iam-account $saemail --quiet
gcloud iam service-accounts keys list --iam-account=$saemail
mv newfile.json $filename
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment