Skip to content

Commit 69098c7

Browse files
authored
Storage access fixes (#225)
* Use AzureAD auth for terraform backend Move away from using shared key credentials for the backend auth in both CI and local dev. * Test deploy * CLI + OIDC * Debug * Remove debug
1 parent 9beb59b commit 69098c7

File tree

8 files changed

+73
-35
lines changed

8 files changed

+73
-35
lines changed

.github/workflows/cicd.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232

3333
- name: Get image tag
3434
id: get_image_tag
35-
run:
35+
run:
3636
case "${GITHUB_REF}" in
3737
*tags*)
3838
echo "tag=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT ;
@@ -57,7 +57,7 @@ jobs:
5757
- build_and_publish
5858
steps:
5959
- uses: actions/checkout@v3
60-
60+
6161
- name: Log in with Azure
6262
uses: azure/login@v1
6363
with:
@@ -86,4 +86,4 @@ jobs:
8686
ARM_CLIENT_ID: ${{ fromJSON(secrets.SECURE_AZURE_CREDENTIALS).clientId }}
8787
ARM_SUBSCRIPTION_ID: ${{ fromJSON(secrets.SECURE_AZURE_CREDENTIALS).subscriptionId }}
8888
ARM_TENANT_ID: ${{ fromJSON(secrets.SECURE_AZURE_CREDENTIALS).tenantId }}
89-
ARM_USE_OIDC: true
89+
ARM_USE_OIDC: true

