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

Core key vault firewall should not be set to "Allow public access from all networks" #4260

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e0753da
Core key vault firewall should not be set to "Allow public access fro…
jonnyry Jan 7, 2025
e33f235
Linting
jonnyry Jan 7, 2025
c9af674
Update core version
jonnyry Jan 7, 2025
9f1ef68
Linting
jonnyry Jan 7, 2025
1fefc9f
Linting
jonnyry Jan 7, 2025
8af920d
Linting
jonnyry Jan 7, 2025
8c24844
Simplified: Remove use of azure tags, make specific to key vault
jonnyry Jan 8, 2025
357891f
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Jan 8, 2025
f6ed85a
Update core version
jonnyry Jan 8, 2025
dcb0b8f
Linting
jonnyry Jan 8, 2025
135be76
Update to deal with scenario where TRE_ID is not available
jonnyry Jan 8, 2025
4a1b8b8
Remove unused null provisioner from .terraform.lock.hcl
jonnyry Jan 8, 2025
d7ce398
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Jan 15, 2025
e9833c4
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Jan 19, 2025
2fa2a95
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 5, 2025
56d91d1
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 6, 2025
6251320
Remove unused scripts: key_vault_list.sh & set_contributor_sp_secret…
jonnyry Feb 6, 2025
de97183
Refactor as per @marrobi
jonnyry Feb 6, 2025
bd786c4
Update letsencrypt.sh
jonnyry Feb 6, 2025
cc2551e
Update kv_add_network_exception.sh
jonnyry Feb 6, 2025
417c195
Update kv_add_network_exception.sh
jonnyry Feb 6, 2025
dee1c14
Update kv_add_network_exception.sh
jonnyry Feb 6, 2025
67c2b2a
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 7, 2025
73049a1
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 9, 2025
6dc3fe0
Update CHANGELOG.md
jonnyry Feb 9, 2025
df52d62
Simplify
jonnyry Feb 9, 2025
3d8e6c1
Update letsencrypt.sh
jonnyry Feb 9, 2025
f480b32
Update kv_add_network_exception.sh
jonnyry Feb 9, 2025
1aaffb2
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 9, 2025
97db895
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 11, 2025
b94443a
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 12, 2025
d65eaf4
Merge branch 'main' into jr/upstream-main/93-close-keyvault-firewall
jonnyry Feb 13, 2025
f2c8c96
Update version.txt
jonnyry Feb 13, 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 @@ -32,6 +32,7 @@ ENHANCEMENTS:
* 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)
* Disable storage account key usage ([[#4227](https://github.com/microsoft/AzureTRE/issues/4227)])
* Update Guacamole dependencies ([[#4232](https://github.com/microsoft/AzureTRE/issues/4232)])
* Core key vault firewall should not be set to "Allow public access from all networks" ([#4250](https://github.com/microsoft/AzureTRE/issues/4250))

BUG FIXES:
* Update KeyVault references in API to use the version so Terraform cascades the update ([#4112](https://github.com/microsoft/AzureTRE/pull/4112))
Expand Down
19 changes: 19 additions & 0 deletions core/terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions core/terraform/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ set -o pipefail
set -o nounset
# set -o xtrace

# add trap to remove deployment network exceptions
# shellcheck disable=SC1091
trap 'source "../../devops/scripts/remove_deployment_network_exceptions.sh"' EXIT

# now add deployment network exceptions
# shellcheck disable=SC1091
source "../../devops/scripts/add_deployment_network_exceptions.sh"

# This is where we can migrate any Terraform before we plan and apply
# For instance deprecated Terraform resources
# shellcheck disable=SC1091
Expand Down
8 changes: 8 additions & 0 deletions core/terraform/destroy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ set -o pipefail
set -o nounset
# set -o xtrace

# add trap to remove deployment network exceptions
# shellcheck disable=SC1091
trap 'source "../../devops/scripts/remove_deployment_network_exceptions.sh"' EXIT

# now add deployment network exceptions
# shellcheck disable=SC1091
source "../../devops/scripts/add_deployment_network_exceptions.sh"

# These variables are loaded in for us
# shellcheck disable=SC2154
../../devops/scripts/terraform_wrapper.sh -g "${TF_VAR_mgmt_resource_group_name}" \
Expand Down
40 changes: 37 additions & 3 deletions core/terraform/keyvault.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
resource "azurerm_key_vault" "kv" {
name = "kv-${var.tre_id}"
name = local.kv_name
tenant_id = data.azurerm_client_config.current.tenant_id
location = azurerm_resource_group.core.location
resource_group_name = azurerm_resource_group.core.name
sku_name = "standard"
enable_rbac_authorization = true
purge_protection_enabled = var.kv_purge_protection_enabled
tags = local.tre_core_tags
tags = merge(local.tre_core_tags, { (local.tre_deployment_network_exception_tag) = "true" })

lifecycle { ignore_changes = [access_policy, tags] }
public_network_access_enabled = local.kv_public_network_access_enabled

network_acls {
default_action = local.kv_network_default_action
bypass = local.kv_network_bypass
ip_rules = [local.myip] # exception for deployment IP, this is removed in remove_deployment_network_exceptions.sh
}

lifecycle {
ignore_changes = [access_policy, tags]
}

# create provisioner required due to https://github.com/hashicorp/terraform-provider-azurerm/issues/18970
#
provisioner "local-exec" {
when = create
command = <<EOT
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
az keyvault network-rule add --name ${local.kv_name} --ip-address ${local.myip} --output none
EOT
}
}

# provisioner required due to ignore_changes = [tags] in azurerm_key_vault.kv
#
resource "null_resource" "add_deployment_tag" {
triggers = {
always_run = timestamp()
}

provisioner "local-exec" {
command = "az resource update --ids ${azurerm_key_vault.kv.id} --set 'tags.${local.tre_deployment_network_exception_tag}=\"true\"' --output none"
}

depends_on = [azurerm_key_vault.kv]
}

resource "azurerm_role_assignment" "keyvault_deployer_role" {
Expand Down
10 changes: 10 additions & 0 deletions core/terraform/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ locals {

cmk_name = "tre-encryption-${var.tre_id}"
encryption_identity_name = "id-encryption-${var.tre_id}"

# used to tag resources (e.g. kv, sa) that require a IP exception for the deployment runner
# adding to their network firewall during TRE deployment (and removing at the end of deployment)
tre_deployment_network_exception_tag = "tre_deployment_network_exception"

# key vault variables
kv_name = "kv-${var.tre_id}"
kv_public_network_access_enabled = true
kv_network_default_action = var.enable_local_debugging ? "Allow" : "Deny"
kv_network_bypass = "AzureServices"
}
4 changes: 4 additions & 0 deletions core/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ terraform {
source = "Azure/azapi"
version = "~> 1.15.0"
}
null = {
source = "hashicorp/null"
version = "3.2.3"
}
}

backend "azurerm" {}
Expand Down
9 changes: 9 additions & 0 deletions core/terraform/scripts/letsencrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ set -e

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

# add trap to remove deployment network exceptions on script exit
# shellcheck disable=SC1091
trap 'source "$script_dir/../../../devops/scripts/remove_deployment_network_exceptions.sh"' EXIT

# now add deployment network exceptions
# shellcheck disable=SC1091
source "$script_dir/../../../devops/scripts/add_deployment_network_exceptions.sh"


if [[ -z ${STORAGE_ACCOUNT} ]]; then
echo "STORAGE_ACCOUNT not set"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion core/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.16"
__version__ = "0.11.17"
100 changes: 100 additions & 0 deletions devops/scripts/add_deployment_network_exceptions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG="tre_deployment_network_exception"

function main() {

set -o errexit
set -o pipefail


# parse params/set up inputs
#
if [[ -z "$TRE_ID" ]]; then
echo -e "Could not open deployment network exceptions: TRE_ID is not set\nExiting...\n"
exit 1
fi

local MY_IP="${PUBLIC_DEPLOYMENT_IP_ADDRESS:-}"

if [[ -z "$MY_IP" ]]; then
MY_IP=$(curl -s "ipecho.net/plain"; echo)
fi

local TRE_CORE_RG="rg-${TRE_ID}"


# find resources that require network exceptions
#
echo -e "\nQuerying resources that require network exceptions adding for deployment..."

if [[ -z "$(az group list --query "[?name=='$TRE_CORE_RG']" --output tsv)" ]]; then
echo -e " Core resource group $TRE_CORE_RG not found\n"
return 0
fi

local AZ_IDS
AZ_IDS=$(az resource list --resource-group "$TRE_CORE_RG" --query "[?tags.${TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG}=='true'].id" --output tsv)

if [ -z "$AZ_IDS" ]; then
echo -e " No resources found\n"
return 0
fi


# add network exceptions
#
local AZ_ID
for AZ_ID in $AZ_IDS; do

local RESOURCE_TYPE
RESOURCE_TYPE=$(az resource show --ids "${AZ_ID}" --query 'type' --output tsv)

if [ "$RESOURCE_TYPE" == "Microsoft.KeyVault/vaults" ]; then
add_keyvault_network_exception "$AZ_ID" "$MY_IP"
fi

done

echo ""

}

function add_keyvault_network_exception() {
local AZ_ID="$1"
local MY_IP="$2"

local KV_NAME
KV_NAME=$(basename "$AZ_ID")

echo " Adding keyvault deployment network exception for $KV_NAME"

az keyvault network-rule add --name "$KV_NAME" --ip-address "$MY_IP" --output none

local ATTEMPT=1
local MAX_ATTEMPTS=10

while true; do

if KV_OUTPUT=$(az keyvault secret list --vault-name "$KV_NAME" --query '[].name' --output tsv 2>&1); then
echo " Keyvault $KV_NAME is now accessible"
break
fi

if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
echo -e "Could not add deployment network exception for $KV_NAME"
echo -e "Unable to access keyvault $KV_NAME after $ATTEMPT/$MAX_ATTEMPTS.\n"
echo -e "$KV_OUTPUT\n"

exit 1
fi

echo " Unable to access keyvault $KV_NAME after $ATTEMPT/$MAX_ATTEMPTS. Waiting for network rules to take effect."
sleep 5
((ATTEMPT++))

done

}

main "$@"
10 changes: 10 additions & 0 deletions devops/scripts/destroy_env_no_terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ then
no_wait_option="--no-wait"
fi

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

# add trap to remove deployment network exceptions on script exit
# shellcheck disable=SC1091
trap 'source "$script_dir/remove_deployment_network_exceptions.sh"' EXIT

# now add deployment network exceptions
# shellcheck disable=SC1091
source "$script_dir/add_deployment_network_exceptions.sh"

group_show_result=$(az group show --name "${core_tre_rg}" > /dev/null 2>&1; echo $?)
if [[ "$group_show_result" != "0" ]]; then
echo "Resource group ${core_tre_rg} not found - skipping destroy"
Expand Down
16 changes: 13 additions & 3 deletions devops/scripts/key_vault_list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ fi

echo "DEBUG: Check keyvault and secrets exist"

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

# add trap to remove deployment network exceptions on script exit
# shellcheck disable=SC1091
trap 'source "$script_dir/remove_deployment_network_exceptions.sh"' EXIT

# now add deployment network exceptions
# shellcheck disable=SC1091
source "$script_dir/add_deployment_network_exceptions.sh"

echo "az keyvault show"
az keyvault show --name kv-${TRE_ID}
az keyvault show --name "kv-${TRE_ID}"

echo "az keyvault secret list"
az keyvault secret list --vault-name kv-${TRE_ID}
az keyvault secret list --vault-name "kv-${TRE_ID}"

echo "az keyvault secret list-deleted"
az keyvault secret list-deleted --vault-name kv-${TRE_ID}
az keyvault secret list-deleted --vault-name "kv-${TRE_ID}"
75 changes: 75 additions & 0 deletions devops/scripts/remove_deployment_network_exceptions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG="tre_deployment_network_exception"

function main() {

set -o errexit
set -o pipefail


# parse params/set up inputs
#
if [[ -z "$TRE_ID" ]]; then
echo -e "Could not close deployment network exceptions: TRE_ID is not set\nExiting...\n"
exit 1
fi

local MY_IP="${PUBLIC_DEPLOYMENT_IP_ADDRESS:-}"

if [[ -z "$MY_IP" ]]; then
MY_IP=$(curl -s "ipecho.net/plain"; echo)
fi

local TRE_CORE_RG="rg-${TRE_ID}"


# find resources that require network exceptions
#
echo -e "\nQuerying resources that require network exceptions removing for deployment..."

if [[ -z "$(az group list --query "[?name=='$TRE_CORE_RG']" --output tsv)" ]]; then
echo -e " Core resource group $TRE_CORE_RG not found\n"
return 0
fi

local AZ_IDS
AZ_IDS=$(az resource list --resource-group "$TRE_CORE_RG" --query "[?tags.${TRE_DEPLOYMENT_NETWORK_EXCEPTION_TAG}=='true'].id" --output tsv)

if [ -z "$AZ_IDS" ]; then
echo -e " No resources found\n"
return 0
fi


# remove network exceptions
#
local AZ_ID
for AZ_ID in $AZ_IDS; do

local RESOURCE_TYPE
RESOURCE_TYPE=$(az resource show --ids "${AZ_ID}" --query 'type' --output tsv)

if [ "$RESOURCE_TYPE" == "Microsoft.KeyVault/vaults" ]; then
remove_keyvault_network_exception "$AZ_ID" "$MY_IP"
fi

done

echo ""

}

function remove_keyvault_network_exception() {
local AZ_ID="$1"
local MY_IP="$2"

local KV_NAME
KV_NAME=$(basename "$AZ_ID")

echo " Removing keyvault deployment network exception for $KV_NAME"

az keyvault network-rule remove --name "$KV_NAME" --ip-address "$MY_IP" --output none
}

main "$@"
Loading
Loading