Skip to content

Commit 05a5bf9

Browse files
fix: DTOSS 12663 Remediate Sonarqube warnings script files (#1886)
* updated script files * corrected sonarqube issue * fixed sonarqube issues
1 parent 634023e commit 05a5bf9

12 files changed

Lines changed: 53 additions & 45 deletions

File tree

application/CohortManager/Set-up/scripts/get_local_settings.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ process_files() {
3535
# Read the content of the found local.settings.json file and echo it into the target file
3636
cat "$file" >> $OUTPUT_SCRIPT
3737
# Check if this is the last file and add EOF accordingly
38-
if [ $index -lt $file_count ]; then
38+
if [[ $index -lt $file_count ]]; then
3939
echo -e "\nEOF" >> $OUTPUT_SCRIPT
4040
fi
4141
done

scripts/azure/GetImageTagsByManifest.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@ while [[ "$#" -gt 0 ]]; do
5555
shift
5656
done
5757

58-
if [ -z "${containerRegistry}" ]; then
58+
if [[ -z "${containerRegistry}" ]]; then
5959
echo "Please provide a container registry argument"
6060
exit 1
6161
fi
6262

6363
# stop processing if no image name is provided
64-
if [ -z "${functionAppName}" ]; then
64+
if [[ -z "${functionAppName}" ]]; then
6565
echo "Please provide an image name as an argument"
6666
exit 1
6767
fi
6868

69-
if [ -z "${subscriptionId}" ]; then
69+
if [[ -z "${subscriptionId}" ]]; then
7070
echo "Please provide a Subscription ID argument"
7171
exit 1
7272
fi
7373

74-
if [ -z "${resourceGroup}" ]; then
74+
if [[ -z "${resourceGroup}" ]]; then
7575
echo "Please provide a resource group argument"
7676
exit 1
7777
fi
7878

79-
if [ verbose == true ]; then
79+
if [[ verbose == true ]]; then
8080
echo "function app name: $functionAppName"
8181
echo "subscription Id: $subscriptionId"
8282
echo "resourceGroup: $resourceGroup"
@@ -88,13 +88,13 @@ fi
8888
digest=$(az acr repository show --name $containerRegistry --image $functionAppName:$tag --query 'digest' --output tsv 2> /dev/null)
8989

9090
# stop processing if the image does not exist
91-
if [ -z "$digest" ]; then
91+
if [[ -z "$digest" ]]; then
9292
echo "Image $image not found in $containerRegistry"
9393
exit 1
9494
fi
9595

9696
# echo the digest
97-
if [ verbose == true ]; then
97+
if [[ verbose == true ]]; then
9898
echo $digest
9999
fi
100100

scripts/deployment/get-docker-names.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ IFS=$', \n'
5454

5555
echo "Adding Docker compose file includes..."
5656
files_to_process=(${COMPOSE_FILES_CSV})
57-
while [ ${#files_to_process[@]} -gt 0 ]; do
57+
while [[ ${#files_to_process[@]} -gt 0 ]]; do
5858
compose_file="${files_to_process[0]}"
5959
files_to_process=("${files_to_process[@]:1}") # Remove the first file from the list
6060
includes=($(yq -r '.include[]' "${compose_file}"))
@@ -121,7 +121,7 @@ for compose_file in ${COMPOSE_FILES_CSV}; do
121121
echo
122122
done
123123

124-
if [ ${#non_matched_changes[@]} -ne 0 ]; then
124+
if [[ ${#non_matched_changes[@]} -ne 0 ]]; then
125125
# Remove duplicates (non-matched items across several compose files)
126126
mapfile -t unique_changes < <(printf "%s\n" "${non_matched_changes[@]}" | sort -u)
127127

scripts/docker/dgoss.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ error() {
2424
cleanup() {
2525
set +e
2626
{ kill "$log_pid" && wait "$log_pid"; } 2> /dev/null
27-
if [ -n "$CONTAINER_LOG_OUTPUT" ]; then
27+
if [[ -n "$CONTAINER_LOG_OUTPUT" ]]; then
2828
cp "$tmp_dir/docker_output.log" "$CONTAINER_LOG_OUTPUT"
2929
fi
3030
rm -rf "$tmp_dir"
@@ -47,7 +47,7 @@ run(){
4747
case "$GOSS_FILES_STRATEGY" in
4848
mount)
4949
info "Starting $CONTAINER_RUNTIME container"
50-
if [ "$CONTAINER_RUNTIME" == "podman" -a $# == 2 ]; then
50+
if [[ "$CONTAINER_RUNTIME" == "podman" -a $# == 2 ]]; then
5151
id=$($CONTAINER_RUNTIME run -d -v "$tmp_dir:/goss:z" "${@:2}" sleep infinity)
5252
else
5353
id=$($CONTAINER_RUNTIME run -d -v "$tmp_dir:/goss:z" "${@:2}")
@@ -113,7 +113,7 @@ case "$1" in
113113
fi
114114
[[ $GOSS_SLEEP ]] && { info "Sleeping for $GOSS_SLEEP"; sleep "$GOSS_SLEEP"; }
115115
info "Container health"
116-
if [ "true" != "$($CONTAINER_RUNTIME inspect -f '{{.State.Running}}' "$id")" ]; then
116+
if [[ "true" != "$($CONTAINER_RUNTIME inspect -f '{{.State.Running}}' "$id")" ]]; then
117117
$CONTAINER_RUNTIME logs "$id" >&2
118118
error "the container failed to start"
119119
fi

scripts/docker/docker.lib.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function version-create-effective-file() {
127127
local version_file="$dir/VERSION"
128128
local build_datetime=${BUILD_DATETIME:-$(date -u +'%Y-%m-%dT%H:%M:%S%z')}
129129

130-
if [ -f "$version_file" ]; then
130+
if [[ -f "$version_file" ]]; then
131131
# shellcheck disable=SC2002
132132
cat "$version_file" | \
133133
sed "s/\(\${yyyy}\|\$yyyy\)/$(date --date="${build_datetime}" -u +"%Y")/g" | \
@@ -167,9 +167,9 @@ function docker-get-image-version-and-pull() {
167167
# match it by name and version regex, if given.
168168
local versions_file="${TOOL_VERSIONS:=$(git rev-parse --show-toplevel)/.tool-versions}"
169169
local version="latest"
170-
if [ -f "$versions_file" ]; then
170+
if [[ -f "$versions_file" ]]; then
171171
line=$(grep "docker/${name} " "$versions_file" | sed "s/^#\s*//; s/\s*#.*$//" | grep "${match_version:-".*"}")
172-
[ -n "$line" ] && version=$(echo "$line" | awk '{print $2}')
172+
[[ -n "$line" ]] && version=$(echo "$line" | awk '{print $2}')
173173
fi
174174

175175
# Split the image version into two, tag name and digest sha256.
@@ -178,7 +178,7 @@ function docker-get-image-version-and-pull() {
178178

179179
# Check if the image exists locally already
180180
if ! docker images | awk '{ print $1 ":" $2 }' | grep -q "^${name}:${tag}$"; then
181-
if [ "$digest" != "latest" ]; then
181+
if [[ "$digest" != "latest" ]]; then
182182
# Pull image by the digest sha256 and tag it
183183
docker pull \
184184
--platform linux/amd64 \
@@ -222,19 +222,19 @@ function _replace-image-latest-by-specific-version() {
222222
local dockerfile="${dir}/Dockerfile.effective"
223223
local build_datetime=${BUILD_DATETIME:-$(date -u +'%Y-%m-%dT%H:%M:%S%z')}
224224

225-
if [ -f "$versions_file" ]; then
225+
if [[ -f "$versions_file" ]]; then
226226
# First, list the entries specific for Docker to take precedence, then the rest but exclude comments
227227
content=$(grep " docker/" "$versions_file"; grep -v " docker/" "$versions_file" ||: | grep -v "^#")
228228
echo "$content" | while IFS= read -r line; do
229-
[ -z "$line" ] && continue
229+
[[ -z "$line" ]] && continue
230230
line=$(echo "$line" | sed "s/^#\s*//; s/\s*#.*$//" | sed "s;docker/;;")
231231
name=$(echo "$line" | awk '{print $1}')
232232
version=$(echo "$line" | awk '{print $2}')
233233
sed -i "s;\(FROM .*\)${name}:latest;\1${name}:${version};g" "$dockerfile"
234234
done
235235
fi
236236

237-
if [ -f "$dockerfile" ]; then
237+
if [[ -f "$dockerfile" ]]; then
238238
# shellcheck disable=SC2002
239239
cat "$dockerfile" | \
240240
sed "s/\(\${yyyy}\|\$yyyy\)/$(date --date="${build_datetime}" -u +"%Y")/g" | \
@@ -292,9 +292,9 @@ function _get-git-branch-name() {
292292

293293
local branch_name=$(git rev-parse --abbrev-ref HEAD)
294294

295-
if [ -n "${GITHUB_HEAD_REF:-}" ]; then
295+
if [[ -n "${GITHUB_HEAD_REF:-}" ]]; then
296296
branch_name=$GITHUB_HEAD_REF
297-
elif [ -n "${GITHUB_REF:-}" ]; then
297+
elif [[ -n "${GITHUB_REF:-}" ]]; then
298298
# shellcheck disable=SC2001
299299
branch_name=$(echo "$GITHUB_REF" | sed "s#refs/heads/##")
300300
fi

scripts/docker/tests/docker.test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function main() {
4444
done
4545
echo "Total: ${#tests[@]}, Passed: $(( ${#tests[@]} - status )), Failed: $status"
4646
test-docker-suite-teardown
47-
[ $status -gt 0 ] && return 1 || return 0
47+
[[ $status -gt 0 ]] && return 1 || return 0
4848
}
4949

5050
# ==============================================================================

scripts/githooks/check-markdown-format.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ function main() {
4949
"branch")
5050
files="$( (git diff --diff-filter=ACMRT --name-only "${BRANCH_NAME:-origin/main}" "*.md"; git diff --name-only "*.md") | sort | uniq )"
5151
;;
52+
*)
53+
echo "Unexpected value for 'check' variable: $check"
54+
exit 1 # exit code for incorrectly formatted file
55+
;;
5256
esac
5357

54-
if [ -n "$files" ]; then
58+
if [[ -n "$files" ]]; then
5559
if command -v markdownlint > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
5660
files="$files" run-markdownlint-natively
5761
else

scripts/githooks/scan-secrets.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ function get-cmd-to-run() {
6060
"staged-changes")
6161
cmd="protect --source $dir --verbose --staged"
6262
;;
63+
*)
64+
echo "Unexpected value for 'check' variable: $check"
65+
exit 126 # Unknown flag exit code
66+
;;
6367
esac
6468
# Include base line file if it exists
65-
if [ -f "$dir/scripts/config/.gitleaks-baseline.json" ]; then
69+
if [[ -f "$dir/scripts/config/.gitleaks-baseline.json" ]]; then
6670
cmd="$cmd --baseline-path $dir/scripts/config/.gitleaks-baseline.json"
6771
fi
6872
# Include the config file

scripts/podman/amd64-build-from-compose.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ set -euo pipefail
44
# Validate required tools
55
check_tools() {
66
local missing_tools=()
7-
7+
88
if ! command -v podman &> /dev/null; then
99
missing_tools+=("podman")
1010
fi
11-
11+
1212
if ! command -v yq &> /dev/null; then
1313
echo "Warning: yq not found, falling back to basic parsing"
1414
return 0
1515
fi
16-
17-
if [ ${#missing_tools[@]} -ne 0 ]; then
16+
17+
if [[ ${#missing_tools[@]} -ne 0 ]]; then
1818
echo "Error: Missing required tools: ${missing_tools[*]}" >&2
1919
echo "Please install the missing tools and try again." >&2
2020
exit 1
@@ -24,7 +24,7 @@ check_tools() {
2424
# Parse compose files using yq or fallback
2525
parse_services() {
2626
local compose_file="$1"
27-
27+
2828
if command -v yq &> /dev/null; then
2929
# Use yq for robust YAML parsing
3030
yq eval '.services | to_entries | .[] | select(.value.build) | [.key, .value.image // ("cohort-manager-" + .key), .value.build.context, .value.build.dockerfile] | @tsv' "$compose_file" 2>/dev/null || echo ""
@@ -41,7 +41,7 @@ parse_services() {
4141
gsub(/:$/, "", $1)
4242
service = $1
4343
}
44-
/^ image:/ {
44+
/^ image:/ {
4545
gsub(/^ image:[ ]*/, "")
4646
gsub(/^[ " ]+|[ " ]+$/, "")
4747
explicit_img = $0
@@ -70,7 +70,7 @@ parse_services() {
7070
}
7171

7272
# Validate arguments
73-
if [ $# -eq 0 ]; then
73+
if [[ $# -eq 0 ]]; then
7474
echo "Usage: $0 <compose-file1> [compose-file2] ..." >&2
7575
exit 1
7676
fi
@@ -91,20 +91,20 @@ for compose_file in "$@"; do
9191
echo "Warning: $compose_file not found, skipping" >&2
9292
continue
9393
fi
94-
94+
9595
echo "Processing $compose_file..."
96-
96+
9797
parse_services "$compose_file" | while IFS=$'\t' read -r service image context dockerfile; do
9898
if [[ -z "$service" || -z "$context" || -z "$dockerfile" ]]; then
9999
continue
100100
fi
101-
101+
102102
dockerfile_path="$context/$dockerfile"
103103
if [[ ! -f "$dockerfile_path" ]]; then
104104
echo "Warning: Dockerfile not found at $dockerfile_path, skipping $service" >&2
105105
continue
106106
fi
107-
107+
108108
# Check if Dockerfile uses Azure Functions base
109109
if grep -q "FROM.*mcr\.microsoft\.com/azure-functions/dotnet-isolated" "$dockerfile_path"; then
110110
echo "Building $image as amd64 (uses Azure Functions base)"
@@ -120,12 +120,12 @@ for compose_file in "$@"; do
120120
fi
121121
fi
122122
done
123-
123+
124124
# Check if the while loop failed (due to pipe)
125-
if [ ${PIPESTATUS[1]} -ne 0 ]; then
125+
if [[ ${PIPESTATUS[1]} -ne 0 ]]; then
126126
echo "Error: Build failed for $compose_file" >&2
127127
exit 1
128128
fi
129129
done
130130

131-
echo "Build complete!"
131+
echo "Build complete!"

scripts/reports/create-sbom-report.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function create-report() {
4848
function run-syft-natively() {
4949

5050

51-
if [ -z "$CHECK_DOCKER_IMAGE" ]; then
51+
if [[ -z "$CHECK_DOCKER_IMAGE" ]]; then
5252
syft scan docker:$CHECK_DOCKER_IMAGE \
5353
--config "$PWD/scripts/config/syft.yaml" \
5454
--output spdx-json="$PWD/$SBOM_REPOSITORY_REPORT.tmp.json"
@@ -68,7 +68,7 @@ function run-syft-in-docker() {
6868
# shellcheck disable=SC2155
6969
local image=$(name=ghcr.io/anchore/syft docker-get-image-version-and-pull)
7070

71-
if [ -z "$CHECK_DOCKER_IMAGE" ]; then
71+
if [[ -z "$CHECK_DOCKER_IMAGE" ]]; then
7272
docker run --rm --platform linux/amd64 \
7373
--volume "$PWD":/workdir \
7474
--volume /var/run/docker.sock:/var/run/docker.sock \

0 commit comments

Comments
 (0)