Skip to content

Commit 61ca955

Browse files
authored
artifactory-automation: harden shell scripting and fix curl JSON body (#339)
Fixes a correctness bug where `TOKEN_USER` and `TOKEN_PASSWORD` were never interpolated into the curl request body (single-quote escaping prevented expansion), and hardens the surrounding shell script against word splitting, glob expansion, and malformed JSON payloads. Changes: - Fix curl `-d` body to use `jq -n --arg` for safe, properly escaped JSON serialization - Quote all variable expansions (`$PRUNE`, `$id`, `$PULL_TOKEN_JSON`, `$GITHUB_OUTPUT`, `$RESP`, `$BODY`) - Add `[[ -n "$id" ]] || continue` guard in the token pruning loop - Use `set +x` before writing the pull token password to `$GITHUB_OUTPUT` to prevent it appearing in Actions debug logs - Switch HTTP status check from arithmetic `-ne` to string `!=` to handle empty/non-numeric values gracefully - Capitalize "Artifactory" consistently in all input descriptions
1 parent 6f71809 commit 61ca955

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

artifactory-automation/action.yaml

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ inputs:
1212
required: true
1313

1414
artifactory_url:
15-
description: 'The url to your artifactory instance'
15+
description: 'The URL to your Artifactory instance'
1616
required: true
1717

1818
artifactory_user:
19-
description: 'The username for artifactory'
19+
description: 'The username for Artifactory'
2020
required: true
2121

2222
artifactory_repository_name:
23-
description: 'The name of the artifactory repository to update'
23+
description: 'The name of the Artifactory repository to update'
2424
required: true
2525

2626
artifactory_token:
27-
description: 'API token for deployment'
27+
description: 'Artifactory API token for deployment'
2828
required: true
2929

3030
ttl:
@@ -33,7 +33,7 @@ inputs:
3333
default: 168h
3434

3535
prune_expired:
36-
description: 'Prune expired tokens (requires identity.list, identity.delete permissions)'
36+
description: 'Prune expired Artifactory tokens (requires identity.list, identity.delete permissions)'
3737
required: false
3838
default: "false"
3939

@@ -55,20 +55,22 @@ runs:
5555
TTL: ${{ inputs.ttl }}
5656
PRUNE: ${{ inputs.prune_expired }}
5757
run: |
58-
if [[ $PRUNE = "true" ]]; then
58+
if [[ "$PRUNE" = "true" ]]; then
5959
for id in $(chainctl iam identities list --parent "$ORGANIZATION" --name "pull token - registry" --expired -o json | jq -r '.items[]?.id'); do
60+
[[ -n "$id" ]] || continue
6061
echo "Deleting old pull token $id"
61-
chainctl iam identities delete $id --parent "$ORGANIZATION" -y
62-
done
62+
chainctl iam identities delete "$id" --parent "$ORGANIZATION" -y
63+
done
6364
fi
6465
6566
PULL_TOKEN_JSON=$(chainctl auth pull-token --ttl "$TTL" --output json)
6667
67-
user=$(jq -r '.identity_id' <<<$PULL_TOKEN_JSON)
68-
password=$(jq -r '.token' <<<$PULL_TOKEN_JSON)
68+
user=$(jq -r '.identity_id' <<<"$PULL_TOKEN_JSON")
69+
password=$(jq -r '.token' <<<"$PULL_TOKEN_JSON")
6970
70-
echo "user=$user" >> $GITHUB_OUTPUT
71-
echo "password=$password" >> $GITHUB_OUTPUT
71+
echo "user=$user" >> "$GITHUB_OUTPUT"
72+
set +x
73+
echo "password=$password" >> "$GITHUB_OUTPUT"
7274
7375
- name: Update Artifactory Registry
7476
shell: bash
@@ -86,18 +88,18 @@ runs:
8688
-X POST \
8789
"$ARTIFACTORY_URL/artifactory/api/repositories/$ARTIFACTORY_REPOSITORY_NAME" \
8890
-H "Content-Type: application/json" \
89-
-d '{
90-
"key": "'$ARTIFACTORY_REPOSITORY_NAME'",
91-
"username": "'$TOKEN_USER'",
92-
"password": "'$TOKEN_PASSWORD'"
93-
}')
91+
-d "$(jq -n \
92+
--arg key "$ARTIFACTORY_REPOSITORY_NAME" \
93+
--arg user "$TOKEN_USER" \
94+
--arg pass "$TOKEN_PASSWORD" \
95+
'{"key":$key,"username":$user,"password":$pass}')")
9496
95-
BODY=$(echo $RESP | sed -e 's/HTTPSTATUS\:.*//g')
96-
STATUS=$(echo $RESP | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
97+
BODY=$(echo "$RESP" | sed -e 's/HTTPSTATUS\:.*//g')
98+
STATUS=$(echo "$RESP" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
9799
98-
if [ "$STATUS" -ne 200 ]; then
99-
echo "Error $STATUS": $BODY
100+
if [[ "$STATUS" != '200' ]]; then
101+
echo "Error ${STATUS}: $BODY"
100102
exit 1
101103
fi
102104
103-
echo $BODY
105+
echo "$BODY"

0 commit comments

Comments
 (0)