Skip to content

Instantly share code, notes, and snippets.

@rahulkj
Last active February 22, 2019 16:42
Show Gist options
  • Save rahulkj/359818460af86450031c02f039fe4fb9 to your computer and use it in GitHub Desktop.
Save rahulkj/359818460af86450031c02f039fe4fb9 to your computer and use it in GitHub Desktop.
Azure create service principal
#!/bin/bash -e
SERVICE_PRINCIPAL_NAME=http://rj-BOSHAzureCPI
read -s -p "Enter Password for service principal user $SERVICE_PRINCIPAL_NAME: " CLIENT_SECRET
AZ_ACCOUNT=$(az login)
SUBSCRIPTION_ID=$(echo "$AZ_ACCOUNT" | jq -r '.[] | .id')
TENANT_ID=$(echo "$AZ_ACCOUNT" | jq -r '.[] | .tenantId')
AZURE_NATIVE_APP=$(az ad app create --display-name "Service Principal for BOSH" \
--password "$CLIENT_SECRET" --homepage "$SERVICE_PRINCIPAL_NAME" \
--identifier-uris "$SERVICE_PRINCIPAL_NAME")
APP_ID=$(echo "$AZURE_NATIVE_APP" | jq -r '.appId')
SP=$(az ad sp create --id $APP_ID)
sleep 30
SP_ROLE_ASSIGNMENT=$(az role assignment create --assignee "$SERVICE_PRINCIPAL_NAME" \
--role "Contributor" --scope /subscriptions/$SUBSCRIPTION_ID)
SP_LOGIN=$(az login --username $SERVICE_PRINCIPAL_NAME \
--password "$CLIENT_SECRET" \
--service-principal --tenant $TENANT_ID)
az provider register --namespace Microsoft.Storage
az provider register --namespace Microsoft.Network
az provider register --namespace Microsoft.Compute
echo "Subscription ID is : $SUBSCRIPTION_ID"
echo "Tenant ID is: $TENANT_ID"
echo "Service Principal Name is: $SERVICE_PRINCIPAL_NAME"
echo "Service Principal Client Sercret is: you are kidding me :-)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment