Skip to content

Commit 74d0b9f

Browse files
committed
Update local agent wrapper script with AWS config/creds flag
1 parent 78dbc96 commit 74d0b9f

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

local_builds/codebuild_build.sh

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
#!/bin/sh
22

3+
function allOSRealPath() {
4+
case $1 in
5+
/* ) echo "$1"; exit;;
6+
* ) echo "$PWD/${1#./}"; exit;;
7+
esac
8+
}
9+
310
function usage {
411
echo "usage: codebuild_build.sh [-i image_name] [-a artifact_output_directory] [options]"
512
echo "Required:"
613
echo " -i Used to specify the customer build container image."
714
echo " -a Used to specify an artifact output directory."
815
echo "Options:"
916
echo " -s Used to specify a source directory. Defaults to the current working directory."
17+
echo " -c Use the AWS configuration and credentials from your local host. This includes ~/.aws and any AWS_* environment variables."
1018
echo " -b Used to specify a buildspec override file. Defaults to buildspec.yml in the source directory."
1119
echo " -e Used to specify a file containing environment variables."
1220
echo " Environment variable file format:"
@@ -20,12 +28,14 @@ function usage {
2028

2129
image_flag=false
2230
artifact_flag=false
31+
awsconfig_flag=false
2332

24-
while getopts "i:a:s:b:e:h" opt; do
33+
while getopts "ci:a:s:b:e:h" opt; do
2534
case $opt in
2635
i ) image_flag=true; image_name=$OPTARG;;
2736
a ) artifact_flag=true; artifact_dir=$OPTARG;;
2837
b ) buildspec=$OPTARG;;
38+
c ) awsconfig_flag=true;;
2939
s ) source_dir=$OPTARG;;
3040
e ) environment_variable_file=$OPTARG;;
3141
h ) usage; exit;;
@@ -50,34 +60,52 @@ then
5060
exit 1
5161
fi
5262

53-
5463
if [ -z "$source_dir" ]
5564
then
5665
source_dir="$(pwd)"
5766
else
58-
source_dir=$(realpath $source_dir)
67+
source_dir=$(allOSRealPath $source_dir)
5968
fi
6069

6170
docker_command="docker run -it -v /var/run/docker.sock:/var/run/docker.sock -e \
6271
\"IMAGE_NAME=$image_name\" -e \
63-
\"ARTIFACTS=$(realpath $artifact_dir)\" -e \
72+
\"ARTIFACTS=$(allOSRealPath $artifact_dir)\" -e \
6473
\"SOURCE=$source_dir\""
6574

6675
if [ -n "$buildspec" ]
6776
then
68-
docker_command+=" -e \"BUILDSPEC=$buildspec\""
77+
docker_command+=" -e \"BUILDSPEC=$(allOSRealPath $buildspec)\""
6978
fi
7079

7180
if [ -n "$environment_variable_file" ]
7281
then
73-
docker_command+=" -v $(dirname $(realpath $environment_variable_file)):/LocalBuild/envFile/ -e \"ENV_VAR_FILE=$(basename $environment_variable_file)\""
82+
docker_command+=" -v $(dirname $(allOSRealPath $environment_variable_file)):/LocalBuild/envFile/ -e \"ENV_VAR_FILE=$(basename $environment_variable_file)\""
83+
fi
84+
85+
if $awsconfig_flag
86+
then
87+
if [ -d "$HOME/.aws" ]
88+
then
89+
docker_command+=" -e \"AWS_CONFIGURATION=$HOME/.aws\""
90+
else
91+
docker_command+=" -e \"AWS_CONFIGURATION=NONE\""
92+
fi
93+
docker_command+="$(env | grep ^AWS_ | while read -r line; do echo " -e \"$line\""; done )"
7494
fi
7595

7696
docker_command+=" amazon/aws-codebuild-local:latest"
7797

98+
# Note we do not expose the AWS_SECRET_ACCESS_KEY or the AWS_SESSION_TOKEN
99+
exposed_command=$docker_command
100+
secure_variables=( "AWS_SECRET_ACCESS_KEY=" "AWS_SESSION_TOKEN=")
101+
for variable in "${secure_variables[@]}"
102+
do
103+
exposed_command="$(echo $exposed_command | sed "s/\($variable\)[^ ]*/\1********\"/")"
104+
done
105+
78106
echo "Build Command:"
79107
echo ""
80-
echo $docker_command
108+
echo $exposed_command
81109
echo ""
82110

83111
eval $docker_command

0 commit comments

Comments
 (0)