Last active
February 10, 2022 04:35
-
-
Save ibrezm1/5e981e6be518b63d52f7853eb528b3ba to your computer and use it in GitHub Desktop.
Rotate GCP google cloud serviceaccount keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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