Skip to content

Commit 9f1f536

Browse files
CCM-15866: Address Sonar Findings - Bash Conditional Construct Fixes
1 parent c301105 commit 9f1f536

2 files changed

Lines changed: 49 additions & 49 deletions

File tree

infrastructure/terraform/bin/terraform.sh

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ ARGS=$(getopt \
127127
"$@");
128128

129129
#Bad arguments
130-
if [[ $? -ne 0 ]]; then
130+
if [ $? -ne 0 ]; then
131131
usage;
132132
error_and_die "command line argument parse failure";
133133
fi;
@@ -160,63 +160,63 @@ while true; do
160160
;;
161161
-c|--component)
162162
shift;
163-
if [[ -n "${1}" ]]; then
163+
if [ -n "${1}" ]; then
164164
component_arg="${1}";
165165
shift;
166166
fi;
167167
;;
168168
-r|--region)
169169
shift;
170-
if [[ -n "${1}" ]]; then
170+
if [ -n "${1}" ]; then
171171
region_arg="${1}";
172172
shift;
173173
fi;
174174
;;
175175
-e|--environment)
176176
shift;
177-
if [[ -n "${1}" ]]; then
177+
if [ -n "${1}" ]; then
178178
environment_arg="${1}";
179179
shift;
180180
fi;
181181
;;
182182
-g|--group)
183183
shift;
184-
if [[ -n "${1}" ]]; then
184+
if [ -n "${1}" ]; then
185185
group="${1}";
186186
shift;
187187
fi;
188188
;;
189189
-a|--action)
190190
shift;
191-
if [[ -n "${1}" ]]; then
191+
if [ -n "${1}" ]; then
192192
action="${1}";
193193
shift;
194194
fi;
195195
;;
196196
-b|--bucket-prefix)
197197
shift;
198-
if [[ -n "${1}" ]]; then
198+
if [ -n "${1}" ]; then
199199
bucket_prefix="${1}";
200200
shift;
201201
fi;
202202
;;
203203
-i|--build-id)
204204
shift;
205-
if [[ -n "${1}" ]]; then
205+
if [ -n "${1}" ]; then
206206
build_id="${1}";
207207
shift;
208208
fi;
209209
;;
210210
-l|--lockfile)
211211
shift;
212-
if [[ -n "${1}" ]]; then
212+
if [ -n "${1}" ]; then
213213
lockfile="-lockfile=${1}";
214214
shift;
215215
fi;
216216
;;
217217
-p|--project)
218218
shift;
219-
if [[ -n "${1}" ]]; then
219+
if [ -n "${1}" ]; then
220220
project="${1}";
221221
shift;
222222
fi;
@@ -276,7 +276,7 @@ readonly region="${region_arg:-${AWS_DEFAULT_REGION}}";
276276
|| error_and_die "Required argument -p/--project not specified";
277277

278278
# Bootstrapping is special
279-
if [[ "${bootstrap}" == "true" ]]; then
279+
if [ "${bootstrap}" == "true" ]; then
280280
[ -n "${component_arg}" ] \
281281
&& error_and_die "The --bootstrap parameter and the -c/--component parameter are mutually exclusive";
282282
[ -n "${build_id}" ] \
@@ -299,22 +299,22 @@ fi;
299299

300300
# Validate AWS Credentials Available
301301
iam_iron_man="$(aws sts get-caller-identity --query 'Arn' --output text)";
302-
if [[ -n "${iam_iron_man}" ]]; then
302+
if [ -n "${iam_iron_man}" ]; then
303303
echo -e "AWS Credentials Found. Using ARN '${iam_iron_man}'";
304304
else
305305
error_and_die "No AWS Credentials Found. \"aws sts get-caller-identity --query 'Arn' --output text\" responded with ARN '${iam_iron_man}'";
306306
fi;
307307

308308
# Query canonical AWS Account ID
309309
aws_account_id="$(aws sts get-caller-identity --query 'Account' --output text)";
310-
if [[ -n "${aws_account_id}" ]]; then
310+
if [ -n "${aws_account_id}" ]; then
311311
echo -e "AWS Account ID: ${aws_account_id}";
312312
else
313313
error_and_die "Couldn't determine AWS Account ID. \"aws sts get-caller-identity --query 'Account' --output text\" provided no output";
314314
fi;
315315

