-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathazure-az-rest-logon.ps1
336 lines (290 loc) · 9.01 KB
/
azure-az-rest-logon.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<#
.SYNOPSIS
script to authenticate for Azure Resource Manager REST API
requires az* modules, setting up an azure ad application spn, and a self signed cert
see azure-az-create-aad-application-spn.ps1 to setup the ad application and cert
output is in $global:token and is used by .\azure-az-rest-query.ps1 script
.LINK
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-az-rest-logon.ps1" -outFile "$pwd\azure-az-rest-logon.ps1";
.\azure-az-rest-logon.ps1
#>
[cmdletbinding()]
param(
[string]$certSubject,
[string]$thumbPrint,
[string]$certStore = "Cert:\CurrentUser\My",
[string]$clientId = "1950a258-227b-4e31-a9cf-717495945fc2",
[string]$clientSecret,
[string]$tenantId,
[switch]$force,
[ValidateSet('arm', 'asm', 'graph')]
[string]$logonType = "arm",
[string]$resource,
[string]$endpoint,
[switch]$interactive
)
$ErrorActionPreference = "continue"
$error.Clear()
$aadDisplayName = "azure-az-rest-logon--$($env:Computername)"
$cert = $null
$resourceArg = $resource
$endpointArg = $endpoint
function main ()
{
if (!(show-tokenInfo))
{
if (!$force)
{
Write-Warning "token has over 1/2 life left. use -force to force new token. returning"
return $false
}
else
{
Write-Host "refreshing token..." -ForegroundColor Yellow
}
}
if (!$tenantId)
{
if (!(check-az))
{
return $false
}
$tenantId = (Get-azContext).Tenant.Id
}
if (!$clientId)
{
if (!(check-az))
{
return $false
}
$clientIds = @((Get-azADApplication -DisplayNameStartWith $aadDisplayName).ApplicationId.Guid)
$clientIds
if ($clientIds.count -gt 1)
{
Write-Warning "multiple client ids / application ids found!!! trying first match only."
}
$clientId = $clientIds[0]
if (!$clientId)
{
Write-Warning "clientid (applicationid) info not provided. exiting"
return
}
Write-Warning "clientid (applicationid) info not provided. trying $($clientId)"
}
if (!$clientSecret)
{
foreach ($cert in Get-Certs)
{
if ((logon-rest $cert))
{
show-tokenInfo
break
}
}
}
else
{
logon-rest
show-tokenInfo
}
}
function acquire-token($resource, $endpoint)
{
if($resourceArg)
{
$resource = $resourceArg
}
if($endpointArg)
{
$endpoint = $endpointArg
}
if($interactive)
{
$result = authorize-user -resource $resource -endpoint $endpoint
}
$result
$error.clear()
$Body = @{
'resource' = $resource
'client_id' = $clientId
'grant_type' = 'client_credentials'
'client_secret' = $clientSecret
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept' = '*/*'}
Body = $Body
Method = 'Post'
URI = $endpoint + "/token"
}
write-host ($body | convertto-json)
write-host ($params | convertto-json)
write-host $clientSecret
$error.Clear()
return Invoke-RestMethod @params -Verbose -Debug
}
function authorize-user($resource, $endpoint)
{
$error.clear()
$uri = $endpoint + "/authorize?&tenant=$tenantId&response_type=code&client_id=$clientId&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient"
$params = @{
Method = 'Get'
URI = $uri
}
write-host ($params | convertto-json)
#write-host $clientSecret
$error.Clear()
return Invoke-WebRequest @params -Verbose -Debug
}
function check-az()
{
try
{
get-command connect-azaccount | Out-Null
}
catch [management.automation.commandNotFoundException]
{
if ((read-host "az not installed but is required for this script. is it ok to install?[y|n]") -imatch "y")
{
write-host "installing minimum required az modules..."
install-module az.accounts
install-module az.resources
import-module az.accounts
import-module az.resources
}
else
{
return $false
}
}
if (Get-azContext)
{
return $true
}
if ($thumbPrint -and $clientId)
{
connect-azaccount -ServicePrincipal `
-CertificateThumbprint $thumbPrint `
-ApplicationId $clientId
#-TenantId $tenantId
}
else
{
if (!(connect-azaccount))
{
return $false
}
}
return $true
}
function Get-Certs
{
if ($thumbPrint)
{
$certs = @(Get-ChildItem $certStore | Where-Object Thumbprint -ieq $thumbPrint)
}
elseif ($certSubject)
{
$certs = @(Get-ChildItem $certStore | Where-Object Subject -imatch $certSubject)
}
else
{
$certSubject = $aadDisplayName
Write-Warning "no cert info provided. trying $($certSubject)"
$certs = @(Get-ChildItem $certStore | Where-Object {$_.Subject -imatch $certSubject -and $_.NotAfter -gt (get-date)})
}
if ($certs.count -gt 1)
{
$certs
write-warning "multiple valid certs!"
}
return $certs
}
function logon-rest($cert)
{
$error.Clear()
if ($cert)
{
write-host ($cert | format-list *)
# works if using thumbprint only
$ClientSecret = [convert]::ToBase64String($cert.GetCertHash())
write-host "clientsecret set to: $($clientSecret)"
if ($cert.NotAfter -lt (get-date))
{
Write-Warning "cert is expired. run .\azure-az-create-aad-application-spn.ps1 to create new cert"
}
}
$tokenEndpoint = "https://login.windows.net/$($tenantId)/oauth2"
$armResource = "https://management.azure.com/"
$asmResource = "https://management.core.windows.net/"
$graphResource = "https://graph.microsoft.com/"
if ($logonType -eq "arm")
{
$token = acquire-token -resource $armResource -endpoint $tokenEndpoint
}
elseif ($logontype -eq "graph")
{
$tokenEndpoint = "https://login.microsoftonline.com/$($tenantId)/oauth2"
$token = acquire-token -resource $graphResource -endpoint $tokenEndpoint
}
else
{
$token = acquire-token -resource $asmResource -endpoint $tokenEndpoint
}
if (!$error)
{
$global:token = $token
Write-Output $global:token
$global:clientId = $clientId
write-host "$logonType access token output saved in `$global:token" -ForegroundColor Yellow
write-host "clientid / applicationid saved in `$global:clientId" -ForegroundColor Yellow
if ($ClientSecret)
{
$global:clientSecret = $clientSecret
write-host "client secret saved in `$global:clientSecret" -ForegroundColor Yellow
}
return $true
}
else
{
$global:token = $null
return $false
}
}
function show-tokenInfo()
{
if ($global:token)
{
$currentToken = $global:token
write-host "current token: $($currentToken)" -ForegroundColor Gray
$epochDate = (get-date "1/1/1970")
$epochTimeNow = [int64](([datetime]::UtcNow) - $epochDate).TotalSeconds
$isExpired = $epochTimeNow -gt $currentToken.expires_on
$outputColor = "Green"
if ($isExpired)
{
$outputColor = "Red"
$global:token = $null
}
$tokenLifetimeMinutes = [int]($currentToken.expires_in / 60)
$notBefore = $epochDate.AddSeconds($currentToken.not_before).ToLocalTime()
$notAfter = $epochDate.AddSeconds($currentToken.expires_on).ToLocalTime()
$notBeforeUtc = $epochDate.AddSeconds($currentToken.not_before).ToString("o")
$notAfterUtc = $epochDate.AddSeconds($currentToken.expires_on).ToString("o")
$minutesLeft = $([int](($currentToken.expires_on - $epochTimeNow) / 60))
write-host "current token info:" -ForegroundColor $outputColor
write-host "`tepoch time now:`t$epochTimeNow" -ForegroundColor $outputColor
write-host "`ttoken lifetime minutes:`t$tokenLifetimeMinutes" -ForegroundColor $outputColor
write-host "`tnot before:`t$notBeforeUtc `t($notBefore)" -ForegroundColor $outputColor
write-host "`tnot after:`t$notAfterUtc `t($notAfter)" -ForegroundColor $outputColor
write-host "`tminutes left before expire:`t$minutesLeft" -ForegroundColor $outputColor
write-host "`tis expired:`t$isExpired" -ForegroundColor $outputColor
if (!$isExpired -and (($tokenLifetimeMinutes / $minutesLeft) -lt 2) -and !$force)
{
return $false
}
}
return $true
}
main