-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-GitHubSecret.ps1
More file actions
445 lines (360 loc) · 16.8 KB
/
Copy pathRemove-GitHubSecret.ps1
File metadata and controls
445 lines (360 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
function Remove-GitHubSecret
{
<#
.SYNOPSIS
Removes a GitHub secret from an explicit repository, environment, organization, or user scope.
.DESCRIPTION
Removes a GitHub secret by using the GitHub CLI. Scope selection is always explicit through
the required -Scope parameter.
Repository scope can omit -Repository and use the current Git repository's origin remote.
Environment, organization, and user scopes require their matching target parameters.
Missing secrets are treated as an idempotent no-op so repeated runs remain safe.
Secret operations require the GitHub CLI (gh). This function intentionally does not use a
Python-based encryption fallback.
.PARAMETER Name
The name of the GitHub secret to remove.
Secret names:
- Can contain only letters, numbers, and underscores
- Cannot start with a number
- Cannot start with the GITHUB_ prefix
- Are case-insensitive when referenced by GitHub
.PARAMETER Scope
The GitHub secret scope. Valid values are Repository, Environment, Organization, and User.
Meanings:
- Repository: a repository-level secret for one repository
- Environment: an environment-level secret for one deployment environment in a repository
- Organization: an organization-level secret that can be shared with repositories
- User: an account-level Codespaces secret for the authenticated user
Companion parameter rules:
- Repository: use -Scope Repository; -Repository is optional
- Environment: use -Scope Environment; -Repository and -Environment are required
- Organization: use -Scope Organization; -Organization is required
- User: use -Scope User for a user-level Codespaces secret; -Repository, -Environment, and -Organization are not valid
Use -Scope User instead of a separate user switch. It maps to GitHub's user-level
Codespaces secret behavior.
.PARAMETER Repository
The target repository in OWNER/REPO or HOST/OWNER/REPO format.
Supported with -Scope Repository and -Scope Environment.
Repository secrets are available only to the specified repository. Environment secrets belong
to a specific environment in the specified repository.
When -Scope Repository is used and -Repository is omitted, the function tries to resolve the
current Git repository's origin remote. If that cannot be determined, specify -Repository
explicitly.
Examples:
- octo-org/service-api
- github.example.com/platform/service-api
.PARAMETER Environment
The deployment environment name for environment secrets.
Supported only with -Scope Environment and requires -Repository.
Environment secrets always target GitHub Actions and belong to a single named environment
within the repository. They are intended for jobs that reference that environment.
GitHub environment names:
- Are not case sensitive
- May not exceed 255 characters
- Must be unique within the repository
GitHub REST endpoints require environment names to be URL-encoded. This function handles
that automatically, so names containing spaces or `/` are supported.
.PARAMETER Organization
The target organization for organization secrets.
Supported only with -Scope Organization.
Organization secrets can be shared with repositories in the organization according to the
access policy that was configured when the secret was created or last updated.
.PARAMETER Application
The secret application. Valid values are actions, codespaces, and dependabot.
Meanings:
- actions: the secret is available to GitHub Actions workflows
- codespaces: the secret is available to GitHub Codespaces
- dependabot: the secret is available to Dependabot
If -Application is omitted, the function defaults to:
- Repository scope: actions
- Environment scope: actions
- Organization scope: actions
- User scope: codespaces
Typical combinations:
- Repository scope: actions, codespaces, or dependabot
- Environment scope: actions only
- Organization scope: actions, codespaces, or dependabot
- User scope: codespaces only
Use these patterns:
- Actions secret: omit -Application or specify -Application actions
- Codespaces secret: specify -Application codespaces for repository or organization scope
- Dependabot secret: specify -Application dependabot for repository or organization scope
- User secret: use -Scope User, which always targets Codespaces
When deleting a secret for a non-default application such as dependabot or codespaces,
specify the same -Application value that was used when the secret was created.
.PARAMETER Token
Optional GitHub personal access token as a SecureString.
If supplied, the token is injected only for the outbound `gh` call and is never written to
command output.
When omitted, the function checks the environment variable named by
-TokenEnvironmentVariableName. If the GitHub CLI is installed, its existing authenticated
session can also be used when no token is supplied.
.PARAMETER TokenEnvironmentVariableName
The environment variable name to check for a GitHub token when -Token is not supplied.
Defaults to GH_TOKEN.
This environment variable is read only when -Token is not supplied. Use it when automation
stores the GitHub token under a non-default name such as GITHUB_ADMIN_TOKEN.
.EXAMPLE
PS > Remove-GitHubSecret -Name 'MY_SECRET' -Scope Repository
Removes a repository-level Actions secret from the current Git repository.
.EXAMPLE
PS > Remove-GitHubSecret -Name 'DEVCONTAINER_PAT' -Scope Repository -Repository 'octo-org/service-api' -Application codespaces
Removes a repository-level Codespaces secret.
.EXAMPLE
PS > Remove-GitHubSecret -Name 'DEPLOY_TOKEN' -Scope Environment -Repository 'octo-org/service-api' -Environment 'Production'
Removes an environment Actions secret.
.EXAMPLE
PS > Remove-GitHubSecret -Name 'DEVCONTAINER_PAT' -Scope User -WhatIf
Shows what would happen before deleting a user-level Codespaces secret.
.EXAMPLE
PS > $token = ConvertTo-SecureString $env:GITHUB_ADMIN_TOKEN -AsPlainText -Force
PS > Remove-GitHubSecret -Name 'NUGET_AUTH_TOKEN' -Scope Organization -Organization 'octo-org' -Application dependabot -Token $token
Removes an organization-level Dependabot secret by using an explicit token.
.EXAMPLE
PS > Remove-GitHubSecret -Name 'CODESPACES_BOOTSTRAP' -Scope Organization -Organization 'octo-org' -Application codespaces
Removes an organization-level Codespaces secret.
.OUTPUTS
GitHub.SecretRemoveResult
Returns a summary object with status, target scope, resolved application, transport used,
and whether the secret changed.
.NOTES
-Scope is required for all GitHub helper functions in this module family.
Secret operations require the GitHub CLI (gh) to be installed and available in PATH.
If the secret does not exist, the function returns an AlreadyAbsent result instead of failing.
.LINK
https://cli.github.com/manual/gh_secret_delete
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Developer/Remove-GitHubSecret.ps1
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType([PSCustomObject])]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[ValidateScript({
if ($_ -match '^(?i:GITHUB_)')
{
throw "GitHub secret names cannot start with the 'GITHUB_' prefix."
}
if ($_ -notmatch '^(?![0-9])[A-Za-z0-9_]+$')
{
throw 'GitHub secret names may only contain letters, numbers, or underscores, and cannot start with a number.'
}
$true
})]
[String]$Name,
[Parameter(Mandatory)]
[ValidateSet('Repository', 'Environment', 'Organization', 'User')]
[String]$Scope,
[Parameter()]
[String]$Repository,
[Parameter()]
[ValidateScript({
if ([string]::IsNullOrWhiteSpace($_))
{
throw 'GitHub environment names cannot be empty or whitespace.'
}
if ($_.Length -gt 255)
{
throw 'GitHub environment names may not exceed 255 characters.'
}
$true
})]
[String]$Environment,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$Organization,
[Parameter()]
[ValidateSet('actions', 'codespaces', 'dependabot')]
[String]$Application,
[Parameter()]
[SecureString]$Token,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$TokenEnvironmentVariableName = 'GH_TOKEN'
)
begin
{
function Import-GitHubConfigurationHelpersIfNeeded
{
if (-not (Get-Variable -Name 'PwshProfileGitHubConfigurationHelpers' -Scope Script -ErrorAction SilentlyContinue))
{
$dependencyDirectory = Join-Path -Path $PSScriptRoot -ChildPath 'Private'
$dependencyPath = Join-Path -Path $dependencyDirectory -ChildPath 'GitHubConfigurationHelpers.ps1'
$dependencyPath = [System.IO.Path]::GetFullPath($dependencyPath)
if (-not (Test-Path -LiteralPath $dependencyPath -PathType Leaf))
{
throw "Required dependency could not be found. Expected location: $dependencyPath"
}
try
{
. $dependencyPath
Write-Verbose "Loaded GitHub configuration helpers from: $dependencyPath"
}
catch
{
throw "Failed to load GitHub configuration helpers from '$dependencyPath': $($_.Exception.Message)"
}
}
}
Import-GitHubConfigurationHelpersIfNeeded
$helpers = $script:PwshProfileGitHubConfigurationHelpers
$maxRetryCount = $helpers.DefaultRetryCount
$initialRetryDelaySeconds = $helpers.DefaultInitialRetryDelaySeconds
$resolvedScope = $Scope
switch ($resolvedScope)
{
'Repository'
{
if ($PSBoundParameters.ContainsKey('Environment'))
{
throw 'Use -Scope Environment when specifying -Environment.'
}
if ($PSBoundParameters.ContainsKey('Organization'))
{
throw 'Use -Scope Organization when specifying -Organization.'
}
}
'Environment'
{
if (-not $PSBoundParameters.ContainsKey('Repository'))
{
throw '-Scope Environment requires -Repository.'
}
if (-not $PSBoundParameters.ContainsKey('Environment'))
{
throw '-Scope Environment requires -Environment.'
}
if ($PSBoundParameters.ContainsKey('Organization'))
{
throw '-Scope Environment does not support -Organization.'
}
}
'Organization'
{
if (-not $PSBoundParameters.ContainsKey('Organization'))
{
throw '-Scope Organization requires -Organization.'
}
if ($PSBoundParameters.ContainsKey('Repository'))
{
throw '-Scope Organization does not support -Repository.'
}
if ($PSBoundParameters.ContainsKey('Environment'))
{
throw '-Scope Organization does not support -Environment.'
}
}
'User'
{
if ($PSBoundParameters.ContainsKey('Repository'))
{
throw '-Scope User does not support -Repository.'
}
if ($PSBoundParameters.ContainsKey('Environment'))
{
throw '-Scope User does not support -Environment.'
}
if ($PSBoundParameters.ContainsKey('Organization'))
{
throw '-Scope User does not support -Organization.'
}
}
}
$getSecretContextParams = @{
Scope = $resolvedScope
Repository = $Repository
Environment = $Environment
Organization = $Organization
Application = $Application
}
$secretContext = & $helpers.GetSecretContext @getSecretContextParams
$transport = & $helpers.ResolveTransport
if ($transport.Name -ne 'GhCli')
{
throw 'GitHub secret operations require the GitHub CLI (gh) to be installed and available in PATH.'
}
$resolveAuthContextParams = @{
Token = $Token
TokenEnvironmentVariableName = $TokenEnvironmentVariableName
RequireToken = $false
}
$authContext = & $helpers.ResolveAuthContext @resolveAuthContextParams
$secretPath = & $helpers.GetSingleItemPath -CollectionPath $secretContext.MetadataCollectionPath -Name $Name
$tryGetGitHubResourceParams = @{
Path = $secretPath
BaseUri = $secretContext.ApiBaseUri
Transport = $transport
AuthContext = $authContext
MaxRetryCount = $maxRetryCount
InitialRetryDelaySeconds = $initialRetryDelaySeconds
Activity = "Get GitHub secret $Name"
}
$existingSecret = & $helpers.TryGetGitHubResource @tryGetGitHubResourceParams
}
process
{
if (-not $existingSecret.Found)
{
return & $helpers.NewOperationResult -TypeName 'GitHub.SecretRemoveResult' -Properties @{
Name = $Name
Scope = $secretContext.Scope
Application = $secretContext.Application
Target = $secretContext.DisplayTarget
Status = 'AlreadyAbsent'
Changed = $false
Transport = $transport.Name
Authentication = $authContext.Source
Message = "Secret '$Name' does not exist for $($secretContext.DisplayTarget)."
}
}
if (-not $PSCmdlet.ShouldProcess("$($secretContext.DisplayTarget) :: $Name", 'Remove GitHub secret'))
{
return & $helpers.NewOperationResult -TypeName 'GitHub.SecretRemoveResult' -Properties @{
Name = $Name
Scope = $secretContext.Scope
Application = $secretContext.Application
Target = $secretContext.DisplayTarget
Status = 'WhatIf'
Changed = $false
Transport = $transport.Name
Authentication = $authContext.Source
Message = 'Secret removal skipped by WhatIf.'
}
}
try
{
$ghArguments = @('secret', 'delete', $Name) + $secretContext.GhTargetArguments
$invokeGhCommandParams = @{
Arguments = $ghArguments
AuthContext = $authContext
MaxRetryCount = $maxRetryCount
InitialRetryDelaySeconds = $initialRetryDelaySeconds
Activity = "Remove GitHub secret $Name"
}
$null = & $helpers.InvokeGhCommand @invokeGhCommandParams
return & $helpers.NewOperationResult -TypeName 'GitHub.SecretRemoveResult' -Properties @{
Name = $Name
Scope = $secretContext.Scope
Application = $secretContext.Application
Target = $secretContext.DisplayTarget
Status = 'Removed'
Changed = $true
Transport = $transport.Name
Authentication = $authContext.Source
Message = "Secret '$Name' was removed."
}
}
catch
{
$getFriendlyErrorMessageParams = @{
Operation = 'remove secret'
Name = $Name
Target = $secretContext.DisplayTarget
Exception = $_.Exception
}
$friendlyMessage = & $helpers.GetFriendlyErrorMessage @getFriendlyErrorMessageParams
throw $friendlyMessage
}
}
}