Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into keithduncan/fix-cre…
Browse files Browse the repository at this point in the history
…dential-path-windows
  • Loading branch information
keithduncan committed Aug 31, 2021
2 parents 8766f51 + d498373 commit 9d82456
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions git-credential-s3-secrets
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,51 @@ parse_url() {
done
}

s3_bucket_region() {
local bucket="$1"

local guess_region="${AWS_DEFAULT_REGION:-}"
if [ -z "${guess_region}" ]
then
# This plug-in may not be executing in an AWS VPC or have access to the IDMS
# Fail fast with the --connect-timeout flag
local token=$(curl -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 60" --fail --silent --show-error --location --connect-timeout 5 http://169.254.169.254/latest/api/token)
if [ -n "${token}" ]
then
guess_region=$(curl -H "X-aws-ec2-metadata-token: $token" --fail --silent --show-error --location http://169.254.169.254/latest/meta-data/placement/region)
fi
fi
if [ -z "${guess_region}" ]
then
guess_region="us-east-1"
fi

# Buckets in us-east-1 have a LocationConstraint of null
# https://docs.aws.amazon.com/cli/latest/reference/s3api/get-bucket-location.html
local bucket_region="$(aws s3api get-bucket-location --bucket "${bucket}" --region "${guess_region}" --output text --query "LocationConstraint || 'us-east-1'")"

echo "${bucket_region}"
}

s3_download() {
local bucket="$1"
local key="$2"
local aws_s3_args=("--quiet" "--region=$AWS_DEFAULT_REGION")

local bucket_region="$(s3_bucket_region "${bucket}")"
if [ -z "${bucket_region}" ]
then
echo "Could not determine the bucket region for ${bucket}" >&2
exit 2
fi

local aws_s3_args=("--quiet" "--region=${bucket_region}")

if [[ "${BUILDKITE_USE_KMS:-true}" =~ ^(true|1)$ ]] ; then
aws_s3_args+=("--sse" "aws:kms")
fi

if ! aws s3 cp "${aws_s3_args[@]}" "s3://$1/$2" - ; then
local s3_uri="s3://${bucket}/${key}"
if ! aws s3 cp "${aws_s3_args[@]}" "${s3_uri}" - ; then
echo "Failed to download s3://$bucket/$key" >&2
exit 1
fi
Expand Down

0 comments on commit 9d82456

Please sign in to comment.