[BUG] Expansion fails with "Hash must be finalized before the hash value is retrieved" when using an object output from a cross-scope module
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 62/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- azure, powershell
- 領域
- cloud, devops, infrastructure
調査の方向性
まず DeploymentVisitor.GetDeploymentScope と ExpressionHelpers.GetUnique を見つけ、次に symbolicNameCodegen を有効にして、提供されている main.bicep、child.bicep、assignments.bicep ファイルで失敗を再現します。明示的なクロススコープ デプロイメントがそのオブジェクト出力を正しく解決し、guid() がハッシュの最終化エラーや重複するリソース名なしで展開されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Existing rule
N/A
Description of the issue
When a Bicep module is deployed to a different scope using the scope property (for example a nested management group), the resource ID recorded for the nested deployment does not account for the explicit scope.
DeploymentVisitor.GetDeploymentScope resolves the subscriptionId, resourceGroup and managementGroup properties of a deployment resource, but ignores the scope property. As a result the nested deployment is registered under its real scope, while the symbol resolves to the parent context scope. These do not match, so TemplateContext.TryGetResource fails to find the deployment.
reference() then falls back to a synthetic mock, and a chain such as reference('customRoles').outputs.policyReader.value.id resolves to an empty value instead of the expected string. When that empty value is passed to guid() as the last argument, expansion fails with:
Hash must be finalized before the hash value is retrieved
There are two defects here:
- The explicit
scopeproperty of a deployment resource is not used when calculating the resource ID, so cross-scope module outputs do not resolve. ExpressionHelpers.GetUniqueonly callsTransformFinalBlockwhen the final argument can be converted to a string. If it cannot, the hash is never finalized and readingHashAlgorithm.Hashthrows. An argument that cannot be converted in any other position is silently skipped instead, which would produce a duplicate name rather than an error.
Error messages
Hash must be finalized before the hash value is retrieved
Inner stack trace:
at System.Security.Cryptography.HashAlgorithm.get_Hash()
at PSRule.Rules.Azure.Arm.Expressions.ExpressionHelpers.GetUnique(Object[] args)
at PSRule.Rules.Azure.Arm.Expressions.Functions.Guid(ITemplateContext context, Object[] args)
Reproduction
The module deployed with scope set to a child management group is required to reproduce the issue. Without scope the same template expands correctly.
main.bicep:
targetScope = 'managementGroup'
resource intermediateRoot 'Microsoft.Management/managementGroups@2023-04-01' = {
scope: tenant()
name: 'mg-intermediate-root'
properties: {
displayName: 'Intermediate Root'
}
}
module customRoles './child.bicep' = {
name: 'customRoles'
scope: intermediateRoot
}
module roleAssignments './assignments.bicep' = {
name: 'roleAssignments'
scope: intermediateRoot
params: {
roleAssignments: [
{
principalId: '00000000-0000-0000-0000-000000000001'
roleDefinitionId: customRoles.outputs.policyReader.id
}
{
principalId: '00000000-0000-0000-0000-000000000002'
roleDefinitionId: customRoles.outputs.policyReader.id
}
]
}
}
child.bicep:
targetScope = 'managementGroup'
resource roleDefinition 'Microsoft.Authorization/roleDefinitions@2022-04-01' = {
name: guid('policyReader', managementGroup().id)
properties: {
roleName: 'Policy Reader'
description: 'Read only access to policy.'
type: 'CustomRole'
permissions: [
{
actions: [
'Microsoft.Authorization/policyAssignments/read'
]
}
]
assignableScopes: [
managementGroup().id
]
}
}
output policyReader object = {
id: roleDefinition.id
name: roleDefinition.name
}
assignments.bicep:
targetScope = 'managementGroup'
type roleAssignmentInput = {
principalId: string
roleDefinitionId: string
}
param roleAssignments roleAssignmentInput[]
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = [
for assignment in roleAssignments: {
name: guid(managementGroup().id, assignment.principalId, assignment.roleDefinitionId)
properties: {
principalId: assignment.principalId
principalType: 'ServicePrincipal'
roleDefinitionId: assignment.roleDefinitionId
}
}
]
Expansion of main.bicep fails. Reducing the name to guid(assignment.roleDefinitionId) also fails, which isolates the failure to that argument. Removing scope: intermediateRoot from the modules allows expansion to succeed.
The template requires symbolicNameCodegen to be enabled in bicepconfig.json to produce the reference('customRoles') form.
Version of PSRule
2.9.0
Version of PSRule for Azure
1.48.0
Additional context
Note that fixing only the hash finalization is not sufficient, and would be worse than the current behaviour. The unresolved argument would be silently skipped, so every role assignment sharing a principal would collapse to the same guid() value and expansion would emit duplicate resource names instead of an error. The scope resolution needs to be fixed so the value resolves correctly.
- 主要言語
- PowerShell
- スター
- 449
- フォーク
- 111
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 8
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Azure/PSRule.Rules.Azure のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Azure/PSRule.Rules.Azure#3929 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
Export-AzRuleData fails with HTTP 400 for DefenderForStorageSettings due to deprecated API versionオープンbug feature: in-flight-export
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
Azure/PSRule.Rules.Azure#3865 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 75/100
Azure/PSRule.Rules.Azure#3920 ·
メンテナーはふだん 2 日以内に返信
-
bug feature: bicep-language feature: pre-flight-expansion
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
Azure/PSRule.Rules.Azure#3907 ·
メンテナーはふだん 2 日以内に返信
-
bug feature: bicep-language feature: pre-flight-expansion
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
Azure/PSRule.Rules.Azure#3884 ·
メンテナーはふだん 2 日以内に返信
Azure/PSRule.Rules.Azure の issue をすべて見る
似ている issue
-
Needs: Triage :mag: Product: Terraform (AVM) Topic: Networking (HS) :globe_with_meridians:
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
Azure/Azure-Landing-Zones#4291 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
googleapis/google-cloud-dart#366 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
kedacore/keda#8225 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug status/needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
prowler-cloud/prowler#12887 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-deployment triage:bot-seen triage:needs-human
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
microsoft/aspire#20533 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信