Skip to content

Commit 226b6d2

Browse files
Merge pull request #51 from JulianHayward/users/kaiaschulz/1.3.2
Bump module version to 1.3.2 and enhance logging for access token val…
2 parents a2c0b46 + d00031d commit 226b6d2

8 files changed

Lines changed: 103 additions & 29 deletions

File tree

README.md

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
You want to have an easy way to interact with the Microsoft Azure API endpoints without getting headache of taking care of valid bearer token and error handling?
66

77
## Table of content
8+
89
- [AzAPICall](#azapicall)
910
- [Table of content](#table-of-content)
1011
- [AzAPICall example](#azapicall-example)
@@ -20,6 +21,8 @@ You want to have an easy way to interact with the Microsoft Azure API endpoints
2021
- [Good to know](#good-to-know)
2122
- [Don´t accept the defaults](#dont-accept-the-defaults)
2223
- [AzAPICall Tracking](#azapicall-tracking)
24+
- [Runtime environment](#runtime-environment)
25+
- [Azure DevOps](#azure-devops)
2326
- [Prerequisites](#prerequisites)
2427
- [Powershell Modules](#powershell-modules)
2528
- [Contribute](#contribute)
@@ -54,38 +57,49 @@ $parameters4AzAPICallModule = @{
5457
$azAPICallConf = initAzAPICall @parameters4AzAPICallModule
5558
```
5659

57-
### How to use AzAPICall ?!
60+
### How to use AzAPICall ?
5861

5962
#### Example for Microsoft Graph
60-
Get AAD Groups:
63+
64+
Get AAD Groups:
65+
6166
```POWERSHELL
6267
AzAPICall -uri "$($azAPICallConf['azAPIEndpointUrls'].MicrosoftGraph)/v1.0/groups" -AzAPICallConfiguration $azAPICallConf
6368
```
69+
6470
_confused by_ '`$($azAPICallConf['azAPIEndpointUrls'].MicrosoftGraph)`'_? It´s basically a reference to the correct endpoint (think public cloud, sovereign clouds). You can of course also hardcode the endpoint URI:_
6571

6672
```POWERSHELL
6773
AzAPICall -uri "https://graph.microsoft.com/v1.0/groups" -AzAPICallConfiguration $azAPICallConf
6874
```
6975

7076
#### Example for Azure Resource Manager
71-
List Azure Subscriptions (expect multiple results):
77+
78+
List Azure Subscriptions (expect multiple results):
79+
7280
```POWERSHELL
7381
AzAPICall -uri "$($azAPICallConf['azAPIEndpointUrls'].ARM)/subscriptions?api-version=2020-01-01" -AzAPICallConfiguration $azAPICallConf
7482
```
75-
Get Azure Subscription (expect one result):
83+
84+
Get Azure Subscription (expect one result):
85+
7686
```POWERSHELL
7787
AzAPICall -uri "$($azAPICallConf['azAPIEndpointUrls'].ARM)/subscriptions/$($subscriptionId)?api-version=2020-01-01" -AzAPICallConfiguration $azAPICallConf -listenOn Content
7888
```
89+
7990
[AzAPICallExample.ps1](pwsh/AzAPICallExample.ps1)
8091

8192
## Public functions
82-
* initAzAPICall
83-
* AzAPICall
84-
* getAzAPICallFunctions
85-
* getAzAPICallRuleSet
86-
* createBearerToken
8793

88-
createBearerToken example:
94+
- initAzAPICall
95+
96+
- AzAPICall
97+
- getAzAPICallFunctions
98+
- getAzAPICallRuleSet
99+
- createBearerToken
100+
101+
createBearerToken example:
102+
89103
```POWERSHELL
90104
$azAPICallConf = initAzAPICall
91105
createBearerToken -AzAPICallConfiguration $azapicallconf -targetEndPoint 'Storage'
@@ -107,7 +121,7 @@ Add a new endpoint -> setAzureEnvironment.ps1
107121

108122
## General Parameters
109123

110-
Parameters that can be used with the initAzAPICall cmdlet
124+
Parameters that can be used with the initAzAPICall cmdlet
111125

112126
Example: [Initialize AzAPICall](#initialize-azapicall)
113127

@@ -121,7 +135,6 @@ Example: [Initialize AzAPICall](#initialize-azapicall)
121135
| AzAPICallCustomRuleSet | `object` | wip | |
122136
| SkipAzContextSubscriptionValidation | `bool` | Only use in case you do not have any valid (quotaId != AAD_* & state != disabled) subscriptions in your tenant OR you do not have any permissions on Azure Resources (Management Groups, Subscriptions, Resource Groups, Resources) and but want to connect non-ARM API endpoints such as Microsoft Graph etc. (Per default a subscription is expected to be present in the Az context, if not then AzAPICall will throw..). | |
123137

124-
125138
## AzAPICall Parameters
126139

127140
Parameters that can be used with the AzAPICall cmdlet
@@ -144,6 +157,7 @@ Example: `AzAPICall -uri "https://management.azure.com/subscriptions?api-version
144157
| unhandledErrorAction | `string` | When a call to an API returns an Error, that error is processed by AzAPICallErrorHandler. If that error is unhandled, AzAPICallErrorHandler will log the error and Throw a message which terminates the script. This happens when parameter -unhandledErrorAction is set to `Stop` (which is also the default if not configured). When -unhandledErrorAction is set to `Continue`, AzAPICallErrorHandler logs the error including full details to raise an issue at the repo and continues processing. When -unhandledErrorAction is set to `ContinueQuiet`, AzAPICallErrorHandler only logs the error (excluding full details to raise an issue at the repo) and continues processing | default is `Stop`, options: `Continue`, `ContinueQuiet` |
145158

146159
## Good to know
160+
147161
### Don´t accept the defaults
148162

149163
By default, endPoints return results in batches of e.g. `100`. You can increase the return count defining e.g. `$top=999` (`$top` requires use of `consistencyLevel` = `eventual`)
@@ -155,6 +169,7 @@ To get some insights on all API calls you can check the `$azAPICallConf['arrayAP
155169
```POWERSHELL
156170
$azAPICallConf['arrayAPICallTracking'][0] | ConvertTo-Json
157171
```
172+
158173
```JSON
159174
{
160175
"CurrentTask": "Microsoft Graph API: Get - Groups",
@@ -183,10 +198,13 @@ $azAPICallConf['arrayAPICallTracking'][0] | ConvertTo-Json
183198
[..]
184199
}"
185200
```
201+
186202
As well you can see how fast a AzAPICall was responding:
203+
187204
```POWERSHELL
188205
($azAPICallConf['arrayAPICallTracking'].Duration | Measure-Object -Average -Maximum -Minimum) | ConvertTo-Json
189206
```
207+
190208
```JSON
191209
{
192210
"Count": 1000,
@@ -199,8 +217,51 @@ As well you can see how fast a AzAPICall was responding:
199217
}
200218
```
201219

220+
## Runtime environment
221+
222+
### Azure DevOps
223+
224+
If you are using a PowerShell script within a pipeline and an `OIDC` service connection, you need to set the [`SYSTEM_ACCESSTOKEN` environment variable](https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken) in the task of your pipeline. This allows the AzAPICall module to use it for token renewal:
225+
226+
```YML
227+
- task: AzurePowerShell@5
228+
displayName: 'OIDC testing with AzurePowerShell@5'
229+
env:
230+
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
231+
inputs:
232+
azureSubscription: '$(ServiceConnection)'
233+
azurePowerShellVersion: LatestVersion
234+
ScriptType: 'InlineScript'
235+
Inline: |
236+
try {
237+
Install-Module -Name 'AzAPICall' -RequiredVersion '1.3.2' -ErrorAction Stop # ? https://www.powershellgallery.com/packages/AzAPICall/1.3.2
238+
}
239+
catch {
240+
Write-Warning '33596ac3-5aab-4704-aef2-e1de6ac71f05'
241+
Throw $_
242+
}
243+
244+
# [..]
245+
```
246+
247+
Otherwise, you will encounter an error message during your pipeline execution:
248+
249+
```POWERSHELL
250+
Logging: /home/vsts/work/1/s/AzAPICall/functions/AzAPICallFunctions.ps1:1672
251+
Line |
252+
1672 | … Logging -logMessage "-ERROR: OIDC ADO - Could not find ac …
253+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
254+
| -ERROR: OIDC ADO - Could not find access token, check if the environment
255+
| variable 'SYSTEM_ACCESSTOKEN' exists and has valid data.
256+
| https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken
257+
258+
##[error]PowerShell exited with code '1'.
259+
```
260+
202261
## Prerequisites
262+
203263
### Powershell Modules
264+
204265
| PowerShell Module |
205266
| ----------------- |
206267
| Az.Accounts |
@@ -210,8 +271,9 @@ As well you can see how fast a AzAPICall was responding:
210271
Your contribution is welcome.
211272

212273
Thanks to the awesome contributors:
213-
* Brooks Vaugn
214-
* Kai Schulz
215-
* Simon Wahlin
216-
* Tim Stock
217-
* Tim Wanierke
274+
275+
- Brooks Vaugn
276+
- Kai Schulz
277+
- Simon Wahlin
278+
- Tim Stock
279+
- Tim Wanierke

pwsh/module/build/AzAPICall.zip

-15 Bytes
Binary file not shown.

pwsh/module/build/AzAPICall/AzAPICall.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'AzAPICall.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.3.1'
15+
ModuleVersion = '1.3.2'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

pwsh/module/build/AzAPICall/functions/AzAPICallFunctions.ps1

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,11 @@ function createBearerToken {
16681668
if ($_ -like '*ClientAssertionCredential authentication failed*') {
16691669
Logging -logMessage " Running on '$(($AzApiCallConfiguration['htParameters']).codeRunPlatform)' OIDC: '$(($AzApiCallConfiguration['htParameters']).accountType)' - Getting Bearer Token from Login endpoint '$(($AzApiCallConfiguration['azAPIEndpointUrls']).Login)'"
16701670

1671+
if ([string]::IsNullOrWhiteSpace($env:SYSTEM_ACCESSTOKEN)) {
1672+
Logging -logMessage "-ERROR: OIDC ADO - Could not find access token, check if the environment variable 'SYSTEM_ACCESSTOKEN' exists and has valid data. https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken" -logMessageWriteMethod 'Error'
1673+
Throw "Error - OIDC ADO - Could not find access token, check if the environment variable 'SYSTEM_ACCESSTOKEN' exists and has valid data. https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken"
1674+
}
1675+
16711676
try {
16721677
$serviceConnectionId = (Get-ChildItem -ErrorAction Stop -Path Env: -Recurse -Include ENDPOINT_DATA_*)[0].Name.Split('_')[2]
16731678
}
@@ -1794,7 +1799,7 @@ function getAzAPICallFunctions {
17941799
function getAzAPICallRuleSet {
17951800
return $function:AzAPICallErrorHandler.ToString()
17961801
}
1797-
function getAzAPICallVersion { return '1.3.1' }
1802+
function getAzAPICallVersion { return '1.3.2' }
17981803

17991804
function getJWTDetails {
18001805
<#
@@ -2088,19 +2093,19 @@ function initAzAPICall {
20882093
}
20892094
function Logging {
20902095
param (
2091-
[Parameter(Mandatory = $true)]
2096+
[Parameter(Mandatory)]
20922097
[string]
20932098
$logMessage,
20942099

2095-
[Parameter(Mandatory = $false)]
2100+
[Parameter()]
20962101
[string]
20972102
$logMessageForegroundColor = $debugForeGroundColor,
20982103

2099-
[Parameter(Mandatory = $false)]
2104+
[Parameter()]
21002105
[string]
21012106
$logMessageWriteMethod = $AzAPICallConfiguration['htParameters'].writeMethod,
21022107

2103-
[Parameter(Mandatory = $false)]
2108+
[Parameter()]
21042109
[bool]
21052110
$preventWriteOutput
21062111
)
@@ -2122,6 +2127,7 @@ function Logging {
21222127
'Progress' { Write-Progress $logMessage }
21232128
'Verbose' { Write-Verbose $logMessage -Verbose }
21242129
'Warning' { Write-Warning $logMessage }
2130+
'Throw' { throw $logMessage }
21252131
Default { Write-Host $logMessage -ForegroundColor $logMessageForegroundColor }
21262132
}
21272133
}

pwsh/module/dev/AzAPICall/AzAPICall.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'AzAPICall.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.3.1'
15+
ModuleVersion = '1.3.2'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

pwsh/module/dev/AzAPICall/functions/Logging.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
function Logging {
22
param (
3-
[Parameter(Mandatory = $true)]
3+
[Parameter(Mandatory)]
44
[string]
55
$logMessage,
66

7-
[Parameter(Mandatory = $false)]
7+
[Parameter()]
88
[string]
99
$logMessageForegroundColor = $debugForeGroundColor,
1010

11-
[Parameter(Mandatory = $false)]
11+
[Parameter()]
1212
[string]
1313
$logMessageWriteMethod = $AzAPICallConfiguration['htParameters'].writeMethod,
1414

15-
[Parameter(Mandatory = $false)]
15+
[Parameter()]
1616
[bool]
1717
$preventWriteOutput
1818
)
@@ -34,6 +34,7 @@
3434
'Progress' { Write-Progress $logMessage }
3535
'Verbose' { Write-Verbose $logMessage -Verbose }
3636
'Warning' { Write-Warning $logMessage }
37+
'Throw' { throw $logMessage }
3738
Default { Write-Host $logMessage -ForegroundColor $logMessageForegroundColor }
3839
}
3940
}

pwsh/module/dev/AzAPICall/functions/createBearerToken.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@
160160
if ($_ -like '*ClientAssertionCredential authentication failed*') {
161161
Logging -logMessage " Running on '$(($AzApiCallConfiguration['htParameters']).codeRunPlatform)' OIDC: '$(($AzApiCallConfiguration['htParameters']).accountType)' - Getting Bearer Token from Login endpoint '$(($AzApiCallConfiguration['azAPIEndpointUrls']).Login)'"
162162

163+
if ([string]::IsNullOrWhiteSpace($env:SYSTEM_ACCESSTOKEN)) {
164+
Logging -logMessage "-ERROR: OIDC ADO - Could not find access token, check if the environment variable 'SYSTEM_ACCESSTOKEN' exists and has valid data. https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken" -logMessageWriteMethod 'Error'
165+
Throw "Error - OIDC ADO - Could not find access token, check if the environment variable 'SYSTEM_ACCESSTOKEN' exists and has valid data. https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken"
166+
}
167+
163168
try {
164169
$serviceConnectionId = (Get-ChildItem -ErrorAction Stop -Path Env: -Recurse -Include ENDPOINT_DATA_*)[0].Name.Split('_')[2]
165170
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
function getAzAPICallVersion { return '1.3.1' }
1+
function getAzAPICallVersion { return '1.3.2' }

0 commit comments

Comments
 (0)