forked from eclipse-cbi/jiro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins-create-credentials-token.sh
executable file
·328 lines (280 loc) · 11.3 KB
/
jenkins-create-credentials-token.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/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 string credentials (token) in the Jenkins credentials store
# TODO: update credentials
# 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]}")")"
JENKINS_CLI="${script_folder}/jenkins-cli.sh"
INSTANCES="${script_folder}/instances"
_create_domain_xml() {
local project_name="${1:-}"
local domain_name="${2:-}"
echo " Creating domain '${domain_name}'..."
"${JENKINS_CLI}" "${INSTANCES}/${project_name}" "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_string_credentials_xml() {
local project_name="${1:-}"
local domain_name="${2:-}"
local id="${3:-}"
local secret="${4:-}"
local description="${5:-}"
echo " Creating string credential '${id}' in domain ${domain_name}..."
"${JENKINS_CLI}" "${INSTANCES}/${project_name}" "create-credentials-by-xml" "system::system::jenkins" "${domain_name}" <<EOF
<org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl>
<scope>GLOBAL</scope>
<id>${id}</id>
<description>${description}</description>
<secret>${secret}</secret>
</org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl>
EOF
}
_update_string_credentials_xml() {
local project_name="${1:-}"
local domain_name="${2:-}"
local id="${3:-}"
local secret="${4:-}"
local description="${5:-}"
echo " Updating string credential '${id}' in domain ${domain_name}..."
"${JENKINS_CLI}" "${INSTANCES}/${project_name}" "update-credentials-by-xml" "system::system::jenkins" "${domain_name}" "${id}" <<EOF
<org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl>
<scope>GLOBAL</scope>
<id>${id}</id>
<description>${description}</description>
<secret>${secret}</secret>
</org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl>
EOF
}
_create_string_credentials() {
local project_name="${1:-}"
local id="${2:-}"
local description="${3:-}"
local secret="${4:-}"
local domain_name="${5:-_}" #if no domain is given, use "_" for system domain
# read secret from stdin
if [[ -z "${secret}" ]]; then
read -p "Secret: " secret
fi
if [[ -z "${secret}" ]]; then
printf "ERROR: secret must be given.\n"
exit 1
fi
# check if credentials already exist
reply="$("${JENKINS_CLI}" "${INSTANCES}/${project_name}" "get-credentials-as-xml" "system::system::jenkins" "${domain_name}" "${id}" 2>&1 || true)"
#echo "reply: ${reply}"
if [[ "${reply}" == "No such domain" && "${domain_name}" != "_" ]]; then
_create_domain_xml "${project_name}" "${domain_name}"
_create_string_credentials_xml "${project_name}" "${domain_name}" "${id}" "${secret}" "${description}"
elif [[ "${reply}" == "No such credential" ]]; then
_create_string_credentials_xml "${project_name}" "${domain_name}" "${id}" "${secret}" "${description}"
elif [[ "${reply}" == "<org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"* ]]; then
_update_string_credentials_xml "${project_name}" "${domain_name}" "${id}" "${secret}" "${description}"
else
echo "Unexpected reply: ${reply}"
exit 1
fi
}
_create_username_password_credentials_xml() {
local project_name="${1:-}"
local domain_name="${2:-}"
local id="${3:-}"
local username="${4:-}"
local password="${5:-}"
local description="${6:-}"
echo " Creating username/password credential '${id}' in domain ${domain_name}..."
"${JENKINS_CLI}" "${INSTANCES}/${project_name}" "create-credentials-by-xml" "system::system::jenkins" "${domain_name}" <<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
}
_update_username_password_credentials_xml() {
local project_name="${1:-}"
local domain_name="${2:-}"
local id="${3:-}"
local username="${4:-}"
local password="${5:-}"
local description="${6:-}"
echo " Updating username/password credential '${id}' in domain ${domain_name}..."
"${JENKINS_CLI}" "${INSTANCES}/${project_name}" "update-credentials-by-xml" "system::system::jenkins" "${domain_name}" "${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_username_token_credentials() {
local project_name="${1:-}"
local id="${2:-}"
local description="${3:-}"
local username="${4:-}"
local token="${5:-}"
local domain_name="${6:-_}" #if no domain is given, use "_" for system domain
if [[ -z "${username}" ]]; then
printf "ERROR: username must be given.\n"
exit 1
fi
if [[ -z "${token}" ]]; then
printf "ERROR: token must be given.\n"
exit 1
fi
# check if credentials already exist
reply="$("${JENKINS_CLI}" "${INSTANCES}/${project_name}" "get-credentials-as-xml" "system::system::jenkins" "${domain_name}" "${id}" 2>&1 || true)"
if [[ "${reply}" == "No such domain" && "${domain_name}" != "_" ]]; then
_create_domain_xml "${project_name}" "${domain_name}"
_create_username_password_credentials_xml "${project_name}" "${domain_name}" "${id}" "${username}" "${token}" "${description}"
elif [[ "${reply}" == "No such credential" ]]; then
_create_username_password_credentials_xml "${project_name}" "${domain_name}" "${id}" "${username}" "${token}" "${description}"
elif [[ "${reply}" == "<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"* ]]; then
_update_username_password_credentials_xml "${project_name}" "${domain_name}" "${id}" "${username}" "${token}" "${description}"
else
echo "Unexpected reply: ${reply}"
exit 1
fi
}
help() {
printf "Available commands:\n"
printf "Command\t\t\tDescription\n\n"
printf "auto\t\t\tTest which token credentials exist and create them (except for sonarcloud).\n"
printf "default\t\t\tCreate any kind of credentials (secret text/token).\n"
printf "github\t\t\tCreate github.com token credentials (secret text/token) and username/token credentials (username/token).\n"
printf "gitlab\t\t\tCreate gitlab.eclipse.org token credentials (secret text/token).\n"
printf "sonarcloud\t\tCreate sonarcloud.io credentials (secret text/token).\n"
printf "npmjs\t\t\tCreate npmjs credentials (secret text/token).\n"
exit 0
}
auto() {
local project_name="${1:-}"
# check that project name is not empty
if [[ -z "${project_name}" ]]; then
printf "ERROR: a project name must be given.\n"
exit 1
fi
echo "Checking for github.com API token..."
if pass "/cbi-pass/bots/${project_name}/github.com/api-token" 2&> /dev/null; then
github "${project_name}"
else
echo " No API token found."
fi
echo "Checking for gitlab.eclipse.org API token..."
if pass "/cbi-pass/bots/${project_name}/gitlab.eclipse.org/token" 2&> /dev/null; then
gitlab "${project_name}"
else
echo " No API token found."
fi
echo "Checking for npmjs.com API token..."
if pass "/cbi-pass/bots/${project_name}/npmjs.com/token" 2&> /dev/null; then
npmjs "${project_name}"
else
echo " No API token found."
fi
#sonarcloud is excluded for now due to possible suffixes
}
github() {
local project_name="${1:-}"
# check that project name is not empty
if [[ -z "${project_name}" ]]; then
printf "ERROR: a project name must be given.\n"
exit 1
fi
local token
username="$(pass "/cbi-pass/bots/${project_name}/github.com/username")"
token="$(pass "/cbi-pass/bots/${project_name}/github.com/api-token")"
_create_string_credentials "${project_name}" "github-bot-token" "GitHub Bot token" "${token}" "api.github.com"
_create_username_token_credentials "${project_name}" "github-bot" "GitHub bot (username/token)" "${username}" "${token}" "api.github.com"
}
gitlab() {
local project_name="${1:-}"
# check that project name is not empty
if [[ -z "${project_name}" ]]; then
printf "ERROR: a project name must be given.\n"
exit 1
fi
local token
token="$(pass "/cbi-pass/bots/${project_name}/gitlab.eclipse.org/api-token")"
_create_string_credentials "${project_name}" "gitlab-api-token" "GitLab token for ${project_name}" "${token}" "gitlab.eclipse.org"
}
sonarcloud() {
local project_name="${1:-}"
local suffix="${2:-}" # optional
# check that project name is not empty
if [[ -z "${project_name}" ]]; then
printf "ERROR: a project name must be given.\n"
exit 1
fi
local short_name="${project_name##*.}"
local token
token="$(pass "/cbi-pass/bots/${project_name}/sonarcloud.io/token")"
if [[ -n "${suffix}" ]]; then
suffix="-${suffix}"
fi
_create_string_credentials "${project_name}" "sonarcloud-token${suffix}" "SonarCloud token for ${short_name}${suffix}" "${token}"
}
npmjs() {
local project_name="${1:-}"
# check that project name is not empty
if [[ -z "${project_name}" ]]; then
printf "ERROR: a project name must be given.\n"
exit 1
fi
local token
token="$(pass "/cbi-pass/bots/${project_name}/npmjs.com/token")"
_create_string_credentials "${project_name}" "npmjs-token" "npmjs.com token" "${token}"
}
_default_usage() {
printf "%s <project_name> <secret_id> <secret_description> <secret> [domain]\n" "${script_name}"
printf "\t%-20s the name of the project to add credentials to Jenkins (e.g. technology.cbi) \n" "project_name"
printf "\t%-20s the id of the secret\n" "secret_id"
printf "\t%-20s the description of the secret\n" "secret_description"
printf "\t%-20s the secret\n" "secret"
printf "\t%-20s the domain of the secret (optional)\n" "domain"
}
default() {
local project_name="${1:-}"
local secret_id="${2:-}"
local secret_description="${3:-}"
local secret="${4:-}"
local domain="${5:-}"
local short_name="${project_name##*.}"
# check that project name is not empty
if [[ -z "${project_name}" ]]; then
printf "ERROR: a project name must be given.\n"
_default_usage
exit 1
fi
# check that secret id is not empty
if [[ -z "${secret_id}" ]]; then
printf "ERROR: a secret id must be given.\n"
_default_usage
exit 1
fi
_create_string_credentials "${secret_id}" "${secret_description}" "${secret}" "${domain}"
}
"$@"
# show help menu, if no first parameter is given
if [[ -z "${1:-}" ]]; then
help
fi