Skip to content

Commit

Permalink
check multiline secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
freddydk committed Dec 1, 2023
1 parent 12f04ef commit 33c575a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Actions/ReadSecrets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Secrets, which name is preceded by an asterisk (*) are encrypted and Base64 enco
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
| parentTelemetryScopeJson | | Specifies the parent telemetry scope for the telemetry signal | {} |
| getSecrets | Yes | Comma-separated list of secrets to get (add appDependencyProbingPathsSecrets to request secrets needed for resolving dependencies in AppDependencyProbingPaths, 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 |
| useGhTokenWorkflowForPush | false | Determines whether you want to use the GhTokenWorkflow secret for TokenForPush | false |
| checkSecretsForCommonMistakes | false | Determines whether you want to check all GitHub secrets for Common mistakes (including newlines), which can lead to problems down the road | false |

## OUTPUT

Expand Down
8 changes: 7 additions & 1 deletion Actions/ReadSecrets/ReadSecrets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
[Parameter(HelpMessage = "Comma-separated list of Secrets to get. Secrets preceded by an asterisk are returned encrypted", Mandatory = $true)]
[string] $getSecrets = "",
[Parameter(HelpMessage = "Determines whether you want to use the GhTokenWorkflow secret for TokenForPush", Mandatory = $false)]
[string] $useGhTokenWorkflowForPush = 'false'
[string] $useGhTokenWorkflowForPush = 'false',
[Parameter(HelpMessage = "Determines whether you want to check all GitHub secrets for Common mistakes (including newlines), which can lead to problems down the road", Mandatory = $false)]
[bool] $checkSecretsForCommonMistakes
)

$buildMutexName = "AL-Go-ReadSecrets"
Expand All @@ -24,6 +26,10 @@ try {
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
Import-Module (Join-Path $PSScriptRoot ".\ReadSecretsHelper.psm1") -ArgumentList $gitHubSecrets

if ($checkSecretsForCommonMistakes) {
CheckSecretsForCommonMistakes
}

$outSecrets = [ordered]@{}
$settings = $env:Settings | ConvertFrom-Json | ConvertTo-HashTable
$keyVaultCredentials = GetKeyVaultCredentials
Expand Down
2 changes: 1 addition & 1 deletion Actions/ReadSecrets/ReadSecretsHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $script:escchars = @(' ','!','\"','#','$','%','\u0026','\u0027','(',')','*','+',
# This can cause problems if these values are used as elsewhere - f.ex. if a line contains a { or a } character, then no JSON string can be parsed from one job to another
# This function checks for multi-line secrets displays warnings if multi-line secrets with lines containing short strings
#
function CheckSecrets {
function CheckSecretsForCommonMistakes {
foreach($secretName in $script:gitHubSecrets.PSObject.Properties.Name) {
$secretValue = $script:gitHubSecrets."$secretName"
if ($secretValue.contains("`n")) {
Expand Down
7 changes: 6 additions & 1 deletion Actions/ReadSecrets/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
description: Determines whether you want to use the GhTokenWorkflow secret for TokenForPush
required: false
default: 'false'
CheckSecretsForCommonMistakes:
description: Determines whether you want to check all GitHub secrets for Common mistakes (including newlines), which can lead to problems down the road
required: false
default: 'false'
outputs:
Secrets:
description: All requested secrets in compressed JSON format
Expand All @@ -31,10 +35,11 @@ runs:
env:
_getSecrets: ${{ inputs.getSecrets }}
_useGhTokenWorkflowForPush: ${{ inputs.useGhTokenWorkflowForPush }}
_checkSecretsForCommonMistakes: ${{ inputs.CheckSecretsForCommonMistakes }}
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
try {
${{ github.action_path }}/ReadSecrets.ps1 -gitHubSecrets '${{ inputs.gitHubSecrets }}' -getSecrets $ENV:_getSecrets -useGhTokenWorkflowForPush $ENV:_useGhTokenWorkflowForPush
${{ github.action_path }}/ReadSecrets.ps1 -gitHubSecrets '${{ inputs.gitHubSecrets }}' -getSecrets $ENV:_getSecrets -useGhTokenWorkflowForPush $ENV:_useGhTokenWorkflowForPush -CheckSecretsForCommonMistakes ($ENV:_checkSecretsForCommonMistakes -eq 'true')
}
catch {
Write-Host "::ERROR::Unexpected error when running action. Error Message: $($_.Exception.Message.Replace("`r",'').Replace("`n",' ')), StackTrace: $($_.ScriptStackTrace.Replace("`r",'').Replace("`n",' <- '))";
Expand Down
1 change: 1 addition & 0 deletions Templates/AppSource App/.github/workflows/CICD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ jobs:
shell: powershell
gitHubSecrets: ${{ toJson(secrets) }}
getSecrets: ${{ steps.DetermineDeliveryTargetSecrets.outputs.ContextSecrets }}
checkSecretsForCommonMistakes: 'true'

- name: Determine Delivery Targets
id: DetermineDeliveryTargets
Expand Down
1 change: 1 addition & 0 deletions Templates/Per Tenant Extension/.github/workflows/CICD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ jobs:
shell: powershell
gitHubSecrets: ${{ toJson(secrets) }}
getSecrets: ${{ steps.DetermineDeliveryTargetSecrets.outputs.ContextSecrets }}
checkSecretsForCommonMistakes: 'true'

- name: Determine Delivery Targets
id: DetermineDeliveryTargets
Expand Down

0 comments on commit 33c575a

Please sign in to comment.