Skip to content

Commit 5c1457a

Browse files
twinpilootMartin Peterspim-simons
authored
feat: Add option InjectAsBase64 for ARM (#438)
* Add InjectAsBase64 * Add docs * Remove version 1.3.1 and update preview * Remove sidebars * Rename * Fix unit test * seperate test for windows and linux since base64 encoding seems to be different on them --------- Co-authored-by: Martin Peters <Martin.Peters@codit.eu> Co-authored-by: Pim Simons <pim.simons@gmail.com>
1 parent 9a81242 commit 5c1457a

9 files changed

Lines changed: 72 additions & 2 deletions

File tree

docs/preview/03-Features/powershell/templates/arm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ It is possible to supply injection instructions in the injection annotation, thi
6060
| `EscapeJson` | Replace double quotes not preceded by a backslash with escaped quotes |
6161
| `ReplaceSpecialChars` | Replace newline characters with literal equivalents, tabs with spaces and `"` with `\"` |
6262
| `InjectAsJsonObject` | Tests if the content is valid JSON and makes sure the content is injected without surrounding double quotes |
63+
| `InjectAsBase64` | Converts the file to a base64 string and injects the result. Useful for binary files |
6364

6465
Usage of multiple injection instructions is supported as well, for example if you need both the `EscapeJson` and `ReplaceSpecialChars` functionality.
6566
The reference to the file to inject can either be a path relative to the 'parent' file or an absolute path.
@@ -70,6 +71,7 @@ ${ FileToInject = ".\Parent Directory\file.xml" }
7071
${ FileToInject = "c:\Parent Directory\file.xml" }
7172
${ FileToInject = ".\Parent Directory\file.xml", EscapeJson, ReplaceSpecialChars }
7273
${ FileToInject = '.\Parent Directory\file.json', InjectAsJsonObject }
74+
${ FileToInject = '.\Parent Directory\file.json', InjectAsBase64 }
7375
```
7476

7577
### 🥇 Recommendations

src/Arcus.Scripting.ARM/Scripts/Inject-ArmContent.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
${ FileToInject = "c:\Parent Directory\file.xml" }
99
${ FileToInject = ".\Parent Directory\file.xml", EscapeJson, ReplaceSpecialChars }
1010
${ FileToInject = '.\Parent Directory\file.json', InjectAsJsonObject }
11-
#>
11+
${ FileToInject = '.\Parent Directory\file.json', InjectAsBase64 }
12+
#>
1213

1314
param(
1415
[string] $Path = $PSScriptRoot
@@ -106,6 +107,13 @@ function InjectFile {
106107
Write-Warning "Content to inject into ARM template file '$filePath' cannot be parsed as a JSON object!"
107108
}
108109
}
110+
111+
if ($optionParts.Contains("InjectAsBase64")) {
112+
Write-Verbose "Read binary file as base64 string"
113+
114+
$bytes = [System.Text.Encoding]::UTF8.GetBytes($newString)
115+
$newString = [Convert]::ToBase64String($bytes)
116+
}
109117
}
110118

111119
if ($surroundContentWithDoubleQuotes) {

src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.ARM.tests.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ InModuleScope Arcus.Scripting.ARM {
5757
$originalContents | Out-File -FilePath $armTemplateFile
5858
}
5959
}
60+
It "Replaces file path with file contents as base64 string (windows)" {
61+
# Arrange
62+
$armTemplateFile = "$PSScriptRoot\Files\arm-template-certificate.json"
63+
$originalContents = Get-Content $armTemplateFile
64+
try {
65+
# Act
66+
Inject-ArmContent -Path $armTemplateFile
67+
68+
# Assert
69+
$expected = Get-Content "$PSScriptRoot\Files\arm-template-certificate-value (windows).txt"
70+
$actual = Get-Content $armTemplateFile
71+
$actual[7] | Should -Be (' "value": "{0}",' -f $expected)
72+
} finally {
73+
$originalContents | Out-File -FilePath $armTemplateFile
74+
}
75+
}
6076
} else {
6177
It "Replaces relative file path with file contents as JSON object (linux)" {
6278
# Arrange
@@ -94,6 +110,22 @@ InModuleScope Arcus.Scripting.ARM {
94110
$originalContents | Out-File -FilePath $armTemplateFile
95111
}
96112
}
113+
It "Replaces file path with file contents as base64 string (linux)" {
114+
# Arrange
115+
$armTemplateFile = "$PSScriptRoot\Files\arm-template-certificate.json"
116+
$originalContents = Get-Content $armTemplateFile
117+
try {
118+
# Act
119+
Inject-ArmContent -Path $armTemplateFile
120+
121+
# Assert
122+
$expected = Get-Content "$PSScriptRoot\Files\arm-template-certificate-value (linux).txt"
123+
$actual = Get-Content $armTemplateFile
124+
$actual[7] | Should -Be (' "value": "{0}",' -f $expected)
125+
} finally {
126+
$originalContents | Out-File -FilePath $armTemplateFile
127+
}
128+
}
97129
}
98130
It "Replaces file path with file contents as escaped JSON and replaced special characters" {
99131
# Arrange

src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.Tests.Integration.pssproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
<Content Include="appsettings.local.json" />
7373
<Content Include="appsettings.json" />
7474
<Content Include="Blobs\arcus.png" />
75+
<Content Include="Files\arm-template-certificate-value %28linux%29.txt" />
76+
<Content Include="Files\arm-template-certificate-value %28windows%29.txt" />
77+
<Content Include="Files\arm-template-certificate.json" />
7578
<Content Include="Files\arm-template-object %28linux%29.json" />
7679
<Content Include="Files\arm-template-object-absolutepath %28linux%29.json" />
7780
<Content Include="Files\arm-template-object-absolutepath %28windows%29.json" />
@@ -82,6 +85,7 @@
8285
<Content Include="Files\arm-template-object %28windows%29.json" />
8386
<Content Include="Files\arm-template-inline.json" />
8487
<Content Include="Files\arm-template-inline-value.json" />
88+
<Content Include="Files\codit.eu.crt" />
8589
<Content Include="Files\IntegrationAccount\Agreements\agreement1.json" />
8690
<Content Include="Files\IntegrationAccount\Agreements\agreement2.json" />
8791
<Content Include="Files\IntegrationAccount\Assemblies\AssemblyThatDoesSomething.dll" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURuakNDQXlPZ0F3SUJBZ0lTQkF6dE9RWWo1U0lSeWIwZ3NhV2VJUVUxTUFvR0NDcUdTTTQ5QkFNRE1ESXgKQ3pBSkJnTlZCQVlUQWxWVE1SWXdGQVlEVlFRS0V3MU1aWFFuY3lCRmJtTnllWEIwTVFzd0NRWURWUV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NEJldXBxbzl6RW82U0xBOUh5QnJBV01xUUIyQU9DU3Mvd01IY2puYURZZjNtRzVsazBLVW5nWmluTFdjc1N3ClRhVnRiMVFFQUFBQmswRENCWFlBQUFRREFFY3dSUUloQU9yZzJGRERtZWlVUlFsaWRDbWczdFVLZ2oyUWtNemkKazBVTGduZU1BSFlzQWlBSSs2Vy9PMjg5d2pRdjRqVUt0ZGRzWFl5N2IyN1Z1c0hIRmxTRDdoZjFQREFLQmdncQpoa2pPUFFRREF3TnBBREJtQWpFQWhjVUVUakFiTXJkcHpwbzlldVlzWHdLU1NqcUovVUs4YmRJREZNSlJxNGlFClUxL0YvZGoxajJUK1dVSVFnNjJ2QWpFQXRreXpRSlFKUEgxY0VDY2ttT2J5TzhwWUR6dDdrWlFRUlN4OEVqclMKSEo4Q042d3Nmb1ErRldHMWpOSEJxU0JTCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tDQpNSUlEbmpDQ0F5T2dBd0lCQWdJU0JBenRPUVlqNVNJUnliMGdzYVdlSVFVMU1Bb0dDQ3FHU000OUJBTURNREl4DQpDekFKQmdOVkJBWVRBbFZUTVJZd0ZBWURWUVFLRXcxTVpYUW5jeUJGYm1OeWVYQjBNUXN3Q1FZRFZRUURFd0pGDQpOVEFlRncweU5ERXhNVGd4T0RNeU5EZGFGdzB5TlRBeU1UWXhPRE15TkRaYU1CTXhFVEFQQmdOVkJBTVRDR052DQpaR2wwTG1WMU1IWXdFQVlIS29aSXpqMENBUVlGSzRFRUFDSURZZ0FFVWJ6dU9pUW82alo0V01iRmJEYUxsb1pXDQpvalh2MXlzcG92cVF4Wko1UlRFQTJoL1o3dm1rMjNVRmJ1NGZ2MDhtOGpVN3BuVjR0Zk1MTld2M1VramlybnV6DQo5K0k2UEVqUlVieUt6UEoyM3EzYldrd1A3W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BBREJtQWpFQWhjVUVUakFiTXJkcHpwbzlldVlzWHdLU1NqcUovVUs4YmRJREZNSlJxNGlFDQpVMS9GL2RqMWoyVCtXVUlRZzYydkFqRUF0a3l6UUpRSlBIMWNFQ2NrbU9ieU84cFlEenQ3a1pRUVJTeDhFanJTDQpISjhDTjZ3c2ZvUStGV0cxak5IQnFTQlMNCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0NCg==
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{
1+
{
22
"test": "this is a test value"
33
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDnjCCAyOgAwIBAgISBAztOQYj5SIRyb0gsaWeIQU1MAoGCCqGSM49BAMDMDIx
3+
CzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MQswCQYDVQQDEwJF
4+
NTAeFw0yNDExMTgxODMyNDdaFw0yNTAyMTYxODMyNDZaMBMxETAPBgNVBAMTCGNv
5+
ZGl0LmV1MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEUbzuOiQo6jZ4WMbFbDaLloZW
6+
ojXv1yspovqQxZJ5RTEA2h/Z7vmk23UFbu4fv08m8jU7pnV4tfMLNWv3Ukjirnuz
7+
9+I6PEjRUbyKzPJ23q3bWkwP7XRcQNFPevOcjp/9o4ICGTCCAhUwDgYDVR0PAQH/
8+
BAQDAgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAMBgNVHRMBAf8E
9+
AjAAMB0GA1UdDgQWBBQu6u/wlVHM3dc5VOHNqVxWL4jV4zAfBgNVHSMEGDAWgBSf
10+
K1/PPCFPnQS37SssxMZwi9LXDTBVBggrBgEFBQcBAQRJMEcwIQYIKwYBBQUHMAGG
11+
FWh0dHA6Ly9lNS5vLmxlbmNyLm9yZzAiBggrBg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