Greetings to my fellow Technology Advocates and Specialists.
In this Session, I will demonstrate How to Rotate Storage Account Keys (Primary & Secondary) and Store it in Key Vault Using Azure DevOps.
I had the Privilege to talk on this topic in TWO Azure Communities:-
NAME OF THE AZURE COMMUNITY
TYPE OF SPEAKER SESSION
Journey to the Cloud 9.0
Virtual
Festive Tech Calendar 2022
Virtual
LIVE RECORDED SESSION:-
LIVE DEMO was Recorded as part of my Presentation in JOURNEY TO THE CLOUD 9.0 Forum/Platform
Duration of My Demo = 55 Mins 42 Secs
LIVE DEMO was Recorded as part of my Presentation in FESTIVE TECH CALENDAR 2022 Forum/Platform
Duration of My Demo = 1 Hour 05 Mins 08 Secs
THIS IS HOW IT LOOKS:-
AUTOMATION OBJECTIVE:-
Validate If Resource Group Containing Key Vault Exists. If No Resource Group Found, Pipeline will FAIL.
Validate If Storage Account Exists inside the Specified Resource Group. If No Storage Account Found, Pipeline will FAIL.
Validate If Key Vault Exists inside the Specified Resource Group. If No Key Vault Found, Pipeline will FAIL.
If All of the above validation is SUCCESSFUL, Depending upon, which Key User wants to rotate (Primary or Secondary), Pipeline will then Rotate the Storage Account Key and Store it in the Key Vault.
IMPORTANT NOTE:-
The YAML Pipeline is tested on WINDOWS BUILD AGENT Only!!!
REQUIREMENTS:-
Azure Subscription.
Azure DevOps Organisation and Project.
Service Principal with Required RBAC ( Contributor) applied on Subscription or Resource Group(s).
Azure Resource Manager Service Connection in Azure DevOps.
Please change the values of the variables accordingly.
The entire YAML pipeline is build using Runtime Parameters and Variables. No Values are Hardcoded.
PART #3:-
This is a 3 Stage Pipeline:-
STAGE #1 - VALIDATE_RG_STORAGE_ACCOUNT_AND_KV:-
In this Stage, Pipeline will validate Resource Group, Storage Account and Key Vault. If any one of the Azure Resource is Not Available, Pipeline will FAIL and the other 2 Stages gets SKIPPED.
- stage: VALIDATE_RG_STORAGE_ACCOUNT_AND_KV
jobs:
- job: VALIDATE_RG_STORAGE_ACCOUNT_AND_KV
displayName: VALIDATE RG STORAGE ACCOUNT & KV
steps:
- task: AzureCLI@2
displayName: SET AZURE ACCOUNT
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az --version
az account set --subscription ${{ parameters.SUBSCRIPTIONID }}
az account show
- task: AzureCLI@2
displayName: VALIDATE RG STORAGE ACCOUNT & RG
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$i = az group exists -n ${{ parameters.RGNAME }}
if ($i -eq "true") {
echo "#####################################################"
echo "Resource Group ${{ parameters.RGNAME }} exists!!!"
echo "#####################################################"
$j = az storage account check-name --name ${{ parameters.STORAGEACCOUNTNAME }} --query "reason" --out tsv
if ($j -eq "AlreadyExists") {
echo "###################################################################"
echo "Storage Account ${{ parameters.STORAGEACCOUNTNAME }} exists!!!"
echo "###################################################################"
$k = az keyvault list --resource-group ${{ parameters.RGNAME }} --query [].name -o tsv
if ($k -eq "${{ parameters.KVNAME }}") {
echo "###################################################################"
echo "Key Vault ${{ parameters.KVNAME }} exists!!!"
echo "###################################################################"
}
else {
echo "###################################################################################################"
echo "Key Vault ${{ parameters.KVNAME }} DOES NOT EXISTS in Resource Group ${{ parameters.RGNAME }}!!!"
echo "###################################################################################################"
exit 1
}
}
else {
echo "#######################################################################################################################"
echo "Storage Account ${{ parameters.STORAGEACCOUNTNAME }} DOES NOT EXISTS in Resource Group ${{ parameters.RGNAME }}!!!"
echo "#######################################################################################################################"
exit 1
}
}
else {
echo "#############################################################"
echo "Resource Group ${{ parameters.RGNAME }} DOES NOT EXISTS!!!"
echo "#############################################################"
exit 1
}
STAGE #2 - RENEW_STORAGE_ACCOUNT_PRIMARY_KEY:-
In this Stage, Pipeline has Conditions in Place.
Condition #1: The Previous Stage has to be Successful.
Condition #2: The User should Select option "Primary".
Top comments (0)