Skip to content

Instantly share code, notes, and snippets.

@abhigets
Last active September 11, 2019 14:47
Show Gist options
  • Save abhigets/a12bc533efac502e82b9219858bdbda8 to your computer and use it in GitHub Desktop.
Save abhigets/a12bc533efac502e82b9219858bdbda8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#get the policy from aws and store it in variable
BUCKET_NAME="your bucket name"
S3_OLD_POLICY=$(aws s3api get-bucket-policy --bucket ${BUCKET_NAME} --query Policy --output text)
echo $S3_OLD_POLICY
echo "---------------"
#get the ip of my host
MY_IP=$(curl ipecho.net/plain;)
echo $MY_IP
echo "---------------"
#append my ip to
S3_NEW_POLICY=$(echo $S3_OLD_POLICY | sed 's;]}}}]};,\"'$MY_IP'\"]}}}]};g')
echo $S3_NEW_POLICY
echo "---------------"
#apply new policy to s3 bucket
aws s3api put-bucket-policy --bucket ${BUCKET_NAME} --policy ${S3_NEW_POLICY}
#if update and restore are different script then one can write the old policy to a file to retrive to later while restoring
#echo $S3_OLD_POLICY > "S3_OLD_POLICY.json"
#Do your task
#like i ran e2e test
#restoring the old policy
#restore the policy if it was initially stored in a file
#S3_OLD_POLICY=$(<"S3_OLD_POLICY.json")
aws s3api put-bucket-policy --bucket ${BUCKET_NAME} --policy ${S3_OLD_POLICY}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment