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

create MLZ configuration storage accounts with lowercase names #222

Merged
merged 4 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/scripts/config/config_create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tf_dir=$(realpath "${3}")
tf_name=$(basename "${tf_dir}")

# generate names
. "${BASH_SOURCE%/*}/generate_names.sh" "${mlz_config}" "${tf_sub_id}" "${tf_name}"
. "${BASH_SOURCE%/*}/generate_names.sh" "${mlz_config}" "${tf_name}"

echo "INFO: creating resources for ${tf_name} Terraform state..."

Expand Down
119 changes: 65 additions & 54 deletions src/scripts/config/generate_names.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# shellcheck disable=SC1090,SC1091,SC2154
# SC1090: Can't follow non-constant source. Use a directive to specify location.
# SC1091: Not following. Shellcheck can't follow non-constant source.
# SC2154: "var is referenced but not assigned". These values come from an external file.
# shellcheck disable=SC1090,SC1091,SC2002,SC2154
#
# Generate MLZ resource names
# rules from: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules
Expand All @@ -19,67 +16,81 @@ error_log() {

usage() {
echo "generate_names.sh: Generate MLZ resource names"
error_log "usage: generate_names.sh <mlz config> <tf sub id> <tf name>"
error_log "usage: generate_names.sh <mlz config> <tf name>"
}

if [[ "$#" -lt 1 ]]; then
usage
exit 1
fi

create_resource_group_names() {
export mlz_config_tag="${mlz_prefix}-${env_name_alphanumeric}-config"
export mlz_rg_name="${mlz_config_tag:0:63}"
}

create_service_principal_name(){
local mlz_sp_name_full="${mlz_prefix}-${env_name_alphanumeric}-terraform-sp"
export mlz_sp_name="${mlz_sp_name_full:0:120}"
}

create_keyvault_names(){
local mlz_kv_name_full="${mlz_prefix}${env_name_alphanumeric}kv${randomish_identifier}"
export mlz_kv_name="${mlz_kv_name_full:0:24}"

export mlz_sp_kv_name="serviceprincipal-clientid"
export mlz_sp_kv_password="serviceprincipal-pwd"
export mlz_sp_obj_name="serviceprincipal-objectid"
export mlz_login_app_kv_name="login-app-clientid"
export mlz_login_app_kv_password="login-app-pwd"
}

create_container_registry_names(){
local mlz_acr_name_full="${mlz_prefix}${env_name_alphanumeric}acr${randomish_identifier}"
export mlz_acr_name="${mlz_acr_name_full:0:50}"

local mlz_fe_app_name_full="${mlz_prefix}-${env_name_alphanumeric}-frontend-app"
export mlz_fe_app_name="${mlz_fe_app_name_full:0:120}"

local mlz_instance_name_full="${mlz_prefix}${env_name_alphanumeric}feinstance${randomish_identifier}"
export mlz_instance_name="${mlz_instance_name_full:0:63}"

local mlz_dns_name_full="${mlz_prefix}${env_name_alphanumeric}dns${randomish_identifier}"
export mlz_dns_name="${mlz_dns_name_full:0:60}"
}

create_terraform_backend_names() {
if [[ $tf_name_raw != "notset" ]]; then
tf_name=$(echo "${tf_name_raw}" | tr -cd '[:alnum:]')

local tfstate_resource_group_name="${mlz_prefix}-${env_name_alphanumeric}-tfstate-${tf_name}"
export tf_rg_name="${tfstate_resource_group_name:0:63}"

local tfstate_storage_account_name="tfsa${tf_name}${env_name_alphanumeric}${randomish_identifier}"
valid_tfstate_storage_account_name=$(echo "${tfstate_storage_account_name:0:24}" | tr '[:upper:]' '[:lower:]')
export tf_sa_name=${valid_tfstate_storage_account_name}

export container_name="tfstate"
fi
}

##########
# main
##########

mlz_config=$(realpath "${1}")
tf_sub_id_raw=${2:-notset}
tf_name_raw=${3:-notset}
tf_name_raw=${2:-notset}

# source variables from MLZ config
. "${mlz_config}"

# remove hyphens for resource naming restrictions
# in the future, do more cleansing
mlz_sub_id_clean=$(echo ${mlz_config_subid} | tr -cd '[:alnum:]')
mlz_env_name_clean=$(echo ${mlz_env_name} | tr -cd '[:alnum:]')
mlz_prefix="mlz"

# Universal names
export container_name="tfstate"
env_name_alphanumeric=$(echo "${mlz_env_name}" | tr -cd '[:alnum:]')
randomish_identifier=${mlz_config_subid:0:8} # take the first octet in the subscription ID

# MLZ naming patterns
mlz_prefix="mlz"
mlz_suffix="${mlz_env_name_clean}${mlz_sub_id_clean}"

mlz_rg_name_full="${mlz_prefix}-config-${mlz_env_name_clean}"
mlz_sp_name_full="${mlz_prefix}-terraform-sp-${mlz_env_name_clean}"
mlz_kv_name_full="${mlz_prefix}kv${mlz_suffix}"
mlz_acr_name_full="${mlz_prefix}acr${mlz_suffix}"
mlz_fe_app_name_full="${mlz_prefix}-frontend-app-${mlz_env_name_clean}"
mlz_instance_name_full="${mlz_prefix}feinstance${mlz_suffix}"
mlz_dns_name_full="${mlz_prefix}dep${mlz_suffix}"

# Name MLZ config resources
export mlz_config_tag="${mlz_prefix}config${mlz_suffix}"
export mlz_rg_name="${mlz_rg_name_full:0:63}"
export mlz_sp_name="${mlz_sp_name_full:0:120}"
export mlz_sp_kv_name="serviceprincipal-clientid"
export mlz_sp_kv_password="serviceprincipal-pwd"
export mlz_sp_obj_name="serviceprincipal-objectid"
export mlz_login_app_kv_name="login-app-clientid"
export mlz_login_app_kv_password="login-app-pwd"
export mlz_kv_name="${mlz_kv_name_full:0:24}"
export mlz_acr_name="${mlz_acr_name_full:0:50}"
export mlz_fe_app_name="${mlz_fe_app_name_full:0:120}"
export mlz_instance_name="${mlz_instance_name_full:0:64}"
export mlz_dns_name="${mlz_dns_name_full:0:60}"

if [[ $tf_name_raw != "notset" ]]; then
# remove hyphens for resource naming restrictions
# in the future, do more cleansing
tf_sub_id_clean=$(echo ${tf_sub_id_raw} | tr -cd '[:alnum:]')
tf_name=$(echo ${tf_name_raw} | tr -cd '[:alnum:]')

# TF naming patterns
tf_rg_name_full="${mlz_prefix}-tfstate-${tf_name}-${mlz_env_name_clean}"
tf_sa_name_full="tfsa${tf_name}${mlz_suffix}"

# Name TF config resources
export tf_rg_name="${tf_rg_name_full:0:63}"
export tf_sa_name="${tf_sa_name_full:0:24}"
fi
create_resource_group_names
create_service_principal_name
create_keyvault_names
create_container_registry_names
create_terraform_backend_names
2 changes: 1 addition & 1 deletion src/scripts/config/generate_vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tf_dir=$(realpath "${4}")
. "${mlz_config}"

# generate names
. "${BASH_SOURCE%/*}/generate_names.sh" "${mlz_config}" "${tf_sub_id}" "${tf_name}"
. "${BASH_SOURCE%/*}/generate_names.sh" "${mlz_config}" "${tf_name}"

# generate a config.vars file
config_vars="${tf_dir}/config.vars"
Expand Down
6 changes: 2 additions & 4 deletions src/scripts/security-center/configure_asc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# shellcheck disable=SC1090,SC2154
# SC1090: Can't follow non-constant source. Use a directive to specify location.
# SC2154: "var is referenced but not assigned". These values come from an external file.
# shellcheck disable=SC1090,SC1091,SC2154
#
# Configures the landing zone subscriptions for Azure Security Center

Expand Down Expand Up @@ -50,7 +48,7 @@ do
if [[ ${ascAutoProv} == "Off" ]]; then

# generate names
. "${BASH_SOURCE%/*}"/generate_names.sh "${mlz_env_name}" "${sub}"
. "${BASH_SOURCE%/*}"/generate_names.sh "${mlz_env_name}"

# Create Resource Group for Log Analytics workspace
rg_exists="az group show \
Expand Down