Allow authentication of resource generator script via service principal (#2622)

* Allow authentication of resource generator script via service principal

* Re-use the given service principal in case of parametric authentication

* Updated variable names

* Renamed parameter and variable from AzureTenant to AzureTenantId

* Added Azure DevOps pipeline to build an agent

* Added link to azure pipeline to instructions

* Fixed typo in link

* Removed unnecessary / double brackets

* Untangled credential instantiation to make it more readable

* Removed example yaml file

* Removed unnecessary variable assignment
This commit is contained in:
Daniel
2021-03-11 09:21:35 +13:00
committed by GitHub
parent 971ee7d2a7
commit 160f5bedb0

View File

@@ -83,6 +83,15 @@ Function GenerateResourcesAndImage {
.PARAMETER GithubFeedToken .PARAMETER GithubFeedToken
GitHub PAT to download tool packages from GitHub Package Registry GitHub PAT to download tool packages from GitHub Package Registry
.PARAMETER AzureClientId
Client id needs to be provided for optional authentication via service principal. Example: "11111111-1111-1111-1111-111111111111"
.PARAMETER AzureClientSecret
Client secret needs to be provided for optional authentication via service principal. Example: "11111111-1111-1111-1111-111111111111"
.PARAMETER AzureTenantId
Tenant needs to be provided for optional authentication via service principal. Example: "11111111-1111-1111-1111-111111111111"
.EXAMPLE .EXAMPLE
GenerateResourcesAndImage -SubscriptionId {YourSubscriptionId} -ResourceGroupName "shsamytest1" -ImageGenerationRepositoryRoot "C:\virtual-environments" -ImageType Ubuntu1604 -AzureLocation "East US" GenerateResourcesAndImage -SubscriptionId {YourSubscriptionId} -ResourceGroupName "shsamytest1" -ImageGenerationRepositoryRoot "C:\virtual-environments" -ImageType Ubuntu1604 -AzureLocation "East US"
#> #>
@@ -102,10 +111,16 @@ Function GenerateResourcesAndImage {
[Parameter(Mandatory = $False)] [Parameter(Mandatory = $False)]
[string] $GithubFeedToken, [string] $GithubFeedToken,
[Parameter(Mandatory = $False)] [Parameter(Mandatory = $False)]
[string] $AzureClientId,
[Parameter(Mandatory = $False)]
[string] $AzureClientSecret,
[Parameter(Mandatory = $False)]
[string] $AzureTenantId,
[Parameter(Mandatory = $False)]
[Switch] $Force [Switch] $Force
) )
if (([string]::IsNullOrEmpty($GithubFeedToken))) if ([string]::IsNullOrEmpty($GithubFeedToken))
{ {
Write-Error "'-GithubFeedToken' parameter is not specified. You have to specify valid GitHub PAT to download tool packages from GitHub Package Registry" Write-Error "'-GithubFeedToken' parameter is not specified. You have to specify valid GitHub PAT to download tool packages from GitHub Package Registry"
exit 1 exit 1
@@ -115,7 +130,14 @@ Function GenerateResourcesAndImage {
$ServicePrincipalClientSecret = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper(); $ServicePrincipalClientSecret = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper();
$InstallPassword = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper(); $InstallPassword = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper();
if ([string]::IsNullOrEmpty($AzureClientId))
{
Connect-AzAccount Connect-AzAccount
} else {
$AzSecureSecret = ConvertTo-SecureString $AzureClientSecret -AsPlainText -Force
$AzureAppCred = New-Object System.Management.Automation.PSCredential($AzureClientId, $AzSecureSecret)
Connect-AzAccount -ServicePrincipal -Credential $AzureAppCred -Tenant $AzureTenantId
}
Set-AzContext -SubscriptionId $SubscriptionId Set-AzContext -SubscriptionId $SubscriptionId
$alreadyExists = $true; $alreadyExists = $true;
@@ -171,6 +193,8 @@ Function GenerateResourcesAndImage {
New-AzStorageAccount -ResourceGroupName $ResourceGroupName -AccountName $storageAccountName -Location $AzureLocation -SkuName "Standard_LRS" New-AzStorageAccount -ResourceGroupName $ResourceGroupName -AccountName $storageAccountName -Location $AzureLocation -SkuName "Standard_LRS"
if ([string]::IsNullOrEmpty($AzureClientId)) {
# Interactive authentication: A service principal is created during runtime.
$spDisplayName = [System.GUID]::NewGuid().ToString().ToUpper() $spDisplayName = [System.GUID]::NewGuid().ToString().ToUpper()
$credentialProperties = @{ StartDate=Get-Date; EndDate=Get-Date -Year 2024; Password=$ServicePrincipalClientSecret } $credentialProperties = @{ StartDate=Get-Date; EndDate=Get-Date -Year 2024; Password=$ServicePrincipalClientSecret }
$credentials = New-Object -TypeName Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property $credentialProperties $credentials = New-Object -TypeName Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property $credentialProperties
@@ -178,7 +202,6 @@ Function GenerateResourcesAndImage {
$spAppId = $sp.ApplicationId $spAppId = $sp.ApplicationId
$spClientId = $sp.ApplicationId $spClientId = $sp.ApplicationId
$spObjectId = $sp.Id
Start-Sleep -Seconds $SecondsToWaitForServicePrincipalSetup Start-Sleep -Seconds $SecondsToWaitForServicePrincipalSetup
New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $spAppId New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $spAppId
@@ -186,6 +209,15 @@ Function GenerateResourcesAndImage {
$sub = Get-AzSubscription -SubscriptionId $SubscriptionId $sub = Get-AzSubscription -SubscriptionId $SubscriptionId
$tenantId = $sub.TenantId $tenantId = $sub.TenantId
# "", "Note this variable-setting script for running Packer with these Azure resources in the future:", "==============================================================================================", "`$spClientId = `"$spClientId`"", "`$ServicePrincipalClientSecret = `"$ServicePrincipalClientSecret`"", "`$SubscriptionId = `"$SubscriptionId`"", "`$tenantId = `"$tenantId`"", "`$spObjectId = `"$spObjectId`"", "`$AzureLocation = `"$AzureLocation`"", "`$ResourceGroupName = `"$ResourceGroupName`"", "`$storageAccountName = `"$storageAccountName`"", "`$install_password = `"$install_password`"", "" # "", "Note this variable-setting script for running Packer with these Azure resources in the future:", "==============================================================================================", "`$spClientId = `"$spClientId`"", "`$ServicePrincipalClientSecret = `"$ServicePrincipalClientSecret`"", "`$SubscriptionId = `"$SubscriptionId`"", "`$tenantId = `"$tenantId`"", "`$spObjectId = `"$spObjectId`"", "`$AzureLocation = `"$AzureLocation`"", "`$ResourceGroupName = `"$ResourceGroupName`"", "`$storageAccountName = `"$storageAccountName`"", "`$install_password = `"$install_password`"", ""
} else {
# Parametrized Authentication via given service principal: The service principal with the data provided via the command line
# is used for all authentication purposes.
$spAppId = $AzureClientId
$spClientId = $AzureClientId
$credentials = $AzureAppCred
$ServicePrincipalClientSecret = $AzureClientSecret
$tenantId = $AzureTenantId
}
Get-LatestCommit -ErrorAction SilentlyContinue Get-LatestCommit -ErrorAction SilentlyContinue
@@ -199,7 +231,6 @@ Function GenerateResourcesAndImage {
-var "client_secret=$($ServicePrincipalClientSecret)" ` -var "client_secret=$($ServicePrincipalClientSecret)" `
-var "subscription_id=$($SubscriptionId)" ` -var "subscription_id=$($SubscriptionId)" `
-var "tenant_id=$($tenantId)" ` -var "tenant_id=$($tenantId)" `
-var "object_id=$($spObjectId)" `
-var "location=$($AzureLocation)" ` -var "location=$($AzureLocation)" `
-var "resource_group=$($ResourceGroupName)" ` -var "resource_group=$($ResourceGroupName)" `
-var "storage_account=$($storageAccountName)" ` -var "storage_account=$($storageAccountName)" `