Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: storage account 403 error when creating new tre env #4406

Merged
merged 38 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3c407d7
Refactor role assignment check to validate both "Storage Blob Data Co…
ShakutaiGit Feb 26, 2025
bdb0886
Remove unused script source from bootstrap.sh
ShakutaiGit Feb 26, 2025
c421afb
Refactor role assignment check to use local variables for clarity
ShakutaiGit Feb 26, 2025
52598a6
Update CHANGELOG and add role assignment for Storage Account Contribu…
ShakutaiGit Feb 26, 2025
1094118
Refactor role assignment check to use counts for validation and strea…
ShakutaiGit Feb 26, 2025
4c26cde
Refactor role assignment check to use non-empty value validation and …
ShakutaiGit Feb 26, 2025
ff6ad3c
CR changes
ShakutaiGit Mar 6, 2025
f2d6464
Merge branch 'main' of https://github.com/microsoft/AzureTRE into fix…
ShakutaiGit Mar 6, 2025
9bd9db3
Refactor role assignment check to verify access via storage container…
ShakutaiGit Mar 6, 2025
5e38237
Implement retry logic for role assignment checks in bootstrap script
ShakutaiGit Mar 6, 2025
1ccec96
Refactor check_terraform_role_assignments function to simplify error …
ShakutaiGit Mar 6, 2025
b8bff96
Enhance error message in check_terraform_role_assignments function to…
ShakutaiGit Mar 6, 2025
0f4d64a
Remove unnecessary echo statement from retry_with_backoff function in…
ShakutaiGit Mar 6, 2025
4d9e220
Add validation for required environment variables in bootstrap script
ShakutaiGit Mar 6, 2025
9698f64
Refactor bootstrap script to streamline Terraform initialization and …
ShakutaiGit Mar 9, 2025
7b3f02f
Add shellcheck disable comments for unbound variable warnings in boot…
ShakutaiGit Mar 9, 2025
d3c03ca
Update terraform_wrapper invocation and add blank line for readabilit…
ShakutaiGit Mar 9, 2025
73ac68a
Bump version to 0.12.7 in version.txt
ShakutaiGit Mar 9, 2025
f8723f6
Refactor bootstrap script to move backend configuration inline and re…
ShakutaiGit Mar 9, 2025
607dabe
Remove redundant function call for writing bootstrap Terraform backend
ShakutaiGit Mar 9, 2025
a41086d
Remove TODO comments from Terraform command output in bootstrap script
ShakutaiGit Mar 9, 2025
afcbe46
Bump version to 0.12.8 in version.txt
ShakutaiGit Mar 9, 2025
f502937
Bump version to 0.12.7 in version.txt
ShakutaiGit Mar 9, 2025
0823bfa
Refactor bootstrap script to inline backend configuration and improve…
ShakutaiGit Mar 10, 2025
ef58d8f
fix: update condition for Azure and ACR login to check for 'make boot…
ShakutaiGit Mar 10, 2025
d1d1c1e
fix: resolve CI issue with Azure login steps for branches containing …
ShakutaiGit Mar 10, 2025
0988a78
Merge branch 'main' of https://github.com/microsoft/AzureTRE into fix…
ShakutaiGit Mar 10, 2025
59a36c7
Merge branch 'main' of https://github.com/microsoft/AzureTRE into fix…
ShakutaiGit Mar 10, 2025
198c8a9
Merge branch 'fix-acr-skip-bootsrap-4416' of https://github.com/micro…
ShakutaiGit Mar 10, 2025
cbb9af9
refactor: move Terraform backend configuration to a dedicated section…
ShakutaiGit Mar 10, 2025
44f2a39
Merge branch 'main' of https://github.com/microsoft/AzureTRE into fix…
ShakutaiGit Mar 10, 2025
e40efd4
fix: update role assignment to use Storage Blob Data Contributor in b…
ShakutaiGit Mar 10, 2025
d89b8e9
fix: refine role assignment query in bootstrap.sh to check only for S…
ShakutaiGit Mar 11, 2025
42a0ff8
fix: update role check in bootstrap.sh to return status instead of ec…
ShakutaiGit Mar 11, 2025
578ae89
fix: update comment for granting Storage Blob Data Contributor role i…
ShakutaiGit Mar 11, 2025
a60f169
fix: update comment for granting Storage Blob Data Contributor permis…
ShakutaiGit Mar 11, 2025
141bcd6
fix: update terraform initialization command to include backend optio…
ShakutaiGit Mar 11, 2025
c1e5441
fix: remove trailing period from comment for granting Storage Blob Da…
ShakutaiGit Mar 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BUG FIXES:
* Fix retry loop in devcontainer action and override commands.sh [#4409](https://github.com/microsoft/AzureTRE/pull/4409)
* Fix terraform output command by adding working directory parameterPR ([#4413](https://github.com/microsoft/AzureTRE/pull/4413)) [#4412](https://github.com/microsoft/AzureTRE/issues/4412)
* Fix CI issue where branch names containing 'bootstrap' would incorrectly skip Azure login steps [#4416](https://github.com/microsoft/AzureTRE/issues/4416) ([#4417](https://github.com/microsoft/AzureTRE/pull/4417))
* Fix 403 storage account error when creating a new TRE environment ([#4405](https://github.com/microsoft/AzureTRE/issues/4405)) in PR [#4406](https://github.com/microsoft/AzureTRE/pull/4406)

## 0.21.0

Expand Down
99 changes: 77 additions & 22 deletions devops/terraform/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,49 @@ set -o errexit
set -o pipefail
set -o nounset

retry_with_backoff() {
local func="$1"
local sleep_time=10
local max_sleep=180

while [ "$sleep_time" -lt "$max_sleep" ]; do
if "$func"; then
return 0
fi
sleep "$sleep_time"
sleep_time=$((sleep_time * 2))
done
return 1
}

init_terraform() {
terraform_output=$(terraform init -input=false -reconfigure 2>&1)
echo "Terraform command output:"
echo "$terraform_output"

if echo "$terraform_output" | grep -q "AuthorizationPermissionMismatch\|403\|Failed to get existing workspaces"; then
return 1
elif echo "$terraform_output" | grep -q "Terraform has been successfully initialized"; then
return 0
fi

echo "Apply Retry mechanism on: ERROR- Unexpected output from terraform init: $terraform_output"
return 1
}

check_role_assignments() {
local roles
# shellcheck disable=SC2154
roles=$(az role assignment list \
--assignee "$USER_OBJECT_ID" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name" \
--query "[?roleDefinitionName=='Storage Blob Data Contributor' || roleDefinitionName=='Storage Account Contributor'].roleDefinitionName" --output tsv)

if [[ $roles == *"Storage Blob Data Contributor"* ]]; then
echo "both"
fi
}

# Baseline Azure resources
echo -e "\n\e[34m»»» 🤖 \e[96mCreating resource group and storage account\e[0m..."
# shellcheck disable=SC2154
Expand Down Expand Up @@ -38,30 +81,44 @@ if [ -n "${ARM_CLIENT_ID:-}" ]; then
else
USER_OBJECT_ID=$(az ad signed-in-user show --query id --output tsv)
fi

az role assignment create --assignee "$USER_OBJECT_ID" \
--role "Storage Account Contributor" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name"

az role assignment create --assignee "$USER_OBJECT_ID" \
--role "Storage Blob Data Contributor" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name"

# Function to check if the role assignment exists
check_role_assignment() {
az role assignment list --assignee "$USER_OBJECT_ID" --role "Storage Blob Data Contributor" --scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name" --query "[].id" --output tsv
}
if ! retry_with_backoff check_role_assignments; then
echo "ERROR: Timeout waiting for az role assignments."
exit 1
fi

# Wait for the role assignment to be applied
echo -e "\n\e[34m»»» ⏳ \e[96mWaiting for role assignment to be applied\e[0m..."
while [ -z "$(check_role_assignment)" ]; do
echo "Waiting for role assignment..."
sleep 10
done
echo "Role assignment applied."

# Blob container
echo -e "\n\e[34m»»» 📦 \e[96mCreating storage containers\e[0m..."
# shellcheck disable=SC2154
az storage container create --account-name "$TF_VAR_mgmt_storage_account_name" --name "$TF_VAR_terraform_state_container_name" --auth-mode login -o table
containers=("$TF_VAR_terraform_state_container_name" "tflogs")
max_retries=8

for container in "${containers[@]}"; do
for ((i=1; i<=max_retries; i++)); do
if az storage container create --account-name "$TF_VAR_mgmt_storage_account_name" --name "$container" --auth-mode login -o table; then
echo "Container '$container' created successfully."
break
else
sleep 10
fi
if [ $i -eq $max_retries ]; then
echo "ERROR: Failed to create container '$container' after $max_retries attempts."
exit 1
fi
done
done

# logs container
az storage container create --account-name "$TF_VAR_mgmt_storage_account_name" --name "tflogs" --auth-mode login -o table

echo -e "\n\e[34m»»» ✨ \e[96mTerraform init\e[0m..."
# shellcheck disable=SC2154
cat > bootstrap_backend.tf <<BOOTSTRAP_BACKEND
terraform {
backend "azurerm" {
Expand All @@ -75,12 +132,11 @@ terraform {
}
BOOTSTRAP_BACKEND


# Set up Terraform
echo -e "\n\e[34m»»» ✨ \e[96mTerraform init\e[0m..."
terraform init -input=false -backend=true -reconfigure

# Import the storage account & res group into state
# shellcheck disable=SC2154
if ! retry_with_backoff init_terraform; then
echo "ERROR: Timeout waiting for Terraform backend role assignments."
exit 1
fi
echo -e "\n\e[34m»»» 📤 \e[96mImporting resources to state\e[0m..."
if ! terraform state show azurerm_resource_group.mgmt > /dev/null; then
echo "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name"
Expand All @@ -91,5 +147,4 @@ if ! terraform state show azurerm_storage_account.state_storage > /dev/null; the
terraform import azurerm_storage_account.state_storage "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name"
fi
echo "State imported"

set +o nounset
Loading