Azure / PSRule.Rules.Azure

Rules to validate Azure resources and infrastructure as code (IaC) using PSRule.
https://azure.github.io/PSRule.Rules.Azure/
MIT License
389 stars 84 forks source link

[BUG] Quotes get incorrectly duplicated #2593

Closed dmgonch closed 9 months ago

dmgonch commented 9 months ago

Existing rule

No response

Description of the issue

An expression that includes string() wrapped into single quotes produces incorrect result. See the correct result produced by az deployment group create --what-if ... command.

Error messages

The result of running repro steps below is incorrect - single quotes withing debug property should not be duplicated:

debug       : A''{"B":5}''
debugLength : 12

Reproduction

Create bug.bicep:

var task = {
  name: 'task1'
  parameters: {
    B: 5
  }
}

var tasks = [
  {
    name: task.name
    parameters: {
      debug: 'A\'${string(task.parameters)}\''
      debugLength: length('A\'${string(task.parameters)}\'')
    }
  }
]

#disable-next-line BCP081 no-deployments-resources
resource taskDeployment 'Microsoft.Resources/deployments@2020-10-01' = {
  name: 'name'
  properties: {
    mode: 'Incremental'
    template: {
      '$schema': 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#'
      contentVersion: '1.0.0.0'
      resources: [
        {
          apiVersion: '2019-12-01'
          type: 'Microsoft.ManagedIdentity/userAssignedIdentities'
          name: 'test'
          properties: {
            tasks: tasks
          }
        }
      ]
    }
  }
}

From powershell run:

az bicep build --file bug.bicep; Export-AzRuleTemplateData -TemplateFile bug.json -OutputPath bug-export.json; (Get-Content -Raw bug-export.json | ConvertFrom-Json | Where-Object { $_.type -eq 'Microsoft.ManagedIdentity/userAssignedIdentities' }).properties.tasks.parameters

Running az deployment group create --what-if -x Ignore --subscription <SUB-ID> -g <RG-NAME> -f bug.bicep produces the expected result:

          parameters.debug:       "A'{"B":5}'"
          parameters.debugLength: 10

Version of PSRule

2.9.0

Version of PSRule for Azure

1.31.3

Additional context

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Linux 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
BernieWhite commented 9 months ago

Thanks for reporting the issue @dmgonch.