-
Notifications
You must be signed in to change notification settings - Fork 37
/
jenkins-create-credentials.sh
executable file
·277 lines (230 loc) · 10.7 KB
/
jenkins-create-credentials.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env bash
#*******************************************************************************
# Copyright (c) 2020 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html,
# or the MIT License which is available at https://opensource.org/licenses/MIT.
# SPDX-License-Identifier: EPL-2.0 OR MIT
#*******************************************************************************
# This script creates credentials in the Jenkins credentials store
# Bash strict-mode
set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
SCRIPT_FOLDER="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
LOCAL_CONFIG="${HOME}/.cbi/config"
if [[ ! -f "${LOCAL_CONFIG}" ]]; then
echo "ERROR: File '$(readlink -f "${LOCAL_CONFIG}")' does not exists"
echo "Create one to configure the location of the password store. Example:"
echo '{"password-store": {"cbi-dir": "~/.password-store/cbi",'
echo ' "it-dir": "~/.password-store/it"}}'
fi
PASSWORD_STORE_DIR="$(jq -r '.["password-store"]["cbi-dir"]' "${LOCAL_CONFIG}")"
export PASSWORD_STORE_DIR
#shellcheck disable=SC1091
source "${SCRIPT_FOLDER}/pass/pass_wrapper.sh"
PROJECT_NAME="${1:-}"
SHORT_NAME="${PROJECT_NAME##*.}"
usage() {
printf "%s <project_name>\n" "${SCRIPT_NAME}"
printf "\t%-16s the name of the project to add credentials to Jenkins\n" "project_name"
}
# check that project name is not empty
if [[ -z "${PROJECT_NAME}" ]]; then
printf "ERROR: a project name must be given.\n"
usage
exit 1
fi
CI_INSTANCE_PATH="${SCRIPT_FOLDER}/instances/${PROJECT_NAME}"
PROJECTS_STORAGE_PASS_DOMAIN="projects-storage.eclipse.org"
GIT_ECLIPSE_PASS_DOMAIN="git.eclipse.org"
GITHUB_PASS_DOMAIN="github.com"
GITLAB_PASS_DOMAIN="gitlab.eclipse.org"
GPG_PASS_DOMAIN="gpg"
create_domain() {
local domain_name="${1:-}"
echo " Creating domain '${domain_name}'..."
"${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" create-credentials-domain-by-xml system::system::jenkins <<EOF
<com.cloudbees.plugins.credentials.domains.Domain>
<name>${domain_name}</name>
<specifications/>
</com.cloudbees.plugins.credentials.domains.Domain>
EOF
}
create_username_password_credentials() {
local domain_name="${1:-}"
local id="${2:-}"
local username="${3:-}"
local password="${4:-}"
local description="${5:-}"
echo " Creating username/password credential '${id}'..."
# check if credentials already exist, update password if yes
local reply
reply=$("${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" get-credentials-as-xml system::system::jenkins "${domain_name}" "${id}" 2>&1 || true)
local cli_command
local update_id
if [[ "${reply}" == "No such domain" ]]; then
create_domain "${domain_name}"
cli_command="create-credentials-by-xml"
update_id=
elif [[ "${reply}" == "No such credential" ]]; then
cli_command="create-credentials-by-xml"
update_id=
elif [[ "${reply}" == "<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"* ]]; then
echo " Credential ${id} already exists. Overwriting..."
cli_command="update-credentials-by-xml"
update_id="${id}"
else
echo "Unexpected reply: ${reply}"
"${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" get-credentials-as-xml system::system::jenkins "${domain_name}" "${id}"
exit 1
fi
# ${update_id} is deliberatly not put in quotes to be only used if credentials are updated. and yes, this is a hack
#shellcheck disable=SC2086
"${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" "${cli_command}" "system::system::jenkins" "${domain_name}" ${update_id} <<EOF
<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
<scope>GLOBAL</scope>
<id>${id}</id>
<username>${username}</username>
<password>${password}</password>
<description>${description}</description>
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
EOF
}
create_ssh_credentials_xml() {
local domain_name="${1:-}"
local id="${2:-}"
local username="${3:-}"
local id_rsa="${4:-}"
local passphrase="${5:-}"
local description="${6:-}"
echo " Creating SSH credential '${id}'..."
# check if credentials already exist
local reply
reply=$("${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" get-credentials-as-xml system::system::jenkins "${domain_name}" "${id}" 2>&1 || true)
local cli_command
local update_id
if [[ "${reply}" == "No such domain" && "${domain_name}" != "_" ]]; then #skip for global domain ("_")
create_domain "${domain_name}"
cli_command="create-credentials-by-xml"
update_id=
elif [[ "${reply}" == "No such credential" ]]; then
cli_command="create-credentials-by-xml"
update_id=
elif [[ "${reply}" == "<com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"* ]]; then
echo " Credential ${id} already exists. Overwriting..."
cli_command="update-credentials-by-xml"
update_id="${id}"
else
echo "Unexpected reply: ${reply}"
exit 1
fi
# ${update_id} is deliberatly not put in quotes to be only used if credentials are updated. and yes, this is a hack
#shellcheck disable=SC2086
"${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" "${cli_command}" "system::system::jenkins" "${domain_name}" ${update_id} <<EOF
<com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>
<scope>GLOBAL</scope>
<id>${id}</id>
<username>${username}</username>
<description>${description}</description>
<passphrase>${passphrase}</passphrase>
<privateKeySource class="com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey\$DirectEntryPrivateKeySource">
<privateKey>${id_rsa}</privateKey>
</privateKeySource>
</com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>
EOF
}
#TODO: how can the bytes be added?
create_file_credentials() {
local domain_name="${1:-}"
local id="${2:-}"
local file_name="${3:-}"
local subkeys="${4:-}"
echo " Creating file credential '${id}'..."
# check if credentials already exist
local reply
reply="$("${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" get-credentials-as-xml system::system::jenkins "${domain_name}" "${id}" 2>&1 || true)"
local cli_command
if [[ "${reply}" == "No such domain" && "${domain_name}" != "_" ]]; then
create_domain "${domain_name}"
cli_command="create-credentials-by-xml"
elif [[ "${reply}" == "No such credential" ]]; then
cli_command="create-credentials-by-xml"
elif [[ "${reply}" == "<org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl"* ]]; then
echo " Credential ${id} already exists. Skipping..."
# Do not overwrite, since we can't set the secret file automatically (yet)
return
else
echo "Unexpected reply: ${reply}"
exit 1
fi
"${SCRIPT_FOLDER}/jenkins-cli.sh" "${CI_INSTANCE_PATH}" "${cli_command}" "system::system::jenkins" "${domain_name}" <<EOF
<org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl>
<scope>GLOBAL</scope>
<id>${id}</id>
<fileName>${file_name}</fileName>
</org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl>
EOF
## TODO: find out how secret bytes need to be encoded
echo " IMPORTANT: secret file needs to be uploaded manually in the WebUI for now!"
#<secretBytes>${subkeys}</secretBytes>
}
create_ssh_credentials() {
local domain_name="${1:-}"
local id="${2:-}"
local description="${3:-}"
local pass_domain="${4:-}"
# read credentials from pass
user="$(passw cbi "/bots/${PROJECT_NAME}/${pass_domain}/username")"
LF_XENTITY="
"
# TODO: does not seem to work
# translate line feeds to LF_XENTITY
#id_rsa=$(passw cbi "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${pass_domain}/id_rsa" | tr '\n' ',' | sed 's/,/\'${LF_XENTITY}'/g')
id_rsa="$(passw cbi "/bots/${PROJECT_NAME}/${pass_domain}/id_rsa")"
# remove trailing line feed (already translated to LF_XENTITY)
if [ "$(echo "${id_rsa}" | wc -c)" -ne 0 ] && [ "$(echo "${id_rsa}" | tail -c -6)" == "${LF_XENTITY}" ]; then
id_rsa="$(echo "${id_rsa}" | head -c -6)"
fi
# escape XML special chars (<, >, &, ", and ' to their matching entities)
passphrase="$(passw cbi "/bots/${PROJECT_NAME}/${pass_domain}/id_rsa.passphrase" | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g')"
create_ssh_credentials_xml "${domain_name}" "${id}" "${user}" "${id_rsa}" "${passphrase}" "${description}"
}
## projects-storage.eclipse.org ##
if [[ -f "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${PROJECTS_STORAGE_PASS_DOMAIN}/id_rsa.gpg" ]]; then
echo "Found ${PROJECTS_STORAGE_PASS_DOMAIN} SSH credentials in password store..."
create_ssh_credentials "_" "projects-storage.eclipse.org-bot-ssh" "ssh://genie.${SHORT_NAME}@projects-storage.eclipse.org" "${PROJECTS_STORAGE_PASS_DOMAIN}"
fi
## git.eclipse.org ##
# always create by default ?
if [[ -f "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${GIT_ECLIPSE_PASS_DOMAIN}/id_rsa.gpg" ]]; then
echo "Found ${GIT_ECLIPSE_PASS_DOMAIN} SSH credentials in password store..."
create_ssh_credentials "_" "git.eclipse.org-bot-ssh" "ssh://genie.${SHORT_NAME}@git.eclipse.org" "${GIT_ECLIPSE_PASS_DOMAIN}"
fi
## GitHub.com ##
# GitHub username/password credentials are deprecated
if [[ -f "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${GITHUB_PASS_DOMAIN}/api-token.gpg" ]]; then
echo "Found ${GITHUB_PASS_DOMAIN} username/token credentials in password store..."
# read credentials from pass
user="$(passw cbi "/bots/${PROJECT_NAME}/${GITHUB_PASS_DOMAIN}/username")"
token="$(passw cbi "/bots/${PROJECT_NAME}/${GITHUB_PASS_DOMAIN}/api-token")"
create_username_password_credentials "api.github.com" "github-bot" "${user}" "${token}" "GitHub bot (username/token)"
fi
if [[ -f "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${GITHUB_PASS_DOMAIN}/id_rsa.gpg" ]]; then
echo "Found ${GITHUB_PASS_DOMAIN} SSH credentials in password store..."
create_ssh_credentials "api.github.com" "github-bot-ssh" "GitHub bot (SSH)" "${GITHUB_PASS_DOMAIN}"
fi
## GitLab ##
if [[ -f "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${GITLAB_PASS_DOMAIN}/id_rsa.gpg" ]]; then
echo "Found ${GITLAB_PASS_DOMAIN} SSH credentials in password store..."
create_ssh_credentials "gitlab.eclipse.org" "gitlab-bot-ssh" "GitLab bot (SSH)" "${GITLAB_PASS_DOMAIN}"
fi
## GPG (for OSSRH) ##
if [[ -f "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${GPG_PASS_DOMAIN}/secret-subkeys.asc.gpg" ]]; then
echo "Found ${GPG_PASS_DOMAIN} credentials in password store..."
# read credentials from pass
subkeys="$(passw cbi "/bots/${PROJECT_NAME}/${GPG_PASS_DOMAIN}/secret-subkeys.asc")"
create_file_credentials "_" "secret-subkeys.asc" "secret-subkeys.asc" "${subkeys}" # id == filename
fi