Skip to content

Commit 9893b7c

Browse files
(GHA) Update the auth action to accept an allowlist (#12181)
Prior to this change, the verification/`authorization` GitHub Action only supported checking the assigned permissions for a user. This worked for normal accounts. However, the managed bot account for the Learn platform doesn't have permissions for this repository. This change adds a new (backwards-compatible) `authorized_accounts` parameter to the GHA. Repository maintainers can now define an allowlist to use for authorization in addition to the permissions to check. If a user is explicitly in the allowlist, the action skips checking their permissions. If a user isn't in the allowlist, they can still pass authorization if they have matching permissions.
1 parent fc8c28d commit 9893b7c

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

.github/actions/.pwsh/scripts/Test-Authorization.ps1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ param(
6969
[Parameter(Mandatory, ParameterSetName='Path')]
7070
[string[]]$TargetPath,
7171
[ValidateSet('Admin', 'Maintain', 'Pull', 'Push', 'Triage')]
72-
[string[]]$ValidPermissions = @('Admin', 'Maintain')
72+
[string[]]$ValidPermissions = @('Admin', 'Maintain'),
73+
[string[]]$AuthorizedAccounts
7374
)
7475

7576
begin {
@@ -101,6 +102,10 @@ begin {
101102
Console = Format-ConsoleStyle -Text $User -DefinedStyle UserName
102103
Markdown = "``$User``"
103104
}
105+
AuthorizedAccounts = @{
106+
Console = Format-ConsoleStyle -Text 'AuthorizedAccounts' -DefinedStyle Success
107+
Markdown = '`AuthorizedAccounts`'
108+
}
104109
}
105110
if (![string]::IsNullOrEmpty($TargetBranch)) {
106111
$ConsoleBranch = Format-ConsoleStyle -Text $TargetBranch -StyleComponent $TargetStyle
@@ -123,6 +128,19 @@ begin {
123128
}
124129

125130
process {
131+
if ($AuthorizedAccounts.Count -gt 0 -and $User -in $AuthorizedAccounts) {
132+
$template = "Account {0} is explicitly permitted per the {1} parameter."
133+
$message = @{
134+
summary = ($template -f $Texts.Author.Markdown, $Texts.AuthorizedAccounts.Markdown)
135+
console = ($template -f $Texts.Author.Console, $Texts.AuthorizedAccounts.Console)
136+
}
137+
$null = $Summary.AppendLine('## Authorization').AppendLine()
138+
$null = $Summary.AppendLine($message.summary).AppendLine()
139+
# Console Logging
140+
$message.console
141+
142+
return
143+
}
126144
try {
127145
$Permissions = Get-AuthorPermission -Owner $Owner -Repo $Repo -Author $User
128146
} catch {
@@ -149,7 +167,7 @@ process {
149167
"$Prefix`t$Setting"
150168
}
151169
#endregion Permission Retrieval Messaging
152-
170+
153171
$null = $Summary.AppendLine('## Result').AppendLine()
154172

155173
# Check for authorization; if the user has any of the valid permissions, they

.github/actions/verification/authorization/v1/Parameters.psd1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929
return $Parameters
3030
}
3131
}
32+
@{
33+
Name = 'AuthorizedAccounts'
34+
Type = 'String[]'
35+
IfNullOrEmpty = {
36+
param($ErrorTarget)
37+
38+
# This parameter is optional, so don't error.
39+
}
40+
Process = {
41+
param($Parameters, $Value, $ErrorTarget)
42+
43+
[string[]]$SpecifiedAccounts = $Value -split ','
44+
if ($SpecifiedAccounts.Count -gt 0) {
45+
$Parameters.AuthorizedAccounts = $SpecifiedAccounts
46+
Write-HostParameter -Name AuthorizedAccounts -Value $Parameters.AuthorizedAccounts
47+
}
48+
return $Parameters
49+
}
50+
}
3251

3352
@{
3453
Name = 'Permissions'

.github/actions/verification/authorization/v1/action.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ description: |
44
branch of a repository or to submit a PR editing repo configuration.
55
author: PowerShell Docs Team
66
inputs:
7+
authorized_accounts:
8+
description: |
9+
Defines one or more authorized accounts to skip permission-checking for. This is best used
10+
for bot accounts, which may not have specific permissions to a repository but are used by
11+
the organization's automation. Must be a comma-separated string of account names.
12+
13+
If a user is in the authorized accounts list, the action skips checking permissions and
14+
passes for that user.
15+
required: false
16+
default: ''
717
permissions:
818
description: |
919
The permissions a user requires to perform a given task. Must be a comma-separated string of
@@ -84,6 +94,7 @@ runs:
8494
INPUT_PERMISSIONS: ${{ inputs.permissions }}
8595
INPUT_TARGET: ${{ inputs.target }}
8696
INPUT_USER: ${{ inputs.user }}
97+
INPUT_AUTHORIZED_ACCOUNTS: ${{ inputs.authorized_accounts }}
8798
GITHUB_TOKEN: ${{ inputs.token }}
8899
run: |
89100
Write-Output "::group::Generic Setup"

.github/actions/verification/authorization/v1/readme.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ jobs:
5454
uses: MicrosoftDocs/PowerShell-Docs/.github/actions/verification/authorization/v1@main
5555
with:
5656
token: ${{ github.token }}
57+
authorized_accounts: 'learn-build-service-prod[bot]'
5758
```
5859
5960
This workflow uses the `pull_request_target` trigger to check whether a Pull Request author is
6061
permitted to submit their Pull Request to the `live` branch. It only runs on Pull Requests which
6162
target the `live` branch, so other Pull Requests don't get a skipped message for this check.
6263

6364
It passes the GitHub token to the action but does not specify a target, relying on the default for
64-
that input, which is the `live` branch.
65+
that input, which is the `live` branch. It does specify that the `learn-build-service-prod[bot]`
66+
managed account is authorized with the `authorized_accounts` parameter. If the account creating a
67+
PR to the `live` branch is the managed account or has either the `Maintain` or `Admin` permission,
68+
the workflow will pass.
6569

6670
### Verifying authorization to change sensitive files
6771

@@ -104,6 +108,21 @@ authorization to change files in those paths.
104108

105109
## Inputs
106110

111+
### `authorized_accounts`
112+
113+
Defines one or more authorized accounts to skip permission-checking for. This is best used for bot
114+
accounts, which may not have specific permissions to a repository but are used by the
115+
organization's automation. Must be a comma-separated string of account names.
116+
117+
If a user is in the authorized accounts list, the action skips checking permissions and passes for
118+
that user.
119+
120+
```yaml
121+
required : false
122+
type : string
123+
default : ''
124+
```
125+
107126
### `permissions`
108127

109128
The permissions a user requires to perform a given task. Must be a comma-separated string of valid

.github/workflows/targeting-valid-branch.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ jobs:
2323
uses: ./.github/actions/verification/authorization/v1
2424
with:
2525
token: ${{ github.token }}
26+
authorized_accounts: learn-build-service-prod[bot]

0 commit comments

Comments
 (0)