Skip to content

Commit 6b3740a

Browse files
MrGigakayman-mk
andauthored
feat: error checking for initial API token call (#1080)
## Description If the API token is invalid or expired, the current implementation does not return that error to inform the end user. This implementation adds error checking, returns that error and then does not continue. This should make debugging much easier compared to it's current state. From my research there are two possible error formats. First error response: 401 unauthorized {"message":"401 Unauthorized"} Second possible error response: Token has expired {"error":"invalid_token","error_description":"Token is expired. You can either do re-authorization or token refresh."} ## Migrations required None ## Verification Provide a bad or expired API token to the module. It should fail and output the reason for failure. --------- Co-authored-by: Matthias Kay <[email protected]>
1 parent 3749ea2 commit 6b3740a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

template/gitlab-runner.tftpl

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ then
4949
# fetch gitlab token from SSM
5050
gitlab_token=$(aws ssm get-parameter --name "${secure_parameter_store_gitlab_token_name}" --with-decryption --region "${secure_parameter_store_region}" | jq -r ".Parameter | .Value")
5151

52-
token=$(curl ${curl_cacert} --request POST -L "${runners_gitlab_url}/api/v4/user/runners" \
52+
response = $(curl ${curl_cacert} --request POST -L "${runners_gitlab_url}/api/v4/user/runners" \
5353
--header "private-token: $gitlab_token" \
5454
--form "tag_list=${gitlab_runner_tag_list}" \
5555
--form "description=${gitlab_runner_description}" \
@@ -58,8 +58,16 @@ then
5858
--form "maximum_timeout=${gitlab_runner_maximum_timeout}" \
5959
--form "runner_type=${gitlab_runner_type}_type" \
6060
$runner_type_param \
61-
--form "access_level=${gitlab_runner_access_level}" \
62-
| jq -r '.token')
61+
--form "access_level=${gitlab_runner_access_level}")
62+
63+
token = $(echo response | jq -r '.token')
64+
if [[ "$token" == null ]]
65+
message = $(echo response | jq -r '.message // .error_description')
66+
if [[ "$message" != null ]]
67+
echo "ERROR: Couldn't register the Runner. GitLab API call returned $message".
68+
exit 1
69+
fi
70+
fi
6371
else
6472
gitlab_runner_registration_token=${gitlab_runner_registration_token}
6573

0 commit comments

Comments
 (0)