Skip to content

Commit 1cde51c

Browse files
author
Ashis Kar
committed
Refactor network exception script to use TF_VAR variables for resource group and storage account names, and improve storage accessibility check logic.
1 parent 1f41328 commit 1cde51c

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

devops/scripts/mgmtstorage_add_network_exception.sh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ function mgmtstorage_remove_network_exception() {
7878
}
7979

8080
function get_resource_group_name() {
81-
if [[ -z "${MGMT_RESOURCE_GROUP_NAME:-}" ]]; then
82-
echo -e "Error: MGMT_RESOURCE_GROUP_NAME is not set\nExiting...\n" >&2
81+
if [[ -z "${TF_VAR_mgmt_resource_group_name:-}" ]]; then
82+
echo -e "Error: TF_VAR_mgmt_resource_group_name is not set\nExiting...\n" >&2
8383
exit 1
8484
fi
85-
echo "$MGMT_RESOURCE_GROUP_NAME"
85+
echo "$TF_VAR_mgmt_resource_group_name"
8686
}
8787

8888
function get_storage_account_name() {
89-
if [[ -z "${MGMT_STORAGE_ACCOUNT_NAME:-}" ]]; then
90-
echo -e "Error: MGMT_STORAGE_ACCOUNT_NAME is not set\nExiting...\n" >&2
89+
if [[ -z "${TF_VAR_mgmt_storage_account_name:-}" ]]; then
90+
echo -e "Error: TF_VAR_mgmt_storage_account_name is not set\nExiting...\n" >&2
9191
exit 1
9292
fi
93-
echo "$MGMT_STORAGE_ACCOUNT_NAME"
93+
echo "$TF_VAR_mgmt_storage_account_name"
9494
}
9595

9696
function get_my_ip() {
@@ -99,7 +99,7 @@ function get_my_ip() {
9999
else
100100
local MY_IP
101101
MY_IP=$(curl -s "https://ipecho.net/plain") || { echo "Error: Failed to fetch IP address" >&2; exit 1; }
102-
102+
103103
if [[ -z "$MY_IP" ]]; then
104104
echo "Error: Could not determine IP address." >&2
105105
exit 1
@@ -123,10 +123,18 @@ function is_ip_in_network_rule() {
123123
COUNT=$(az storage account network-rule list --resource-group "$RESOURCE_GROUP" --account-name "$SA_NAME" --query "length(ipRules[?ipAddressOrRange=='$MY_IP'])" --output tsv)
124124

125125
if [[ "$COUNT" -gt 0 ]]; then
126-
# Step 2: Try accessing storage to confirm access
127-
if az storage container list --account-name "$SA_NAME" --auth-mode login --output none 2>/dev/null; then
128-
return 0 # Success: IP is in rules AND access is confirmed
126+
# Step 2: Verify storage accessibility by listing containers...
127+
containers=$(az storage container list --account-name "$SA_NAME" --auth-mode login --query "[].name" --output tsv)
128+
if [[ -z "$containers" ]]; then
129+
# No containers found, assume success.
130+
return 0
129131
fi
132+
for container in $containers; do
133+
if ! az storage blob list --container-name "$container" --account-name "$SA_NAME" --auth-mode login --output none; then
134+
return 1 # Failure if blob listing fails in any container
135+
fi
136+
done
137+
return 0 # Success if blob list works for all containers
130138
fi
131139

132140
return 1 # Either rule not added or access is still restricted

0 commit comments

Comments
 (0)