Skip to content

Freddydk/avoidallsecrets #1735

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

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
472893f
determinesecrets
freddydk May 8, 2025
0bc965a
dump
freddydk May 8, 2025
ee0f473
remove
freddydk May 8, 2025
f68b458
dump
freddydk May 8, 2025
24776a9
use script
freddydk May 8, 2025
9b461b4
remove dump
freddydk May 8, 2025
619ac55
check secretName
freddydk May 8, 2025
be50210
use env
freddydk May 9, 2025
73d839e
use calculated secrets
freddydk May 9, 2025
385bfb9
Merge branch 'main' into avoidallsecrets
freddydk May 9, 2025
4b38887
Merge branch 'main' into avoidallsecrets
freddydk May 12, 2025
a5de2f4
Simplify readsecrets
freddydk May 15, 2025
850468f
Merge branch 'avoidallsecrets' of https://github.com/freddydk/AL-Go i…
freddydk May 15, 2025
dd25a34
precommit
freddydk May 15, 2025
4cd8483
remove getsecrets
freddydk May 15, 2025
5705f00
import
freddydk May 15, 2025
8f6846a
use jsonStr
freddydk May 15, 2025
c8350f4
env
freddydk May 15, 2025
89a7797
correct ref
freddydk May 15, 2025
79d8a25
s
freddydk May 15, 2025
d17843c
msg
freddydk May 15, 2025
f1dd80a
remove encrypted pwd
freddydk May 15, 2025
191b5e0
Merge branch 'main' into avoidallsecrets
freddydk May 15, 2025
69bd866
fix analyzer
freddydk May 15, 2025
810603e
Merge branch 'avoidallsecrets' of https://github.com/freddydk/AL-Go i…
freddydk May 15, 2025
6e8b82e
fix
freddydk May 15, 2025
6e9584f
check max
freddydk May 15, 2025
0871381
readd buildPP
freddydk May 15, 2025
50b1e07
add github_token
freddydk May 15, 2025
31a84e4
add script
freddydk May 15, 2025
77e4572
add script
freddydk May 15, 2025
6ab5130
use app
freddydk May 15, 2025
bc217f4
Update Actions/ReadSecrets/ReadSecrets.ps1
freddydk May 15, 2025
340bd4e
Merge branch 'main' into freddydk/avoidallsecrets
freddydk May 17, 2025
7a3374a
Merge branch 'main' into freddydk/avoidallsecrets
freddydk May 20, 2025
9a80383
Avoidallsecrets (#1748)
freddydk May 21, 2025
cf73c1e
Merge branch 'main' into freddydk/avoidallsecrets
freddydk May 21, 2025
0ae5561
use env
freddydk May 21, 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
107 changes: 107 additions & 0 deletions Actions/DetermineSecrets/DetermineSecrets.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Param(
[Parameter(HelpMessage = "Comma-separated list of Secrets to get.", Mandatory = $true)]
[string] $getSecrets = "",
[Parameter(HelpMessage = "Determines whether you want to use the GhTokenWorkflow secret for TokenForPush", Mandatory = $false)]
[string] $useGhTokenWorkflowForPush = 'false'
)

. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)

$settings = $env:Settings | ConvertFrom-Json | ConvertTo-HashTable

# Build an array of secrets to get (and the names of the secrets)
$script:secretsCollection = [System.Collections.ArrayList]::new()
$script:secretNames = @{}

function AddSecret {
Param(
[string] $secret,
[switch] $useMapping
)

if ($secret) {
$secretName = $secret
$secretNameProperty = "$($secretName)SecretName"
if ($useMapping.IsPresent -and $settings.Keys -contains $secretNameProperty) {
$secretName = $settings."$secretNameProperty"
}
# Secret is the AL-Go name of the secret
# SecretName is the actual name of the secret to get from the KeyVault or GitHub environment
if ($secretName -and ($script:secretsCollection -notcontains $secret)) {
# Add secret to the collection of secrets to get
$script:secretsCollection += $secret
$script:secretNames += @{
"$secret" = "$secretName"
}
}
}
}

AddSecret -secret 'AZURE_CREDENTIALS' -useMapping
foreach($secret in ($getSecrets.Split(',') | Select-Object -Unique)) {
switch ($secret) {
'TokenForPush' {
AddSecret -secret 'TokenForPush'
if ($useGhTokenWorkflowForPush -eq 'true') {
# If we are using the ghTokenWorkflow for commits, we need to get ghTokenWorkflow secret
AddSecret -secret 'ghTokenWorkflow' -useMapping
}
else {
AddSecret -secret 'github_token'
}
}
'GitSubmodulesToken' {
# If we are getting the gitSubModules token, we might need to get the github token as well
AddSecret -secret $secret -useMapping
AddSecret -secret 'github_token'
}
'AppDependencySecrets' {
# Loop through appDependencyProbingPaths and trustedNuGetFeeds and add secrets to the collection of secrets to get
$settingsCollection = @()
if ($settings.Keys -contains 'appDependencyProbingPaths') {
$settingsCollection += $settings.appDependencyProbingPaths
}
if ($settings.Keys -contains 'trustedNuGetFeeds') {
$settingsCollection += $settings.trustedNuGetFeeds
}
foreach($settingsItem in $settingsCollection) {
if ($settingsItem.PsObject.Properties.name -eq "AuthTokenSecret") {
AddSecret -secret $settingsItem.authTokenSecret
}
}
# Look through installApps and installTestApps for secrets and add them to the collection of secrets to get
foreach($installSettingsKey in @('installApps','installTestApps')) {
if ($settings.Keys -contains $installSettingsKey) {
$settings."$installSettingsKey" | ForEach-Object {
# If any of the installApps URLs contains '${{SECRETNAME}}' we need to get the secret
$pattern = '.*(\$\{\{\s*([^}]+?)\s*\}\}).*'
if ($_ -match $pattern) {
AddSecret -secret $matches[2]
}
}
}
}
}
default {
AddSecret -secret $secret -useMapping
}
}
}

