Skip to content

Commit e0753da

Browse files
committed
Core key vault firewall should not be set to "Allow public access from all networks" #4250
1 parent 97debdc commit e0753da

12 files changed

+287
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ENHANCEMENTS:
3232
* Upgrade Python version from 3.8 to 3.12 ([#3949](https://github.com/microsoft/AzureTRE/issues/3949))Upgrade Python version from 3.8 to 3.12 (#3949)
3333
* Disable storage account key usage ([[#4227](https://github.com/microsoft/AzureTRE/issues/4227)])
3434
* Update Guacamole dependencies ([[#4232](https://github.com/microsoft/AzureTRE/issues/4232)])
35+
* Core key vault firewall should not be set to "Allow public access from all networks" ([#4250](https://github.com/microsoft/AzureTRE/issues/4250))
3536

3637
BUG FIXES:
3738
* Update KeyVault references in API to use the version so Terraform cascades the update ([#4112](https://github.com/microsoft/AzureTRE/pull/4112))

core/terraform/.terraform.lock.hcl

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/terraform/deploy.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ set -o pipefail
55
set -o nounset
66
# set -o xtrace
77

8+
# add trap to remove deployment network exceptions
9+
trap 'source "../../devops/scripts/remove_deployment_network_exceptions.sh"' EXIT
10+
11+
# now add deployment network exceptions
12+
source "../../devops/scripts/add_deployment_network_exceptions.sh"
13+
814
# This is where we can migrate any Terraform before we plan and apply
915
# For instance deprecated Terraform resources
1016
# shellcheck disable=SC1091

core/terraform/destroy.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ set -o pipefail
55
set -o nounset
66
# set -o xtrace
77

8+
# add trap to remove deployment network exceptions on script exit
9+
trap 'source "$script_dir/remove_deployment_network_exceptions.sh"' EXIT
10+
11+
# now add deployment network exceptions
12+
source "$script_dir/add_deployment_network_exceptions.sh"
13+
814
# These variables are loaded in for us
915
# shellcheck disable=SC2154
1016
../../devops/scripts/terraform_wrapper.sh -g "${TF_VAR_mgmt_resource_group_name}" \

core/terraform/keyvault.tf

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
11
resource "azurerm_key_vault" "kv" {
2-
name = "kv-${var.tre_id}"
2+
name = local.kv_name
33
tenant_id = data.azurerm_client_config.current.tenant_id
44
location = azurerm_resource_group.core.location
55
resource_group_name = azurerm_resource_group.core.name
66
sku_name = "standard"
77
enable_rbac_authorization = true
88
purge_protection_enabled = var.kv_purge_protection_enabled
9-
tags = local.tre_core_tags
9+
tags = merge(local.tre_core_tags, { "${local.tre_deployment_network_exception_tag}" = "true" })
1010

11-
lifecycle { ignore_changes = [access_policy, tags] }
11+
public_network_access_enabled = local.kv_public_network_access_enabled
12+
13+
network_acls {
14+
default_action = local.kv_network_default_action
15+
bypass = local.kv_network_bypass
16+
ip_rules = [ local.myip ] # exception for deployment IP, this is removed in remove_deployment_network_exceptions.sh
17+
}
18+
19+
lifecycle {
20+
ignore_changes = [access_policy, tags]
21+
}
22+
23+
# create provisioner required due to https://github.com/hashicorp/terraform-provider-azurerm/issues/18970
24+
#
25+
provisioner "local-exec" {
26+
when = create
27+
command = <<EOT
28+
az keyvault update --name ${local.kv_name} --public-network-access ${local.kv_public_network_access_enabled ? "Enabled" : "Disabled"} --default-action ${local.kv_network_default_action} --bypass ${local.kv_network_bypass} --output none
29+
az keyvault network-rule add --name ${local.kv_name} --ip-address ${local.myip} --output none
30+
EOT
31+
}
32+
}
33+
34+
# provisioner required due to ignore_changes = [tags] in azurerm_key_vault.kv
35+
#
36+
resource "null_resource" "add_deployment_tag" {
37+
triggers = {
38+
always_run = timestamp()
39+
}
40+
41+
provisioner "local-exec" {
42+
command = "az resource update --ids ${azurerm_key_vault.kv.id} --set 'tags.${local.tre_deployment_network_exception_tag}=\"true\"' --output none"
43+
}
44+
45+
depends_on = [azurerm_key_vault.kv]
1246
}
1347

1448
resource "azurerm_role_assignment" "keyvault_deployer_role" {

core/terraform/locals.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ locals {
4646

4747
cmk_name = "tre-encryption-${var.tre_id}"
4848
encryption_identity_name = "id-encryption-${var.tre_id}"
49+
50+
# used to tag resources (e.g. kv, sa) that require a IP exception for the deployment runner
51+
# adding to their network firewall during TRE deployment (and removing at the end of deployment)
52+
tre_deployment_network_exception_tag = "tre_deployment_network_exception"
53+
54+
# key vault variables
55+
kv_name = "kv-${var.tre_id}"
56+
kv_public_network_access_enabled = true
57+
kv_network_default_action = var.enable_local_debugging ? "Allow" : "Deny"
58+
kv_network_bypass = "AzureServices"
4959
}

core/terraform/scripts/letsencrypt.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ set -e
33

44
script_dir=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
55

6+
# add trap to remove deployment network exceptions on script exit
7+
trap 'source "$script_dir/../../../devops/scripts/remove_deployment_network_exceptions.sh"' EXIT
8+
9+
# now add deployment network exceptions
10+
source "$script_dir/../../../devops/scripts/add_deployment_network_exceptions.sh"
11+
12+
613
if [[ -z ${STORAGE_ACCOUNT} ]]; then
714
echo "STORAGE_ACCOUNT not set"
815
exit 1
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
3+
TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG="tre_deployment_network_exception"
4+
5+
function main() {
6+
7+
set -o errexit
8+
set -o pipefail
9+
10+
11+
# parse params/set up inputs
12+
#
13+
if [[ -z "$TRE_ID" ]]; then
14+
echo -e "Could not open deployment network exceptions: TRE_ID is not set\nExiting...\n"
15+
exit 1
16+
fi
17+
18+
local MY_IP="${PUBLIC_DEPLOYMENT_IP_ADDRESS:-}"
19+
20+
if [[ -z "$MY_IP" ]]; then
21+
MY_IP=$(curl -s "ipecho.net/plain"; echo)
22+
fi
23+
24+
local TRE_CORE_RG="rg-${TRE_ID}"
25+
26+
27+
# find resources that require network exceptions
28+
#
29+
echo -e "\nQuerying resources that require network exceptions adding for deployment..."
30+
31+
if [[ -z "$(az group list --query "[?name=='$TRE_CORE_RG']" --output tsv)" ]]; then
32+
echo -e " Core resource group $TRE_CORE_RG not found\n"
33+
return 0
34+
fi
35+
36+
local AZ_IDS
37+
AZ_IDS=$(az resource list --resource-group "$TRE_CORE_RG" --query "[?tags.${TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG}=='true'].id" --output tsv)
38+
39+
if [ -z "$AZ_IDS" ]; then
40+
echo -e " No resources found\n"
41+
return 0
42+
fi
43+
44+
45+
# add network exceptions
46+
#
47+
local AZ_ID
48+
for AZ_ID in $AZ_IDS; do
49+
50+
local RESOURCE_TYPE
51+
RESOURCE_TYPE=$(az resource show --ids "${AZ_ID}" --query 'type' --output tsv)
52+
53+
if [ "$RESOURCE_TYPE" == "Microsoft.KeyVault/vaults" ]; then
54+
add_keyvault_network_exception "$AZ_ID" "$MY_IP"
55+
fi
56+
57+
done
58+
59+
echo ""
60+
61+
}
62+
63+
function add_keyvault_network_exception() {
64+
local AZ_ID="$1"
65+
local MY_IP="$2"
66+
67+
local KV_NAME
68+
KV_NAME=$(basename "$AZ_ID")
69+
70+
echo " Adding keyvault deployment network exception for $KV_NAME"
71+
72+
az keyvault network-rule add --name "$KV_NAME" --ip-address "$MY_IP" --output none
73+
74+
local ATTEMPT=1
75+
local MAX_ATTEMPTS=10
76+
77+
while true; do
78+
79+
if KV_OUTPUT=$(az keyvault secret list --vault-name "$KV_NAME" --query '[].name' --output tsv 2>&1); then
80+
echo " Keyvault $KV_NAME is now accessible"
81+
break
82+
fi
83+
84+
if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
85+
echo -e "Could not add deployment network exception for $KV_NAME"
86+
echo -e "Unable to access keyvault $KV_NAME after $ATTEMPT/$MAX_ATTEMPTS.\n"
87+
echo -e "$KV_OUTPUT\n"
88+
89+
exit 1
90+
fi
91+
92+
echo " Unable to access keyvault $KV_NAME after $ATTEMPT/$MAX_ATTEMPTS. Waiting for network rules to take effect."
93+
sleep 5
94+
((ATTEMPT++))
95+
96+
done
97+
98+
}
99+
100+
main "$@"

devops/scripts/destroy_env_no_terraform.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ then
6666
no_wait_option="--no-wait"
6767
fi
6868

69+
script_dir=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
70+
71+
# add trap to remove deployment network exceptions on script exit
72+
trap 'source "$script_dir/remove_deployment_network_exceptions.sh"' EXIT
73+
74+
# now add deployment network exceptions
75+
source "$script_dir/add_deployment_network_exceptions.sh"
76+
6977
group_show_result=$(az group show --name "${core_tre_rg}" > /dev/null 2>&1; echo $?)
7078
if [[ "$group_show_result" != "0" ]]; then
7179
echo "Resource group ${core_tre_rg} not found - skipping destroy"

devops/scripts/key_vault_list.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ fi
77

88
echo "DEBUG: Check keyvault and secrets exist"
99

10+
script_dir=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
11+
12+
# add trap to remove deployment network exceptions on script exit
13+
trap 'source "$script_dir/remove_deployment_network_exceptions.sh"' EXIT
14+
15+
# now add deployment network exceptions
16+
source "$script_dir/add_deployment_network_exceptions.sh"
17+
1018
echo "az keyvault show"
1119
az keyvault show --name kv-${TRE_ID}
1220

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG="tre_deployment_network_exception"
4+
5+
function main() {
6+
7+
set -o errexit
8+
set -o pipefail
9+
10+
11+
# parse params/set up inputs
12+
#
13+
if [[ -z "$TRE_ID" ]]; then
14+
echo -e "Could not close deployment network exceptions: TRE_ID is not set\nExiting...\n"
15+
exit 1
16+
fi
17+
18+
local MY_IP="${PUBLIC_DEPLOYMENT_IP_ADDRESS:-}"
19+
20+
if [[ -z "$MY_IP" ]]; then
21+
MY_IP=$(curl -s "ipecho.net/plain"; echo)
22+
fi
23+
24+
local TRE_CORE_RG="rg-${TRE_ID}"
25+
26+
27+
# find resources that require network exceptions
28+
#
29+
echo -e "\nQuerying resources that require network exceptions removing for deployment..."
30+
31+
if [[ -z "$(az group list --query "[?name=='$TRE_CORE_RG']" --output tsv)" ]]; then
32+
echo -e " Core resource group $TRE_CORE_RG not found\n"
33+
return 0
34+
fi
35+
36+
local AZ_IDS
37+
AZ_IDS=$(az resource list --resource-group "$TRE_CORE_RG" --query "[?tags.${TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG}=='true'].id" --output tsv)
38+
39+
if [ -z "$AZ_IDS" ]; then
40+
echo -e " No resources found\n"
41+
return 0
42+
fi
43+
44+
45+
# remove network exceptions
46+
#
47+
local AZ_ID
48+
for AZ_ID in $AZ_IDS; do
49+
50+
local RESOURCE_TYPE
51+
RESOURCE_TYPE=$(az resource show --ids "${AZ_ID}" --query 'type' --output tsv)
52+
53+
if [ "$RESOURCE_TYPE" == "Microsoft.KeyVault/vaults" ]; then
54+
remove_keyvault_network_exception "$AZ_ID" "$MY_IP"
55+
fi
56+
57+
done
58+
59+
echo ""
60+
61+
}
62+
63+
function remove_keyvault_network_exception() {
64+
local AZ_ID="$1"
65+
local MY_IP="$2"
66+
67+
local KV_NAME
68+
KV_NAME=$(basename "$AZ_ID")
69+
70+
echo " Removing keyvault deployment network exception for $KV_NAME"
71+
72+
az keyvault network-rule remove --name "$KV_NAME" --ip-address "$MY_IP" --output none
73+
}
74+
75+
main "$@"

devops/scripts/set_contributor_sp_secrets.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ set -e
1616
#
1717

1818
echo -e "\n\e[34m»»» 🤖 \e[96mCreating (or updating) service principal ID and secret to Key Vault\e[0m..."
19+
20+
script_dir=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
21+
22+
# add trap to remove deployment network exceptions on script exit
23+
trap 'source "$script_dir/remove_deployment_network_exceptions.sh"' EXIT
24+
25+
# now add deployment network exceptions
26+
source "$script_dir/add_deployment_network_exceptions.sh"
27+
28+
1929
key_vault_name="kv-$TRE_ID"
2030
az account set --subscription $ARM_SUBSCRIPTION_ID
2131
az keyvault secret set --name deployment-processor-azure-client-id --vault-name $key_vault_name --value $RESOURCE_PROCESSOR_CLIENT_ID

0 commit comments

Comments
 (0)