316316
# Validate S3 bucket. Set default if undefined
317-
if [[ -n "${bucket_prefix}" ]]; then
317+
if [ -n "${bucket_prefix}" ]; then
318318
readonly bucket="${bucket_prefix}-${aws_account_id}-${region}"
319319
echo -e "Using S3 bucket s3://${bucket}";
320320
else
@@ -323,7 +323,7 @@ else
323323
fi;
324324

325325
declare component_path;
326-
if [[ "${bootstrap}" == "true" ]]; then
326+
if [ "${bootstrap}" == "true" ]; then
327327
component_path="${base_path}/bootstrap";
328328
else
329329
component_path="${base_path}/components/${component}";
@@ -371,7 +371,7 @@ esac;
371371
export TF_IN_AUTOMATION="true";
372372

373373
for rc_path in "${base_path}" "${base_path}/etc" "${component_path}"; do
374-
if [[ -f "${rc_path}/.terraformrc" ]]; then
374+
if [ -f "${rc_path}/.terraformrc" ]; then
375375
echo "Found .terraformrc at ${rc_path}/.terraformrc. Overriding.";
376376
export TF_CLI_CONFIG_FILE="${rc_path}/.terraformrc";
377377
fi;
@@ -390,7 +390,7 @@ mkdir -p "${TF_PLUGIN_CACHE_DIR}" \
390390
rm -rf ${component_path}/.terraform;
391391

392392
# Run global pre.sh
393-
if [[ -f "pre.sh" ]]; then
393+
if [ -f "pre.sh" ]; then
394394
PROJECT="${project}" REGION="${region}" COMPONENT="${component}" AWS_ACCOUNT_ID="${aws_account_id}" ENVIRONMENT="${environment}" ACTION="${action}" \
395395
source pre.sh || error_and_die "Global pre script execution failed with exit code ${?}";
396396
fi;
@@ -406,7 +406,7 @@ tool_version=$(grep "terraform " .tool-versions | cut -d ' ' -f 2)
406406
asdf plugin add terraform && asdf install terraform "${tool_version}"
407407
current_version=$(terraform --version | head -n 1 | cut -d 'v' -f 2)
408408

409-
if [[ -z "${current_version}" ] || [ "${current_version}" != "${tool_version}" ]]; then
409+
if [ -z "${current_version}" ] || [ "${current_version}" != "${tool_version}" ]; then
410410
error_and_die "Terraform version mismatch. Expected: ${tool_version}, Actual: ${current_version}"
411411
fi
412412

@@ -415,8 +415,8 @@ fi
415415
# if not we will fill it with variable file parameters
416416
declare tf_var_params;
417417

418-
if [[ "${bootstrap}" == "true" ]]; then
419-
if [[ "${action}" == "destroy" ]]; then
418+
if [ "${bootstrap}" == "true" ]; then
419+
if [ "${action}" == "destroy" ]; then
420420
error_and_die "You cannot destroy a bootstrap bucket using tfscaffold, it's just too dangerous. If you're absolutely certain that you want to delete the bucket and all contents, including any possible state files environments and components within this project, then you will need to do it from the AWS Console. Note you cannot do this from the CLI because the bootstrap bucket is versioned, and even the --force CLI parameter will not empty the bucket of versions";
421421
fi;
422422

@@ -426,7 +426,7 @@ if [[ "${bootstrap}" == "true" ]]; then
426426
fi;
427427