# Calculate output for secrets
# one output called FORMATSTR with the content: {{"secret1":{0},"secret2":{1},"secret3":{2}}}
# and one environment variable per secret called S0, S1, S2 with the name of the GitHub Secret (or Azure DevOps secret) to look for
if ($script:secretsCollection.Count -gt 32) {
throw "Maximum number of secrets exceeded."
}

$cnt = 0
$formatArr = @()
foreach($secret in $script:secretsCollection) {
$formatArr += @("""$Secret"":{$cnt}")
Add-Content -Encoding UTF8 -Path $ENV:GITHUB_ENV -Value "S$cnt=$($script:secretNames[$secret])"
Write-Host "S$cnt=$($script:secretNames[$secret])"
$cnt++
}
Add-Content -Encoding UTF8 -Path $ENV:GITHUB_OUTPUT -Value "FORMATSTR={{$($formatArr -join ',')}}"
Write-Host "FORMATSTR={{$($formatArr -join ',')}}"
32 changes: 32 additions & 0 deletions Actions/DetermineSecrets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Determine secrets

Determine the secrets needed for the workflow

## INPUT

### ENV variables

| Name | Description |
| :-- | :-- |
| Settings | env.Settings must be set by a prior call to the ReadSettings Action |

### Parameters

| Name | Required | Description | Default value |
| :-- | :-: | :-- | :-- |
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
| getSecrets | Yes | Comma-separated list of secrets to get (add appDependencySecrets to request secrets needed for resolving dependencies in AppDependencyProbingPaths and TrustedNuGetFeeds, add TokenForPush in order to request a token to use for pull requests and commits). Secrets preceded by an asterisk are returned encrypted | |
| useGhTokenWorkflowForPush | false | Determines whether you want to use the GhTokenWorkflow secret for TokenForPush | false |

## OUTPUT

### ENV variables

none

### OUTPUT variables

| Name | Description |
| :-- | :-- |
| FORMATSTR | A format string to be used when transferring the secrets to ReadSecrets |
| S0,S1,S2,...,S31 | The actual names of the GitHub secrets to look for |
34 changes: 34 additions & 0 deletions Actions/DetermineSecrets/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Determine Secrets
author: Microsoft Corporation
inputs:
shell:
description: Shell in which you want to run the action (powershell or pwsh)
required: false
default: powershell
getSecrets:
description: Comma-separated list of Secrets to get. Secrets preceded by an asterisk are returned encrypted
required: true
useGhTokenWorkflowForPush:
description: Determines whether you want to use the GhTokenWorkflow secret for TokenForPush
required: false
default: 'false'
outputs:
FORMATSTR:
description: A format string to be used when transferring the secrets to ReadSecrets
value: ${{ steps.DetermineSecrets.outputs.FORMATSTR }}
runs:
using: composite
steps:
- name: run
shell: ${{ inputs.shell }}
id: DetermineSecrets
env:
_getSecrets: ${{ inputs.getSecrets }}
_useGhTokenWorkflowForPush: ${{ inputs.useGhTokenWorkflowForPush }}
run: |
${{ github.action_path }}/../Invoke-AlGoAction.ps1 -ActionName "DetermineSecrets" -Action {
${{ github.action_path }}/DetermineSecrets.ps1 -getSecrets $ENV:_getSecrets -useGhTokenWorkflowForPush $ENV:_useGhTokenWorkflowForPush
}
branding:
icon: terminal
color: blue
6 changes: 2 additions & 4 deletions Actions/ReadSecrets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Read secrets from GitHub secrets or Azure Keyvault for AL-Go workflows
The secrets read and added to the output are the secrets specified in the getSecrets parameter
Additionally, the secrets specified by the authTokenSecret in AppDependencyProbingPaths and TrustedNuGetFeeds are read if appDependencySecrets is specified in getSecrets
All secrets included in the Secrets output are Base64 encoded to avoid issues with national characters
Secrets, which name is preceded by an asterisk (\*) are encrypted and Base64 encoded

## INPUT

Expand All @@ -19,8 +18,7 @@ Secrets, which name is preceded by an asterisk (\*) are encrypted and Base64 enc
| Name | Required | Description | Default value |
| :-- | :-: | :-- | :-- |
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
| gitHubSecrets | Yes | GitHub secrets in a json structure | |
| getSecrets | Yes | Comma-separated list of secrets to get (add appDependencySecrets to request secrets needed for resolving dependencies in AppDependencyProbingPaths and TrustedNuGetFeeds, add TokenForPush in order to request a token to use for pull requests and commits). Secrets preceded by an asterisk are returned encrypted | |
| gitHubSecrets | Yes | A JSON structure with all secrets needed. The structure already contains the existing GitHub secrets | |
| useGhTokenWorkflowForPush | false | Determines whether you want to use the GhTokenWorkflow secret for TokenForPush | false |

## OUTPUT
Expand All @@ -33,5 +31,5 @@ none

| Name | Description |
| :-- | :-- |
| Secrets | A compressed json construct with all requested secrets base64 encoded. Secrets preceded by an asterisk (\*) are encrypted before base64 encoding. The secret value + the base64 value of the secret value are masked in the log |
| Secrets | A compressed json construct with all requested secrets base64 encoded. The secret value + the base64 value of the secret value are masked in the log |
| TokenForPush | The token to use when workflows are pushing changes (either directly, or via pull requests). This is either the GITHUB_TOKEN or the GhTokenWorkflow secret (based on the env variable useGhTokenWorkflowForPush) |
Loading
Loading