Skip to content

Commit 0c59144

Browse files
committed
Unset xtrace while logging into acr via podman to prevent exposing the acr login secret
Reference: https://portal.microsofticm.com/imp/v5/incidents/details/752908291/summary
1 parent 1db8fd5 commit 0c59144

File tree

2 files changed

+82
-21
lines changed

2 files changed

+82
-21
lines changed

pkg/deploy/generator/scripts/util-common.sh

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,72 @@
11
#!/bin/bash
22
# Internal Functions and Constants
33

4+
# declare -r empty_str=""
5+
#
46
# empty_str - constant; used by functions for optional nameref string arguements
5-
# empty_str=""
67
# shellcheck disable=SC2034
78
declare -r empty_str=""
89

9-
# role_gateway - constant; Is used to determine which VMSS is being bootstrapped
10+
# declare -r role_gateway="gateway"
11+
#
1012
# this should be referenced by scripts sourcing this file
1113
# role_gateway="gateway"
1214
declare -r role_gateway="gateway"
13-
# role_rp - constant; Is used to determine which VMSS is being bootstrapped
15+
16+
# declare -r role_rp="rp"
17+
#
1418
# this should be referenced by scripts sourcing this file
1519
# role_rp="rp"
1620
declare -r role_rp="rp"
21+
22+
# declare -r role_devproxy="devproxy"
23+
#
1724
# role_devproxy - constant; Is used to determine which VMSS is being bootstrapped
18-
# role_devproxy="devproxy"
1925
declare -r role_devproxy="devproxy"
26+
27+
# declare -r us_gov_cloud="AzureUSGovernment"
28+
#
2029
# us_gov_cloud - constant; Is the name of AZURECLOUDNAME for US government cloud
21-
# us_gov_cloud="AzureUSGovernment"
2230
declare -r us_gov_cloud="AzureUSGovernment"
2331

32+
# declare -i XTRACE_IS_SET
33+
#
34+
# Global variable used to keep track of if xtrace was/is set.
35+
declare -i XTRACE_IS_SET
36+
37+
# declare -i XTRACE_SET=1
38+
#
39+
# constant value used to set XTRACE_IS_SET
40+
declare -ir XTRACE_SET=1
41+
42+
# declare -i XTRACE_UNSET=0
43+
#
44+
# constant value used to set XTRACE_IS_SET
45+
declare -ir XTRACE_UNSET=0
46+
47+
# xtrace_set_capture()
48+
#
49+
# Captures if xtrace is set in the current shell using global variable XTRACE_IS_SET.
50+
# * Used for reapplying xtrace setting after disabling.
51+
# * Sets XTRACE_IS_SET=XTRACE_SET (if true) or XTRACE_IS_SET=XTRACE_UNSET (if false).
52+
xtrace_set_capture() {
53+
[[ $- =~ "x" ]] && XTRACE_IS_SET=$XTRACE_SET || XTRACE_IS_SET=$XTRACE_UNSET
54+
}
55+
56+
# xtrace_unset()
57+
#
58+
# Un-sets xtrace (if set)
59+
xtrace_unset() {
60+
(( XTRACE_IS_SET == XTRACE_SET )) && set +x
61+
}
62+
63+
# xtrace_set()
64+
#
65+
# Sets xtrace (if unset)
66+
xtrace_set() {
67+
(( XTRACE_IS_SET == XTRACE_UNSET )) && set -x
68+
}
69+
2470
# log is a wrapper for echo that includes the function name
2571
# Args
2672
# 1) msg - string

pkg/deploy/generator/scripts/util-system.sh

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pull_container_images() {
141141
log "starting"
142142

143143
# shellcheck disable=SC2034
144-
local -ri retry_time=30
144+
local -ir retry_time=30
145145
cmd=(
146146
az
147147
login
@@ -157,24 +157,39 @@ pull_container_images() {
157157
mkdir -p /root/.docker
158158
touch /etc/containers/nodocker
159159

160+
[ -n "${registry_conf}" ] && write_file REGISTRY_AUTH_FILE registry_conf "true"
161+
160162
# This name is used in the case that az acr login searches for this in it's environment
163+
# exported here as it's used by podman login and subsequent podman pull
161164
export REGISTRY_AUTH_FILE="/root/.docker/config.json"
162165

163-
if [ -n "${registry_conf}" ]; then
164-
write_file REGISTRY_AUTH_FILE registry_conf true
165-
fi
166-
167-
log "logging into prod acr"
168-
cmd=(
169-
az
170-
acr
171-
login
172-
--name
173-
# TODO replace this with variable expansion
174-
# Reference: https://www.shellcheck.net/wiki/SC2001
175-
"$(sed -e 's|.*/||' <<<"$ACRRESOURCEID")"
176-
)
177-
166+
# shellcheck disable=SC2329
167+
_() {
168+
local -r acr="$1"
169+
local -r registry="$2"
170+
171+
xtrace_set_capture
172+
xtrace_unset
173+
174+
log "logging into container registry $2"
175+
az acr login \
176+
--name "$acr" \
177+
--expose-token \
178+
--output tsv \
179+
--query accessToken \
180+
| podman login \
181+
--username "00000000-0000-0000-0000-000000000000" \
182+
--password-stdin \
183+
"$registry"
184+
local -ir status=$?
185+
186+
xtrace_set
187+
return "$status"
188+
}
189+
190+
local -r acr_name="${ACRRESOURCEID##*/}"
191+
local -r registry_name="${acr_name}.azurecr.io"
192+
cmd=(_ "$acr_name" "$registry_name")
178193
retry cmd retry_time
179194

180195
# shellcheck disable=SC2068

0 commit comments

Comments
 (0)