Skip to content

Commit 7fff30c

Browse files
authored
AzurePowerShell idtoken sample (#67)
1 parent 3f8a53b commit 7fff30c

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

scripts/azure-devops/create-oidctoken.yml

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ jobs:
118118
workingDirectory: $(Build.ArtifactStagingDirectory)
119119
120120
- task: AzureCLI@2
121-
displayName: 'Inline script'
121+
displayName: 'AzureCLI script'
122122
inputs:
123123
azureSubscription: '$(azureConnectionWIF)'
124124
scriptType: pscore
@@ -149,9 +149,53 @@ jobs:
149149
-Method Post | Set-Variable oidcTokenResponse
150150
151151
$oidcToken = $oidcTokenResponse.oidcToken
152-
if ($oidcToken) {
153-
Write-Host "idToken (masked):"
154-
$oidcToken -replace '.','*'
152+
if ($oidcToken -match "^ey") {
153+
Write-Host "REST API returned a JWT token"
154+
} elseif ($oidcToken) {
155+
throw "OIDC token in unexpected format"
156+
} else {
157+
throw "Failed to request OIDC token"
158+
}
159+
failOnStandardError: true
160+
workingDirectory: $(Build.ArtifactStagingDirectory)
161+
162+
- task: AzurePowerShell@5
163+
displayName: 'AzurePowerShell script'
164+
inputs:
165+
azurePowerShellVersion: LatestVersion
166+
azureSubscription: '$(azureConnectionWIF)'
167+
pwsh: true
168+
scriptType: InlineScript
169+
inline: |
170+
Write-Host "Service Connection ID: ${env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID}"
171+
Write-Host "Service Connection endpoint data:"
172+
Get-ChildItem -Path Env: -Recurse `
173+
-Include AZURESUBSCRIPTION_*, SYSTEM_OIDC* `
174+
| Sort-Object -Property Name `
175+
| ForEach-Object {
176+
if ($_.Name -match 'SECRET|TOKEN') {
177+
$_.Value = '***'
178+
}
179+
$_
180+
} `
181+
| Format-Table -HideTableHeaders -Property @{Expression='Name';Width=75}, @{Expression='Value';Width=175} -Wrap `
182+
| Out-String -Width 256
183+
184+
$oidcTokenUrl = "${env:SYSTEM_OIDCREQUESTURI}?api-version=7.1&serviceConnectionId=${env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID}"
185+
Write-Host "oidcTokenUrl: $oidcTokenUrl"
186+
187+
Invoke-RestMethod -Headers @{
188+
Authorization = "Bearer $(System.AccessToken)"
189+
'Content-Type' = 'application/json'
190+
} `
191+
-Uri $oidcTokenUrl `
192+
-Method Post | Set-Variable oidcTokenResponse
193+
194+
$oidcToken = $oidcTokenResponse.oidcToken
195+
if ($oidcToken -match "^ey") {
196+
Write-Host "REST API returned a JWT token"
197+
} elseif ($oidcToken) {
198+
throw "OIDC token in unexpected format"
155199
} else {
156200
throw "Failed to request OIDC token"
157201
}
@@ -160,4 +204,4 @@ jobs:
160204

161205
- publish: $(Build.ArtifactStagingDirectory)
162206
displayName: 'Publish json files'
163-
artifact: $(azureConnectionWIF)
207+
artifact: $(azureConnectionWIF)

0 commit comments

Comments
 (0)