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