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

Add bootstraps command variations #271

Merged
merged 1 commit into from
Jul 1, 2022
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
63 changes: 40 additions & 23 deletions scripts/lib/azure_ad.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,63 @@ create_federated_identity() {
success " - service principal created."

if [ "${sp}" = '' ]; then
information "Failed to create the app and sp. retrying"
sleep 5
create_federated_identity ${appName}
information "Failed to create the app and sp. Check your permissions."
exit 1
fi

app_object_id=$(echo ${app} | jq -r ".id")
client_id=$(echo ${sp} | jq -r ".appId")
object_id=$(echo ${sp} | jq -r ".id")
export app_object_id=$(echo ${app} | jq -r ".id")
sp_client_id=$(echo ${sp} | jq -r ".appId")
sp_object_id=$(echo ${sp} | jq -r ".id")

register_gitops_secret ${gitops_pipelines} "AZURE_CLIENT_ID" ${client_id}
register_gitops_secret ${gitops_pipelines} "AZURE_OBJECT_ID" ${object_id}
register_gitops_secret ${gitops_pipelines} "AZURE_TENANT_ID" ${tenant_id}
if [ ! -z ${gitops_pipelines} ]; then
register_gitops_secret ${gitops_pipelines} "AZURE_CLIENT_ID" ${sp_client_id}
register_gitops_secret ${gitops_pipelines} "AZURE_OBJECT_ID" ${app_object_id}
register_gitops_secret ${gitops_pipelines} "AZURE_TENANT_ID" ${tenant_id}
fi

else
success " - application already created."
success " - service principal already created."
fi

app=$(az ad app list --filter "displayname eq '${appName}'" -o json --only-show-errors) && debug "app: ${app}"
sp=$(az ad sp list --filter "DisplayName eq '${appName}'" --only-show-errors) && debug "sp: ${sp}"
export app_object_id=$(echo ${app} | jq -r ".[0].id")
sp_client_id=$(echo ${app} | jq -r ".[0].appId")
sp_object_id=$(echo ${sp} | jq -r ".[0].id")

app=$(az ad app list --filter "displayname eq '${appName}'" -o json --only-show-errors)
sp=$(az ad sp list --filter "DisplayName eq '${appName}'" --only-show-errors)
export app_object_id=$(echo ${app} | jq -r ".[0].id")
create_gitops_federated_credentials ${gitops_pipelines} ${appName}

az role assignment create \
--role "Owner" \
--assignee-object-id $(echo ${sp} | jq -r ".[0].id") \
--assignee-principal-type ServicePrincipal \
--scope /subscriptions/${TF_VAR_tfstate_subscription_id} \
--only-show-errors
if [ ! -z ${gitops_pipelines} ]; then
register_gitops_secret ${gitops_pipelines} "AZURE_CLIENT_ID" ${sp_client_id}
register_gitops_secret ${gitops_pipelines} "AZURE_OBJECT_ID" ${app_object_id}
register_gitops_secret ${gitops_pipelines} "AZURE_TENANT_ID" ${tenant_id}
fi

fi

if [ ! -z ${gitops_pipelines} ]; then
create_gitops_federated_credentials ${gitops_pipelines} ${appName}

scope="/subscriptions/${sub_management:=$(az account show --query id -o tsv)}"
information "Granting Reader role to ${appName} on ${scope}"
az role assignment create \
--role "Reader" \
--assignee-object-id ${sp_object_id} \
--assignee-principal-type ServicePrincipal \
--scope ${scope} \
--only-show-errors
fi
}


function create_federated_credentials {

cred=$(az rest --uri "https://graph.microsoft.com/beta/applications/${app_object_id}/federatedIdentityCredentials" --query "value[?name=='${1}'].{name:name}[0]" -o tsv)
debug "az rest --uri \"https://graph.microsoft.com/beta/applications/${app_object_id}/federatedIdentityCredentials\" --query \"value[?name==\'${1}\'].{name:name}[0]\" -o json"

cred=$(az rest --uri "https://graph.microsoft.com/beta/applications/${app_object_id}/federatedIdentityCredentials" --query "value[?name=='${1}'].{name:name}[0]" -o json | jq -r .name)
debug "value is '${cred}'"

if [ -z "${cred}" ]; then
info "Adding federated credential to ${app_object_id} with 'name':'${1}','subject':'${2}','description':'${3}'"
if [ "${cred}" = '' ]; then
information "Adding federated credential to ${app_object_id} with 'name':'${1}','subject':'${2}','description':'${3}'"

az rest --method POST \
--uri "https://graph.microsoft.com/beta/applications/${app_object_id}/federatedIdentityCredentials" \
Expand Down
9 changes: 6 additions & 3 deletions scripts/lib/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ bootstrap() {
create_federated_identity ${aad_app_name}
fi

process_gitops_agent_pool ${gitops_agent_pool_type}
if [ ! -z ${gitops_pipelines} ]; then
process_gitops_agent_pool ${gitops_agent_pool_type}
fi

if [ ! -z ${bootstrap_script} ]; then
register_rover_context
${bootstrap_script} "topology_file=${caf_ignite_playbook}" "GITOPS_SERVER_URL=${GITOPS_SERVER_URL}" "RUNNER_NUMBERS=${gitops_number_runners}" "AGENT_TOKEN=${AGENT_TOKEN}" "gitops_agent=${gitops_agent_pool_type}" "ROVER_AGENT_DOCKER_IMAGE=${ROVER_AGENT_DOCKER_IMAGE}" "AZURE_OBJECT_ID=${app_object_id}" "subscription_deployment_mode=${subscription_deployment_mode}" "sub_management=${sub_management}" "sub_connectivity=${sub_connectivity}" "sub_identity=${sub_identity}" "sub_security=${sub_security}"
${bootstrap_script} "topology_file=${caf_ignite_playbook}" "GITOPS_SERVER_URL=${GITOPS_SERVER_URL}" "RUNNER_NUMBERS=${gitops_number_runners}" "AGENT_TOKEN=${AGENT_TOKEN}" "gitops_agent=${gitops_agent_pool_type}" "ROVER_AGENT_DOCKER_IMAGE=${ROVER_AGENT_DOCKER_IMAGE}" "AZURE_OBJECT_ID=${app_object_id}" "subscription_deployment_mode=${subscription_deployment_mode}" "sub_management=${sub_management}" "sub_connectivity=${sub_connectivity}" "sub_identity=${sub_identity}" "sub_security=${sub_security}" "gitops_pipelines=${gitops_pipelines}"
fi

information "Done."
}

assert_sessions() {
Expand Down Expand Up @@ -69,7 +72,7 @@ assert_gitops_session() {


process_gitops_agent_pool() {
information "@call process_gitops_agent_pool"
information "@call process_gitops_agent_pool for ${1}"

case "${1}" in
"github")
Expand Down
1 change: 0 additions & 1 deletion scripts/rover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export TF_IN_AUTOMATION="true" #Overriden in logger if log-severity is passed in
export TF_VAR_tf_cloud_organization=${TF_CLOUD_ORGANIZATION}
export TF_VAR_tf_cloud_hostname=${TF_CLOUD_HOSTNAME:="app.terraform.io"}
export REMOTE_credential_path_json=${REMOTE_credential_path_json:="$(echo ~)/.terraform.d/credentials.tfrc.json"}
export gitops_pipelines="github"
export gitops_terraform_backend_type=${TF_VAR_backend_type:="azurerm"}
export gitops_agent_pool_type=${GITOPS_AGENT_POOL_TYPE:="github"}
export gitops_agent_pool_name=${GITOPS_AGENT_POOL_NAME}
Expand Down