Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support authenticating as GitHub Apps instead of using Personal Access Tokens #1311

Open
wants to merge 110 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
a091a35
Support GitHub App Auth
freddydk Nov 17, 2024
9463f40
catch
freddydk Nov 17, 2024
dd0cbea
10
freddydk Nov 17, 2024
7c7253d
dumps
freddydk Nov 17, 2024
32aab8d
mask multi
freddydk Nov 17, 2024
0c191ab
add message
freddydk Nov 17, 2024
df1c6e6
precommit
freddydk Nov 17, 2024
ff16e55
token exchange
freddydk Nov 18, 2024
add7df9
getreal
freddydk Nov 18, 2024
38b7860
tests
freddydk Nov 18, 2024
af60cca
getreal
freddydk Nov 18, 2024
6f09716
x
freddydk Nov 18, 2024
e3e4968
use real
freddydk Nov 18, 2024
0f9ae18
use real
freddydk Nov 18, 2024
d0157f6
no user
freddydk Nov 18, 2024
f358ab8
use githubOwner
freddydk Nov 18, 2024
936c912
dump
freddydk Nov 18, 2024
945ca71
use repo
freddydk Nov 18, 2024
dae2cdc
no config
freddydk Nov 18, 2024
cd8b221
use repo
freddydk Nov 18, 2024
1524490
fix token
freddydk Nov 18, 2024
28232d6
mask value
freddydk Nov 18, 2024
1402a6f
move
freddydk Nov 18, 2024
9491ba7
use gh
freddydk Nov 18, 2024
9c30d28
remove silent
freddydk Nov 18, 2024
8b10b20
remove line breaks
freddydk Nov 19, 2024
3f1c796
Merge branch 'main' into noPAT
freddydk Nov 19, 2024
e9ee5e8
fix e2e
freddydk Nov 19, 2024
2501a3b
Merge branch 'noPAT' of https://github.com/freddydk/AL-Go into noPAT
freddydk Nov 19, 2024
112ebc3
silent
freddydk Nov 19, 2024
85014aa
use gfh
freddydk Nov 19, 2024
cea8eda
jit
freddydk Nov 19, 2024
9ba3af2
use real token
freddydk Nov 19, 2024
cdd459c
get token for repo
freddydk Nov 19, 2024
d7d440b
inc count
freddydk Nov 19, 2024
cd03ea6
token cache
freddydk Nov 19, 2024
0496032
Merge branch 'main' into noPAT
freddydk Nov 28, 2024
6ab67c5
Merge branch 'main' into noPAT
freddydk Dec 30, 2024
8c84f06
Merge branch 'main' into noPAT
freddydk Jan 12, 2025
270f0ad
reauth
freddydk Jan 12, 2025
cdab137
dumps
freddydk Jan 12, 2025
15723ba
getreal
freddydk Jan 12, 2025
c2692d8
renew
freddydk Jan 12, 2025
c0ff065
set token
freddydk Jan 12, 2025
8290ab5
no auth
freddydk Jan 12, 2025
a788755
do not mask empty
freddydk Jan 12, 2025
632e1ad
2 secrets
freddydk Jan 14, 2025
7c04d8c
e2e bugs
freddydk Jan 14, 2025
81c452b
remove dump
freddydk Jan 14, 2025
55c7a90
dump
freddydk Jan 14, 2025
04742d1
check existingbranch
freddydk Jan 14, 2025
2b3f47d
check
freddydk Jan 14, 2025
5f505e1
update var name
freddydk Jan 15, 2025
65d54a2
use -algoauthapp
freddydk Jan 15, 2025
9184a2f
use e2epat
freddydk Jan 15, 2025
3c7c447
use token
freddydk Jan 15, 2025
62fb73c
use ghtoken
freddydk Jan 15, 2025
a5569cb
use app
freddydk Jan 15, 2025
73178aa
Merge branch 'main' into noPAT
freddydk Jan 15, 2025
926f101
documentation
freddydk Jan 16, 2025
f642f27
release note
freddydk Jan 16, 2025
ff85721
remove line
freddydk Jan 16, 2025
2528f07
Merge branch 'main' into noPAT
freddydk Jan 16, 2025
06bb22f
rename
freddydk Jan 20, 2025
e3c0b87
Merge branch 'main' into noPAT
freddydk Jan 20, 2025
fc0e339
remove dump
freddydk Jan 20, 2025
f72ecf1
Merge branch 'noPAT' of https://github.com/freddydk/AL-Go into noPAT
freddydk Jan 20, 2025
f92b3dc
Merge branch 'main' into noPAT
freddydk Jan 20, 2025
a476091
comment
freddydk Jan 20, 2025
f52be0a
Merge branch 'noPAT' of https://github.com/freddydk/AL-Go into noPAT
freddydk Jan 20, 2025
3bf8de8
Merge branch 'main' into noPAT
freddydk Jan 20, 2025
32ddfd9
Merge branch 'main' into noPAT
freddydk Jan 22, 2025
1d0fdd5
move release note
freddydk Jan 22, 2025
c9415a0
add permissions
freddydk Jan 22, 2025
78e3ee1
trailing spaces
freddydk Jan 22, 2025
8395f83
dump
freddydk Jan 22, 2025
d2a0a5b
Merge branch 'main' into noPAT
mazhelez Jan 22, 2025
9f750ca
fix err
freddydk Jan 23, 2025
87c90f8
remove verbose
freddydk Jan 23, 2025
4604775
get access token on nuget
freddydk Jan 23, 2025
5180a30
allow token to be empty
freddydk Jan 23, 2025
e6f4a8d
all repos
freddydk Jan 23, 2025
eff2cb9
add contents
freddydk Jan 23, 2025
d2fb99f
=
freddydk Jan 23, 2025
4636616
actions read
freddydk Jan 23, 2025
a4204d0
no permissions
freddydk Jan 23, 2025
8238403
add permissions
freddydk Jan 23, 2025
5407dcf
Update Scenarios/Contribute.md
freddydk Jan 23, 2025
00afbb7
Update Scenarios/GhTokenWorkflow.md
freddydk Jan 23, 2025
0100108
Update Scenarios/GhTokenWorkflow.md
freddydk Jan 23, 2025
92a7e6e
use read and write token
freddydk Jan 23, 2025
c49a9f9
release notes
freddydk Jan 23, 2025
d17fef3
Merge branch 'main' into noPAT
freddydk Jan 23, 2025
ec0c203
set writetoken
freddydk Jan 23, 2025
b4def6d
Merge branch 'noPAT' of https://github.com/freddydk/AL-Go into noPAT
freddydk Jan 23, 2025
1d939b6
use readtoken
freddydk Jan 23, 2025
440c60f
link to doc
freddydk Jan 23, 2025
097b445
Update Scenarios/GhTokenWorkflow.md
freddydk Jan 24, 2025
db3ff49
Update Scenarios/GhTokenWorkflow.md
freddydk Jan 24, 2025
c9f7c99
Update Scenarios/GhTokenWorkflow.md
freddydk Jan 24, 2025
55b910a
review
freddydk Jan 24, 2025
ce383fe
added note
freddydk Jan 24, 2025
d43735a
dump
freddydk Jan 24, 2025
a0dc6d4
add help
freddydk Jan 24, 2025
aebff0d
error msg
freddydk Jan 24, 2025
6222970
precommit and CI
freddydk Jan 24, 2025
a368903
revert
freddydk Jan 24, 2025
6013b81
dump cache usage
freddydk Jan 25, 2025
7efaff8
move back
freddydk Jan 25, 2025
eb166e1
remove cache (never used)
freddydk Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Actions/AL-Go-Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $defaultCICDPushBranches = @( 'main', 'release/*', 'feature/*' )
$defaultCICDPullRequestBranches = @( 'main' )
$runningLocal = $local.IsPresent
$defaultBcContainerHelperVersion = "preview" # Must be double quotes. Will be replaced by BcContainerHelperVersion if necessary in the deploy step - ex. "https://github.com/organization/navcontainerhelper/archive/refs/heads/branch.zip"
$notSecretProperties = @("Scopes","TenantId","BlobName","ContainerName","StorageAccountName","ServerUrl","ppUserName")
$notSecretProperties = @("Scopes","TenantId","BlobName","ContainerName","StorageAccountName","ServerUrl","ppUserName","GitHubAppClientId")
freddydk marked this conversation as resolved.
Show resolved Hide resolved

