mirror of
https://github.com/Micke-K/IntuneManagement.git
synced 2026-11-27 09:22:26 +01:00
Add -Federated switch for GitHub Actions federated credential auth (#440)
Silent and batch runs can now sign in with a GitHub Actions OIDC token instead of a client secret or certificate. With -Federated, the tool requests the token and passes it to MSAL with WithClientAssertion. The access token is reused until it is within five minutes of expiry, so a bulk export signs in once rather than on every Graph request. Secret and certificate sign-in are unchanged. Contributed by @mattskare.
This commit is contained in:
@@ -89,7 +89,9 @@ function Initialize-CloudAPIManagement
|
|||||||
[string]
|
[string]
|
||||||
$GraphEnvironment,
|
$GraphEnvironment,
|
||||||
[string]
|
[string]
|
||||||
$GCCType
|
$GCCType,
|
||||||
|
[switch]
|
||||||
|
$Federated
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -104,6 +106,7 @@ function Initialize-CloudAPIManagement
|
|||||||
|
|
||||||
$global:hideUI = ($Silent -eq $true)
|
$global:hideUI = ($Silent -eq $true)
|
||||||
$global:SilentBatchFile = $SilentBatchFile
|
$global:SilentBatchFile = $SilentBatchFile
|
||||||
|
$global:UseFederated = ($Federated -eq $true)
|
||||||
|
|
||||||
if($tenantId)
|
if($tenantId)
|
||||||
{
|
{
|
||||||
@@ -123,9 +126,13 @@ function Initialize-CloudAPIManagement
|
|||||||
Write-Warning "Azure App Id is missing. Use -AppId <AppID> on the command line"
|
Write-Warning "Azure App Id is missing. Use -AppId <AppID> on the command line"
|
||||||
}
|
}
|
||||||
|
|
||||||
if($global:ClientSecret -or $global:ClientCert)
|
if($global:ClientSecret -or $global:ClientCert -or $global:UseFederated)
|
||||||
{
|
{
|
||||||
if($global:ClientSecret)
|
if($global:UseFederated)
|
||||||
|
{
|
||||||
|
Write-Host "Using GitHub Actions Federated Credential"
|
||||||
|
}
|
||||||
|
elseif($global:ClientSecret)
|
||||||
{
|
{
|
||||||
Write-Host "Using Azure App Secret"
|
Write-Host "Using Azure App Secret"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -753,9 +753,14 @@ function Add-MSALPrereq_old
|
|||||||
|
|
||||||
function Connect-MSALClientApp
|
function Connect-MSALClientApp
|
||||||
{
|
{
|
||||||
param($clientId, $tenantId, $secret, $Certificate)
|
param($clientId, $tenantId, $secret, $Certificate, [switch]$Federated)
|
||||||
$scopes = [String[]]".default"
|
$scopes = [String[]]".default"
|
||||||
|
|
||||||
|
if($Federated) {
|
||||||
|
if($global:MSALToken -and $global:MSALToken.ExpiresOn.UtcDateTime -gt [DateTime]::UtcNow.AddMinutes(5)) { return }
|
||||||
|
$script:MSALApp = $null
|
||||||
|
}
|
||||||
|
|
||||||
if(-not $script:MSALApp)
|
if(-not $script:MSALApp)
|
||||||
{
|
{
|
||||||
if($global:UseGraphEnvironment) {
|
if($global:UseGraphEnvironment) {
|
||||||
@@ -793,7 +798,19 @@ function Connect-MSALClientApp
|
|||||||
}
|
}
|
||||||
$ClientApplicationBuilder = [Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($clientId).WithCertificate($cert).WithAuthority([URI]::new($authority)) #.WithRedirectUri($redirectUri)
|
$ClientApplicationBuilder = [Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($clientId).WithCertificate($cert).WithAuthority([URI]::new($authority)) #.WithRedirectUri($redirectUri)
|
||||||
}
|
}
|
||||||
else
|
elseif($Federated)
|
||||||
|
{
|
||||||
|
if(-not $env:ACTIONS_ID_TOKEN_REQUEST_URL)
|
||||||
|
{
|
||||||
|
Write-LogError "ACTIONS_ID_TOKEN_REQUEST_URL is missing. Run in GitHub Actions with 'permissions: id-token: write'"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
$uri = "$($env:ACTIONS_ID_TOKEN_REQUEST_URL)&audience=api://AzureADTokenExchange"
|
||||||
|
$assertion = (Invoke-RestMethod -Uri $uri -Headers @{ Authorization = "Bearer $env:ACTIONS_ID_TOKEN_REQUEST_TOKEN" }).value
|
||||||
|
# shortcut: string overload is flagged "not recommended" by MSAL, a delegate overload cannot run without a runspace
|
||||||
|
$ClientApplicationBuilder = [Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($clientId).WithClientAssertion($assertion).WithAuthority([URI]::new($authority))
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1037,6 +1054,10 @@ function Connect-MSALUser
|
|||||||
{
|
{
|
||||||
Connect-MSALClientApp $global:AzureAppId $global:TenantId -certificate $global:ClientCert
|
Connect-MSALClientApp $global:AzureAppId $global:TenantId -certificate $global:ClientCert
|
||||||
}
|
}
|
||||||
|
elseif($global:AzureAppId -and $global:UseFederated -and $global:TenantId)
|
||||||
|
{
|
||||||
|
Connect-MSALClientApp $global:AzureAppId $global:TenantId -Federated
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Write-Log "Azure AppId, Tenant Id and Sercret/Cert must be specified for batch jobs" 3
|
Write-Log "Azure AppId, Tenant Id and Sercret/Cert must be specified for batch jobs" 3
|
||||||
@@ -1052,7 +1073,7 @@ function Connect-MSALUser
|
|||||||
|
|
||||||
if($global:MainAppStarted -eq $false)
|
if($global:MainAppStarted -eq $false)
|
||||||
{
|
{
|
||||||
$script:AppLogin = (Get-SettingValue "GraphAzureAppLogin") -or ($global:TenantId -and $global:AzureAppId -and ($global:ClientSecret -or $global:ClientCert))
|
$script:AppLogin = (Get-SettingValue "GraphAzureAppLogin") -or ($global:TenantId -and $global:AzureAppId -and ($global:ClientSecret -or $global:ClientCert -or $global:UseFederated))
|
||||||
|
|
||||||
if((Get-SettingValue "GraphAzureAppLogin") -and -not $global:TenantId)
|
if((Get-SettingValue "GraphAzureAppLogin") -and -not $global:TenantId)
|
||||||
{
|
{
|
||||||
@@ -1069,8 +1090,8 @@ function Connect-MSALUser
|
|||||||
|
|
||||||
# Get login info for silent job from settings
|
# Get login info for silent job from settings
|
||||||
if(-not $global:AzureAppId) { $global:AzureAppId = Get-SettingValue "GraphAzureAppId" -TenantID $global:TenantId }
|
if(-not $global:AzureAppId) { $global:AzureAppId = Get-SettingValue "GraphAzureAppId" -TenantID $global:TenantId }
|
||||||
if(-not $global:ClientSecret -and -not $global:ClientCert) { $global:ClientSecret = Get-SettingValue "GraphAzureAppSecret" -TenantID $global:TenantId }
|
if(-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated) { $global:ClientSecret = Get-SettingValue "GraphAzureAppSecret" -TenantID $global:TenantId }
|
||||||
if(-not $global:ClientSecret -and -not $global:ClientCert) { $global:ClientCert = Get-SettingValue "GraphAzureAppCert" -TenantID $global:TenantId }
|
if(-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated) { $global:ClientCert = Get-SettingValue "GraphAzureAppCert" -TenantID $global:TenantId }
|
||||||
|
|
||||||
if($global:AzureAppId -and $global:ClientSecret -and $global:TenantId)
|
if($global:AzureAppId -and $global:ClientSecret -and $global:TenantId)
|
||||||
{
|
{
|
||||||
@@ -1080,6 +1101,10 @@ function Connect-MSALUser
|
|||||||
{
|
{
|
||||||
Connect-MSALClientApp $global:AzureAppId $global:TenantId -certificate $global:ClientCert
|
Connect-MSALClientApp $global:AzureAppId $global:TenantId -certificate $global:ClientCert
|
||||||
}
|
}
|
||||||
|
elseif($global:AzureAppId -and $global:UseFederated -and $global:TenantId)
|
||||||
|
{
|
||||||
|
Connect-MSALClientApp $global:AzureAppId $global:TenantId -Federated
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Write-Log "Azure AppId, Tenant Id and Sercret/Cert must be specified for App logins" 3
|
Write-Log "Azure AppId, Tenant Id and Sercret/Cert must be specified for App logins" 3
|
||||||
@@ -2442,4 +2467,4 @@ function Show-MSALDecodedToken {
|
|||||||
Show-ModalForm $title $dg
|
Show-ModalForm $title $dg
|
||||||
}
|
}
|
||||||
|
|
||||||
Export-ModuleMember -alias * -function *
|
Export-ModuleMember -alias * -function *
|
||||||
|
|||||||
@@ -1405,7 +1405,7 @@ function Invoke-InitSilentBatchJob
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if(-not $global:AzureAppId -or (-not $global:ClientSecret -and -not $global:ClientCert))
|
if(-not $global:AzureAppId -or (-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated))
|
||||||
{
|
{
|
||||||
# Get login info for silent job from settings
|
# Get login info for silent job from settings
|
||||||
$global:AzureAppId = Get-SettingValue "GraphAzureAppId" -TenantID $global:TenantId
|
$global:AzureAppId = Get-SettingValue "GraphAzureAppId" -TenantID $global:TenantId
|
||||||
@@ -1419,7 +1419,7 @@ function Invoke-InitSilentBatchJob
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if(-not $global:ClientSecret -and -not $global:ClientCert)
|
if(-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated)
|
||||||
{
|
{
|
||||||
Write-Log "Secret or Certificate must be specified. Either specify Secret/Certificate in Settings or Command Line" 3
|
Write-Log "Secret or Certificate must be specified. Either specify Secret/Certificate in Settings or Command Line" 3
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ param(
|
|||||||
[string]
|
[string]
|
||||||
$Secret,
|
$Secret,
|
||||||
[string]
|
[string]
|
||||||
$Certificate
|
$Certificate,
|
||||||
|
[switch]
|
||||||
|
$Federated
|
||||||
)
|
)
|
||||||
Import-Module ($PSScriptRoot + "\CloudAPIPowerShellManagement.psd1") -Force
|
Import-Module ($PSScriptRoot + "\CloudAPIPowerShellManagement.psd1") -Force
|
||||||
$param = $PSBoundParameters
|
$param = $PSBoundParameters
|
||||||
|
|||||||
Reference in New Issue
Block a user