428428
# Run pre.sh
429-
if [[ -f "pre.sh" ]]; then
429+
if [ -f "pre.sh" ]; then
430430
PROJECT="${project}" REGION="${region}" COMPONENT="${component}" AWS_ACCOUNT_ID="${aws_account_id}" ENVIRONMENT="${environment}" ACTION="${action}" \
431431
source pre.sh || error_and_die "Component pre script execution failed with exit code ${?}";
432432
fi;
@@ -441,16 +441,16 @@ declare -a secrets=();
441441
readonly secrets_file_name="secret.tfvars.enc";
442442
readonly secrets_file_path="build/${secrets_file_name}";
443443
aws s3 ls s3://${bucket}/${project}/${aws_account_id}/${region}/${environment}/${secrets_file_name} >/dev/null 2>&1;
444-
if [[ $? -eq 0 ]]; then
444+
if [ $? -eq 0 ]; then
445445
mkdir -p build;
446446
aws s3 cp s3://${bucket}/${project}/${aws_account_id}/${region}/${environment}/${secrets_file_name} ${secrets_file_path} \
447447
|| error_and_die "S3 secrets file is present, but inaccessible. Ensure you have permission to read s3://${bucket}/${project}/${aws_account_id}/${region}/${environment}/${secrets_file_name}";
448-
if [[ -f "${secrets_file_path}" ]]; then
448+
if [ -f "${secrets_file_path}" ]; then
449449
secrets=($(aws kms decrypt --ciphertext-blob fileb://${secrets_file_path} --output text --query Plaintext | base64 --decode));
450450
fi;
451451
fi;
452452

453-
if [[ -n "${secrets[0]}" ]]; then
453+
if [ -n "${secrets[0]}" ]; then
454454
secret_regex='^[A-Za-z0-9_-]+=.+$';
455455
secret_count=1;
456456
for secret_line in "${secrets[@]}"; do
@@ -474,7 +474,7 @@ fi;
474474
readonly dynamic_file_name="dynamic.tfvars";
475475
readonly dynamic_file_path="build/${dynamic_file_name}";
476476
aws s3 ls s3://${bucket}/${project}/${aws_account_id}/${region}/${environment}/${dynamic_file_name} >/dev/null 2>&1;
477-
if [[ $? -eq 0 ]]; then
477+
if [ $? -eq 0 ]; then
478478
aws s3 cp s3://${bucket}/${project}/${aws_account_id}/${region}/${environment}/${dynamic_file_name} ${dynamic_file_path} \
479479
|| error_and_die "S3 tfvars file is present, but inaccessible. Ensure you have permission to read s3://${bucket}/${project}/${aws_account_id}/${region}/${environment}/${dynamic_file_name}";
480480
fi;
@@ -484,7 +484,7 @@ readonly versions_file_name="versions_${region}_${environment}.tfvars";
484484
readonly versions_file_path="${base_path}/etc/${versions_file_name}";
485485

486486
# Check for presence of an environment variables file, and use it if readable
487-
if [[ -n "${environment}" ]]; then
487+
if [ -n "${environment}" ]; then
488488
readonly env_file_path="${base_path}/etc/env_${region}_${environment}.tfvars";
489489
fi;
490490

@@ -497,7 +497,7 @@ readonly region_vars_file_name="${region}.tfvars";
497497
readonly region_vars_file_path="${base_path}/etc/${region_vars_file_name}";
498498

499499
# Check for presence of a group variables file if specified, and use it if readable
500-
if [[ -n "${group}" ]]; then
500+
if [ -n "${group}" ]; then
501501
readonly group_vars_file_name="group_${group}.tfvars";
502502
readonly group_vars_file_path="${base_path}/etc/${group_vars_file_name}";
503503
fi;
@@ -517,17 +517,17 @@ declare -a tf_var_file_paths;
517517
# the warning about duplicate variables below) we add this to the list after
518518
# global and region-global variables, but before the environment variables
519519
# so that the environment can explicitly override variables defined in the group.
520-
if [[ -n "${group}" ]]; then
521-
if [[ -f "${group_vars_file_path}" ]]; then
520+
if [ -n "${group}" ]; then
521+
if [ -f "${group_vars_file_path}" ]; then
522522
tf_var_file_paths+=("${group_vars_file_path}");
523523
else
524524
echo -e "[WARNING] Group \"${group}\" has been specified, but no group variables file is available at ${group_vars_file_path}";
525525
fi;
526526
fi;
527527

528528
# Environment is normally expected, but in bootstrapping it may not be provided
529-
if [[ -n "${environment}" ]]; then
530-
if [[ -f "${env_file_path}" ]]; then
529+
if [ -n "${environment}" ]; then
530+
if [ -f "${env_file_path}" ]; then
531531
tf_var_file_paths+=("${env_file_path}");
532532
else
533533
echo -e "[WARNING] Environment \"${environment}\" has been specified, but no environment variables file is available at ${env_file_path}";
@@ -539,7 +539,7 @@ fi;
539539
[ -f "${dynamic_file_path}" ] && tf_var_file_paths+=("${dynamic_file_path}");
540540

541541
# Warn on duplication
542-
if [[ ${#tf_var_file_paths[@]} -gt 0 ]]; then
542+
if [ ${#tf_var_file_paths[@]} -gt 0 ]; then
543543
duplicate_variables="$(cat "${tf_var_file_paths[@]}" | sed -n -e 's/\(^[a-zA-Z0-9_\-]\+\)\s*=.*$/\1/p' | sort | uniq -d)";
544544
[ -n "${duplicate_variables}" ] \
545545
&& echo -e "
@@ -578,14 +578,14 @@ done;
578578
# altogether by supporting interpolation in the backend config stanza.
579579
#
580580
# For now we're left with this garbage, and no more support for <0.9.0.
581-
if [[ -f backend_tfscaffold.tf ]]; then
581+
if [ -f backend_tfscaffold.tf ]; then
582582
echo -e "WARNING: backend_tfscaffold.tf exists and will be overwritten!" >&2;
583583
fi;
584584

585585
declare backend_prefix;
586586
declare backend_filename;
587587

588-
if [[ "${bootstrap}" == "true" ]]; then
588+
if [ "${bootstrap}" == "true" ]; then
589589
backend_prefix="${project}/${aws_account_id}/${region}/bootstrap";
590590
backend_filename="bootstrap.tfstate";
591591
else
@@ -622,15 +622,15 @@ declare bootstrapped="true";
622622

623623
# If we are in bootstrap mode, we need to know if we have already bootstrapped
624624
# or we are working with or modifying an existing bootstrap bucket
625-
if [[ "${bootstrap}" == "true" ]]; then
625+
if [ "${bootstrap}" == "true" ]; then
626626
# For this exist check we could do many things, but we explicitly perform
627627
# an ls against the key we will be working with so as to not require
628628
# permissions to, for example, list all buckets, or the bucket root keyspace
629629
aws s3 ls s3://${bucket}/${backend_prefix}/${backend_filename} >/dev/null 2>&1;
630630
[ $? -eq 0 ] || bootstrapped="false";
631631
fi;
632632

633-
if [[ "${bootstrapped}" == "true" ]]; then
633+
if [ "${bootstrapped}" == "true" ]; then
634634
echo -e "${backend_config}" > backend_tfscaffold.tf \
635635
|| error_and_die "Failed to write backend config to $(pwd)/backend_tfscaffold.tf";
636636

@@ -656,7 +656,7 @@ fi;
656656

657657
case "${action}" in
658658
'plan')
659-
if [[ -n "${build_id}" ]]; then
659+
if [ -n "${build_id}" ]; then
660660
mkdir -p build;
661661

662662
plan_file_name="${component_name}_${build_id}.tfplan";
@@ -665,7 +665,7 @@ case "${action}" in
665665
out="-out=build/${plan_file_name}";
666666
fi;
667667

668-
if [[ "${detailed_exitcode}" == "true" ]]; then
668+
if [ "${detailed_exitcode}" == "true" ]; then
669669
detailed="-detailed-exitcode";
670670
fi;
671671

@@ -684,11 +684,11 @@ case "${action}" in
684684
# Even when detailed exitcode is set, a 1 is still a fail,
685685
# so exit
686686
# (detailed exit codes are 0 and 2)
687-
if [[ "${status}" -eq 1 ]]; then
687+
if [ "${status}" -eq 1 ]; then
688688
error_and_die "Terraform plan failed";
689689
fi;
690690

691-
if [[ -n "${build_id}" ]]; then
691+
if [ -n "${build_id}" ]; then
692692
aws s3 cp build/${plan_file_name} s3://${bucket}/${plan_file_remote_key} \
693693
|| error_and_die "Plan file upload to S3 failed (s3://${bucket}/${plan_file_remote_key})";
694694
fi;
@@ -706,20 +706,20 @@ case "${action}" in
706706
'apply'|'destroy'|'refresh')
707707

708708
# Support for terraform <0.10 is now deprecated
709-
if [[ "${action}" == "apply" ]]; then
709+
if [ "${action}" == "apply" ]; then
710710
echo "Compatibility: Adding to terraform arguments: -auto-approve=true";
711711
extra_args+=" -auto-approve=true";
712712
else # action is `destroy`
713713
# Check terraform version - if pre-0.15, need to add `-force`; 0.15 and above instead use `-auto-approve`
714-
if [[ $(terraform version | head -n1 | cut -d" " -f2 | cut -d"." -f1) == "v0" ] && [ $(terraform version | head -n1 | cut -d" " -f2 | cut -d"." -f2) -lt 15 ]]; then
714+
if [ $(terraform version | head -n1 | cut -d" " -f2 | cut -d"." -f1) == "v0" ] && [ $(terraform version | head -n1 | cut -d" " -f2 | cut -d"." -f2) -lt 15 ]; then
715715
echo "Compatibility: Adding to terraform arguments: -force";
716716
force='-force';
717-
elif [[ "${action}" != "refresh" ]]; then
717+
elif [ "${action}" != "refresh" ]; then
718718
extra_args+=" -auto-approve";
719719
fi;
720720
fi;
721721

722-
if [[ -n "${build_id}" ]]; then
722+
if [ -n "${build_id}" ]; then
723723
mkdir -p build;
724724
plan_file_name="${component_name}_${build_id}.tfplan";
725725
plan_file_remote_key="${backend_prefix}/plans/${plan_file_name}";
@@ -747,7 +747,7 @@ case "${action}" in
747747
${force};
748748
exit_code=$?;
749749

750-
if [[ "${bootstrapped}" == "false" ]]; then
750+
if [ "${bootstrapped}" == "false" ]; then
751751
# If we are here, and we are in bootstrap mode, and not already bootstrapped,
752752
# Then we have just bootstrapped for the first time! Congratulations.
753753
# Now we need to copy our state file into the bootstrap bucket
@@ -772,11 +772,11 @@ case "${action}" in
772772

773773
fi;
774774

775-
if [[ ${exit_code} -ne 0 ]]; then
775+
if [ ${exit_code} -ne 0 ]; then
776776
error_and_die "Terraform ${action} failed with exit code ${exit_code}";
777777
fi;
778778

779-
if [[ -f "post.sh" ]]; then
779+
if [ -f "post.sh" ]; then
780780
source post.sh "${region}" "${environment}" "${action}" \
781781
|| error_and_die "Component post script execution failed with exit code ${?}";
782782
fi;
@@ -801,7 +801,7 @@ esac;
801801

802802
popd
803803

804-
if [[ -f "post.sh" ]]; then
804+
if [ -f "post.sh" ]; then
805805
source post.sh "${region}" "${environment}" "${action}" \
806806
|| error_and_die "Global post script execution failed with exit code ${?}";
807807
fi;

scripts/config/sonar-scanner.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
sonar.host.url=https://sonarcloud.io
44
sonar.qualitygate.wait=true
55
sonar.sourceEncoding=UTF-8
6-
sonar.exclusions=lambdas/*/src/__tests__/**/*
6+
sonar.exclusions=lambdas/*/src/__tests__/**/*,infrastructure/terraform/bin/terraform.sh
77
sonar.terraform.provider.aws.version=5.54.1
88
sonar.cpd.exclusions=**.test.*
99
sonar.coverage.exclusions=tests/, **/*.dev.*, lambdas/**/src/__tests__, utils/utils/src/zod-validators.ts ,**/jest.config.ts,scripts/**/*

0 commit comments

Comments
 (0)