Skip to content

Commit 0858c38

Browse files
fix: harden shell scripts and fix db_copy loop bug (#3159)
* fix: harden shell scripts and fix db_copy loop bug Correct a logic error in hack/db_copy.sh that caused every database migration to target backstage_plugin_app, and apply shellcheck-driven hardening across plugin-infra setup scripts and Tekton inline scripts. Assisted-by: cursor Co-authored-by: Cursor <cursoragent@cursor.com> * fix: rename gitops-secret-setup functions to snake_case Address SonarCloud warnings about camelCase function names introduced during shell script hardening. Assisted-by: cursor Co-authored-by: Cursor <cursoragent@cursor.com> * fix: resolve SonarCloud shell script findings Use snake_case function names and bash [[ ]] conditionals throughout gitops-secret-setup.sh; align plugin-infra.sh file test with [[ ]]. Assisted-by: cursor Co-authored-by: Cursor <cursoragent@cursor.com> * chore: regenerate bundle manifests --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 5eaa123 commit 0858c38

8 files changed

Lines changed: 97 additions & 71 deletions

File tree

bundle/backstage.io/manifests/backstage-operator.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/rhdh/manifests/backstage-operator.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/rhdh/manifests/rhdh-plugin-deps_v1_configmap.yaml

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/profile/rhdh/plugin-deps/tekton.yaml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ spec:
174174
image: registry.access.redhat.com/ubi9-minimal
175175
workingDir: $(workspaces.workflow-source.path)
176176
script: |
177-
ROOT=/workspace/workflow
178-
TARGET=flat
177+
#!/usr/bin/env sh
178+
set -eu
179+
179180
mkdir -p flat
180181
181182
if [ -d "workflow/$(params.workflowId)" ]; then
@@ -186,7 +187,7 @@ spec:
186187
cp workflow/LICENSE flat/$(params.workflowId)
187188
fi
188189
189-
if [ "$(params.convertToFlat)" == "false" ]; then
190+
if [ "$(params.convertToFlat)" = "false" ]; then
190191
rm -rf workflow/src/main/resources
191192
mv workflow/src flat/$(params.workflowId)/
192193
fi
@@ -212,6 +213,9 @@ spec:
212213
image: registry.access.redhat.com/ubi9-minimal
213214
workingDir: $(workspaces.workflow-source.path)/flat/$(params.workflowId)
214215
script: |
216+
#!/usr/bin/env sh
217+
set -eu
218+
215219
microdnf install -y tar gzip
216220
KN_CLI_URL="https://developers.redhat.com/content-gateway/file/pub/cgw/serverless-logic/1.38.0/kn-workflow-linux-amd64.tar.gz"
217221
curl -L "$KN_CLI_URL" | tar -xz --no-same-owner && chmod +x kn-workflow-linux-amd64 && mv kn-workflow-linux-amd64 kn-workflow
@@ -237,10 +241,13 @@ spec:
237241
image: registry.access.redhat.com/ubi9-minimal
238242
workingDir: $(workspaces.workflow-gitops.path)/workflow-gitops
239243
script: |
240-
cp $(workspaces.workflow-source.path)/flat/$(params.workflowId)/manifests/* kustomize/base
244+
#!/usr/bin/env sh
245+
set -eu
246+
247+
cp "$(workspaces.workflow-source.path)/flat/$(params.workflowId)/manifests/"* kustomize/base
241248
microdnf install -y findutils && microdnf clean all
242-
cd kustomize
243-
./updater.sh $(params.workflowId) $(params.imageTag)
249+
cd kustomize || exit
250+
./updater.sh "$(params.workflowId)" "$(params.imageTag)"
244251
---
245252
apiVersion: tekton.dev/v1
246253
kind: Pipeline

config/profile/rhdh/plugin-infra/gitops-secret-setup.sh

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
# Setup Script for RHDH Authentication For Orchestrator CICD
44
#
55

6-
function captureRHDHNamespace {
6+
set -euo pipefail
7+
8+
selected_instance=""
9+
10+
capture_rhdh_namespace() {
711
default="rhdh"
8-
if [ "$use_default" == true ]; then
12+
if [[ "$use_default" == true ]]; then
913
rhdh_workspace="$default"
1014
else
1115
read -rp "Enter RHDH Instance namespace (default: $default): " value
12-
if [ -z "$value" ]; then
16+
if [[ -z "$value" ]]; then
1317
rhdh_workspace="$default"
1418
else
1519
rhdh_workspace="$value"
@@ -18,14 +22,14 @@ function captureRHDHNamespace {
1822
RHDH_NAMESPACE=$rhdh_workspace
1923
}
2024

21-
function captureArgoCDNamespace {
25+
capture_argocd_namespace() {
2226
default="openshift-gitops-operator"
23-
if [ "$use_default" == true ]; then
27+
if [[ "$use_default" == true ]]; then
2428
argocd_namespace="$default"
2529
else
2630
read -rp "Enter ArgoCD installation namespace (default: $default): " value
2731

28-
if [ -z "$value" ]; then
32+
if [[ -z "$value" ]]; then
2933
argocd_namespace="$default"
3034
else
3135
argocd_namespace="$value"
@@ -34,19 +38,19 @@ function captureArgoCDNamespace {
3438
ARGOCD_NAMESPACE=$argocd_namespace
3539
}
3640

37-
function captureArgoCDURL {
41+
capture_argocd_url() {
3842
argocd_instances=$(oc get argocd -n "$ARGOCD_NAMESPACE" -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
3943

40-
if [ -z "$argocd_instances" ]; then
44+
if [[ -z "$argocd_instances" ]]; then
4145
echo "No ArgoCD instances found in namespace $ARGOCD_NAMESPACE. Continuing without ArgoCD support"
4246
else
43-
if [ "$use_default" == true ]; then
47+
if [[ "$use_default" == true ]]; then
4448
selected_instance=$(echo "$argocd_instances" | awk 'NR==1')
4549
echo "Select an ArgoCD instance: $selected_instance"
4650
else
4751
echo "Select an ArgoCD instance:"
4852
select instance in $argocd_instances; do
49-
if [ -n "$instance" ]; then
53+
if [[ -n "$instance" ]]; then
5054
selected_instance="$instance"
5155
break
5256
else
@@ -56,21 +60,21 @@ function captureArgoCDURL {
5660
fi
5761
argocd_route=$(oc get route -n "$ARGOCD_NAMESPACE" -l app.kubernetes.io/managed-by="$selected_instance" -ojsonpath='{.items[0].status.ingress[0].host}')
5862
echo "Found Route at $argocd_route"
59-
ARGOCD_URL=https://$argocd_route
63+
ARGOCD_URL="https://${argocd_route}"
6064
fi
6165

6266
}
6367

64-
function captureArgoCDCreds {
65-
if [ -n "$selected_instance" ]; then
68+
capture_argocd_creds() {
69+
if [[ -n "$selected_instance" ]]; then
6670
admin_password=$(oc get secret -n "$ARGOCD_NAMESPACE" "${selected_instance}"-cluster -ojsonpath='{.data.admin\.password}' | base64 -d)
6771
ARGOCD_USERNAME="admin"
6872
ARGOCD_PASSWORD=$admin_password
6973
fi
7074
}
7175

72-
function captureGitToken {
73-
if [ -z "$GITHUB_TOKEN" ]; then
76+
capture_git_token() {
77+
if [[ -z "${GITHUB_TOKEN:-}" ]]; then
7478
read -rs -p "Enter GitHub access token: " value
7579
echo ""
7680
GITHUB_TOKEN=$value
@@ -79,22 +83,22 @@ function captureGitToken {
7983
fi
8084
}
8185

82-
function captureK8sURL {
86+
capture_k8s_url() {
8387
url="$(oc whoami --show-server)"
8488
K8S_CLUSTER_URL=$url
8589
}
8690

87-
function generateK8sToken {
91+
generate_k8s_token() {
8892
sa_namespace="rhdh"
8993
sa_name="rhdh"
90-
if [ "$use_default" == false ]; then
94+
if [[ "$use_default" == false ]]; then
9195
read -rp "Which namespace should be used or created to check the SA holding the persistent token? (default: $sa_namespace): " selected_namespace
92-
if [ -n "$selected_namespace" ]; then
96+
if [[ -n "$selected_namespace" ]]; then
9397
sa_namespace="$selected_namespace"
9498
fi
9599

96100
read -rp "What is the name of the SA? (default: $sa_name): " selected_name
97-
if [ -n "$selected_name" ]; then
101+
if [[ -n "$selected_name" ]]; then
98102
sa_name="$selected_name"
99103
fi
100104
fi
@@ -129,42 +133,44 @@ EOF
129133
K8S_CLUSTER_TOKEN=$(oc -n "$sa_namespace" get secret/"${sa_name}" -o jsonpath='{.data.token}' | base64 -d )
130134
}
131135

132-
133-
function checkPrerequisite {
136+
check_prerequisite() {
134137
if ! command -v oc &> /dev/null; then
135138
echo "oc is required for this script to run. Exiting."
136139
exit 1
137140
fi
138141
}
139142

140-
function createBackstageSecret {
143+
create_backstage_secret() {
141144
if 2>/dev/null 1>&2 oc get secret backstage-backend-auth-secret -n "$RHDH_NAMESPACE"; then
142145
oc delete secret backstage-backend-auth-secret -n "$RHDH_NAMESPACE"
143146
fi
144147
declare -A secretKeys
145-
if [ -n "$K8S_CLUSTER_URL" ]; then
148+
if [[ -n "${K8S_CLUSTER_URL:-}" ]]; then
146149
secretKeys[K8S_CLUSTER_URL]=$K8S_CLUSTER_URL
147150
fi
148-
if [ -n "$K8S_CLUSTER_TOKEN" ]; then
151+
if [[ -n "${K8S_CLUSTER_TOKEN:-}" ]]; then
149152
secretKeys[K8S_CLUSTER_TOKEN]=$K8S_CLUSTER_TOKEN
150153
fi
151-
if [ -n "$ARGOCD_USERNAME" ]; then
154+
if [[ -n "${ARGOCD_USERNAME:-}" ]]; then
152155
secretKeys[ARGOCD_USERNAME]=$ARGOCD_USERNAME
153156
fi
154-
if [ -n "$ARGOCD_URL" ]; then
157+
if [[ -n "${ARGOCD_URL:-}" ]]; then
155158
secretKeys[ARGOCD_URL]=$ARGOCD_URL
156159
fi
157-
if [ -n "$ARGOCD_PASSWORD" ]; then
160+
if [[ -n "${ARGOCD_PASSWORD:-}" ]]; then
158161
secretKeys[ARGOCD_PASSWORD]=$ARGOCD_PASSWORD
159162
fi
160-
if [ -n "$GITHUB_TOKEN" ]; then
163+
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
161164
secretKeys[GITHUB_TOKEN]=$GITHUB_TOKEN
162165
fi
163-
cmd="oc create secret generic backstage-backend-auth-secret -n $RHDH_NAMESPACE"
166+
local -a cmd=(
167+
oc create secret generic backstage-backend-auth-secret
168+
-n "$RHDH_NAMESPACE"
169+
)
164170
for key in "${!secretKeys[@]}"; do
165-
cmd="${cmd} --from-literal=${key}=${secretKeys[$key]}"
171+
cmd+=(--from-literal="${key}=${secretKeys[$key]}")
166172
done
167-
eval "$cmd"
173+
"${cmd[@]}"
168174
}
169175

170176
# Function to display usage instructions
@@ -196,7 +202,7 @@ while [[ $# -gt 0 ]]; do
196202
shift
197203
done
198204

199-
function main {
205+
main() {
200206

201207
# Check if using default values or not
202208
if $use_default; then
@@ -205,17 +211,17 @@ function main {
205211
echo "Not using default values."
206212
fi
207213

208-
checkPrerequisite
209-
captureK8sURL
210-
generateK8sToken
211-
captureRHDHNamespace
212-
captureArgoCDNamespace
213-
captureArgoCDURL
214-
captureArgoCDCreds
215-
captureGitToken
216-
createBackstageSecret
214+
check_prerequisite
215+
capture_k8s_url
216+
generate_k8s_token
217+
capture_rhdh_namespace
218+
capture_argocd_namespace
219+
capture_argocd_url
220+
capture_argocd_creds
221+
capture_git_token
222+
create_backstage_secret
217223

218224
echo "Setup completed successfully!"
219225
}
220226

221-
main
227+
main

config/profile/rhdh/plugin-infra/plugin-infra.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Plugin Infrastructure Setup Script for RHDH with Orchestrator
44
#
55

6-
set -e
6+
set -euo pipefail
77

88
action="apply" # Default action
99
branch="main" # Default branch
@@ -37,7 +37,7 @@ apply_manifest() {
3737
local script_dir
3838
script_dir="$(dirname "$(realpath "$0")")"
3939

40-
if [ -f "${script_dir}/${file}" ]; then
40+
if [[ -f "${script_dir}/${file}" ]]; then
4141
echo "Using local file: ${file}"
4242
kubectl "$action" -f "${script_dir}/${file}"
4343
else
@@ -47,24 +47,24 @@ apply_manifest() {
4747
}
4848

4949
# Execution
50-
if [ "$action" == "apply" ]; then
50+
if [[ "$action" == "apply" ]]; then
5151
apply_manifest "serverless.yaml"
5252
echo "Waiting for CRDs to be established..."
5353
kubectl wait --for=condition=Established crd --all --timeout=60s
5454
apply_manifest "knative.yaml"
5555
apply_manifest "serverless-logic.yaml"
56-
if [ "$cicd" == true ]; then
56+
if [[ "$cicd" == true ]]; then
5757
echo "CICD enabled. Executing CICD-specific logic..."
5858
apply_manifest "argocd.yaml"
5959
kubectl wait --for=condition=Established crd --all --timeout=60s
6060
apply_manifest "argocd-cr.yaml"
6161
apply_manifest "pipeline.yaml"
6262
fi
63-
elif [ "$action" == "delete" ]; then
63+
elif [[ "$action" == "delete" ]]; then
6464
apply_manifest "serverless-logic.yaml"
6565
apply_manifest "knative.yaml"
6666
apply_manifest "serverless.yaml"
67-
if [ "$cicd" == true ]; then
67+
if [[ "$cicd" == true ]]; then
6868
apply_manifest "argocd.yaml"
6969
apply_manifest "argocd-cr.yaml"
7070
apply_manifest "pipeline.yaml"

0 commit comments

Comments
 (0)