Skip to content

Commit 863307d

Browse files
author
Rick Rainey
authored
Merge pull request #50 from Microsoft/acr-setup-02
#37 changes requested for automated test in PR #47 (see comments)
2 parents efb966e + 5bcd226 commit 863307d

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The artifacts used to deploy this project include bash scripts and Terraform tem
1616
``` bash
1717
az login
1818
```
19-
4. Run `acr-sp-init.sh`. For example, the command below will provdision an Azure Container Registry (ACR) in East US and configure the two service principals in Azure Active Directory; one with AcrPush permission and another with AcrPull permission scoped to the ACR. The script parameter values are used to construct the name of the resource group, ACR, and service principals.
19+
4. Run `acr-sp-init.sh`. For example, the command below will provision an Azure Container Registry (ACR) in East US and configure the two service principals in Azure Active Directory; one with _AcrPush_ permission and another with _AcrPull_ permission scoped to the ACR. The script parameter values are used to construct the name of the resource group, ACR, and service principals.
2020

2121
``` bash
2222
$ ./acr-sp-init.sh -a Cblt -l eastus -s CoreProd
@@ -26,11 +26,13 @@ The artifacts used to deploy this project include bash scripts and Terraform tem
2626

2727
### Automated Test
2828

29-
The automated test for this setup step is in `./tests/acr-sp-init-test.sh`. It can be executed at the command line as shown below, or as part of a CI pipeline.
29+
The automated test for this setup step is in `./tests/acr-sp-init-test.sh`. It can be executed at the command line as shown below, or as part of a CI pipeline. If you don't want the test to cleanup resources, then pass the `-n` switch.
3030
3131
``` bash
3232
az login
33-
./tests/acr-sp-init-sh
33+
34+
# Run test and cleanup resources created.
35+
./tests/acr-sp-init-test.sh
3436
```
3537
3638
## Setup Shared / Core Infrastructure

setup/acr-sp-init.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,27 @@ az group create --name $rgName --location $location
105105

106106
# Create the container registry.
107107
acrName="${appname}${locationCode}acr${suffix}"
108-
acrId=$(az acr create --resource-group $rgName --name $acrName --sku Standard --query id)
108+
acrNameAvailable=$(az acr check-name --name $acrName --query nameAvailable)
109+
if [[ "$acrNameAvailable" == false ]]; then
110+
# Check to see if the ACR is already in the resource group and location we want.
111+
# If it is, then we can just continue. Otherwise, we need to abort.
112+
acrLocation=""
113+
acrLocation=$(az acr show --name $acrName --resource-group $rgName --query location)
114+
acrLocation="${acrLocation//\"}"
115+
if [[ -z $acrLocation || $acrLocation != $location ]]; then
116+
echo "Container registry '$acrName' already exists but is not in the requested resource group '$rgName' and location '$location'."
117+
echo "Modify your script inputs so a unique DNS name can be inferred."
118+
exit 1;
119+
else
120+
echo "Using existing container registry '$acrName' in resource group '$rgName'."
121+
acrId=$(az acr show --name $acrName --resource-group $rgName --query id)
122+
fi
123+
else
124+
echo "Creating container registry '$acrName' in resource group '$rgName'."
125+
acrId=$(az acr create --resource-group $rgName --name $acrName --sku Standard --query id)
126+
# ToDo: Should parameterize 'sku' in the future
127+
fi
109128
acrId="${acrId//\"}"
110-
# ToDo: Should parameterize 'sku' in the future
111129

112130
# Used to find/create service principals and role assignments to ACR.
113131
declare -A spAcrNameAndRole=(

tests/acr-sp-init-test.sh

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,62 @@ suffix="Cntso Dev"
77

88
bash ../setup/acr-sp-init.sh -a $appName -l $location -s "${suffix}"
99

10+
cleanup=true
11+
while getopts "n" opt; do
12+
case $opt in
13+
n)
14+
# Don't cleanup test resources.
15+
cleanup=false
16+
;;
17+
esac
18+
done
19+
1020
# Expected resource group properties
1121
rgName="cblt-usea-rg-cntsodev"
1222
rgLocation="eastus"
13-
expectedRgValues='['${rgName}','${rgLocation}']'
23+
expectedRgValues='['\"${rgName}\"','\"${rgLocation}\"']'
1424

1525
# Expected ACR properties
1626
acrName="cbltuseaacrcntsodev"
1727
acrLocation="eastus"
18-
expectedAcrValues='['${acrName}','${acrLocation}']'
28+
expectedAcrValues='['\"${acrName}\"','\"${acrLocation}\"']'
1929

2030
# Expected service principal properties
2131
declare -A spAcrNameAndRole=(
2232
["http://cblt-usea-sp-cntsodev-pull"]="AcrPull"
2333
["http://cblt-usea-sp-cntsodev-push"]="AcrPush"
2434
)
2535

36+
function assertEqual() {
37+
if [[ $# != 3 ]]; then
38+
echo "Unexpected number of parameters passed to '$0'."
39+
echo " Parameter 1 - actual value"
40+
echo " Parameter 2 - expected value"
41+
echo " Parameter 3 - description of resource"
42+
exit 1;
43+
fi
44+
45+
if [[ "$1" != "$2" ]]; then
46+
echo "Error: Unexpected '$3' values."
47+
echo " Actual value: '$1'"
48+
echo " Expected value: '$2'"
49+
exit 1;
50+
fi
51+
}
52+
2653
# Assertions
54+
2755
# Test resource group
2856
echo "Testing resource group..."
2957
resourceValues=$(az group show --name $rgName --query '[name,location]' --output JSON)
3058
resourceValues=${resourceValues//[[:space:]]/}
31-
resourceValues=${resourceValues//\"/}
32-
33-
if [[ "$resourceValues" != "$expectedRgValues" ]]; then
34-
echo "Error: Unexpected resource group values."
35-
echo " Results returned '${resourceValues}'"
36-
echo " expected '${expectedRgValues}'"
37-
exit 1;
38-
fi
59+
assertEqual $resourceValues $expectedRgValues "resource group"
3960

4061
# Test ACR
4162
echo "Testing container registry..."
4263
resourceValues=$(az acr show --name $acrName --query '[name,location]' --output JSON)
4364
resourceValues=${resourceValues//[[:space:]]/}
44-
resourceValues=${resourceValues//\"/}
45-
46-
if [[ "$resourceValues" != "$expectedAcrValues" ]]; then
47-
echo "Error: Unexpected container registry values."
48-
echo " Results returned '${resourceValues}'"
49-
echo " expected '${expectedAcrValues}'"
50-
exit 1;
51-
fi
65+
assertEqual $resourceValues $expectedAcrValues "container registry"
5266

5367
# Test service principals
5468
echo "Testing service principals..."
@@ -73,26 +87,33 @@ done
7387

7488
echo "Tests passed successfully"
7589

76-
# Clean up tests results
77-
echo "Cleaning up test resources"
78-
79-
echo " Cleaning up service principals and role assignments..."
80-
for spName in ${!spAcrNameAndRole[@]}
81-
do
82-
# Clean up service principals and role assignments
83-
spAppId=$(az ad sp show --id ${spName} --query appId)
84-
spAppId="${spAppId//\"}"
85-
az ad sp delete --id ${spAppId}
86-
done
87-
88-
# Clean up container registry
89-
echo " Cleaning up container registry..."
90-
az acr delete --name ${acrName}
91-
92-
# Clean up resource group
93-
echo " Cleaning up resource group..."
94-
az group delete --name ${rgName} --yes
95-
96-
97-
98-
90+
# Clean up tests resources
91+
if [[ "$cleanup" == true ]]; then
92+
echo "Cleaning up test resources"
93+
94+
for spName in ${!spAcrNameAndRole[@]}
95+
do
96+
# Clean up role assignments
97+
echo " Cleaning up role assignment '${spAcrNameAndRole[$spName]}' to ACR '$acrName' for service principal '$spName'."
98+
az role assignment delete --assignee ${spName} --scope ${acrId} --role ${spAcrNameAndRole[$spName]}
99+
100+
# Clean up service principals if it's mine to delete
101+
spAppId=$(az ad sp show --id ${spName} --query appId)
102+
spAppId="${spAppId//\"}"
103+
spIsMineToDelete=$(az ad sp list --show-mine --query '[].appId | contains(@, `'${spAppId}'`)')
104+
if [[ "$spIsMineToDelete" == true ]]; then
105+
echo " Cleaning up service principal '$spName'."
106+
az ad sp delete --id $spAppId
107+
else
108+
echo " Not cleaning up service principal '$spName' in Azure AD because it belongs to another user."
109+
fi
110+
done
111+
112+
# Clean up container registry
113+
echo " Cleaning up container registry..."
114+
az acr delete --name ${acrName}
115+
116+
# Clean up resource group
117+
echo " Cleaning up resource group..."
118+
az group delete --name ${rgName} --yes
119+
fi

0 commit comments

Comments
 (0)