deployment/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The logic for the deployment workflow is encapsulated in the [bin/deploy](bin/de
1010
scripts/console --deploy
1111
```
1212

13+
To have access to the remote backend terraform state, the identity (App Registration in CI, or local corp credential if local) will need to have the `Storage Blob Data Owner` role on the `pctesttfstate` storage account.
14+
1315
## Manual resources
1416

1517
### Deployment secrets Key Vault

deployment/bin/deploy

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,20 @@ while [[ "$#" -gt 0 ]]; do case $1 in
4949
;;
5050
esac done
5151

52-
disable_shared_access_keys() {
53-
echo "Disabling shared access key on storage account..."
54-
az storage account update \
55-
--name ${SAK_STORAGE_ACCOUNT} \
56-
--resource-group ${SAK_RESOURCE_GROUP} \
57-
--allow-shared-key-access false \
58-
--output none
59-
60-
if [ $? -ne 0 ]; then
61-
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
62-
echo "WARNING: Failed to turn off shared key access on the storage account."
63-
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
64-
exit 2
65-
fi
66-
}
67-
6852
# Always disable shared access keys on script exit
6953
trap disable_shared_access_keys EXIT
7054

7155
###################################
7256
# Check and configure environment #
7357
###################################
74-
SAK_STORAGE_ACCOUNT=pctapisstagingsa
75-
SAK_RESOURCE_GROUP=pct-apis-westeurope-staging_rg
58+
59+
# Enable shared access keys on storage accounts that must have properties read
60+
# [storage_account]=resource_group
61+
declare -A SAK_STORAGE_ACCOUNTS
62+
SAK_STORAGE_ACCOUNTS=(
63+
["pctapisstagingsa"]="pct-apis-westeurope-staging_rg"
64+
["pcfilestest"]="pc-test-manual-resources"
65+
)
7666

7767
if [[ -z ${TERRAFORM_DIR} ]]; then
7868
echo "Must pass in TERRAFORM_DIR with -t"
@@ -94,6 +84,12 @@ setup_env
9484
echo "===== Running Deploy ====="
9585
echo "IMAGE_TAG: ${IMAGE_TAG}"
9686

87+
if [ -z "$ARM_CLIENT_ID" ]; then
88+
export ARM_CLIENT_ID=$(az account show --query user.name -o tsv)
89+
echo "Using Azure CLI auth with username: ${ARM_CLIENT_ID}"
90+
fi
91+
92+
9793
# ---------------------------------------------------
9894

9995
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
@@ -113,16 +109,7 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
113109
if [[ "${SKIP_TF}" != 1 ]]; then
114110
echo "Deploying infrastructure with Terraform..."
115111

116-
echo "Enabling shared key access for storage account..."
117-
# Terraform isn't able to read all resources from a storage account if shared key access is disabled
118-
# so while we're deploying, we need to enable it. Since we haven't run TF yet, we don't have the name of the account
119-
# so they are hardcoded here. This is a temporary workaround until this is resolved
120-
# https://github.com/hashicorp/terraform-provider-azurerm/issues/25218
121-
az storage account update \
122-
--name ${SAK_STORAGE_ACCOUNT} \
123-
--resource-group ${SAK_RESOURCE_GROUP} \
124-
--allow-shared-key-access true \
125-
--output none
112+
enable_shared_access_keys
126113

127114
terraform init --upgrade
128115

@@ -176,7 +163,6 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
176163
--wait \
177164
--timeout 2m0s \
178165
-f ${DEPLOY_VALUES_FILE} \
179-
--debug
180166

181167
echo "================"
182168
echo "==== Tiler ====="
@@ -189,7 +175,6 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
189175
--wait \
190176
--timeout 2m0s \
191177
-f ${DEPLOY_VALUES_FILE} \
192-
--debug
193178

194179
echo "=================="
195180
echo "==== Ingress ====="

deployment/bin/lib

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,43 @@ function get_cidr_range() {
131131
IFS='.' read -r -a ip_parts <<< "$runnerIpAddress"
132132
echo "${ip_parts[0]}.${ip_parts[1]}.0.0/16"
133133
}
134+
135+
function disable_shared_access_keys() {
136+
echo "Disabling shared access key on storage account..."
137+
138+
for SAK_STORAGE_ACCOUNT in "${!SAK_STORAGE_ACCOUNTS[@]}"; do
139+
SAK_RESOURCE_GROUP=${SAK_STORAGE_ACCOUNTS[$SAK_STORAGE_ACCOUNT]}
140+
141+
az storage account update \
142+
--name ${SAK_STORAGE_ACCOUNT} \
143+
--resource-group ${SAK_RESOURCE_GROUP} \
144+
--allow-shared-key-access false \
145+
--output none
146+
147+
if [ $? -ne 0 ]; then
148+
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
149+
echo "WARNING: Failed to turn off shared key access on the storage account."
150+
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
151+
exit 2
152+
fi
153+
done
154+
}
155+
156+
function enable_shared_access_keys() {
157+
echo "Enabling shared key access for storage account..."
158+
# Terraform isn't able to read all resources from a storage account if shared key access is disabled
159+
# so while we're deploying, we need to enable it. Since we haven't run TF yet, we don't have the name of the account
160+
# so they are hardcoded here. This is a temporary workaround until this is resolved
161+
# https://github.com/hashicorp/terraform-provider-azurerm/issues/25218
162+
163+
for SAK_STORAGE_ACCOUNT in "${!SAK_STORAGE_ACCOUNTS[@]}"; do
164+
SAK_RESOURCE_GROUP=${SAK_STORAGE_ACCOUNTS[$SAK_STORAGE_ACCOUNT]}
165+
166+
echo " - enabling ${SAK_STORAGE_ACCOUNT} / ${SAK_RESOURCE_GROUP}"
167+
az storage account update \
168+
--name ${SAK_STORAGE_ACCOUNT} \
169+
--resource-group ${SAK_RESOURCE_GROUP} \
170+
--allow-shared-key-access true \
171+
--output none
172+
done
173+
}

deployment/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ services:
88
environment:
99
- ACR_STAC_REPO=${ACR_STAC_REPO:-pccomponentstest.azurecr.io/planetary-computer-apis/stac}
1010
- ACR_TILER_REPO=${ACR_TILER_REPO:-pccomponentstest.azurecr.io/planetary-computer-apis/tiler}
11-
- IMAGE_TAG
11+
- IMAGE_TAG=${IMAGE_TAG:-latest}
1212
- GIT_COMMIT
1313

1414
- ARM_SUBSCRIPTION_ID=${ARM_SUBSCRIPTION_ID:-a84a690d-585b-4c7c-80d9-851a48af5a50}
15-
- ARM_TENANT_ID
15+
- ARM_TENANT_ID=${ARM_TENANT_ID:-72f988bf-86f1-41af-91ab-2d7cd011db47}
1616
- ARM_CLIENT_ID
1717
- ARM_USE_OIDC
1818
- ARM_OIDC_TOKEN

deployment/terraform/resources/providers.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
provider "azurerm" {
22
features {}
33
use_oidc = true
4+
5+
# This could be used instead of temporarily enabling shared key access once
6+
# this issue is resolved.
7+
# https://github.com/hashicorp/terraform-provider-azurerm/issues/23142
8+
# storage_use_azuread = true
49
}
510

611
terraform {

deployment/terraform/resources/storage_account.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ resource "azurerm_storage_table" "ipexceptionlist" {
3737
name = "ipexceptionlist"
3838
storage_account_name = azurerm_storage_account.pc.name
3939
}
40+
41+
resource "azurerm_storage_table" "blobstoragebannedip" {
42+
name = "blobstoragebannedip"
43+
storage_account_name = azurerm_storage_account.pc.name
44+
}

deployment/terraform/staging/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ terraform {
3131
container_name = "pc-test-api"
3232
key = "pqe-apis.tfstate"
3333
use_oidc = true
34+
use_azuread_auth = true
3435
}
3536
}
3637

0 commit comments

Comments
 (0)