Skip to content

fix(misconf): recognize Azure Monitor diagnostic settings for storage logging - #11764

Open
bharathvemula wants to merge 2 commits into
aquasecurity:mainfrom
bharathvemula:fix/11754-storage-diagnostic-logging-forkbase
Open

bharathvemula wants to merge 2 commits into
aquasecurity:mainfrom
bharathvemula:fix/11754-storage-diagnostic-logging-forkbase

Conversation

@bharathvemula

Copy link
Copy Markdown

Description

Storage logging configured through Azure Monitor diagnostic settings was invisible to the misconfiguration checks: the IaC model had nowhere to record it, so AZU-0057 and AZU-0009 only ever saw Storage Analytics (queue_properties.logging) configuration. This PR adds per-service diagnostic logging to the Azure storage model and adapts azurerm_monitor_diagnostic_setting resources in the Terraform adapter, so the checks can recognise logging configured this way.

A service logs requests when a diagnostic setting targeting it enables the StorageRead, StorageWrite or StorageDelete log categories, or the allLogs category group. A setting targeting the storage account itself applies to all of its services.

  • New DiagnosticLogging struct on the storage Account (Blob, File, Queue, Table bools). It is per-service rather than account-level, because the two checks need different precision: AZU-0057 passes when at least one service logs, while AZU-0009 passes only when the queue service logs.
  • The adapter finds settings by reference in target_resource_id, and also by literal resource ID containing /storageaccounts/<name>/ (for settings defined with a literal ID instead of a reference). The target suffix selects the service; an unresolvable target is ignored rather than guessed.
  • Only request-logging categories count: other log categories (e.g. Transaction) and enabled_metric blocks do not. category_group = "audit" deliberately does not count, per the issue spec (only the three categories or allLogs).
  • Regenerated the cloud schema (pkg/iac/rego/schemas/cloud.json). This is required, not bookkeeping: the scanner type-checks check modules against this schema, so a Rego update referencing account.diagnosticlogging fails to compile without it (rego_type_error: undefined ref).

Before and after

Evaluated the real vendored AZU-0057 / AZU-0009 checks against the adapted model for the exact configuration in #11754, and again with the proposed Rego below:

Configuration Before (vendored checks) After (with proposed Rego)
Issue repro: blob diagnostic logging, BlobStorage, no queues AZU-0057 fails AZU-0057 passes
Queue service with queue diagnostic logging, no analytics logging AZU-0009 and AZU-0057 fail both pass
No logging at all AZU-0057 fails AZU-0057 still fails

Coordinated update in aquasecurity/trivy-checks

This PR is the trivy-side prerequisite; the checks themselves need the following update (see also aquasecurity/trivy-checks#637). Happy to open that PR as well once this one is reviewed.

storage_account_logging_enabled.rego (AZU-0057):

logging_disabled(account) if {
	value.is_false(account.queueproperties.enablelogging)
	not service_logs_requests(account)
}

# A service logs requests when it has a diagnostic setting that enables
# StorageRead, StorageWrite or StorageDelete, or the allLogs category group.
service_logs_requests(account) if value.is_true(account.diagnosticlogging.blob)

service_logs_requests(account) if value.is_true(account.diagnosticlogging.file)

service_logs_requests(account) if value.is_true(account.diagnosticlogging.queue)

service_logs_requests(account) if value.is_true(account.diagnosticlogging.table)

queue_services_logging_enabled.rego (AZU-0009): add import data.lib.cloud.value and one line in the deny rule:

	not account.queueproperties.enablelogging.value
	not value.is_true(account.diagnosticlogging.queue)

Out of scope / follow-ups

Related issues

Related PRs

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed). Not needed — no user-facing behaviour change beyond the checks seeing more configuration.
  • I've included a "before" and "after" example to the description (if the PR is a user interface change). Before/after check results are included above.

Testing

  • Added Test_AdaptDiagnosticLogging with eight cases: the issue's exact repro (blob service), queue service, allLogs category group, non-request category and metrics not counting, account-level setting applying to all services, dynamic enabled_log block, a setting targeting another account not counting, and a literal target resource ID. All pass, as do the existing Test_Adapt cases.
  • go test ./pkg/iac/adapters/terraform/azure/storage/ ./pkg/iac/adapters/arm/storage/ ./pkg/iac/rego/ -count=1 passes; go vet clean; gofmt clean; schema verify passes (go run -tags=mage_schema ./magefiles -- verify).
  • Verified end to end at the Rego level with a scratch test (removed before submitting): the real vendored checks were run against the adapted model for the issue repro, the queue case, and a no-logging control, with both the vendored and the proposed Rego above.

🤖 This PR was prepared with AI assistance (Muse). The fix was designed from the issue spec, reproduced against the real vendored checks, and verified with the tests listed above.

… logging

