Skip to content

Commit a1ece31

Browse files
committed
feat: add scrit to renew api-token for gitlab
Signed-off-by: sebastien.heurtematte <[email protected]>
1 parent c39de6a commit a1ece31

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

gitlab/gitlab_admin.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,41 @@ create_api_token() {
227227
echo "${token}"
228228
}
229229

230+
check_api_token_validity() {
231+
local username="${1:-}"
232+
_check_parameter "username" "${username}"
233+
local user_id
234+
user_id="$(_get_id_from_username "${username}")"
235+
local name="CI token"
236+
237+
impersonation_tokens=$(curl -sSL --header "${TOKEN_HEADER}" \
238+
"${API_BASE_URL}/users/${user_id}/impersonation_tokens")
239+
240+
expired=true
241+
expires_at=""
242+
# Vérifier chaque token
243+
for token in $(echo "$impersonation_tokens" | jq -c '.[]'); do
244+
name=$(echo "$token" | jq -r '.name')
245+
if [ "$name" == "CI token" ]; then
246+
revoked=$(echo "$token" | jq -r '.revoked')
247+
active=$(echo "$token" | jq -r '.active')
248+
expires_at=$(echo "$token" | jq -r '.expires_at')
249+
250+
# echo "Revoked: $revoked - Active: $active - Expires at: $expires_at"
251+
252+
if [[ "$active" == "true" ]] && [[ "$revoked" == "false" ]]; then
253+
expired=false
254+
fi
255+
fi
256+
done
257+
if [ "$expired" == "true" ]; then
258+
echo "CI Token ${username}(${user_id}) expired or revoked: $expires_at"
259+
exit 1
260+
else
261+
echo "CI Token ${username}(${user_id}) is still valid"
262+
fi
263+
}
264+
230265
create_bot_user() {
231266
local project_name="${1:-}"
232267
local username="${2:-}"

gitlab/gitlab_bot_token_renew.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bash
2+
#*******************************************************************************
3+
# Copyright (c) 2024 Eclipse Foundation and others.
4+
# This program and the accompanying materials are made available
5+
# under the terms of the Eclipse Public License 2.0
6+
# which is available at http://www.eclipse.org/legal/epl-v20.html
7+
# SPDX-License-Identifier: EPL-2.0
8+
#*******************************************************************************
9+
10+
# Create bot user in GitLab and set up SSH key
11+
12+
# Bash strict-mode
13+
# set -o errexit
14+
set -o nounset
15+
set -o pipefail
16+
17+
IFS=$'\n\t'
18+
SCRIPT_FOLDER="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
19+
CI_ADMIN_ROOT="${SCRIPT_FOLDER}/.."
20+
JIRO_ROOT_FOLDER="$("${CI_ADMIN_ROOT}/utils/local_config.sh" "get_var" "jiro-root-dir")"
21+
OTTERDOG_CONFIGS_ROOT="$("${CI_ADMIN_ROOT}/utils/local_config.sh" "get_var" "otterdog-configs-root-dir")"
22+
GITLAB_PASS_DOMAIN="gitlab.eclipse.org"
23+
24+
#shellcheck disable=SC1091
25+
source "${SCRIPT_FOLDER}/../pass/pass_wrapper.sh"
26+
set +o errexit
27+
28+
export VAULT_ADDR=${VAULT_ADDR:-https:\/\/secretsmanager.eclipse.org}
29+
export VAULT_AUTH_METHOD=${VAULT_AUTH_METHOD:-token}
30+
export VAULT_TOKEN=${VAULT_TOKEN:-""}
31+
32+
VAULT_MOUNT_PATH="cbi"
33+
34+
# Check if the API token is still valid and renew it if necessary
35+
renew_tokens() {
36+
secrets=$(vault kv list -mount="${VAULT_MOUNT_PATH}" -format=json)
37+
if [ "$?" -ne 0 ]; then
38+
echo "Error listing secrets at mount: ${VAULT_MOUNT_PATH}}"
39+
return 1
40+
fi
41+
for project in $(echo "$secrets" | jq -r '.[]'); do
42+
local project_id="${project%/}"
43+
echo "############### Check project: ${project_id} ###############"
44+
token=$(vault kv get -mount="${VAULT_MOUNT_PATH}" -field="api-token" "${project_id}/gitlab.eclipse.org" 2>/dev/null) || true
45+
if [ -n "$token" ]; then
46+
# echo "Check token for ${key}"
47+
username=$(vault kv get -mount="${VAULT_MOUNT_PATH}" -field="username" "${project_id}/gitlab.eclipse.org" 2>/dev/null) || true
48+
"${SCRIPT_FOLDER}/gitlab_admin.sh" check_api_token_validity "${username}"
49+
if [ "$?" -ne 0 ]; then
50+
create_token "${project_id}" "${username}"
51+
update_jenkins "${project_id}"
52+
update_otterdog "${project_id}"
53+
fi;
54+
fi
55+
done
56+
}
57+
58+
# Create a new API token for the bot user
59+
create_token() {
60+
local project_id="${1:-}"
61+
local username="${2:-}"
62+
token="$("${SCRIPT_FOLDER}/gitlab_admin.sh" "create_api_token" "${username}")"
63+
echo "Adding API token to pass: bots/${project_id}/${GITLAB_PASS_DOMAIN}/api-token"
64+
echo "${token}" | passw cbi insert --echo "bots/${project_id}/${GITLAB_PASS_DOMAIN}/api-token"
65+
}
66+
67+
# Update Jenkins configuration
68+
update_jenkins() {
69+
local project_id="${1:-}"
70+
if [[ -d "${JIRO_ROOT_FOLDER}/instances/${project_id}" ]]; then
71+
echo "Recreate token in Jenkins instance for ${project_id}"
72+
"${JIRO_ROOT_FOLDER}/jenkins-create-credentials-token.sh" "gitlab" "${project_id}"
73+
"${JIRO_ROOT_FOLDER}/jenkins-create-credentials-token.sh" "gitlab_pat" "${project_id}"
74+
else
75+
echo "No Jenkins instance found for ${project_id}"
76+
fi
77+
}
78+
79+
# Update Otterdog configuration
80+
update_otterdog() {
81+
local project_id="${1:-}"
82+
local short_name="${project_id##*.}"
83+
pushd "${OTTERDOG_CONFIGS_ROOT}" > /dev/null
84+
find=$(jq --arg project_id "$project_id" '.organizations[] | select(.name == $project_id)' < otterdog.json)
85+
if [[ -n "${find}" ]]; then
86+
echo "Update token with Otterdog for eclipse-${short_name} - ${project_id}"
87+
PASSWORD_STORE_DIR="$("${SCRIPT_FOLDER}/../utils/local_config.sh" "get_var" "cbi-dir" "password-store")"
88+
export PASSWORD_STORE_DIR
89+
otterdog fetch-config -f "eclipse-${short_name}"
90+
otterdog apply -f "eclipse-${short_name}" -n --update-secrets --update-filter "*GITLAB_API_TOKEN"
91+
else
92+
echo "No Otterdog configuration found for ${project_id}"
93+
fi
94+
popd > /dev/null
95+
}
96+
97+
renew_tokens

0 commit comments

Comments
 (0)