Skip to content

Commit 5eb3173

Browse files
Fix to support @secure() values in Bicep (#25586)
1 parent a69f35c commit 5eb3173

File tree

7 files changed

+210
-116
lines changed

7 files changed

+210
-116
lines changed

src/Resources/ResourceManager/Json/PSJsonSerializer.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
using System.Management.Automation.Internal;
2222
using System.Management.Automation.Language;
2323
using System.Reflection;
24+
using System.Security;
2425
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
26+
using Microsoft.WindowsAzure.Commands.Common;
2527
using Newtonsoft.Json;
2628
using Newtonsoft.Json.Linq;
2729

@@ -31,17 +33,20 @@ public static class PSJsonSerializer
3133
{
3234
public struct SerializeContext
3335
{
34-
public SerializeContext(int maxDepth)
36+
public SerializeContext(int maxDepth, bool serializeSecureString)
3537
{
3638
this.MaxDepth = maxDepth;
39+
this.SerializeSecureString = serializeSecureString;
3740
}
3841

3942
public int MaxDepth { get; }
43+
44+
public bool SerializeSecureString { get; }
4045
}
4146

42-
public static string Serialize(object value)
47+
public static string Serialize(object value, bool serializeSecureString = false)
4348
{
44-
var context = new SerializeContext(1024);
49+
var context = new SerializeContext(1024, serializeSecureString);
4550

4651
return Serialize(value, context);
4752
}
@@ -77,6 +82,19 @@ private static object ProcessValue(object value, int currentDepth, SerializeCont
7782
value = psObject.BaseObject;
7883
}
7984

85+
if (value is SecureString secureString)
86+
{
87+
// This requires a conscious opt-in, rather than being the default behavior - to avoid accidentally leaking sensitive information.
88+
if (context.SerializeSecureString)
89+
{
90+
return secureString.ConvertToString();
91+
}
92+
else
93+
{
94+
throw new InvalidOperationException("Unable to serialize secure string value");
95+
}
96+
}
97+
8098
if (value == NullString.Value || value == DBNull.Value)
8199
{
82100
return null;

src/Resources/ResourceManager/Utilities/BicepUtility.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public BicepBuildParamsStdout BuildBicepParamFile(string bicepParamFilePath, IRe
128128
{
129129
CheckMinimalVersionRequirement(MinimalVersionRequirementForBicepparamFileBuildWithInlineOverrides);
130130
writeVerbose?.Invoke($"Overriding the following parameters: {string.Join(", ", overrideParams.Keys)}");
131-
envVars["BICEP_PARAMETERS_OVERRIDES"] = PSJsonSerializer.Serialize(overrideParams);
131+
// As per https://github.com/Azure/bicep/issues/12481, secure string parameters must be serialized.
132+
envVars["BICEP_PARAMETERS_OVERRIDES"] = PSJsonSerializer.Serialize(overrideParams, serializeSecureString: true);
132133
}
133134

134135
var stdout = RunBicepCommand(

src/Resources/Resources.Test/ScenarioTests/DeploymentTests.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,8 @@ function Test-NewDeploymentFromBicepparamFileWithOverrides
976976
"def": "ghi"
977977
},
978978
"int": 42,
979-
"bool": true
979+
"bool": true,
980+
"secureString": "glabble"
980981
}
981982
'@ | ConvertFrom-Json
982983

@@ -986,7 +987,12 @@ function Test-NewDeploymentFromBicepparamFileWithOverrides
986987
New-AzResourceGroup -Name $rgname -Location $rglocation
987988

988989
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateParameterFile deployWithParamOverrides.bicepparam `
989-
-myArray @("abc") -myObject @{"def" = "ghi";} -myString "hello" -myInt 42 -myBool $true
990+
-myArray @("abc") `
991+
-myObject @{"def" = "ghi";} `
992+
-myString "hello" `
993+
-myInt 42 `
994+
-myBool $true `
995+
-mySecureString (ConvertTo-SecureString -String "glabble" -AsPlainText -Force)
990996

991997
# Assert
992998
Assert-AreEqual Succeeded $deployment.ProvisioningState

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.DeploymentTests/TestNewDeploymentFromBicepparamFileWithOverrides.json

Lines changed: 173 additions & 110 deletions
Large diffs are not rendered by default.

src/Resources/Resources.Test/deployWithParamOverrides.bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ param myString string
33
param myObject object
44
param myInt int
55
param myBool bool
6+
@secure()
7+
param mySecureString string
68

79
output all object = {
810
array: myArray
911
string: myString
1012
object: myObject
1113
int: myInt
1214
bool: myBool
15+
#disable-next-line outputs-should-not-contain-secrets
16+
secureString: mySecureString
1317
}

src/Resources/Resources.Test/deployWithParamOverrides.bicepparam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ param myObject = {}
55
param myString = ''
66
param myInt = 0
77
param myBool = false
8+
param mySecureString = ''

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed overriding of Bicep parameters in Deployment cmdlets to support `SecureString` parameters.
2223
* Added Test cmdlets for Deployment Stacks.
2324

2425
## Version 7.2.0

0 commit comments

Comments
 (0)