Storage logging configured through azurerm_monitor_diagnostic_setting was invisible to AZU-0057 and AZU-0009: the IaC model had no field for it, so the checks only ever saw Storage Analytics logging. Add per-service DiagnosticLogging to the storage account model and adapt diagnostic settings in the Terraform adapter, so a service counts as logging when a setting enables StorageRead, StorageWrite or StorageDelete, or the allLogs category group. Regenerate the cloud schema so the coordinated trivy-checks Rego update type-checks.

Signed-off-by: Bharath Vemula <vemula.sai8@gmail.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@nikpivkin nikpivkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ARM templates with Microsoft.Insights/diagnosticSettings are not adapted yet, and #11754 covers them too. ARM support needs to be added in this PR.

case strings.HasSuffix(target, "/tableservices/default"):
return "table"
default:
return "account"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Log categories like StorageRead and the allLogs group work only on service targets such as .../blobServices/default. A setting on the account itself supports only metrics, so Azure rejects it, but here it marks all four services as logging. A target that is not a service could be ignored.

for _, module := range modules {
for _, resource := range module.GetResourcesByType("azurerm_storage_account") {
account := adaptAccount(resource)
account := adaptAccount(resource, module)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Containers are looked up in adaptAccounts, and the diagnostic settings could be found there too instead of passing module to adaptAccount.

// an azurerm_monitor_diagnostic_setting resource, so the storage logging
// checks can see logging configured via Azure Monitor instead of only
// Storage Analytics logging.
func adaptDiagnosticLogging(module *terraform.Module, accountResource *terraform.Block, account *storage.Account) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The other helpers here return a value and do not change the account through a pointer. This one could take the found settings and return storage.DiagnosticLogging. If diagnosticSettingService returned a pointer to the field, or nil, the string constants and the mark closure would not be needed.

var diagnosticLogCategories = []string{"StorageRead", "StorageWrite", "StorageDelete"}

// adaptDiagnosticLogging marks the storage services that log requests through
// an azurerm_monitor_diagnostic_setting resource, so the storage logging

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The part starting with "so the storage logging checks" is about the checks, not this function, and could be removed.

add(setting)
}

// A setting in another file may target the account with a literal resource

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

References work across files of one module. The literal ID is needed when the setting has no reference to the account at all.

CustomerManagedKey CustomerManagedKey
}

// DiagnosticLogging records, per storage service, whether request logging is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same rule is repeated in the diagnosticLogCategories and diagnosticSettingLogsRequests comments. It could stay only here.

}
}

func Test_AdaptDiagnosticLogging(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These cases could go into the Test_Adapt table.

},
},
}
defaultAccount := func(logging storage.DiagnosticLogging) storage.Account {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The existing cases write the expected account in full inside the table. The new ones could do the same without defaultAccount, the shared orphanAccount and the function called right away at line 344.

expected storage.Storage
}{
{
// Exact reproduction from #11754: blob request logging is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment could be removed, the case name says enough.

},
},
{
name: "dynamic enabled_log block",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expanding dynamic blocks is the parser's job. This case could be removed.

bharathvemula added a commit to bharathvemula/trivy that referenced this pull request Nov 27, 2026
Address review feedback on aquasecurity#11764: add ARM template support for
Microsoft.Insights/diagnosticSettings (parallel to the Terraform
adapter), so AZU-0057/AZU-0009 pass when a service logs requests
through Azure Monitor. Settings are found by scope for top-level
resources or by nesting under a service resource; a setting targeting
the account itself is ignored, as it can only carry metrics.

Also refactor the Terraform adapter per review: look the settings up
in adaptAccounts and pass them to adaptAccount, have
adaptDiagnosticLogging return storage.DiagnosticLogging, and map a
setting to its DiagnosticLogging field directly instead of via string
constants and a mark closure. The log-category rule is now stated once
in the storage provider package, which owns the category list. The
diagnostic test cases move into the Test_Adapt table with full expected
accounts, and the dynamic-block case is dropped.

Signed-off-by: Satya Sai Bharath Vemula <vemula.sai8@gmail.com>
Address review feedback on aquasecurity#11764: add ARM template support for
Microsoft.Insights/diagnosticSettings (parallel to the Terraform
adapter), so AZU-0057/AZU-0009 pass when a service logs requests
through Azure Monitor. Settings are found by scope for top-level
resources or by nesting under a service resource; a setting targeting
the account itself is ignored, as it can only carry metrics.

Also refactor the Terraform adapter per review: look the settings up
in adaptAccounts and pass them to adaptAccount, have
adaptDiagnosticLogging return storage.DiagnosticLogging, and map a
setting to its DiagnosticLogging field directly instead of via string
constants and a mark closure. The log-category rule is now stated once
in the storage provider package, which owns the category list. The
diagnostic test cases move into the Test_Adapt table with full expected
accounts, and the dynamic-block case is dropped.

Signed-off-by: Satya Sai Bharath Vemula <vemula.sai8@gmail.com>
@bharathvemula
bharathvemula force-pushed the fix/11754-storage-diagnostic-logging-forkbase branch from 994c819 to 16280c6 Compare November 27, 2026 18:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants