Skip to content

Commit 89d2566

Browse files
committed
login using Azure Workload ID when available
- use AZURE_FEDERATED_TOKEN_FILE while logging in via wi - overload Azure client ID with Azure workload ID for WI based auth - use auth mode for azure storage commands when using federated identity - SC2086 use unquoted expansion with alt value
1 parent 5a3f86f commit 89d2566

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

hack/ensure-acr-login.sh

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,11 @@ set +o xtrace
2222
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
2323
cd "${REPO_ROOT}" || exit 1
2424

25-
if [[ "${REGISTRY:-}" =~ capzci\.azurecr\.io ]]; then
25+
if [[ "${REGISTRY:-}" =~ \.azurecr\.io ]]; then
2626
# if we are using the prow Azure Container Registry, login.
2727
"${REPO_ROOT}/hack/ensure-azcli.sh"
2828
: "${AZURE_SUBSCRIPTION_ID:?Environment variable empty or not defined.}"
2929
az account set -s "${AZURE_SUBSCRIPTION_ID}"
30-
az acr login --name capzci
31-
# TODO(mainred): When using ACR, `az acr login` impacts the authentication of `docker buildx build --push` when the
32-
# ACR, capzci in our case, has anonymous pull enabled.
33-
# Use `docker login` as a suggested workaround and remove this target when the issue is resolved.
34-
# Issue link: https://github.com/Azure/acr/issues/582
35-
# Failed building link: https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_cloud-provider-azure/974/pull-cloud-provider-azure-e2e-ccm-capz/1480459040440979456
36-
docker login -u "${AZURE_CLIENT_ID}" -p "${AZURE_CLIENT_SECRET}" capzci.azurecr.io
30+
acrname="${REGISTRY%%.*}"
31+
az acr login --name "$acrname"
3732
fi

hack/ensure-azcli.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,16 @@ if [[ -z "$(command -v az)" ]]; then
2525
AZ_REPO=$(lsb_release -cs)
2626
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${AZ_REPO} main" | tee /etc/apt/sources.list.d/azure-cli.list
2727
apt-get update && apt-get install -y azure-cli
28-
az login --service-principal -u "${AZURE_CLIENT_ID}" -p "${AZURE_CLIENT_SECRET}" --tenant "${AZURE_TENANT_ID}" > /dev/null
28+
29+
if [[ -n "${AZURE_FEDERATED_TOKEN_FILE:-}" ]]; then
30+
# AZURE_CLIENT_ID has been overloaded with Azure Workload ID in the preset-azure-cred-wi.
31+
# This is done to avoid exporting Azure Workload ID as AZURE_CLIENT_ID in the test scenarios.
32+
az login --service-principal -u "${AZURE_CLIENT_ID}" -t "${AZURE_TENANT_ID}" --federated-token "$(cat "${AZURE_FEDERATED_TOKEN_FILE}")" > /dev/null
33+
34+
# Use --auth-mode "login" in az storage commands.
35+
ENABLE_AUTH_MODE_LOGIN="true"
36+
export ENABLE_AUTH_MODE_LOGIN
37+
else
38+
az login --service-principal -u "${AZURE_CLIENT_ID}" -p "${AZURE_CLIENT_SECRET}" --tenant "${AZURE_TENANT_ID}" > /dev/null
39+
fi
2940
fi

