-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcreateServicePrincipal.ps1
More file actions
177 lines (152 loc) · 9.45 KB
/
Copy pathcreateServicePrincipal.ps1
File metadata and controls
177 lines (152 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<#
.DESCRIPTION
This script is ran by the servicePrincipalRunbook and it creates an app registration in Azure Active Directory.
This app registration is the WVDServicePrincipal that is used to setup the service connection between the DevOps project and the Azure subscription.
#>
#Initializing variables from automation account
$SubscriptionId = Get-AutomationVariable -Name 'subscriptionid'
$ResourceGroupName = Get-AutomationVariable -Name 'ResourceGroupName'
$fileURI = Get-AutomationVariable -Name 'fileURI'
$AutomationAccountName = Get-AutomationVariable -Name 'AccountName'
$AppName = Get-AutomationVariable -Name 'AppName'
# Download files required for this script from github ARMRunbookScripts/static folder
$FileNames = "msft-wvd-saas-api.zip,msft-wvd-saas-web.zip,AzureModules.zip"
$SplitFilenames = $FileNames.split(",")
foreach($Filename in $SplitFilenames){
Invoke-WebRequest -Uri "$fileURI/ARMRunbookScripts/static/$Filename" -OutFile "C:\$Filename"
}
# Install required Az modules and AzureAD
Expand-Archive "C:\AzureModules.zip" -DestinationPath 'C:\Modules\Global' -ErrorAction SilentlyContinue
Import-Module Az.Accounts -Global
Import-Module Az.Resources -Global
Import-Module Az.Websites -Global
Import-Module Az.Automation -Global
Import-Module AzureAD -Global
Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope Process -Force -Confirm:$false
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine -Force -Confirm:$false
Get-ExecutionPolicy -List
#The name of the Automation Credential Asset this runbook will use to authenticate to Azure.
$AzCredentialsAsset = 'AzureCredentials'
#Authenticate Azure
#Get the credential with the above name from the Automation Asset store
$AzCredentials = Get-AutomationPSCredential -Name $AzCredentialsAsset
$AzCredentials.password.MakeReadOnly()
$username = $AzCredentials.username
Connect-AzAccount -Environment 'AzureCloud' -Credential $AzCredentials
Connect-AzureAD -AzureEnvironmentName 'AzureCloud' -Credential $AzCredentials
Select-AzSubscription -SubscriptionId $SubscriptionId
# Get the context
$context = Get-AzContext
if ($context -eq $null)
{
Write-Error "Please authenticate to Azure & Azure AD using Login-AzAccount and Connect-AzureAD cmdlets and then run this script"
exit
}
# Get the Role Assignment of the authenticated user
$RoleAssignment = Get-AzRoleAssignment -SignInName $context.Account
# Validate whether the authenticated user having the Owner or Contributor role
if ($RoleAssignment.RoleDefinitionName -eq "Owner" -or $RoleAssignment.RoleDefinitionName -eq "Contributor")
{
#$requiredAccessName=$ResourceURL.Split("/")[3]
$redirectURL = "https://" + "$AppName" + ".azurewebsites.net" + "/"
# Check whether the AD Application exist/ not
$azAdApplication = Get-AzADApplication -DisplayName $AppName -ErrorAction SilentlyContinue
if ($azAdApplication -ne $null)
{
$appId = $azAdApplication.ApplicationId
Write-Output "An AAD Application already exists with AppName $AppName(Application Id: $appId). Will attempt to handle deployment with this existing application." -Verbose
}
else {
try
{
Write-Output "Creating new application..."
# Create a new AD Application with provided AppName
$azAdApplication = New-AzureADApplication -DisplayName $AppName -PublicClient $false -AvailableToOtherTenants $false -ReplyUrls $redirectURL
}
catch
{
Write-Error "You must call the Connect-AzureAD cmdlet before calling any other cmdlets"
exit
}
}
$azAdApplication = Get-AzADApplication -DisplayName $AppName -ErrorAction SilentlyContinue
# Create a Client Secret
$StartDate = Get-Date
$EndDate = $StartDate.AddYears(280)
$Guid = New-Guid
$PasswordCredential = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordCredential
$PasswordCredential.StartDate = $StartDate
$PasswordCredential.EndDate = $EndDate
$PasswordCredential.KeyId = $Guid
$PasswordCredential.Value = ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($Guid)))) + "="
$ClientSecret = $PasswordCredential.Value
Write-Output "Creating a new Application in AAD" -Verbose
# Create an app credential to the Application
$secureClientSecret = ConvertTo-SecureString -String $ClientSecret -AsPlainText -Force
New-AzADAppCredential -ObjectId $azAdApplication.ObjectId -Password $secureClientSecret -StartDate $StartDate -EndDate $EndDate
# Get the applicationId
$applicationId = $azAdApplication.ApplicationId
Write-Output "Azure AAD Application creation completed successfully with AppName $AppName (Application Id is: $applicationId)" -Verbose
# Create new Service Principal
Write-Output "Creating a new Service Principal" -Verbose
$ServicePrincipal = Get-AzADServicePrincipal -ApplicationId $applicationId -ErrorAction SilentlyContinue
if ($ServicePrincipal -ne $null)
{
Write-Output "A service principal already exists for this AAD application. Will attempt to handle deployment with this existing service principal." -Verbose
}
else {
try
{
$ServicePrincipal = New-AzADServicePrincipal -ApplicationId $applicationId
}
catch
{
Write-Error "You must call the Connect-AzureAD cmdlet before calling any other cmdlets"
exit
}
}
# Get the Service Principal
Get-AzADServicePrincipal -ApplicationId $applicationId
Write-Output "Service Principal creation completed successfully for AppName $AppName (Application Id is: $applicationId)" -Verbose
$ownerId = (Get-AzADUser -UserPrincipalName $username).Id
Add-AzureADApplicationOwner -ObjectId $azAdApplication.ObjectId -RefObjectId $ownerId
Write-Output "Azure admin successfully assigned owner role on the service principal" -Verbose
#Collecting AzureService Management Api permission and set to client app registration
$AzureServMgmtApi = Get-AzADServicePrincipal -ApplicationId "797f4846-ba00-4fd7-ba43-dac1f8f63013"
$AzureAdServMgmtApi = Get-AzureADServicePrincipal -ObjectId $AzureServMgmtApi.Id
$AzureServMgmtApiResouceAcessObject = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
$AzureServMgmtApiResouceAcessObject.ResourceAppId = $AzureAdServMgmtApi.AppId
foreach ($SerVMgmtAPipermission in $AzureAdServMgmtApi.Oauth2Permissions) {
$AzureServMgmtApiResouceAcessObject.ResourceAccess += New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess" -ArgumentList $SerVMgmtAPipermission.Id,"Scope"
}
# Set Microsoft Graph API permission to Client App Registration
$MsftGraphApi = Get-AzADServicePrincipal -ApplicationId "00000003-0000-0000-c000-000000000000"
$AzureGraphApiPrincipal = Get-AzureADServicePrincipal -ObjectId $MsftGraphApi.Id
$AzureGraphApiAccessObject = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
$AzureGraphApiAccessObject.ResourceAppId = $AzureGraphApiPrincipal.AppId
$permission = $AzureGraphApiPrincipal.Oauth2Permissions | Where-Object { $_.Value -eq "User.Read" }
$AzureGraphApiAccessObject.ResourceAccess = New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess" -ArgumentList $permission.Id,"Scope"
$permission2 = $AzureGraphApiPrincipal.Oauth2Permissions | Where-Object { $_.Value -eq "User.ReadWrite" }
$AzureGraphApiAccessObject.ResourceAccess += New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess" -ArgumentList $permission2.Id,"Scope"
$permission3 = $AzureGraphApiPrincipal.Oauth2Permissions | Where-Object { $_.Value -eq "Group.ReadWrite.all" }
$AzureGraphApiAccessObject.ResourceAccess += New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess" -ArgumentList $permission3.Id,"Scope"
$permission4 = $AzureGraphApiPrincipal.AppRoles | Where-Object { $_.Value -eq "Application.ReadWrite.OwnedBy" }
$AzureGraphApiAccessObject.ResourceAccess += New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess" -ArgumentList $permission4.Id,"Role"
# Add the WVD API,Log Analytics API and Microsoft Graph API permissions to the ADApplication
Set-AzureADApplication -ObjectId $azAdApplication.ObjectId -RequiredResourceAccess $AzureServMgmtApiResouceAcessObject,$AzureGraphApiAccessObject -ErrorAction Stop
#Set-AzureADApplication -ObjectId $azAdApplication.ObjectId -Oauth2Permissions $AzureAdOauth2Object -Oauth2RequirePostResponse $false -Oauth2AllowImplicitFlow $true
# Create credential for the service principal and store in the automation account
$global:servicePrincipalCredentials = New-Object System.Management.Automation.PSCredential ($applicationId, $secureClientSecret)
New-AzAutomationCredential -AutomationAccountName $AutomationAccountName -Name "ServicePrincipalCred" -Value $servicePrincipalCredentials -ResourceGroupName $ResourceGroupName
# Create new automation variables with the newly created service principal details in them for use in the devops setup script
New-AzAutomationVariable -AutomationAccountName $AutomationAccountName -Name "PrincipalId" -Encrypted $False -Value $applicationId -ResourceGroupName $ResourceGroupName
New-AzAutomationVariable -AutomationAccountName $AutomationAccountName -Name "Secret" -Encrypted $False -Value $secureClientSecret -ResourceGroupName $ResourceGroupName
New-AzAutomationVariable -AutomationAccountName $AutomationAccountName -Name "ObjectId" -Encrypted $False -Value $azAdApplication.ObjectId -ResourceGroupName $ResourceGroupName
# Assign service principal contributor and user acess administrator roles on subscription level
New-AzRoleAssignment -RoleDefinitionName "Contributor" -ApplicationId $applicationId
New-AzRoleAssignment -RoleDefinitionName "User Access Administrator" -ApplicationId $applicationId
}
else
{
Write-Error "Authenticated user should have the Owner/Contributor permissions"
}