forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure-rm-create-aad-application-spn.ps1
314 lines (266 loc) · 12.1 KB
/
azure-rm-create-aad-application-spn.ps1
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<#
creates new azurermadapplication for use with logging in to azurerm using password or cert
to enable script execution, you may need to Set-ExecutionPolicy Bypass -Force
# can be used with scripts for example
# connect-azurermaccount -ServicePrincipal -CertificateThumbprint $cert.Thumbprint -ApplicationId $app.ApplicationId -TenantId $tenantId
# requires free AAD base subscription
# https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authenticate-service-principal#provide-credentials-through-automated-powershell-script
example command:
iwr https://tinyurl.com/create-azure-client-id | iex
example command to save and/or pass arguments:
(new-object net.webclient).downloadfile("https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-rm-create-aad-application-spn.ps1","$(get-location)\azure-rm-create-aad-application-spn.ps1");
then:
.\azure-rm-create-aad-application-spn.ps1 -aadDisplayName azure-rm-rest-logon -logontype certthumb
or
.\azure-rm-create-aad-application-spn.ps1
# 190209
#>
param(
[pscredential]$credentials,
[string]$aadDisplayName = "azure-rm-rest-logon/$($env:Computername)",
[string]$certStore = "cert:\CurrentUser\My",
[string]$uri,
[switch]$list,
[string]$pfxPath = "$($env:temp)\$($aadDisplayName).pfx",
[string]$logonType = 'certthumb'
)
# ----------------------------------------------------------------------------------------------------------------
function main()
{
$retryCount = 10
$cert = $null
$keyCredential = $null
$thumbprint = $null
$ClientSecret = $null
$keyvalue = $Null
$error.Clear()
$app = $null
# authenticate
try
{
get-command connect-azurermaccount | Out-Null
}
catch [management.automation.commandNotFoundException]
{
if ((read-host "azurerm not installed but is required for this script. is it ok to install?[y|n]") -imatch "y")
{
write-host "installing minimum required azurerm modules..."
install-module azurerm.profile
install-module azurerm.resources
import-module azurerm.profile
import-module azurerm.resources
}
else
{
return 1
}
}
if (!(Get-AzureRmResourceGroup))
{
connect-azurermaccount
if (!(Get-AzureRmResourceGroup))
{
Write-Warning "unable to authenticate to azurerm. returning..."
return 1
}
}
if (!$uri)
{
$uri = "https://$($aadDisplayName)"
}
$tenantId = (Get-AzureRmContext).Tenant.Id
if ((Get-AzureRmADApplication -DisplayNameStartWith $aadDisplayName -ErrorAction SilentlyContinue))
{
$app = Get-AzureRmADApplication -DisplayNameStartWith $aadDisplayName
if ((read-host "AAD application exists: $($aadDisplayName). Do you want to delete?[y|n]") -imatch "y")
{
remove-AzureRmADApplication -objectId $app.objectId -Force
$app = $null
$id = Get-AzureRmADServicePrincipal -SearchString $aadDisplayName
if (@($id).Count -eq 1)
{
Remove-AzureRmADServicePrincipal -ObjectId $id
}
}
}
$certs = @(Get-ChildItem $certStore | Where-Object {$_.Subject -imatch "$($aadDisplayName)" -and $_.NotAfter -gt (get-date)})
if($certs.Count -ge 1)
{
$count = 1
foreach($certItem in $certs)
{
"$($count). $($certItem.Subject) $($certItem.NotAfter) $($certItem.Thumbprint)"
$count++
}
if(($result = read-host "enter line number of existing cert to use or 0 to create new. normally an existing cert from list should be used.") -gt 0)
{
$cert = $certs[$result]
}
}
if (!$list)
{
if ($logontype -ieq 'cert')
{
Write-Warning "this does not work for rest authentication, but does work for ps auth"
if(!$cert)
{
$cert = New-SelfSignedCertificate -CertStoreLocation $certStore `
-Subject "CN=$($aadDisplayName)" `
-KeyExportPolicy Exportable `
-Provider "Microsoft Enhanced RSA and AES Cryptographic Provider"
}
if (!$credentials)
{
$credentials = (get-credential)
}
$pwd = ConvertTo-SecureString -String $credentials.Password -Force -AsPlainText
if([io.file]::Exists($pfxPath))
{
[io.file]::Delete($pfxPath)
}
Export-PfxCertificate -cert "cert:\currentuser\my\$($cert.thumbprint)" -FilePath $pfxPath -Password $pwd
$cert509 = New-Object System.Security.Cryptography.X509Certificates.X509Certificate($pfxPath, $pwd)
$thumbprint = $cert509.thumbprint
$keyValue = [System.Convert]::ToBase64String($cert509.GetCertHash())
write-host "New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -CertValue $keyValue -EndDate $cert.NotAfter -StartDate $cert.NotBefore"
if($oldAdApp = Get-AzureRmADApplication -DisplayNameStartWith $aadDisplayName)
{
remove-AzureRmADApplication -ObjectId $oldAdApp.objectId
}
$DebugPreference = "Continue"
if(!$app)
{
write-host "New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -CertValue $keyValue -EndDate $($cert.NotAfter) -StartDate $($cert.NotBefore) -verbose"
$app = New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -CertValue ($cert.GetRawCertData()) -EndDate ($cert.NotAfter) -StartDate ($cert.NotBefore) -verbose #-Debug
}
$app
#$DebugPreference = "SilentlyContinue"
$app = New-AzureRmADAppCredential -applicationId ($app.ApplicationId) -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -KeyCredentials $KeyCredential -Verbose
}
elseif ($logontype -ieq 'certthumb')
{
if(!$cert)
{
$cert = New-SelfSignedCertificate -CertStoreLocation $certStore `
-Subject "$($aadDisplayName)" `
-KeyExportPolicy Exportable `
-Provider "Microsoft Enhanced RSA and AES Cryptographic Provider"
}
$keyValue = [System.Convert]::ToBase64String($cert.GetCertHash())
$thumbprint = $cert.Thumbprint
$ClientSecret = [System.Convert]::ToBase64String($cert.GetCertHash())
$pwd = ConvertTo-SecureString -String $ClientSecret -Force -AsPlainText
write-host "New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -Password $pwd -EndDate $($cert.NotAfter)"
if(!$app)
{
$app = New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -Password $pwd -EndDate ($cert.NotAfter)
}
}
elseif ($logontype -ieq 'key')
{
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$ClientSecret = [System.Convert]::ToBase64String($bytes)
$pwd = ConvertTo-SecureString -String $ClientSecret -Force -AsPlainText
$endDate = [System.DateTime]::Now.AddYears(2)
if(!$app)
{
$app = New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $URI -IdentifierUris $URI -Password $pwd -EndDate $endDate
}
write-host "client secret: $($ClientSecret)" -ForegroundColor Yellow
}
else
{
write-warning "credentials need to be psadcredentials to work"
if (!$credentials)
{
write-warning "no credentials, exiting"
exit 1
}
# to use password
if(!$app)
{
$app = New-AzureRmADApplication -DisplayName $aadDisplayName -HomePage $uri -IdentifierUris $uri -PasswordCredentials $credentials
}
}
$app
$count = 0
while ($count -lt $retryCount)
{
#todo check if principal exists
$error.clear()
start-sleep -Seconds 10
write-host "attempt $count New-AzureRmADServicePrincipal -ApplicationId $($app.ApplicationId)" # -DisplayName $aadDisplayName"
New-AzureRmADServicePrincipal -ApplicationId ($app.ApplicationId) #-DisplayName $aadDisplayName
if(!$error)
{
break
}
$count++
}
if($error)
{
write-error "unable to add new principal, exiting"
return 1
}
$count = 0
while ($count -lt $retryCount)
{
try
{
write-host "$($count) -- sleeping 10 seconds while new service principal is created."
start-sleep -Seconds 10
write-host "attempt $($count) to add role assignments read and contribute"
New-AzureRmRoleAssignment -RoleDefinitionName Reader -ServicePrincipalName ($app.ApplicationId)
if (!$error)
{
write-host "role assignments read added" -ForegroundColor green
write-host "to remove 'read' permissions, run: Remove-AzureRmRoleAssignment -RoleDefinitionName Reader -ServicePrincipalName $($app.ApplicationId)"
}
if (!$error)
{
New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName ($app.ApplicationId)
write-host "role assignments contribute added" -ForegroundColor green
write-host "to remove 'contributor' permissions, run: Remove-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $($app.ApplicationId)"
break
}
}
catch
{
if($error -imatch "401")
{
write-error ($error | out-string)
write-warning "unable to add role assignments read and or contribute due to permissions."
break
}
}
write-verbose ($error | out-string)
$error.Clear()
$count++
}
if ($logontype -ieq 'cert' -or $logontype -ieq 'certthumb')
{
write-host "for use in script: connect-azurermaccount -ServicePrincipal -CertificateThumbprint $($cert.Thumbprint) -ApplicationId $($app.ApplicationId) -TenantId $($tenantId)"
write-host "certificate thumbprint: $($cert.Thumbprint)"
}
}
$app
write-host "application id: $($app.ApplicationId)" -ForegroundColor Cyan
write-host "tenant id: $($tenantId)" -ForegroundColor Cyan
write-host "application identifier Uri: $($uri)" -ForegroundColor Cyan
write-host "keyValue: $($keyValue)" -ForegroundColor Cyan
write-host "clientsecret: $($clientsecret)" -ForegroundColor Cyan
write-host "clientsecret BASE64:$([convert]::ToBase64String([text.encoding]::Unicode.GetBytes($clientsecret)))"
write-host "thumbprint: $($thumbprint)" -ForegroundColor Cyan
write-host "pfx path: $($pfxPath)" -ForegroundColor Cyan
$global:thumbprint = $thumbprint
$global:applicationId = $app.Applicationid
$global:tenantId = $tenantId
$global:clientSecret = $ClientSecret
$global:keyValue = $keyValue
write-host "clientid / applicationid saved in `$global:applicationId" -ForegroundColor Yellow
write-host "clientsecret / base64 thumb saved in `$global:clientSecret" -ForegroundColor Yellow
}
# ----------------------------------------------------------------------------------------------------------------
main