From fbcb065cc15d8ea4f4fa1f46dbae98b26f492399 Mon Sep 17 00:00:00 2001 From: Merill Fernando Date: Wed, 11 Nov 2026 17:14:04 +1100 Subject: [PATCH] feat(packs): reach a private pack without a token in an environment variable Install-MtPack, Update-MtPack and Restore-MtPack take -Token (SecureString). Without it, Invoke-MtPackGitHubRequest uses the first sign-in that can read the repository: the Connect-MtGitHub session (github.com only), gh auth token, then GH_TOKEN and GITHUB_TOKEN. No token is stored, and it is sent to api.github.com only. Refs #2790 Co-Authored-By: Claude Fable 5.2 --- .../internal/packs/Get-MtGitHubCliToken.ps1 | 25 ++ .../internal/packs/Get-MtPackCandidate.ps1 | 11 +- .../internal/packs/Get-MtPackGitHubToken.ps1 | 50 +++ .../packs/Invoke-MtPackGitHubRequest.ps1 | 127 +++++-- .../internal/packs/Invoke-MtPackInstall.ps1 | 11 +- .../packs/Resolve-MtPackGitHubSource.ps1 | 27 +- powershell/public/packs/Install-MtPack.ps1 | 22 +- powershell/public/packs/Restore-MtPack.ps1 | 22 +- powershell/public/packs/Update-MtPack.ps1 | 22 +- .../Invoke-MtPackGitHubRequest.Tests.ps1 | 316 ++++++++++++++++++ website/docs/packs/index.md | 58 ++++ 11 files changed, 642 insertions(+), 49 deletions(-) create mode 100644 powershell/internal/packs/Get-MtGitHubCliToken.ps1 create mode 100644 powershell/internal/packs/Get-MtPackGitHubToken.ps1 create mode 100644 powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 diff --git a/powershell/internal/packs/Get-MtGitHubCliToken.ps1 b/powershell/internal/packs/Get-MtGitHubCliToken.ps1 new file mode 100644 index 0000000000..105c870d0f --- /dev/null +++ b/powershell/internal/packs/Get-MtGitHubCliToken.ps1 @@ -0,0 +1,25 @@ +function Get-MtGitHubCliToken { + <# + .SYNOPSIS + Asks the GitHub CLI for the github.com token it is signed in with, or returns $null. + + .DESCRIPTION + Runs `gh auth token --hostname github.com` when the gh CLI is installed. The token is returned to the + caller and kept nowhere. Returns $null when gh is not installed, is not signed in, or fails. + #> + [CmdletBinding()] + [OutputType([string])] + param() + + $gh = Get-Command -Name 'gh' -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 + if (-not $gh) { return $null } + try { + $output = & $gh.Source auth token --hostname github.com 2>$null + if ($LASTEXITCODE -ne 0) { return $null } + $token = ([string]($output | Select-Object -First 1)).Trim() + if ($token) { $token } else { $null } + } catch { + Write-Debug "gh auth token failed: $_" + $null + } +} diff --git a/powershell/internal/packs/Get-MtPackCandidate.ps1 b/powershell/internal/packs/Get-MtPackCandidate.ps1 index 18cd76f3d9..4a3d2d0b1b 100644 --- a/powershell/internal/packs/Get-MtPackCandidate.ps1 +++ b/powershell/internal/packs/Get-MtPackCandidate.ps1 @@ -28,9 +28,14 @@ # Fetch exactly this commit (Restore-MtPack), skipping release selection. [Parameter()] [string] $Commit, # The branch or tag the commit was installed from (the lock file's Ref), which it may be reachable from. - [Parameter()] [string] $CommitRef + [Parameter()] [string] $CommitRef, + # The -Token of the pack command, passed on to Invoke-MtPackGitHubRequest. + [Parameter()] [AllowNull()] [securestring] $Token ) + $auth = @{} + if ($Token) { $auth.Token = $Token } + # Staging is only a temporary copy to read, so it happens under -WhatIf too. $WhatIfPreference = $false $parsed = ConvertFrom-MtPackSource -Source $Source @@ -52,14 +57,14 @@ } 'GitHub' { if ($Commit) { $parsed.Ref = $Commit } - $resolved = Resolve-MtPackGitHubSource -Source $parsed -Version $Version -IncludeRecent:$IncludeRecent -CooldownHours $CooldownHours -ReachableFrom @($CommitRef | Where-Object { $_ }) + $resolved = Resolve-MtPackGitHubSource @auth -Source $parsed -Version $Version -IncludeRecent:$IncludeRecent -CooldownHours $CooldownHours -ReachableFrom @($CommitRef | Where-Object { $_ }) foreach ($p in $resolved.PSObject.Properties) { if ($candidate.Contains($p.Name)) { $candidate[$p.Name] = $p.Value } } $candidate.Source = "github.com/$($resolved.Owner)/$($resolved.Repo)$(if ($resolved.Folder) { "/$($resolved.Folder)" })" $staging = Join-Path ([System.IO.Path]::GetTempPath()) "maester-pack-$([guid]::NewGuid().ToString('n'))" $candidate.Staging = $staging $null = New-Item -Path $staging -ItemType Directory -Force $archive = Join-Path $staging 'archive.zip' - $null = Invoke-MtPackGitHubRequest -Path "repos/$($resolved.Owner)/$($resolved.Repo)/zipball/$($resolved.Commit)" -OutFile $archive + $null = Invoke-MtPackGitHubRequest @auth -Path "repos/$($resolved.Owner)/$($resolved.Repo)/zipball/$($resolved.Commit)" -OutFile $archive $root = Join-Path $staging 'pack' Expand-MtPackArchive -Archive $archive -Destination $root -Folder $resolved.Folder Remove-Item -LiteralPath $archive -Force diff --git a/powershell/internal/packs/Get-MtPackGitHubToken.ps1 b/powershell/internal/packs/Get-MtPackGitHubToken.ps1 new file mode 100644 index 0000000000..2d1c168205 --- /dev/null +++ b/powershell/internal/packs/Get-MtPackGitHubToken.ps1 @@ -0,0 +1,50 @@ +function Get-MtPackGitHubToken { + <# + .SYNOPSIS + Reads the GitHub token one source holds for pack installs, or returns $null. + + .DESCRIPTION + Invoke-MtPackGitHubRequest asks each source in turn, at the moment of a request, and keeps none of the + answers: + + - Parameter: the -Token a person passed to Install-MtPack, Update-MtPack or Restore-MtPack. + - Session: the sign-in Connect-MtGitHub holds for this session. It is used only when that connection is + to https://api.github.com and has not expired, so a token for a GitHub Enterprise Cloud tenant with + data residency (api..ghe.com) is never sent to github.com. + - GitHubCli: `gh auth token`, when the gh CLI is installed and signed in (Get-MtGitHubCliToken). + - GH_TOKEN and GITHUB_TOKEN: the environment variables, which pipelines set. + #> + [CmdletBinding()] + [OutputType([string])] + param( + [Parameter(Mandatory)] + [ValidateSet('Parameter', 'Session', 'GitHubCli', 'GH_TOKEN', 'GITHUB_TOKEN')] + [string] $Source, + + # The -Token parameter of the pack command, for the Parameter source. + [Parameter()] + [AllowNull()] + [securestring] $Token + ) + + switch ($Source) { + 'Parameter' { + if (-not $Token -or $Token.Length -eq 0) { return $null } + return [System.Net.NetworkCredential]::new('', $Token).Password + } + 'Session' { + $connection = $__MtSession.GitHubConnection + $header = $__MtSession.GitHubAuthHeader + if (-not $connection -or -not $connection.Connected -or $header -isnot [System.Collections.IDictionary]) { return $null } + if ([string]$connection.ApiBaseUri -ne 'https://api.github.com') { return $null } + $expires = if ($connection.PSObject.Properties['TokenExpiresAt']) { $connection.TokenExpiresAt } + if ($expires -and ([datetimeoffset]$expires) -le [datetimeoffset]::UtcNow) { return $null } + $value = [string]$header['Authorization'] + if ($value -match '^Bearer\s+(\S+)$') { return $Matches[1] } + return $null + } + 'GitHubCli' { return Get-MtGitHubCliToken } + 'GH_TOKEN' { if ($env:GH_TOKEN) { return $env:GH_TOKEN } return $null } + 'GITHUB_TOKEN' { if ($env:GITHUB_TOKEN) { return $env:GITHUB_TOKEN } return $null } + } +} diff --git a/powershell/internal/packs/Invoke-MtPackGitHubRequest.ps1 b/powershell/internal/packs/Invoke-MtPackGitHubRequest.ps1 index ee11367914..07b59f0163 100644 --- a/powershell/internal/packs/Invoke-MtPackGitHubRequest.ps1 +++ b/powershell/internal/packs/Invoke-MtPackGitHubRequest.ps1 @@ -4,15 +4,28 @@ Calls the GitHub REST API for pack installs (Maester Packs design, section 5.1). .DESCRIPTION - Sends GH_TOKEN or GITHUB_TOKEN, when one is set, to api.github.com only: a private repository needs it, - and it lifts GitHub's limit of 60 anonymous requests an hour. With -OutFile the response is saved to a - file (an archive download); GitHub redirects those to codeload.github.com, which receives no token. + A private repository needs a GitHub token, and a token lifts GitHub's limit of 60 anonymous requests an + hour. The token is read at the moment of the request (Get-MtPackGitHubToken), is sent to api.github.com + only, and is kept nowhere. With -OutFile the response is saved to a file (an archive download); GitHub + redirects those to codeload.github.com, which receives no token. + + With -Token, that token is the only one used. Without it, each source is asked in turn and the first + that can read the repository is used: + + 1. the sign-in Connect-MtGitHub holds for this session, when it is to github.com; + 2. `gh auth token`, when the GitHub CLI is installed and signed in; + 3. the GH_TOKEN environment variable, then GITHUB_TOKEN; + 4. no token. + + A source GitHub answers with 401, 403 or 404 is passed over for the next one, since GitHub answers 404 + for a private repository a token cannot read. Only the name of the source that worked is remembered, for + each repository, so later requests start with it. Returns the parsed JSON, or $null for 404 with -AllowNotFound. Other failures throw with GitHub's message and, when the rate limit was reached, how to raise it. -UsedToken says whether a token was sent. #> [CmdletBinding()] - [OutputType([object], [string])] + [OutputType([object], [object[]], [string])] param( # The path after https://api.github.com/, such as 'repos/contoso/pack'. [Parameter(Mandatory)] @@ -28,35 +41,93 @@ # Receives whether a token was sent (a [ref] to a bool). [Parameter()] - [ref] $UsedToken + [ref] $UsedToken, + + # The -Token of the pack command. When given, no other source is asked. + [Parameter()] + [AllowNull()] + [securestring] $Token ) - $token = if ($env:GH_TOKEN) { $env:GH_TOKEN } elseif ($env:GITHUB_TOKEN) { $env:GITHUB_TOKEN } else { $null } - if ($UsedToken) { $UsedToken.Value = [bool]$token } - $headers = @{ Accept = 'application/vnd.github+json'; 'X-GitHub-Api-Version' = '2022-11-28'; 'User-Agent' = 'Maester' } - if ($token) { $headers['Authorization'] = "Bearer $token" } $uri = "https://api.github.com/$($Path.TrimStart('/'))" - try { - if ($OutFile) { - $null = Invoke-WebRequest -Uri $uri -Headers $headers -OutFile $OutFile -MaximumRedirection 5 -ErrorAction Stop - return $OutFile + $labels = @{ + Parameter = 'the -Token parameter' + Session = 'the Connect-MtGitHub sign-in' + GitHubCli = 'the GitHub CLI (gh auth token)' + GH_TOKEN = 'GH_TOKEN' + GITHUB_TOKEN = 'GITHUB_TOKEN' + } + $explicit = $Token -and $Token.Length -gt 0 + $order = if ($explicit) { @('Parameter') } else { @('Session', 'GitHubCli', 'GH_TOKEN', 'GITHUB_TOKEN', 'Anonymous') } + + # The source that last read this repository goes first. Its name is remembered, never its token. + if (-not $script:__MtPackGitHubTokenSource) { $script:__MtPackGitHubTokenSource = @{} } + $repository = if ($Path.TrimStart('/') -match '^repos/([^/?]+/[^/?]+)') { $Matches[1].ToLowerInvariant() } + $proven = if (-not $explicit -and $repository) { $script:__MtPackGitHubTokenSource[$repository] } + if ($proven) { $order = @($proven) + @($order | Where-Object { $_ -ne $proven }) } + + $sent = [System.Collections.Generic.List[string]]::new() + $tried = [System.Collections.Generic.List[string]]::new() + $failure = $null + foreach ($source in $order) { + $value = $null + if ($source -ne 'Anonymous') { + $value = Get-MtPackGitHubToken -Source $source -Token $Token + # gh answers with GH_TOKEN when it is set, so the same token is not sent twice. + if (-not $value -or $sent.Contains($value)) { continue } + $sent.Add($value) + $tried.Add($labels[$source]) } - Invoke-RestMethod -Uri $uri -Headers $headers -ErrorAction Stop - } catch { - $status = $null - if ($_.Exception.PSObject.Properties['Response'] -and $_.Exception.Response) { $status = [int]$_.Exception.Response.StatusCode } - if ($status -eq 404 -and $AllowNotFound) { return $null } - $message = $_.ErrorDetails.Message - try { if ($message) { $message = ($message | ConvertFrom-Json -ErrorAction Stop).message } } catch { Write-Debug $_ } - if (-not $message) { $message = $_.Exception.Message } - if ($status -in 403, 429 -and $message -match 'rate limit') { - $hint = if ($token) { 'Wait for the limit to reset.' } else { 'Set GH_TOKEN (for example from `gh auth token`) to raise the limit.' } - throw "GitHub's API rate limit was reached while reading $uri. $hint" + $headers = @{ Accept = 'application/vnd.github+json'; 'X-GitHub-Api-Version' = '2022-11-28'; 'User-Agent' = 'Maester' } + if ($value) { $headers['Authorization'] = "Bearer $value" } + try { + if ($OutFile) { + $null = Invoke-WebRequest -Uri $uri -Headers $headers -OutFile $OutFile -MaximumRedirection 5 -ErrorAction Stop + $result = $OutFile + } else { + $result = Invoke-RestMethod -Uri $uri -Headers $headers -ErrorAction Stop + } + } catch { + $status = $null + if ($_.Exception.PSObject.Properties['Response'] -and $_.Exception.Response) { $status = [int]$_.Exception.Response.StatusCode } + $message = $_.ErrorDetails.Message + try { if ($message) { $message = ($message | ConvertFrom-Json -ErrorAction Stop).message } } catch { Write-Debug $_ } + if (-not $message) { $message = $_.Exception.Message } + if ($status -in 403, 429 -and $message -match 'rate limit') { + $hint = if ($value) { 'Wait for the limit to reset.' } else { 'Sign in with `gh auth login` or Connect-MtGitHub, or pass -Token, to raise the limit.' } + throw "GitHub's API rate limit was reached while reading $uri. $hint" + } + $failure = [pscustomobject]@{ Status = $status; Message = $message } + # The source that already read this repository says a path is missing: no other source will find it. + if ($status -eq 404 -and $source -eq $proven) { break } + if ($status -in 401, 403, 404) { + if ($source -eq $proven) { $script:__MtPackGitHubTokenSource.Remove($repository) } + Write-Verbose "GitHub answered $status for $uri with $(if ($value) { $labels[$source] } else { 'no token' })." + continue + } + break + } finally { + $headers.Remove('Authorization') + $value = $null } - if ($status -eq 404) { - $hint = if ($token) { '' } else { ' If it is a private repository, set GH_TOKEN to a token that can read it.' } - throw "GitHub returned 404 Not Found for $uri.$hint" + if ($UsedToken) { $UsedToken.Value = $source -ne 'Anonymous' } + if (-not $explicit -and $repository) { $script:__MtPackGitHubTokenSource[$repository] = $source } + Write-Verbose "Read $uri with $(if ($source -eq 'Anonymous') { 'no token' } else { $labels[$source] })." + if ($result -is [array]) { return , $result } + return $result + } + + if ($UsedToken) { $UsedToken.Value = $tried.Count -gt 0 } + if ($failure.Status -eq 404) { + if ($AllowNotFound) { return $null } + $hint = if ($explicit) { + ' It does not exist, or the token passed to -Token cannot read it. A fine-grained token needs read-only Contents access to the repository.' + } elseif ($tried.Count -gt 0) { + " It does not exist, or it is a private repository that $($tried -join ' and ') cannot read. Pass -Token with a token that can read it." + } else { + ' If it is a private repository, sign in with `gh auth login`, or pass -Token with a token that can read it.' } - throw "GitHub request $uri failed$(if ($status) { " ($status)" }): $message" + throw "GitHub returned 404 Not Found for $uri.$hint" } + throw "GitHub request $uri failed$(if ($failure.Status) { " ($($failure.Status))" }): $($failure.Message)" } diff --git a/powershell/internal/packs/Invoke-MtPackInstall.ps1 b/powershell/internal/packs/Invoke-MtPackInstall.ps1 index d73c8d4fd8..988fb591e3 100644 --- a/powershell/internal/packs/Invoke-MtPackInstall.ps1 +++ b/powershell/internal/packs/Invoke-MtPackInstall.ps1 @@ -38,10 +38,15 @@ # The pack IDs being installed above this one, to refuse cycles. [Parameter()] [string[]] $Stack = @(), # Return -Previous when the fetched files have its digest, without asking again. - [Parameter()] [switch] $SkipUnchanged + [Parameter()] [switch] $SkipUnchanged, + # The -Token of the pack command, used for this pack and the packs it requires. + [Parameter()] [AllowNull()] [securestring] $Token ) - $candidate = Get-MtPackCandidate -Source $Source -Version $Version -IncludeRecent:$IncludeRecent -CooldownHours $Policy.CooldownHours + $auth = @{} + if ($Token) { $auth.Token = $Token } + + $candidate = Get-MtPackCandidate @auth -Source $Source -Version $Version -IncludeRecent:$IncludeRecent -CooldownHours $Policy.CooldownHours try { if ($candidate.Errors.Count -gt 0) { $list = @($candidate.Errors | Select-Object -First 15 | ForEach-Object { " $($_.Code): $(ConvertTo-MtPackSafeText $_.Message -MaxLength 300)" }) -join [Environment]::NewLine @@ -77,7 +82,7 @@ } if (-not ($r.PSObject.Properties['Source'] -and $r.Source)) { throw "The pack $id requires $($r.Id), which is not installed and has no Source to install it from." } Write-Host "The pack $id requires $($r.Id)$(if ($range) { " $range" }); installing it first." - $entry = Invoke-MtPackInstall -Source $r.Source -Lock $Lock -Policy $Policy -AcceptRisk $AcceptRisk -IncludeRecent:$IncludeRecent -Force:$Force -NonInteractive:$NonInteractive -Stack @($Stack + $id) + $entry = Invoke-MtPackInstall @auth -Source $r.Source -Lock $Lock -Policy $Policy -AcceptRisk $AcceptRisk -IncludeRecent:$IncludeRecent -Force:$Force -NonInteractive:$NonInteractive -Stack @($Stack + $id) if ($entry.Id -ne $r.Id) { throw "The pack $id requires $($r.Id) from $($r.Source), but that source holds $($entry.Id)." } if ($range -and -not (Test-MtVersionRange -Range $range -Version $entry.Version)) { throw "The pack $id requires $($r.Id) $range, but $($r.Source) has version $($entry.Version)." } foreach ($s in @($entry.Services)) { $requiredServices.Add($s) } diff --git a/powershell/internal/packs/Resolve-MtPackGitHubSource.ps1 b/powershell/internal/packs/Resolve-MtPackGitHubSource.ps1 index 5cbaf73b44..a9c4b8ec76 100644 --- a/powershell/internal/packs/Resolve-MtPackGitHubSource.ps1 +++ b/powershell/internal/packs/Resolve-MtPackGitHubSource.ps1 @@ -35,12 +35,17 @@ [Parameter()] [int] $CooldownHours = 72, # More branches or tags a requested commit may be reachable from, such as the ref in the lock file. # Each must be a real branch or tag of the repository; a commit ID is ignored. - [Parameter()] [string[]] $ReachableFrom = @() + [Parameter()] [string[]] $ReachableFrom = @(), + # The -Token of the pack command, passed on to Invoke-MtPackGitHubRequest. + [Parameter()] [AllowNull()] [securestring] $Token ) + $auth = @{} + if ($Token) { $auth.Token = $Token } + $usedToken = $false $repoPath = "repos/$($Source.Owner)/$($Source.Repo)" - $repository = Invoke-MtPackGitHubRequest -Path $repoPath -UsedToken ([ref] $usedToken) + $repository = Invoke-MtPackGitHubRequest @auth -Path $repoPath -UsedToken ([ref] $usedToken) $owner = $Source.Owner $repo = $Source.Repo if ($repository.full_name -and $repository.full_name -ne "$owner/$repo") { @@ -52,12 +57,12 @@ # Invoke-RestMethod writes an empty JSON array as one empty array, not as nothing: enumerate it, so that a # repository without releases has none and installs from its default branch. - $releases = @(Invoke-MtPackGitHubRequest -Path "$repoPath/releases?per_page=30" | ForEach-Object { $_ } | + $releases = @(Invoke-MtPackGitHubRequest @auth -Path "$repoPath/releases?per_page=30" | ForEach-Object { $_ } | Where-Object { $_ -and $_.tag_name -and -not $_.draft -and -not $_.prerelease } | Sort-Object { [datetime]$_.published_at } -Descending) $commitOf = { param($ref) - $commit = Invoke-MtPackGitHubRequest -Path "$repoPath/commits/$([uri]::EscapeDataString($ref))" + $commit = Invoke-MtPackGitHubRequest @auth -Path "$repoPath/commits/$([uri]::EscapeDataString($ref))" [string]$commit.sha } $ageOf = { @@ -77,7 +82,7 @@ @($name.Substring(5)) } else { @("heads/$name", "tags/$name") } foreach ($n in $names) { - $candidateRef = Invoke-MtPackGitHubRequest -Path "$repoPath/git/ref/$([uri]::EscapeDataString($n))" -AllowNotFound + $candidateRef = Invoke-MtPackGitHubRequest @auth -Path "$repoPath/git/ref/$([uri]::EscapeDataString($n))" -AllowNotFound if ($candidateRef -and $candidateRef.ref -ceq "refs/$n") { return $candidateRef } } $null @@ -88,7 +93,7 @@ $newer = $null if ($requested) { $release = $releases | Where-Object { $_.tag_name -eq $requested -or $_.tag_name -eq "v$requested" -or "v$($_.tag_name)" -eq $requested } | Select-Object -First 1 - if (-not $release) { $release = Invoke-MtPackGitHubRequest -Path "$repoPath/releases/tags/$([uri]::EscapeDataString($requested))" -AllowNotFound } + if (-not $release) { $release = Invoke-MtPackGitHubRequest @auth -Path "$repoPath/releases/tags/$([uri]::EscapeDataString($requested))" -AllowNotFound } $ref = if ($release) { [string]$release.tag_name } else { $requested } if ($release) { $commit = & $commitOf "refs/tags/$ref" } else { @@ -106,7 +111,7 @@ if ($extraRef) { $refs += [string]$extraRef.ref } } $refs += @($releases | ForEach-Object { "refs/tags/$($_.tag_name)" }) - if (-not (Test-MtPackCommitReachable -RepositoryPath $repoPath -Commit $commit -Ref $refs)) { + if (-not (Test-MtPackCommitReachable @auth -RepositoryPath $repoPath -Commit $commit -Ref $refs)) { throw "The commit $commit is not on the default branch, a release$(if (@($ReachableFrom | Where-Object { $_ }).Count -gt 0) { ' or the branch or tag it was installed from' }) of $owner/$repo. GitHub serves commits from forks under the original repository's name, so Maester installs only commits that belong to the repository itself." } $release = $releases | Where-Object { $_.target_commitish -eq $commit } | Select-Object -First 1 @@ -168,11 +173,15 @@ function Test-MtPackCommitReachable { param( [Parameter(Mandatory)] [string] $RepositoryPath, [Parameter(Mandatory)] [string] $Commit, - [Parameter(Mandatory)] [AllowEmptyCollection()] [string[]] $Ref + [Parameter(Mandatory)] [AllowEmptyCollection()] [string[]] $Ref, + [Parameter()] [AllowNull()] [securestring] $Token ) + $auth = @{} + if ($Token) { $auth.Token = $Token } + foreach ($r in ($Ref | Where-Object { $_ } | Select-Object -Unique -First 10)) { - $compare = Invoke-MtPackGitHubRequest -Path "$RepositoryPath/compare/$([uri]::EscapeDataString($r))...$Commit" -AllowNotFound + $compare = Invoke-MtPackGitHubRequest @auth -Path "$RepositoryPath/compare/$([uri]::EscapeDataString($r))...$Commit" -AllowNotFound # 'behind' or 'identical': the commit is an ancestor of the ref, or is the ref. if ($compare -and $compare.status -in 'behind', 'identical') { return $true } } diff --git a/powershell/public/packs/Install-MtPack.ps1 b/powershell/public/packs/Install-MtPack.ps1 index a21d673d67..599c95e350 100644 --- a/powershell/public/packs/Install-MtPack.ps1 +++ b/powershell/public/packs/Install-MtPack.ps1 @@ -57,6 +57,16 @@ Answer yes to the approval question. A pack of High risk, or one newer than the cooldown, still needs its code typed unless -AcceptRisk and -IncludeRecent cover it. + .PARAMETER Token + A GitHub token that can read the pack's repository, as a SecureString, for a private or internal + repository. It is used for this command only, is sent to api.github.com only, and is not stored. When it + is given, no other sign-in is tried. Without it, Maester uses the first of these that can read the + repository: the sign-in of Connect-MtGitHub in this session, the GitHub CLI (`gh auth token`, when gh is + installed and signed in), then the GH_TOKEN and GITHUB_TOKEN environment variables. An environment + variable can be read by every process and module in the session, including installed packs, so prefer + the other ways outside a pipeline. A fine-grained token needs only read-only Contents access to the + one repository. + .PARAMETER PassThru Return the lock file entry of the pack. Without it, Install-MtPack writes what was installed and how to run it, and returns nothing. @@ -82,6 +92,11 @@ Installs the demo pack, then runs only its tests, by the pack's code. + .EXAMPLE + Install-MtPack contoso/private-pack -Token (Get-Secret -Name PackReadToken) + + Installs a pack from a private repository with a token read from a secret vault. + .LINK https://maester.dev/docs/commands/Install-MtPack #> @@ -115,7 +130,10 @@ [switch] $Force, [Parameter()] - [switch] $PassThru + [switch] $PassThru, + + [Parameter()] + [securestring] $Token ) $where = Resolve-MtPackLockPath -PackLock $PackLock -Scope $Scope -Path (Get-Location).Path @@ -125,7 +143,7 @@ $quiet = $NonInteractive -or (Test-MtPackNonInteractive) $before = @($lock.Packs | ForEach-Object { $_.Id }) - $entry = Invoke-MtPackInstall -Source $Source -Version $Version -Lock $lock -Policy $policy -AcceptRisk $AcceptRisk -IncludeRecent:$IncludeRecent -Force:$Force -NonInteractive:$quiet -Previous ($lock.Packs | Where-Object { $_.Id -eq $Source -or $_.Source -eq $Source } | Select-Object -First 1) + $entry = Invoke-MtPackInstall -Token $Token -Source $Source -Version $Version -Lock $lock -Policy $policy -AcceptRisk $AcceptRisk -IncludeRecent:$IncludeRecent -Force:$Force -NonInteractive:$quiet -Previous ($lock.Packs | Where-Object { $_.Id -eq $Source -or $_.Source -eq $Source } | Select-Object -First 1) if (-not $entry) { return } Write-MtPackLock -Path $where.LockPath -Lock $lock diff --git a/powershell/public/packs/Restore-MtPack.ps1 b/powershell/public/packs/Restore-MtPack.ps1 index bfe20073e4..bf7b920c7c 100644 --- a/powershell/public/packs/Restore-MtPack.ps1 +++ b/powershell/public/packs/Restore-MtPack.ps1 @@ -20,6 +20,16 @@ The lock file to restore from. By default the one beside the Maester config found from the current folder, else the user's. + .PARAMETER Token + A GitHub token that can read the pack's repository, as a SecureString, for a private or internal + repository. It is used for this command only, is sent to api.github.com only, and is not stored. When it + is given, no other sign-in is tried. Without it, Maester uses the first of these that can read the + repository: the sign-in of Connect-MtGitHub in this session, the GitHub CLI (`gh auth token`, when gh is + installed and signed in), then the GH_TOKEN and GITHUB_TOKEN environment variables. An environment + variable can be read by every process and module in the session, including installed packs, so prefer + the other ways outside a pipeline. A fine-grained token needs only read-only Contents access to the + one repository. + .EXAMPLE Restore-MtPack @@ -30,6 +40,11 @@ Restores, then runs, in a pipeline. + .EXAMPLE + Restore-MtPack -Token (Get-Secret -Name PackReadToken) + + Restores packs from private repositories with a token read from a secret vault. + .LINK https://maester.dev/docs/commands/Restore-MtPack #> @@ -40,7 +55,10 @@ [string[]] $Id, [Parameter()] - [string] $PackLock + [string] $PackLock, + + [Parameter()] + [securestring] $Token ) Write-Verbose "Restore-MtPack: Id=$($Id -join ', ') PackLock=$PackLock" @@ -61,7 +79,7 @@ $Present = $false if (Test-Path -LiteralPath $Target -PathType Container) { $Present = (Get-MtPackDigest -Path $Target).Digest -eq $Entry.Digest } if (-not $Present -and $PSCmdlet.ShouldProcess("$($Entry.Id) $($Entry.Version)", 'Restore the pack')) { - $Candidate = Get-MtPackCandidate -Source $Entry.Source -Commit $Entry.Commit -CommitRef $(if ($Entry.PSObject.Properties['Ref']) { [string]$Entry.Ref }) -IncludeRecent + $Candidate = Get-MtPackCandidate -Token $Token -Source $Entry.Source -Commit $Entry.Commit -CommitRef $(if ($Entry.PSObject.Properties['Ref']) { [string]$Entry.Ref }) -IncludeRecent if ($Candidate.Digest -ne $Entry.Digest) { throw "its files are not the approved files (digest $($Candidate.Digest), the lock file has $($Entry.Digest))." } $null = Save-MtPackToStore -Path $Candidate.Root -Digest $Entry.Digest $Status = 'Restored' diff --git a/powershell/public/packs/Update-MtPack.ps1 b/powershell/public/packs/Update-MtPack.ps1 index 6cf0220fd3..f2c5590f9a 100644 --- a/powershell/public/packs/Update-MtPack.ps1 +++ b/powershell/public/packs/Update-MtPack.ps1 @@ -31,6 +31,16 @@ .PARAMETER Force Answer yes to the approval question. + .PARAMETER Token + A GitHub token that can read the pack's repository, as a SecureString, for a private or internal + repository. It is used for this command only, is sent to api.github.com only, and is not stored. When it + is given, no other sign-in is tried. Without it, Maester uses the first of these that can read the + repository: the sign-in of Connect-MtGitHub in this session, the GitHub CLI (`gh auth token`, when gh is + installed and signed in), then the GH_TOKEN and GITHUB_TOKEN environment variables. An environment + variable can be read by every process and module in the session, including installed packs, so prefer + the other ways outside a pipeline. A fine-grained token needs only read-only Contents access to the + one repository. + .EXAMPLE Update-MtPack @@ -41,6 +51,11 @@ Updates one pack to a given release. + .EXAMPLE + Update-MtPack contoso.private-pack -Token (Get-Secret -Name PackReadToken) + + Updates a pack from a private repository with a token read from a secret vault. + .LINK https://maester.dev/docs/commands/Update-MtPack #> @@ -67,7 +82,10 @@ [switch] $NonInteractive, [Parameter()] - [switch] $Force + [switch] $Force, + + [Parameter()] + [securestring] $Token ) Write-Verbose "Update-MtPack: Id=$($Id -join ', ') Version=$Version" @@ -86,7 +104,7 @@ $source = ($previous.Source -replace '@[^/@]+$', '') if (-not $PSCmdlet.ShouldProcess($previous.Id, 'Update the pack')) { continue } try { - $entry = Invoke-MtPackInstall -Source $source -Version $Version -Lock $lock -Policy $policy -AcceptRisk $AcceptRisk -IncludeRecent:$IncludeRecent -Force:$Force -NonInteractive:$quiet -Previous $previous -SkipUnchanged -Confirm:$false + $entry = Invoke-MtPackInstall -Token $Token -Source $source -Version $Version -Lock $lock -Policy $policy -AcceptRisk $AcceptRisk -IncludeRecent:$IncludeRecent -Force:$Force -NonInteractive:$quiet -Previous $previous -SkipUnchanged -Confirm:$false } catch { Write-Error "The pack $($previous.Id) was not updated: $($_.Exception.Message)" continue diff --git a/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 b/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 new file mode 100644 index 0000000000..edba6469da --- /dev/null +++ b/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 @@ -0,0 +1,316 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../../Maester.psd1" -Force + + $script:savedGhToken = $env:GH_TOKEN + $script:savedGitHubToken = $env:GITHUB_TOKEN +} + +AfterAll { + $env:GH_TOKEN = $script:savedGhToken + $env:GITHUB_TOKEN = $script:savedGitHubToken + InModuleScope Maester { + $__MtSession.GitHubConnection = $null + $__MtSession.GitHubAuthHeader = $null + $script:__MtPackGitHubTokenSource = $null + } +} + +Describe 'Pack GitHub token: where the token comes from' { + BeforeEach { + $env:GH_TOKEN = $null + $env:GITHUB_TOKEN = $null + InModuleScope Maester { + $__MtSession.GitHubConnection = $null + $__MtSession.GitHubAuthHeader = $null + $script:__MtPackGitHubTokenSource = $null + $script:calls = [System.Collections.Generic.List[object]]::new() + # Tokens GitHub answers 404 for, as it does for a private repository a token cannot read. + $script:cannotRead = @() + $script:anonymousCanRead = $false + + function script:Set-TestGitHubSession { + param([string] $Token = 'session-token', [string] $ApiBaseUri = 'https://api.github.com', $ExpiresAt) + $__MtSession.GitHubAuthHeader = @{ Authorization = "Bearer $Token"; Accept = 'application/vnd.github+json' } + $__MtSession.GitHubConnection = [pscustomobject]@{ Connected = $true; ApiBaseUri = $ApiBaseUri; TokenExpiresAt = $ExpiresAt } + } + + Mock Get-MtGitHubCliToken { $null } + Mock Invoke-RestMethod { + $sent = if ($Headers.ContainsKey('Authorization')) { $Headers['Authorization'] -replace '^Bearer ' } else { $null } + $script:calls.Add([pscustomobject]@{ Uri = [string]$Uri; Token = $sent }) + $refused = if ($sent) { $sent -in $script:cannotRead } else { -not $script:anonymousCanRead } + if ($refused) { + $response = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::NotFound) + throw [Microsoft.PowerShell.Commands.HttpResponseException]::new('Not Found', $response) + } + [pscustomobject]@{ full_name = 'contoso/pack' } + } + } + } + + It 'uses -Token before everything else, and nothing else with it' { + $env:GH_TOKEN = 'env-gh' + $env:GITHUB_TOKEN = 'env-github' + InModuleScope Maester { + Set-TestGitHubSession + Mock Get-MtGitHubCliToken { 'cli-token' } + $used = $false + $token = [System.Net.NetworkCredential]::new('', 'parameter-token').SecurePassword + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' -Token $token -UsedToken ([ref] $used) + + $used | Should -BeTrue + $script:calls.Token | Should -Be @('parameter-token') + Should -Invoke Get-MtGitHubCliToken -Times 0 + } + } + + It 'does not fall back to another sign-in when -Token cannot read the repository' { + $env:GH_TOKEN = 'env-gh' + InModuleScope Maester { + Set-TestGitHubSession + Mock Get-MtGitHubCliToken { 'cli-token' } + $script:cannotRead = @('parameter-token') + $token = [System.Net.NetworkCredential]::new('', 'parameter-token').SecurePassword + + { Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' -Token $token } | Should -Throw '*404*-Token*' + + $script:calls.Token | Should -Be @('parameter-token') + } + } + + It 'uses the Connect-MtGitHub sign-in before the GitHub CLI and the environment' { + $env:GH_TOKEN = 'env-gh' + $env:GITHUB_TOKEN = 'env-github' + InModuleScope Maester { + Set-TestGitHubSession + Mock Get-MtGitHubCliToken { 'cli-token' } + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + + $script:calls.Token | Should -Be @('session-token') + Should -Invoke Get-MtGitHubCliToken -Times 0 + } + } + + It 'uses the GitHub CLI before the environment when the session has no sign-in' { + $env:GH_TOKEN = 'env-gh' + $env:GITHUB_TOKEN = 'env-github' + InModuleScope Maester { + Mock Get-MtGitHubCliToken { 'cli-token' } + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + + $script:calls.Token | Should -Be @('cli-token') + } + } + + It 'uses GH_TOKEN before GITHUB_TOKEN' { + $env:GH_TOKEN = 'env-gh' + $env:GITHUB_TOKEN = 'env-github' + InModuleScope Maester { + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + $script:calls.Token | Should -Be @('env-gh') + } + } + + It 'uses GITHUB_TOKEN last' { + $env:GITHUB_TOKEN = 'env-github' + InModuleScope Maester { + $used = $false + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' -UsedToken ([ref] $used) + $script:calls.Token | Should -Be @('env-github') + $used | Should -BeTrue + } + } + + It 'sends no token when there is none' { + InModuleScope Maester { + $script:anonymousCanRead = $true + $used = $true + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' -UsedToken ([ref] $used) + $script:calls.Count | Should -Be 1 + $script:calls[0].Token | Should -BeNullOrEmpty + $used | Should -BeFalse + } + } + + It 'moves to the next sign-in when one cannot read the repository, in order' { + $env:GH_TOKEN = 'env-gh' + $env:GITHUB_TOKEN = 'env-github' + InModuleScope Maester { + Set-TestGitHubSession + Mock Get-MtGitHubCliToken { 'cli-token' } + $script:cannotRead = @('session-token', 'cli-token', 'env-gh') + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + + $script:calls.Token | Should -Be @('session-token', 'cli-token', 'env-gh', 'env-github') + } + } + + It 'starts later requests for a repository with the sign-in that read it' { + InModuleScope Maester { + Set-TestGitHubSession + Mock Get-MtGitHubCliToken { 'cli-token' } + $script:cannotRead = @('session-token') + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + $null = Invoke-MtPackGitHubRequest -Path 'repos/Contoso/Pack/releases?per_page=30' + + $script:calls.Token | Should -Be @('session-token', 'cli-token', 'cli-token') + # The name of the source is remembered, never the token. + $script:__MtPackGitHubTokenSource['contoso/pack'] | Should -Be 'GitHubCli' + } + } + + It 'does not send the same token twice when gh answers with GH_TOKEN' { + $env:GH_TOKEN = 'env-gh' + InModuleScope Maester { + Mock Get-MtGitHubCliToken { 'env-gh' } + $script:cannotRead = @('env-gh') + + { Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' } | Should -Throw + + @($script:calls | Where-Object Token -EQ 'env-gh').Count | Should -Be 1 + } + } + + It 'does not use a session sign-in to ' -ForEach @( + @{ Case = 'another GitHub host'; ApiBaseUri = 'https://api.contoso.ghe.com'; ExpiresAt = $null } + @{ Case = 'github.com that has expired'; ApiBaseUri = 'https://api.github.com'; ExpiresAt = [datetimeoffset]::UtcNow.AddMinutes(-5) } + ) { + InModuleScope Maester -Parameters @{ ApiBaseUri = $ApiBaseUri; ExpiresAt = $ExpiresAt } { + Set-TestGitHubSession -ApiBaseUri $ApiBaseUri -ExpiresAt $ExpiresAt + $script:anonymousCanRead = $true + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + + $script:calls.Token | Should -BeNullOrEmpty + } + } + + It 'does not use the sign-in of a session that is not connected' { + InModuleScope Maester { + $__MtSession.GitHubAuthHeader = @{ Authorization = 'Bearer stale-token' } + $__MtSession.GitHubConnection = [pscustomobject]@{ Connected = $false; FailureReason = 'TokenInvalid' } + $script:anonymousCanRead = $true + + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + + $script:calls.Token | Should -BeNullOrEmpty + } + } + + It 'sends every request to api.github.com' { + $env:GH_TOKEN = 'env-gh' + InModuleScope Maester { + Set-TestGitHubSession + $script:cannotRead = @('session-token') + $null = Invoke-MtPackGitHubRequest -Path '/repos/contoso/pack' + $script:calls.Uri | Select-Object -Unique | Should -Be 'https://api.github.com/repos/contoso/pack' + } + } + + It 'returns $null for 404 with -AllowNotFound, without asking other sources once one has read the repository' { + $env:GH_TOKEN = 'env-gh' + InModuleScope Maester { + Set-TestGitHubSession + $null = Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' + $script:cannotRead = @('session-token') + + Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack/git/ref/tags%2Fv9' -AllowNotFound | Should -BeNullOrEmpty + + $script:calls.Token | Should -Be @('session-token', 'session-token') + } + } + + It 'names what was tried, and not GH_TOKEN, when nothing can read the repository' { + InModuleScope Maester { + Set-TestGitHubSession + $script:cannotRead = @('session-token') + $failure = $null + try { Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' } catch { $failure = "$_" } + $failure | Should -BeLike '*404 Not Found*the Connect-MtGitHub sign-in*-Token*' + $failure | Should -Not -Match 'set GH_TOKEN' + $failure | Should -Not -Match 'session-token' + } + } + + It 'tells someone with no sign-in how to reach a private repository' { + InModuleScope Maester { + { Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' } | Should -Throw '*private repository*gh auth login*-Token*' + } + } + + It 'stops at a rate limit instead of trying the next sign-in' { + $env:GH_TOKEN = 'env-gh' + InModuleScope Maester { + Mock Invoke-RestMethod { + $script:calls.Add([pscustomobject]@{ Uri = [string]$Uri; Token = $Headers['Authorization'] }) + $response = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::Forbidden) + throw [Microsoft.PowerShell.Commands.HttpResponseException]::new('API rate limit exceeded', $response) + } + Set-TestGitHubSession + + { Invoke-MtPackGitHubRequest -Path 'repos/contoso/pack' } | Should -Throw '*rate limit*Wait for the limit to reset*' + + $script:calls.Count | Should -Be 1 + } + } +} + +Describe 'Pack GitHub token: the -Token parameter' { + It ' takes -Token as a SecureString' -ForEach @( + @{ Command = 'Install-MtPack' } + @{ Command = 'Update-MtPack' } + @{ Command = 'Restore-MtPack' } + ) { + $parameter = (Get-Command $Command).Parameters['Token'] + $parameter | Should -Not -BeNullOrEmpty + $parameter.ParameterType | Should -Be ([securestring]) + } + + It 'is passed to every GitHub request of an install' { + InModuleScope Maester { + $script:tokens = [System.Collections.Generic.List[object]]::new() + Mock Invoke-MtPackGitHubRequest { + $script:tokens.Add($Token) + switch -Wildcard ($Path) { + 'repos/contoso/pack' { [pscustomobject]@{ id = 7; full_name = 'contoso/pack'; default_branch = 'main'; owner = [pscustomobject]@{ id = 9; login = 'contoso' } } } + 'repos/contoso/pack/releases*' { , @() } + 'repos/contoso/pack/commits/*' { [pscustomobject]@{ sha = 'c' * 40 } } + '*/zipball/*' { throw 'stop after the download was asked for' } + } + } + $token = [System.Net.NetworkCredential]::new('', 'parameter-token').SecurePassword + + { Get-MtPackCandidate -Source 'contoso/pack' -Token $token -IncludeRecent } | Should -Throw '*stop after the download*' + + $script:tokens.Count | Should -BeGreaterThan 2 + foreach ($t in $script:tokens) { [object]::ReferenceEquals($t, $token) | Should -BeTrue } + } + } + + It 'is not sent to GitHub requests when it was not given' { + InModuleScope Maester { + Mock Invoke-MtPackGitHubRequest { throw 'stop' } + { Get-MtPackCandidate -Source 'contoso/pack' } | Should -Throw + Should -Invoke Invoke-MtPackGitHubRequest -ParameterFilter { $null -eq $Token } + } + } +} + +Describe 'Pack GitHub token: Get-MtSession' { + It 'does not return the session sign-in the pack commands use' { + InModuleScope Maester { + $__MtSession.GitHubAuthHeader = @{ Authorization = 'Bearer session-token' } + try { + (Get-MtSession).GitHubAuthHeader['Authorization'] | Should -Be '' + (Get-MtSession).Keys | Should -Not -Contain '__MtPackGitHubTokenSource' + } finally { + $__MtSession.GitHubAuthHeader = $null + } + } + } +} diff --git a/website/docs/packs/index.md b/website/docs/packs/index.md index 7fc7edf832..9d0cdc360d 100644 --- a/website/docs/packs/index.md +++ b/website/docs/packs/index.md @@ -21,6 +21,64 @@ Invoke-Maester -Pack CNT Some packs ship with Maester. They need no install and run as soon as they are turned on (see [Packs that ship with Maester](#packs-that-ship-with-maester)). +## Private packs + +A private pack lives in a private or internal GitHub repository. Only people who can read that repository can +install it, which makes it the way to share tests inside one company: checks for your own policies, naming rules or +internal systems that should not be published. Anyone you give access to the repository, such as a partner or a +customer, can install it as well. + +Install it like any other pack. `Install-MtPack`, `Update-MtPack` and `Restore-MtPack` need a GitHub sign-in that +can read the repository, and use the first of these that can: + +1. **`-Token`**, a `SecureString`, for a script that reads a token from a vault. When you pass it, nothing else + is tried. +2. **The Maester session's GitHub sign-in**, after `Connect-MtGitHub`, when it is to github.com. The Maester + GitHub App must be able to read the pack's repository. +3. **The GitHub CLI.** When `gh` is installed and signed in, Maester asks it for a token (`gh auth token`) at the + moment of each request and does not keep it. +4. **`GH_TOKEN`, then `GITHUB_TOKEN`**, the environment variables a pipeline sets. + +On your own computer, sign in to the GitHub CLI once and there is nothing to set: + +```powershell +gh auth login +Install-MtPack contoso/maester-internal-checks +``` + +With a token from a vault: + +```powershell +Install-MtPack contoso/maester-internal-checks -Token (Get-Secret -Name PackReadToken) +``` + +Whichever way it gets it, Maester sends the token to `api.github.com` only and does not store it. Use a token +that can only read this one repository: a fine-grained token with read-only **Contents**. + +:::caution[Keep tokens out of environment variables] +Everything that runs in the session can read an environment variable, including the packs you run, and every +process the session starts inherits it. Outside a pipeline, use the GitHub CLI or `-Token` instead. If you do set +`GH_TOKEN`, do not set it in your profile, and remove it before `Invoke-Maester`. Running an installed pack does +not need a token: only `Install-MtPack`, `Update-MtPack` and `Restore-MtPack` download from GitHub. +::: + +A private pack goes through the same checks as a public one, and is pinned in the lock file the same way. What is +different: + +- It is never listed on packs.maester.cloud, and cannot be published there. +- Nothing about it is sent to Maester: no install event, and run telemetry never names it. +- A pipeline needs a token too. In GitHub Actions the workflow's own `GITHUB_TOKEN` reads only the repository + the workflow runs in, so a pack in another private repository needs a token that can read that repository. + Give it to the restore step only, not to the whole job: + + ```yaml + - name: Restore packs + shell: pwsh + env: + GH_TOKEN: ${{ secrets.PACK_READ_TOKEN }} + run: Restore-MtPack + ``` + ## Installing a pack `Install-MtPack` takes the pack's GitHub repository (`owner/repo`, `owner/repo/folder`, `owner/repo@ref` or a