@@ -34,6 +34,11 @@ declare -r SECRET_NAME="${SECRET_NAME:-}"
3434# set with the user's access token obtained from Quay.
3535declare ACCESS_TOKEN=" "
3636
37+ # Quay robot account for register
38+ declare QUAY_ROBOT_SHORT_NAME=" ${QUAY_ROBOT_SHORT_NAME:- rhtap_rw} "
39+ declare QUAY_ROBOT_USERNAME=" "
40+ declare QUAY_ROBOT_TOKEN=" "
41+
3742#
3843# Functions
3944#
@@ -160,8 +165,8 @@ quay_create_secret() {
160165 oc create secret docker-registry " ${SECRET_NAME} " \
161166 --namespace=" ${NAMESPACE} " \
162167 --docker-server=" ${QUAY_HOSTNAME} " \
163- --docker-username=" ${QUAY_USERNAME } " \
164- --docker-password=" ${QUAY_PASSWORD } " \
168+ --docker-username=" ${QUAY_ROBOT_USERNAME } " \
169+ --docker-password=" ${QUAY_ROBOT_TOKEN } " \
165170 --docker-email=" ${QUAY_EMAIL} " \
166171 --dry-run=client \
167172 --output=yaml |
@@ -196,6 +201,151 @@ quay_create_secret() {
196201 return 1
197202}
198203
204+ # Create a robot account in organization with the name informed via environment,
205+ # using the super-user's ACCESS_TOKEN to authorize the request.
206+ quay_create_robot_account () {
207+ local quay_url=" https://${QUAY_HOSTNAME} /api/v1/organization/${QUAY_ORGANIZATION} /robots/${QUAY_ROBOT_SHORT_NAME} "
208+ local data=(
209+ " {"
210+ " \" description\" : \" Quay robot account for ${QUAY_ORGANIZATION} \" ,"
211+ " \" unstructured_metadata\" : {}"
212+ " }"
213+ )
214+ local create_response token
215+
216+ info " Creating Quay robot account ${QUAY_ROBOT_SHORT_NAME} ..."
217+ create_response=$(
218+ curl \
219+ --silent \
220+ --insecure \
221+ --location \
222+ --request PUT \
223+ --header ' Content-Type: application/json' \
224+ --header " Authorization: Bearer ${ACCESS_TOKEN} " \
225+ --data " ${data[*]} " \
226+ " ${quay_url} "
227+ )
228+
229+ # When robot account already exists, the script should continue without failing.
230+ if [[ -z " ${create_response} " || " ${create_response} " == * " Existing robot" * ]]; then
231+ warn " Robot account already exists!"
232+ return 0
233+ fi
234+
235+ # When robot account creation fails, the script should fail completely.
236+ if [[ -z " ${create_response} " || (" ${create_response} " ! = * " created" * ) ]]; then
237+ fail " Failed to create robot account!"
238+ fi
239+
240+ info " Extracting token from the response..."
241+ # When response doesn't contain the expected "token", the script should
242+ # fail completely.
243+ token=$( echo " ${create_response} " | jq --raw-output ' .token' )
244+ if [[ -z " ${token} " || " ${token} " == " null" ]]; then
245+ fail " Failed to get robot account token!"
246+ fi
247+
248+ info " Robot account created successfully!"
249+ export QUAY_ROBOT_TOKEN=" ${token} "
250+ export QUAY_ROBOT_USERNAME=" ${QUAY_ORGANIZATION} +${QUAY_ROBOT_SHORT_NAME} "
251+ }
252+
253+ # Create a new permission prototype in organization, that will automatically
254+ # grant admin permission of repositories to robot account
255+ quay_create_permission_prototype () {
256+ local quay_url=" https://${QUAY_HOSTNAME} /api/v1/organization/${QUAY_ORGANIZATION} /prototypes"
257+ local data=(
258+ " {"
259+ " \" role\" : \" admin\" ,"
260+ " \" activating_user\" : {"
261+ " \" name\" : \"\" "
262+ " },"
263+ " \" delegate\" : {"
264+ " \" name\" : \" ${QUAY_ROBOT_USERNAME} \" ,"
265+ " \" kind\" : \" user\" "
266+ " }"
267+ " }"
268+ )
269+ local create_response
270+
271+ info " Creating new permission prototype in organization ${QUAY_ORGANIZATION} ..."
272+ create_response=$(
273+ curl \
274+ --silent \
275+ --insecure \
276+ --location \
277+ --request POST \
278+ --header ' Content-Type: application/json' \
279+ --header " Authorization: Bearer ${ACCESS_TOKEN} " \
280+ --data " ${data[*]} " \
281+ " ${quay_url} "
282+ )
283+
284+ if [[ -z " ${create_response} " || " ${create_response} " != * " ${QUAY_ROBOT_USERNAME} " * ]]; then
285+ fail " Failed to create new permission prototype!"
286+ fi
287+
288+ info " Create new permission prototype successfully!"
289+ }
290+
291+ # Create a new team in organization with creator role
292+ quay_create_team () {
293+ local team_name=" ${QUAY_ORGANIZATION} -creator"
294+ local quay_url=" https://${QUAY_HOSTNAME} /api/v1/organization/${QUAY_ORGANIZATION} /team/${team_name} "
295+ local data=(
296+ " {"
297+ " \" role\" : \" creator\" ,"
298+ " \" description\" : \" Team with creator role for ${QUAY_ORGANIZATION} \" "
299+ " }"
300+ )
301+ local create_response
302+
303+ info " Creating new team with creator role in organization ${QUAY_ORGANIZATION} ..."
304+ create_response=$(
305+ curl \
306+ --silent \
307+ --insecure \
308+ --location \
309+ --request PUT \
310+ --header ' Accept: application/json' \
311+ --header ' Content-Type: application/json' \
312+ --header " Authorization: Bearer ${ACCESS_TOKEN} " \
313+ --data " ${data[*]} " \
314+ " ${quay_url} "
315+ )
316+
317+ if [[ -z " ${create_response} " || " ${create_response} " != * " ${team_name} " * ]]; then
318+ fail " Failed to create new team with creator role!"
319+ fi
320+
321+ info " Create new team with creator role successfully!"
322+ }
323+
324+ # # Assign the robot account to the team with creator role
325+ quay_assign_robot_to_team () {
326+ local team_name=" ${QUAY_ORGANIZATION} -creator"
327+ local quay_url=" https://${QUAY_HOSTNAME} /api/v1/organization/${QUAY_ORGANIZATION} /team/${team_name} /members/${QUAY_ROBOT_USERNAME} "
328+
329+ local create_response
330+
331+ info " Assigning robot account to team ${team_name} ..."
332+ create_response=$(
333+ curl \
334+ --silent \
335+ --insecure \
336+ --location \
337+ --request PUT \
338+ --header " Authorization: Bearer ${ACCESS_TOKEN} " \
339+ " ${quay_url} "
340+ )
341+
342+ if [[ -z " ${create_response} " || " ${create_response} " != * " ${QUAY_ROBOT_USERNAME} " * ]]; then
343+ fail " Failed to assign robot account to team!"
344+ fi
345+
346+ info " Assign robot account to team successfully!"
347+ }
348+
199349# Initializes the Quay super-user and creates a "docker-registry" secret with the
200350# credentials informed via environment variables.
201351quay_helper () {
@@ -217,13 +367,17 @@ quay_helper() {
217367 return 0
218368 fi
219369
370+ quay_create_organization
371+ quay_create_robot_account
372+ quay_create_permission_prototype
373+ quay_create_team
374+ quay_assign_robot_to_team
375+
220376 quay_create_secret || {
221377 warn " Failed to create secret!"
222378 return 1
223379 }
224380
225- quay_create_organization
226-
227381 return 0
228382}
229383
0 commit comments