Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block nested deployment resources with inner scoped evaluation and symbolic references to outer scope #11884

Conversation

jeskew
Copy link
Contributor

@jeskew jeskew commented Sep 19, 2023

Resolves #11846

This PR updates EmitLimitationCalculator to emit an error when a resource of type Microsoft.Resources/deployments uses inner scoped expression evaluation but refers to symbols defined in the outer scope from within the template property.

This PR may be a breaking change for some templates, as the added check may catch scenarios that would not result in a runtime deployment failure, e.g., when a template has a symbol with the same name and type defined in both contexts:

var fizz = 'buzz'

resource nested 'Microsoft.Resources/deployments@2020-10-01' = {
  name: 'name'
  properties: {
    mode: 'Incremental'
    expressionEvaluationOptions: {
      scope: 'inner'
    }
    template: {
      '$schema': 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#'
      contentVersion: '1.0.0.0'
      variables: {
        fizz: 'pop'
      }
      resources: [
        {
          apiVersion: '2022-09-01'
          type: 'Microsoft.Resources/tags'
          name: 'default'
          properties: {
            tags: {
              tag1: fizz // <-- Not an error, but surprise! Its value is 'pop', not 'buzz'
            }
          }
        }
      ]
    }
  }
}

In that case, the user may be using a Bicep symbolic reference to save a few characters, but what the template will do (dereference the inner-scoped symbol) does not line up with the communicated intent of the syntax (i.e., to dereference the outer-scoped symbol), so I chose to flag that as an error, too, as this usage would almost certainly represent a logical error even if the compiled nested deployment would not raise a runtime error.

Microsoft Reviewers: Open in CodeFlow

@jeskew jeskew requested a review from a team September 19, 2023 14:01
@github-actions
Copy link
Contributor

github-actions bot commented Sep 19, 2023

Test this change out locally with the following install scripts (Action run 6278976773)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 6278976773
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 6278976773"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 6278976773
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 6278976773"

@github-actions
Copy link
Contributor

github-actions bot commented Sep 19, 2023

Test Results

     132 files  ±0       132 suites  ±0   3h 34m 28s ⏱️ + 4m 20s
10 553 tests +1  10 553 ✔️ +1  0 💤 ±0  0 ±0 
50 763 runs  +4  50 763 ✔️ +4  0 💤 ±0  0 ±0 

Results for commit 995d0fa. ± Comparison against base commit e964970.

♻️ This comment has been updated with latest results.

Copy link
Member

@majastrz majastrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@anthony-c-martin
Copy link
Member

[nit] Shouldn't this be a linter rule rather than part of the emit limitation calculations?

@jeskew
Copy link
Contributor Author

jeskew commented Sep 22, 2023

[nit] Shouldn't this be a linter rule rather than part of the emit limitation calculations?

I thought this fit better conceptually with other emit limitations, but moving this to a linter instead would give users who are impacted by the breaking change a way to disable the check if they want to.

@jeskew jeskew requested review from majastrz and a team September 22, 2023 19:05
@jeskew jeskew merged commit 5f83b12 into main Sep 22, 2023
47 checks passed
@jeskew jeskew deleted the jeskew/emit-diagnostic-on-outer-scoped-ref-for-inner-evaluated-nested-deployment branch September 22, 2023 20:48
@anthony-c-martin
Copy link
Member

[nit] Shouldn't this be a linter rule rather than part of the emit limitation calculations?

I thought this fit better conceptually with other emit limitations, but moving this to a linter instead would give users who are impacted by the breaking change a way to disable the check if they want to.

Just realized I didn't explain my thinking, so wanted to note it down:

It's a bit of a grey area - my opinion is generally that the core compiler logic should not be resource-aware - that's more the job of the linter. We have chosen to not fully block Microsoft.Resources/deployments, but we also haven't special-cased it in any way, so although it is technically "our" resource type, I feel like we don't and shouldn't treat it as such in Bicep.

In general, I suspect that people are utilizing this functionality accidentally without realizing they should be instead using modules. I feel like we should strongly discourage unless we can see a compelling reason to use it instead of modules; it's full of footguns like this one.

@maskati
Copy link

maskati commented Oct 24, 2023

FYI a use case of nested deployment template referencing outer scope is a native bicep implementation of #5805 (reply in thread). This is essentially a way of implementing role assignment extension deployments for generic parent resource types. Instead of loading an ARM JSON as in the linked comment, I define a Microsoft.Resources/deployments resource and parameterize this using outer scoped references to have the compiler generate ARM expressions for these within the template itself.

@anthony-c-martin
Copy link
Member

anthony-c-martin commented Oct 24, 2023

@maskati - I believe there's a simpler solution to this today, as we now allow direct referencing of .json templates with the module syntax - see this comment: #5805 (comment)

@maskati
Copy link

maskati commented Oct 24, 2023

Nice! I didn't know you could do that.

@StephenWeatherford
Copy link
Contributor

@anthony-c-martin cc @puicchan I'll be doing a round of rule fixes/additions in the next few months. Do you think we should have a linter rule warning against Microsoft.Resources.deployments usage period?

@jeskew
Copy link
Contributor Author

jeskew commented Oct 25, 2023

@StephenWeatherford we have one that was recently added: #11848

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding unused parameter with type expression fails deployment
5 participants