-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadSecrets.ps1
130 lines (116 loc) · 5.13 KB
/
ReadSecrets.ps1
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
Param(
[Parameter(HelpMessage = "Settings from template repository in compressed Json format", Mandatory = $false)]
[string] $settingsJson = '{"keyVaultName": ""}',
[Parameter(HelpMessage = "Comma separated list of Secrets to get", Mandatory = $true)]
[string] $secrets = "",
[Parameter(HelpMessage = "Specifies the parent telemetry scope for the telemetry signal", Mandatory = $false)]
[string] $parentTelemetryScopeJson = '7b7d'
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version 2.0
$telemetryScope = $null
$bcContainerHelperPath = $null
# IMPORTANT: No code that can fail should be outside the try/catch
$buildMutexName = "AL-Go-ReadSecrets"
$buildMutex = New-Object System.Threading.Mutex($false, $buildMutexName)
try {
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
$BcContainerHelperPath = DownloadAndImportBcContainerHelper -baseFolder $ENV:GITHUB_WORKSPACE
import-module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve)
$telemetryScope = CreateScope -eventId 'DO0078' -parentTelemetryScopeJson $parentTelemetryScopeJson
try {
if (!$buildMutex.WaitOne(1000)) {
Write-Host "Waiting for other process executing ReadSecrets"
$buildMutex.WaitOne() | Out-Null
Write-Host "Other process completed ReadSecrets"
}
}
catch [System.Threading.AbandonedMutexException] {
Write-Host "Other process terminated abnormally"
}
Import-Module (Join-Path $PSScriptRoot ".\ReadSecretsHelper.psm1")
$outSecrets = [ordered]@{}
$settings = $settingsJson | ConvertFrom-Json | ConvertTo-HashTable
$outSettings = $settings
$keyVaultName = $settings.KeyVaultName
if ([string]::IsNullOrEmpty($keyVaultName) -and (IsKeyVaultSet)) {
$credentialsJson = Get-KeyVaultCredentials -dontmask | ConvertTo-HashTable
if ($credentialsJson.ContainsKey("KeyVaultName")) {
$keyVaultName = $credentialsJson.KeyVaultName
}
}
[System.Collections.ArrayList]$secretsCollection = @()
$secrets.Split(',') | ForEach-Object {
$secret = $_
$secretNameProperty = "$($secret)SecretName"
if ($settings.containsKey($secretNameProperty)) {
$secret = "$($secret)=$($settings."$secretNameProperty")"
}
$secretsCollection += $secret
}
@($secretsCollection) | ForEach-Object {
$secretSplit = $_.Split('=')
$envVar = $secretSplit[0]
$secret = $envVar
if ($secretSplit.Count -gt 1) {
$secret = $secretSplit[1]
}
if ($secret) {
$value = GetSecret -secret $secret -keyVaultName $keyVaultName
if ($value) {
$json = @{}
try {
$json = $value | ConvertFrom-Json | ConvertTo-HashTable
}
catch {
}
if ($json.Keys.Count) {
if ($value.contains("`n")) {
throw "JSON Secret $secret contains line breaks. JSON Secrets should be compressed JSON (i.e. NOT contain any line breaks)."
}
$json.Keys | ForEach-Object {
MaskValue -key "$($secret).$($_)" -value $json."$_"
}
}
$base64value = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value))
Add-Content -Path $env:GITHUB_ENV -Value "$envVar=$base64value"
$outSecrets += @{ "$envVar" = $base64value }
Write-Host "$envVar successfully read from secret $secret"
$secretsCollection.Remove($_)
}
}
}
if ($outSettings.ContainsKey('appDependencyProbingPaths')) {
$outSettings.appDependencyProbingPaths | ForEach-Object {
if ($_.PsObject.Properties.name -eq "AuthTokenSecret") {
$_.authTokenSecret = GetSecret -secret $_.authTokenSecret -keyVaultName $keyVaultName
}
}
}
if ($secretsCollection) {
Write-Host "The following secrets was not found: $(($secretsCollection | ForEach-Object {
$secretSplit = @($_.Split('='))
if ($secretSplit.Count -eq 1) {
$secretSplit[0]
}
else {
"$($secretSplit[0]) (Secret $($secretSplit[1]))"
}
$outSecrets += @{ ""$($secretSplit[0])"" = """" }
}) -join ', ')"
}
$outSecretsJson = $outSecrets | ConvertTo-Json -Compress
Add-Content -Path $env:GITHUB_ENV -Value "RepoSecrets=$outSecretsJson"
$outSettingsJson = $outSettings | ConvertTo-Json -Compress
Add-Content -Path $env:GITHUB_ENV -Value "Settings=$OutSettingsJson"
TrackTrace -telemetryScope $telemetryScope
}
catch {
OutputError -message "ReadSecrets action failed.$([environment]::Newline)Error: $($_.Exception.Message)$([environment]::Newline)Stacktrace: $($_.scriptStackTrace)"
TrackException -telemetryScope $telemetryScope -errorRecord $_
exit
}
finally {
CleanupAfterBcContainerHelper -bcContainerHelperPath $bcContainerHelperPath
$buildMutex.ReleaseMutex()
}