$runAlPipelineOverrides = @(
"DockerPull"
Expand Down Expand Up @@ -1315,12 +1315,13 @@ function CloneIntoNewFolder {
$baseFolder = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
New-Item $baseFolder -ItemType Directory | Out-Null
Set-Location $baseFolder
$serverUri = [Uri]::new($env:GITHUB_SERVER_URL)
$serverUrl = "$($serverUri.Scheme)://$($actor):$($token)@$($serverUri.Host)/$($env:GITHUB_REPOSITORY)"

# Environment variables for hub commands
$env:GITHUB_USER = $actor
$env:GITHUB_TOKEN = $token
$env:GITHUB_TOKEN = GetRealToken -token $token

$serverUri = [Uri]::new($env:GITHUB_SERVER_URL)
$serverUrl = "$($serverUri.Scheme)://$($env:GITHUB_USER):$($env:GITHUB_TOKEN)@$($serverUri.Host)/$($env:GITHUB_REPOSITORY)"

# Configure git
invoke-git config --global user.email "[email protected]"
Expand Down
7 changes: 2 additions & 5 deletions Actions/CheckForUpdates/CheckForUpdates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ if ($update -eq 'Y') {
throw "A personal access token with permissions to modify Workflows is needed. You must add a secret called GhTokenWorkflow containing a personal access token. You can Generate a new token from https://github.com/settings/tokens. Make sure that the workflow scope is checked."
}
else {
$token = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($token))
$token = GetRealToken -token ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($token)))
}
}

