-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.sh
executable file
·92 lines (64 loc) · 3.27 KB
/
setup.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
#!/bin/bash
# Name of the Azure AD app
appDisplayName="chgeuer-repo-demo2"
# repository on github
githubUser="chgeuer"
githubRepo="azure-workload-identity-github"
# Azure storage account and blob container name
account_name="isvreleases"
container_name="backendrelease"
## Setup
### Azure AD
#### Ensure you're in the right AAD
az account show
#### Create a new app
az ad app create --display-name "${appDisplayName}"
applicationObjectId="$( az ad app list --display-name "${appDisplayName}" | jq -r '.[0].id' )"
az ad sp create --id "${applicationObjectId}"
spJson="$( az ad sp list --display-name "${appDisplayName}" | jq -r '.[0]' )"
spClientId="$( echo "${spJson}" | jq -r '.appId' )"
spObjectId="$( echo "${spJson}" | jq -r '.id' )"
echo "Service Principal ClientID ${spClientId} object ID ${spObjectId}"
#### Set the federated credential
# audience="https://github.com/${githubUser}"
audience="api://AzureADTokenExchange"
json="$( echo "{}" \
| jq --arg x "federatedCred-${githubUser}-${githubRepo}" '.name=$x' \
| jq --arg x "Github repo ${githubUser}/${githubRepo}" '.description=$x' \
| jq --arg x "https://token.actions.githubusercontent.com" '.issuer=$x' \
| jq --arg x "repo:${githubUser}/${githubRepo}:ref:refs/heads/main" '.subject=$x' \
| jq '.audiences=[]' \
| jq --arg x "${audience}" '.audiences[.audiences | length] |= .+ $x' \
)"
# echo "${json}" | jq .
# https://docs.microsoft.com/en-us/graph/api/application-post-federatedidentitycredentials?view=graph-rest-beta&tabs=http#request
az rest \
--method POST \
--uri "https://graph.microsoft.com/beta/applications/${applicationObjectId}/federatedIdentityCredentials/" \
--body "${json}"
# az rest \
# --method GET \
# --uri "https://graph.microsoft.com/beta/applications/${applicationObjectId}/federatedIdentityCredentials/"
######################################
#### Set the Github secrets
######################################
AZURE_TENANT_ID="$( az account show | jq -r '.tenantId' )"
echo "Set ${githubUser}/${githubRepo} secret AZURE_TENANT_ID to ${AZURE_TENANT_ID}"
gh secret set --repo "${githubUser}/${githubRepo}" AZURE_TENANT_ID --body "${AZURE_TENANT_ID}"
echo "Set ${githubUser}/${githubRepo} secret AZURE_CLIENT_ID to ${spClientId}"
gh secret set --repo "${githubUser}/${githubRepo}" AZURE_CLIENT_ID --body "${spClientId}"
######################################
#### Grant permissions on the storage account container
######################################
# account_name="isvreleases"
# container_name="backendrelease"
subscriptionId="$( az account show | jq -r '.id' )"
resourceGroup="$( az storage account show --name "${account_name}" | jq -r '.resourceGroup' )"
scope="/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Storage/storageAccounts/${account_name}/blobServices/default/containers/${container_name}"
role="Storage Blob Data Contributor"
echo "Authorizing ${spObjectId} to be a '${role}' on ${scope}"
az role assignment create \
--role "${role}" \
--scope "${scope}" \
--assignee-principal-type "ServicePrincipal" \
--assignee-object-id "${spObjectId}"