forked from eclipse-cbi/jiro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins-create-credentials.sh
executable file
·252 lines (207 loc) · 9.58 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
#!/usr/bin/env bash
# This script creates credentials 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]}")")"
PASSWORD_STORE_DIR="${HOME}/.password-store/cbi-pass"
export PASSWORD_STORE_DIR
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
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" "${script_folder}/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_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=$(${script_folder}/jenkins-cli.sh ${script_folder}/instances/${PROJECT_NAME} 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}"
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
"${script_folder}/jenkins-cli.sh" "${script_folder}/instances/${PROJECT_NAME}" "${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=$(${script_folder}/jenkins-cli.sh ${script_folder}/instances/${PROJECT_NAME} 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
"${script_folder}/jenkins-cli.sh" "${script_folder}/instances/${PROJECT_NAME}" "${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
reply="$(${script_folder}/jenkins-cli.sh ${script_folder}/instances/${PROJECT_NAME} 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."
# Do not overwrite, since we can't set the a secret file automatically (yet)
return
else
echo "Unexpected reply: ${reply}"
exit 1
fi
"${script_folder}/jenkins-cli.sh" "${script_folder}/instances/${PROJECT_NAME}" "${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="$(pass "/bots/${PROJECT_NAME}/${pass_domain}/username")"
LF_XENTITY="
"
# TODO: does not seem to work
# translate line feeds to LF_XENTITY
#id_rsa=$(pass "${PASSWORD_STORE_DIR}/bots/${PROJECT_NAME}/${pass_domain}/id_rsa" | tr '\n' ',' | sed 's/,/\'${LF_XENTITY}'/g')
id_rsa="$(pass "/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="$(pass "/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="$(pass "/bots/${PROJECT_NAME}/${GITHUB_PASS_DOMAIN}/username")"
token="$(pass "/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="$(pass "/bots/${PROJECT_NAME}/${GPG_PASS_DOMAIN}/secret-subkeys.asc")"
create_file_credentials "_" "secret-subkeys.asc" "secret-subkeys.asc" "${subkeys}" # id == filename
fi