# Use Authenticated API request to avoid the 60 API calls per hour limit
$headers = @{
"Accept" = "application/vnd.github.baptiste-preview+json"
"Authorization" = "Bearer $token"
}
$headers = GetHeaders -token $token

if (-not $templateUrl.Contains('@')) {
$templateUrl += "@main"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)

function IsGitHubPagesAvailable() {
$headers = GetHeader -token $env:GITHUB_TOKEN
$headers = GetHeaders -token $env:GITHUB_TOKEN
$url = "$($ENV:GITHUB_API_URL)/repos/$($ENV:GITHUB_REPOSITORY)/pages"
try {
Write-Host "Requesting GitHub Pages settings from GitHub"
Expand All @@ -20,7 +20,7 @@ function IsGitHubPagesAvailable() {
}

function GetGitHubEnvironments() {
$headers = GetHeader -token $env:GITHUB_TOKEN
$headers = GetHeaders -token $env:GITHUB_TOKEN
freddydk marked this conversation as resolved.
Show resolved Hide resolved
$url = "$($ENV:GITHUB_API_URL)/repos/$($ENV:GITHUB_REPOSITORY)/environments"
try {
Write-Host "Requesting environments from GitHub"
Expand All @@ -36,7 +36,7 @@ function GetGitHubEnvironments() {
function Get-BranchesFromPolicy($ghEnvironment) {
if ($ghEnvironment) {
# Environment is defined in GitHub - check protection rules
$headers = GetHeader -token $env:GITHUB_TOKEN
$headers = GetHeaders -token $env:GITHUB_TOKEN
$branchPolicy = ($ghEnvironment.protection_rules | Where-Object { $_.type -eq "branch_policy" })
if ($branchPolicy) {
if ($ghEnvironment.deployment_branch_policy.protected_branches) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ function Get-ModifiedFiles {

$url = "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)/compare/$($ghEvent.pull_request.base.sha)...$($ghEvent.pull_request.head.sha)"

$headers = @{
"Authorization" = "token $token"
"Accept" = "application/vnd.github.baptiste-preview+json"
}
$headers = GetHeaders -token $token

$response = (InvokeWebRequest -Headers $headers -Uri $url).Content | ConvertFrom-Json

Expand Down
85 changes: 73 additions & 12 deletions Actions/Github-Helper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ function GetReleases {
)

Write-Host "Analyzing releases $api_url/repos/$repository/releases"
$releases = (InvokeWebRequest -Headers (GetHeader -token $token) -Uri "$api_url/repos/$repository/releases").Content | ConvertFrom-Json
$releases = (InvokeWebRequest -Headers (GetHeaders -token $token) -Uri "$api_url/repos/$repository/releases").Content | ConvertFrom-Json
if ($releases.Count -gt 1) {
# Sort by SemVer tag
try {
Expand Down Expand Up @@ -556,7 +556,42 @@ function GetLatestRelease {
$latestRelease
}

function GetHeader {
function GetRealToken {
freddydk marked this conversation as resolved.
Show resolved Hide resolved
freddydk marked this conversation as resolved.
Show resolved Hide resolved
Param(
[string] $token
)

if (!($token.StartsWith("{"))) {
# not a json token
return $token
freddydk marked this conversation as resolved.
Show resolved Hide resolved
}
else {
try {
$json = $token | ConvertFrom-Json
$gitHubAppClientId = $json.GitHubAppClientId
$privateKey = $json.PrivateKey
Write-Host "Using GitHub App with ClientId $gitHubAppClientId for authentication"
Fixed Show fixed Hide fixed
$jwt = GenerateJwtForTokenRequest -gitHubAppClientId $gitHubAppClientId -privateKey $privateKey
$headers = @{
"Accept" = "application/vnd.github+json"
"Authorization" = "Bearer $jwt"
"X-GitHub-Api-Version" = "2022-11-28"
}
Write-Host "Get App Info"
$appinfo = Invoke-RestMethod -Method GET -UseBasicParsing -Headers $headers -Uri "$ENV:GITHUB_API_URL/repos/$ENV:GITHUB_REPOSITORY/installation"
Write-Host "Get Token Response"
$tokenResponse = Invoke-RestMethod -Method POST -UseBasicParsing -Headers $headers -Uri $appInfo.access_tokens_url
Write-Host "return token"
Fixed Show fixed Hide fixed
return $tokenResponse.token
}
catch {
# Not a json token
return $token
}
}
}

function GetHeaders {
param (
[string] $token,
[string] $accept = "application/vnd.github+json",
Expand All @@ -567,9 +602,9 @@ function GetHeader {
"X-GitHub-Api-Version" = $apiVersion
}
if (![string]::IsNullOrEmpty($token)) {
$headers["Authorization"] = "token $token"
$realToken = GetRealToken -token $token
$headers["Authorization"] = "token $realToken"
}

return $headers
}

Expand Down Expand Up @@ -616,7 +651,7 @@ function GetReleaseNotes {
$postParams["target_commitish"] = $target_commitish
}

InvokeWebRequest -Headers (GetHeader -token $token) -Method POST -Body ($postParams | ConvertTo-Json) -Uri "$api_url/repos/$repository/releases/generate-notes"
InvokeWebRequest -Headers (GetHeaders -token $token) -Method POST -Body ($postParams | ConvertTo-Json) -Uri "$api_url/repos/$repository/releases/generate-notes"
}

function DownloadRelease {
Expand All @@ -636,7 +671,7 @@ function DownloadRelease {
if ([string]::IsNullOrEmpty($token)) {
$token = invoke-gh -silent -returnValue auth token
}
$headers = GetHeader -token $token -accept "application/octet-stream"
$headers = GetHeaders -token $token -accept "application/octet-stream"
foreach($project in $projects.Split(',')) {
# GitHub replaces series of special characters with a single dot when uploading release assets
$project = [Uri]::EscapeDataString($project.Replace('\','_').Replace('/','_').Replace(' ','.')).Replace('%2A','*').Replace('%3F','?').Replace('%','')
Expand Down Expand Up @@ -674,7 +709,7 @@ function CheckRateLimit {
[string] $token = ''
)

$headers = GetHeader -token $token
$headers = GetHeaders -token $token
$rate = (InvokeWebRequest -Headers $headers -Uri "https://api.github.com/rate_limit").Content | ConvertFrom-Json
$rate | ConvertTo-Json -Depth 99 | Out-Host
$rate = $rate.rate
Expand Down Expand Up @@ -771,7 +806,7 @@ function CheckBuildJobsInWorkflowRun {
[string] $workflowRunId
)

$headers = GetHeader -token $token
$headers = GetHeaders -token $token
$per_page = 100
$page = 1

Expand Down Expand Up @@ -822,7 +857,7 @@ function FindLatestSuccessfulCICDRun {
[string] $token
)

$headers = GetHeader -token $token
$headers = GetHeaders -token $token
$lastSuccessfulCICDRun = 0
$per_page = 100
$page = 1
Expand Down Expand Up @@ -899,7 +934,7 @@ function GetArtifactsFromWorkflowRun {

Write-Host "Getting artifacts for workflow run $workflowRun, mask $mask, projects $projects and version $version"

$headers = GetHeader -token $token
$headers = GetHeaders -token $token

$foundArtifacts = @()
$per_page = 100
Expand Down Expand Up @@ -975,7 +1010,7 @@ function GetArtifacts {
)

$refname = $branch.Replace('/','_')
$headers = GetHeader -token $token
$headers = GetHeaders -token $token
if ($version -eq 'latest') { $version = '*' }

# For latest version, use the artifacts from the last successful CICD run
Expand Down Expand Up @@ -1082,7 +1117,7 @@ function DownloadArtifact {
if ([string]::IsNullOrEmpty($token)) {
$token = invoke-gh -silent -returnValue auth token
}
$headers = GetHeader -token $token
$headers = GetHeaders -token $token
$foldername = Join-Path $path $artifact.Name
$filename = "$foldername.zip"
InvokeWebRequest -Headers $headers -Uri $artifact.archive_download_url -OutFile $filename
Expand All @@ -1098,3 +1133,29 @@ function DownloadArtifact {
return $filename
}
}

function GenerateJwtForTokenRequest {
Param(
[string] $gitHubAppClientId,
[string] $privateKey
)

$header = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{
alg = "RS256"
typ = "JWT"
}))).TrimEnd('=').Replace('+', '-').Replace('/', '_');

$payload = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{
freddydk marked this conversation as resolved.
Show resolved Hide resolved
iat = [System.DateTimeOffset]::UtcNow.AddSeconds(-10).ToUnixTimeSeconds()
exp = [System.DateTimeOffset]::UtcNow.AddMinutes(10).ToUnixTimeSeconds()
iss = $gitHubAppClientId
}))).TrimEnd('=').Replace('+', '-').Replace('/', '_');
$signature = pwsh -command {
$rsa = [System.Security.Cryptography.RSA]::Create()
$privateKey = "$($args[1])"
$rsa.ImportFromPem($privateKey)
$signature = [Convert]::ToBase64String($rsa.SignData([System.Text.Encoding]::UTF8.GetBytes($args[0]), [System.Security.Cryptography.HashAlgorithmName]::SHA256, [System.Security.Cryptography.RSASignaturePadding]::Pkcs1)).TrimEnd('=').Replace('+', '-').Replace('/', '_')
Write-OutPut $signature
} -args "$header.$payload", $privateKey
return "$header.$payload.$signature"
}
2 changes: 1 addition & 1 deletion Actions/PullPowerPlatformChanges/GitCommitChanges.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Set-Location -Path $location

# Environment variables for hub commands
$env:GITHUB_USER = $actor
$env:GITHUB_TOKEN = $token
$env:GITHUB_TOKEN = GetRealToken -token $token

# Commit from the new folder
Write-Host "Committing changes from the new folder $Location\$PowerPlatformSolutionName to branch $gitHubBranch"
Expand Down
8 changes: 6 additions & 2 deletions Actions/ReadSecrets/ReadSecretsHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function MaskValue {
)

Write-Host "Masking value for $key"
Write-Host "::add-mask::$value"
$value.Split("`n") | ForEach-Object {
freddydk marked this conversation as resolved.
Show resolved Hide resolved
Write-Host "::add-mask::$_"
}

$val2 = ""
$value.ToCharArray() | ForEach-Object {
Expand All @@ -47,7 +49,9 @@ function MaskValue {
}

if ($val2 -ne $value) {
Write-Host "::add-mask::$val2"
$val2.Split("`n") | ForEach-Object {
Write-Host "::add-mask::$_"
}
}
Write-Host "::add-mask::$([Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value)))"
}
Expand Down
Loading
Loading