-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUpdate-StorageAccountApplicationManifest.ps1
More file actions
272 lines (230 loc) · 10.7 KB
/
Update-StorageAccountApplicationManifest.ps1
File metadata and controls
272 lines (230 loc) · 10.7 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
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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$AppDisplayNamePrefix,
[Parameter(Mandatory = $true)]
[string]$ClientId,
[Parameter(Mandatory = $true)]
[string]$GraphEndpoint,
[Parameter(Mandatory = $false)]
[string]$PrivateEndpoint = "false",
[Parameter(Mandatory = $false)]
[string]$EnableCloudGroupSids = "false"
)
$ErrorActionPreference = "Stop"
# Convert strings to boolean
$PrivateLink = [System.Convert]::ToBoolean($PrivateEndpoint)
$UpdateTag = [System.Convert]::ToBoolean($EnableCloudGroupSids)
# Setup Logging
$logPath = "C:\Windows\Logs"
$logFile = Join-Path -Path $logPath -ChildPath "Update-StorageAccountApplicationManifest-$(Get-Date -Format 'yyyyMMdd-HHmm').log"
Start-Transcript -Path $logFile -Force
# Helper function to invoke Graph API with retry logic for DoD endpoints
function Invoke-GraphApiWithRetry {
param (
[Parameter(Mandatory = $true)]
[string] $GraphEndpoint,
[Parameter(Mandatory = $true)]
[string] $AccessToken,
[Parameter(Mandatory = $true)]
[ValidateSet('Get', 'Post', 'Patch', 'Delete')]
[string] $Method,
[Parameter(Mandatory = $true)]
[string] $Uri,
[Parameter()]
[string] $Body,
[Parameter()]
[hashtable] $Headers = @{}
)
# Ensure GraphEndpoint doesn't have trailing slash
$graphBase = if ($GraphEndpoint[-1] -eq '/') {
$GraphEndpoint.Substring(0, $GraphEndpoint.Length - 1)
} else {
$GraphEndpoint
}
# Setup headers
$requestHeaders = $Headers.Clone()
$requestHeaders['Authorization'] = "Bearer $AccessToken"
if (-not $requestHeaders.ContainsKey('Content-Type')) {
$requestHeaders['Content-Type'] = 'application/json'
}
# List of endpoints to try
$endpointsToTry = @($graphBase)
# If we're using GCCH endpoint, also try DoD
if ($graphBase -eq 'https://graph.microsoft.us') {
$endpointsToTry += 'https://dod-graph.microsoft.us'
}
$lastError = $null
foreach ($endpoint in $endpointsToTry) {
try {
$attemptUri = "$endpoint$Uri"
$params = @{
Uri = $attemptUri
Method = $Method
Headers = $requestHeaders
}
if ($Body -and $Method -in @('Post', 'Patch')) {
$params['Body'] = $Body
}
$result = Invoke-RestMethod @params
# If we succeeded with a different endpoint than the one provided, log it
if ($endpoint -ne $graphBase) {
Write-Warning "Graph API call succeeded with alternate endpoint: $endpoint"
Write-Warning "Consider updating GraphEndpoint parameter to: $endpoint"
}
return $result
}
catch {
$lastError = $_
$statusCode = $null
if ($_.Exception.Response) {
$statusCode = [int]$_.Exception.Response.StatusCode
}
# Try to extract detailed error from Graph API response
$errorDetails = ""
try {
if ($_.Exception.Response) {
$responseStream = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($responseStream)
$responseBody = $reader.ReadToEnd()
$reader.Close()
$responseStream.Close()
$errorObj = $responseBody | ConvertFrom-Json
if ($errorObj.error) {
$errorDetails = "`n Error Code: $($errorObj.error.code)`n Error Message: $($errorObj.error.message)"
if ($errorObj.error.details) {
$errorDetails += "`n Details: $($errorObj.error.details | ConvertTo-Json -Compress)"
}
}
}
}
catch {
# If we can't parse error details, just continue
}
# Retry on authentication/authorization errors (401, 403) or if endpoint not found (404 on base endpoint)
if ($statusCode -in @(401, 403, 404) -and $endpoint -ne $endpointsToTry[-1]) {
Write-Warning "Graph API call to $endpoint failed with status $statusCode$errorDetails. Trying alternate endpoint..."
continue
}
else {
# Don't retry - either not an auth error or we've tried all endpoints
Write-Error "Graph API call failed with status $statusCode : $($_.Exception.Message)$errorDetails"
throw
}
}
}
# If we get here, all endpoints failed
Write-Error "All Graph API endpoints failed. Last error: $($lastError.Exception.Message)"
throw $lastError
}
try {
Write-Output "============================================"
Write-Output "PHASE 1: Update Storage Account Application Manifest"
Write-Output "This updates tags and identifier URIs for privatelink FQDN support"
Write-Output "============================================"
# Get Graph Access Token using Managed Identity
$GraphUri = if ($GraphEndpoint[-1] -eq '/') { $GraphEndpoint.Substring(0, $GraphEndpoint.Length - 1) } else { $GraphEndpoint }
$TokenUri = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=$GraphUri&client_id=$ClientId"
Write-Output "Requesting access token from IMDS..."
$Response = Invoke-RestMethod -Headers @{ Metadata = "true" } -Uri $TokenUri
If ($Response) {
Write-Output "Successfully obtained access token"
$AccessToken = $Response.access_token
}
else {
throw "Failed to obtain access token from IMDS."
}
# Search for the application by DisplayName
$searchUri = "/v1.0/applications?" + '$filter=' + "startswith(displayName, '$AppDisplayNamePrefix')"
Write-Output "Searching for applications with prefix: $AppDisplayNamePrefix"
try {
$searchHeaders = @{ "ConsistencyLevel" = "eventual" }
$searchResp = Invoke-GraphApiWithRetry -GraphEndpoint $GraphUri -AccessToken $AccessToken -Method Get -Uri $searchUri -Headers $searchHeaders
if ($searchResp.value.Count -eq 0) {
throw "No application found starting with '$AppDisplayNamePrefix'."
}
Write-Output "Found $($searchResp.value.Count) applications starting with '$AppDisplayNamePrefix'."
}
catch {
Write-Error ("Failed to search for application: " + $_.Exception.Message)
throw $_
}
foreach ($app in $searchResp.value) {
$appObjectId = $app.id
$appName = $app.displayName
Write-Output "Processing Application: $appName (ObjectId: $appObjectId)"
$uri = "/v1.0/applications/$appObjectId"
# 1. Update Tags
If ($UpdateTag) {
Write-Output "Updating tags with kdc_enable_cloud_group_sids..."
$tags = @("kdc_enable_cloud_group_sids")
$body = @{ tags = $tags } | ConvertTo-Json -Depth 5
try {
Invoke-GraphApiWithRetry -GraphEndpoint $GraphUri -AccessToken $AccessToken -Method Patch -Uri $uri -Body $body
Write-Output "Tags updated successfully for $appName."
}
catch {
Write-Error ("Failed to update tags for $appName : " + $_.Exception.Message)
throw
}
}
# 2. Update IdentifierUris for PrivateLink
if ($PrivateLink) {
Write-Output "Updating IdentifierUris for PrivateLink FQDN support..."
try {
# Get current app again to ensure we have latest identifierUris
$currentApp = Invoke-GraphApiWithRetry -GraphEndpoint $GraphUri -AccessToken $AccessToken -Method Get -Uri $uri
$currentUris = $currentApp.identifierUris
$newUris = @($currentUris)
$urisChanged = $false
Write-Output "Current IdentifierUris:"
foreach ($existingUri in $currentUris) {
Write-Output " - $existingUri"
}
foreach ($identifierUri in $currentUris) {
# Check for standard file endpoint pattern (works across clouds: windows.net, usgovcloudapi.net, etc.)
# Only process URIs that have a proper scheme (api://, http://, https://) to comply with Azure AD policy
if ($identifierUri -match '\.file\.core\.' -and
$identifierUri -notmatch '\.privatelink\.file\.core\.' -and
$identifierUri -match '^(api|http|https)://') {
# Insert .privatelink before .file.core.
$privateLinkUri = $identifierUri -replace '\.file\.core\.', '.privatelink.file.core.'
# Add to list if not already present (preserving existing URIs)
if ($newUris -notcontains $privateLinkUri) {
Write-Output " Adding PrivateLink URI: $privateLinkUri"
$newUris += $privateLinkUri
$urisChanged = $true
}
}
}
if ($urisChanged) {
$uriBody = @{ identifierUris = $newUris } | ConvertTo-Json -Depth 5
Invoke-GraphApiWithRetry -GraphEndpoint $GraphUri -AccessToken $AccessToken -Method Patch -Uri $uri -Body $uriBody
Write-Output "IdentifierUris updated successfully for $appName."
Write-Output "New IdentifierUris:"
foreach ($newUri in $newUris) {
Write-Output " - $newUri"
}
}
else {
Write-Output "PrivateLink IdentifierUris already present or not applicable for $appName."
}
}
catch {
Write-Error ("Failed to update IdentifierUris for $appName : " + $_.Exception.Message)
throw
}
}
}
Write-Output "============================================"
Write-Output "PHASE 1 COMPLETE: Manifest updated successfully"
Write-Output "Storage account applications can now authenticate via privatelink endpoints"
Write-Output "============================================"
}
catch {
Write-Error "PHASE 1 FAILED: $($_.Exception.Message)"
throw $_
}
finally {
Stop-Transcript
}