scripts/kind-with-registry.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,27 @@ function checkAZWIENVPreReqsAndCreateFiles() {
9393
echo "AZWI_RESOURCE_GROUP environment variable required - Azure resource group to store required Workload Identity artifacts"
9494
exit 1
9595
fi
96+
9697
if [ "$(az group exists --name "${AZWI_RESOURCE_GROUP}" --output tsv)" == 'false' ]; then
9798
echo "Creating resource group '${AZWI_RESOURCE_GROUP}' in '${AZWI_LOCATION}'"
9899
az group create --name "${AZWI_RESOURCE_GROUP}" --location "${AZWI_LOCATION}" --output none --only-show-errors --tags creationTimestamp="${TIMESTAMP}" jobName="${JOB_NAME}" buildProvenance="${BUILD_PROVENANCE}"
99100
fi
101+
100102
# Ensure that our connection to storage is inherited from the existing Azure login context
101103
unset AZURE_STORAGE_KEY
102104
unset AZURE_STORAGE_ACCOUNT
105+
103106
if ! az storage account show --name "${AZWI_STORAGE_ACCOUNT}" --resource-group "${AZWI_RESOURCE_GROUP}" > /dev/null 2>&1; then
104107
echo "Creating storage account '${AZWI_STORAGE_ACCOUNT}' in '${AZWI_RESOURCE_GROUP}'"
105108
az storage account create --resource-group "${AZWI_RESOURCE_GROUP}" --name "${AZWI_STORAGE_ACCOUNT}" --output none --only-show-errors --tags creationTimestamp="${TIMESTAMP}" jobName="${JOB_NAME}" buildProvenance="${BUILD_PROVENANCE}"
106-
az storage blob service-properties update --account-name "${AZWI_STORAGE_ACCOUNT}" --static-website
109+
az storage blob service-properties ${ENABLE_AUTH_MODE_LOGIN:+"--auth-mode login"} update --account-name "${AZWI_STORAGE_ACCOUNT}" --static-website
107110
fi
111+
108112
if ! az storage container show --name "${AZWI_STORAGE_CONTAINER}" --account-name "${AZWI_STORAGE_ACCOUNT}" > /dev/null 2>&1; then
109113
echo "Creating storage container '${AZWI_STORAGE_CONTAINER}' in '${AZWI_STORAGE_ACCOUNT}'"
110-
az storage container create --name "${AZWI_STORAGE_CONTAINER}" --account-name "${AZWI_STORAGE_ACCOUNT}" --output none --only-show-errors
114+
az storage container ${ENABLE_AUTH_MODE_LOGIN:+"--auth-mode login"} create --name "${AZWI_STORAGE_CONTAINER}" --account-name "${AZWI_STORAGE_ACCOUNT}" --output none --only-show-errors
111115
fi
116+
112117
SERVICE_ACCOUNT_ISSUER=$(az storage account show --name "${AZWI_STORAGE_ACCOUNT}" -o json | jq -r .primaryEndpoints.web)
113118
export SERVICE_ACCOUNT_ISSUER
114119
AZWI_OPENID_CONFIG_FILEPATH="${REPO_ROOT}/openid-configuration.json"
@@ -131,30 +136,37 @@ EOF
131136
openssl rsa -in "${SERVICE_ACCOUNT_SIGNING_KEY_FILEPATH}" -pubout -out "${SERVICE_ACCOUNT_SIGNING_PUB_FILEPATH}"
132137
AZWI_JWKS_JSON_FILEPATH="${REPO_ROOT}/jwks.json"
133138
"${AZWI}" jwks --public-keys "${SERVICE_ACCOUNT_SIGNING_PUB_FILEPATH}" --output-file "${AZWI_JWKS_JSON_FILEPATH}"
139+
134140
echo "Uploading openid-configuration document to '${AZWI_STORAGE_ACCOUNT}' storage account"
135141
upload_to_blob "${AZWI_OPENID_CONFIG_FILEPATH}" ".well-known/openid-configuration"
142+
136143
echo "Uploading jwks document to '${AZWI_STORAGE_ACCOUNT}' storage account"
137144
upload_to_blob "${AZWI_JWKS_JSON_FILEPATH}" "openid/v1/jwks"
138-
echo "Removing key access on storage account as no further data writes are required"
139-
az storage account update -n "${AZWI_STORAGE_ACCOUNT}" -g "${AZWI_RESOURCE_GROUP}" --subscription "${AZURE_SUBSCRIPTION_ID}" --allow-shared-key-access=false --output none --only-show-errors
140145
fi
146+
141147
if [ -z "${AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY}" ]; then
142148
if [ -z "${USER_IDENTITY}" ]; then
143149
echo "USER_IDENTITY environment variable required if not bringing your own identity via AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY"
144150
exit 1
145151
fi
152+
146153
az identity create -n "${USER_IDENTITY}" -g "${AZWI_RESOURCE_GROUP}" -l "${AZWI_LOCATION}" --output none --only-show-errors --tags creationTimestamp="${TIMESTAMP}" jobName="${JOB_NAME}" buildProvenance="${BUILD_PROVENANCE}"
147154
AZURE_IDENTITY_ID=$(az identity show -n "${USER_IDENTITY}" -g "${AZWI_RESOURCE_GROUP}" --query clientId -o tsv)
148155
AZURE_IDENTITY_ID_PRINCIPAL_ID=$(az identity show -n "${USER_IDENTITY}" -g "${AZWI_RESOURCE_GROUP}" --query principalId -o tsv)
156+
149157
echo "${AZURE_IDENTITY_ID}" > "${AZURE_IDENTITY_ID_FILEPATH}"
150158
until az role assignment create --assignee-object-id "${AZURE_IDENTITY_ID_PRINCIPAL_ID}" --role "Owner" --scope "/subscriptions/${AZURE_SUBSCRIPTION_ID}" --assignee-principal-type ServicePrincipal --output none --only-show-errors; do
151159
sleep 5
152160
done
161+
162+
echo "Creating federated credentials for capz-federated-identity"
153163
az identity federated-credential create -n "capz-federated-identity" \
154164
--identity-name "${USER_IDENTITY}" \
155165
-g "${AZWI_RESOURCE_GROUP}" \
156166
--issuer "${SERVICE_ACCOUNT_ISSUER}" \
157167
--subject "system:serviceaccount:capz-system:capz-manager" --output none --only-show-errors
168+
169+
echo "Creating federated credentials for aso-federated-identity"
158170
az identity federated-credential create -n "aso-federated-identity" \
159171
--identity-name "${USER_IDENTITY}" \
160172
-g "${AZWI_RESOURCE_GROUP}" \
@@ -168,7 +180,7 @@ function upload_to_blob() {
168180
local blob_name=$2
169181

170182
echo "Uploading ${file_path} to '${AZWI_STORAGE_ACCOUNT}' storage account"
171-
az storage blob upload \
183+
az storage blob upload ${ENABLE_AUTH_MODE_LOGIN:+"--auth-mode login"} \
172184
--container-name "${AZWI_STORAGE_CONTAINER}" \
173185
--file "${file_path}" \
174186
--name "${blob_name}" \

0 commit comments

Comments
 (0)