Skip to content

Commit 9fb13f6

Browse files
Add KeyVaultKeyReference support for GitHub App authentication
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
1 parent ee9c258 commit 9fb13f6

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ To get started with your own GitHub PowerShell based action, [create a new repos
1515
| `Token` | Log in using an Installation Access Token (IAT). | false | `${{ github.token }}` |
1616
| `ClientID` | Log in using a GitHub App, with the App's Client ID and Private Key. | false | |
1717
| `PrivateKey` | Log in using a GitHub App, with the App's Client ID and Private Key. | false | |
18+
| `KeyVaultKeyReference` | Log in using a GitHub App, with the App's Client ID and KeyVault Key Reference. | false | |
1819
| `Debug` | Enable debug output for the whole action. | false | `'false'` |
1920
| `Verbose` | Enable verbose output for the whole action. | false | `'false'` |
2021
| `Version` | Specifies the exact version of the GitHub module to install. | false | |
@@ -176,7 +177,35 @@ jobs:
176177
}
177178
```
178179

179-
#### Example 5: Using outputs from the script
180+
#### Example 5: Run a GitHub PowerShell script with a GitHub App using a Client ID and KeyVault Key Reference
181+
182+
Runs a script that uses the GitHub PowerShell module with a GitHub App authenticated via Azure KeyVault. This example retrieves the GitHub App details.
183+
184+
> [!NOTE]
185+
> This authentication method requires the `azure/login` action to authenticate with Azure first. The KeyVault Key Reference should be a URL pointing to the private key stored in Azure KeyVault.
186+
187+
```yaml
188+
jobs:
189+
Run-Script:
190+
runs-on: ubuntu-latest
191+
steps:
192+
- name: Login to Azure
193+
uses: azure/login@v1
194+
with:
195+
creds: ${{ secrets.AZURE_CREDENTIALS }}
196+
197+
- name: Run script
198+
uses: PSModule/GitHub-Script@v1
199+
with:
200+
ClientID: ${{ secrets.CLIENT_ID }}
201+
KeyVaultKeyReference: ${{ secrets.KEYVAULT_KEY_REFERENCE }}
202+
Script: |
203+
LogGroup "Get-GitHubApp" {
204+
Get-GitHubApp
205+
}
206+
```
207+
208+
#### Example 6: Using outputs from the script
180209

181210
Runs a script that uses the GitHub PowerShell module and outputs the result.
182211

@@ -201,7 +230,7 @@ Runs a script that uses the GitHub PowerShell module and outputs the result.
201230
Write-GitHubNotice -Message $result.Zen -Title 'GitHub Zen'
202231
```
203232

204-
#### Example 6: Run a script with credential cleanup
233+
#### Example 7: Run a script with credential cleanup
205234

206235
Runs a script with `PreserveCredentials` set to `false` to automatically disconnect GitHub credentials after execution.
207236

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ inputs:
2323
PrivateKey:
2424
description: Log in using a GitHub App, using the App's Client ID and Private Key.
2525
required: false
26+
KeyVaultKeyReference:
27+
description: Log in using a GitHub App, using the App's Client ID and KeyVault Key Reference.
28+
required: false
2629
Debug:
2730
description: Enable debug output for the whole action.
2831
required: false
@@ -80,6 +83,7 @@ runs:
8083
PSMODULE_GITHUB_SCRIPT_INPUT_Token: ${{ inputs.Token }}
8184
PSMODULE_GITHUB_SCRIPT_INPUT_ClientID: ${{ inputs.ClientID }}
8285
PSMODULE_GITHUB_SCRIPT_INPUT_PrivateKey: ${{ inputs.PrivateKey }}
86+
PSMODULE_GITHUB_SCRIPT_INPUT_KeyVaultKeyReference: ${{ inputs.KeyVaultKeyReference }}
8387
PSMODULE_GITHUB_SCRIPT_INPUT_Debug: ${{ inputs.Debug }}
8488
PSMODULE_GITHUB_SCRIPT_INPUT_Verbose: ${{ inputs.Verbose }}
8589
PSMODULE_GITHUB_SCRIPT_INPUT_Version: ${{ inputs.Version }}

scripts/init.ps1

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,28 @@ process {
7878
$providedToken = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_Token)
7979
$providedClientID = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID)
8080
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_PrivateKey)
81+
$providedKeyVaultKeyReference = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_KeyVaultKeyReference)
82+
83+
# Validate mutual exclusion of PrivateKey and KeyVaultKeyReference
84+
if ($providedPrivateKey -and $providedKeyVaultKeyReference) {
85+
throw 'Only one of PrivateKey or KeyVaultKeyReference can be provided.'
86+
}
87+
88+
# Validate that if ClientID is provided, exactly one of PrivateKey or KeyVaultKeyReference is also provided
89+
if ($providedClientID -and -not ($providedPrivateKey -or $providedKeyVaultKeyReference)) {
90+
throw 'When ClientID is provided, either PrivateKey or KeyVaultKeyReference must also be provided.'
91+
}
92+
8193
$moduleStatus = [pscustomobject]@{
82-
Name = $Name
83-
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
84-
Prerelease = $Prerelease
85-
'Already installed' = $null -ne $alreadyInstalled
86-
'Already imported' = $null -ne $alreadyImported
87-
'Provided Token' = $providedToken
88-
'Provided ClientID' = $providedClientID
89-
'Provided PrivateKey' = $providedPrivateKey
94+
Name = $Name
95+
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
96+
Prerelease = $Prerelease
97+
'Already installed' = $null -ne $alreadyInstalled
98+
'Already imported' = $null -ne $alreadyImported
99+
'Provided Token' = $providedToken
100+
'Provided ClientID' = $providedClientID
101+
'Provided PrivateKey' = $providedPrivateKey
102+
'Provided KeyVaultKeyReference' = $providedKeyVaultKeyReference
90103
}
91104
if ($showInit) {
92105
Write-Output 'Module status:'
@@ -101,6 +114,13 @@ process {
101114
Silent = (-not $showInit)
102115
}
103116
Connect-GitHub @params
117+
} elseif ($providedClientID -and $providedKeyVaultKeyReference) {
118+
$params = @{
119+
ClientID = $env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID
120+
KeyVaultKeyReference = $env:PSMODULE_GITHUB_SCRIPT_INPUT_KeyVaultKeyReference
121+
Silent = (-not $showInit)
122+
}
123+
Connect-GitHub @params
104124
} elseif ($providedToken) {
105125
$params = @{
106126
Token = $env:PSMODULE_GITHUB_SCRIPT_INPUT_Token

0 commit comments

Comments
 (0)