Repository navigation
fix(misconf): recognize Azure Monitor diagnostic settings for storage logging - #11764
bharathvemula wants to merge 2 commits into
Conversation
… 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>
|
|
1 similar comment
|
|
| case strings.HasSuffix(target, "/tableservices/default"): | ||
| return "table" | ||
| default: | ||
| return "account" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
The same rule is repeated in the diagnosticLogCategories and diagnosticSettingLogsRequests comments. It could stay only here.
| } | ||
| } | ||
|
|
||
| func Test_AdaptDiagnosticLogging(t *testing.T) { |
There was a problem hiding this comment.
These cases could go into the Test_Adapt table.
| }, | ||
| }, | ||
| } | ||
| defaultAccount := func(logging storage.DiagnosticLogging) storage.Account { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This comment could be removed, the case name says enough.
| }, | ||
| }, | ||
| { | ||
| name: "dynamic enabled_log block", |
There was a problem hiding this comment.
Expanding dynamic blocks is the parser's job. This case could be removed.
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>
994c819 to
16280c6
Compare
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-0057andAZU-0009only ever saw Storage Analytics (queue_properties.logging) configuration. This PR adds per-service diagnostic logging to the Azure storage model and adaptsazurerm_monitor_diagnostic_settingresources 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,StorageWriteorStorageDeletelog categories, or theallLogscategory group. A setting targeting the storage account itself applies to all of its services.DiagnosticLoggingstruct on the storageAccount(Blob,File,Queue,Tablebools). It is per-service rather than account-level, because the two checks need different precision:AZU-0057passes when at least one service logs, whileAZU-0009passes only when the queue service logs.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.Transaction) andenabled_metricblocks do not.category_group = "audit"deliberately does not count, per the issue spec (only the three categories orallLogs).pkg/iac/rego/schemas/cloud.json). This is required, not bookkeeping: the scanner type-checks check modules against this schema, so a Rego update referencingaccount.diagnosticloggingfails to compile without it (rego_type_error: undefined ref).Before and after
Evaluated the real vendored
AZU-0057/AZU-0009checks against the adapted model for the exact configuration in #11754, and again with the proposed Rego below:BlobStorage, no queuesAZU-0057failsAZU-0057passesAZU-0009andAZU-0057failAZU-0057failsAZU-0057still failsCoordinated 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):queue_services_logging_enabled.rego(AZU-0009): addimport data.lib.cloud.valueand one line in the deny rule:Out of scope / follow-ups
Microsoft.Insights/diagnosticSettingsresources; that is a separate change.logblocks ofazurerm_monitor_diagnostic_settingare not read (onlyenabled_log).Related issues
Related PRs
Checklist
Testing
Test_AdaptDiagnosticLoggingwith eight cases: the issue's exact repro (blob service), queue service,allLogscategory group, non-request category and metrics not counting, account-level setting applying to all services, dynamicenabled_logblock, a setting targeting another account not counting, and a literal target resource ID. All pass, as do the existingTest_Adaptcases.go test ./pkg/iac/adapters/terraform/azure/storage/ ./pkg/iac/adapters/arm/storage/ ./pkg/iac/rego/ -count=1passes;go vetclean;gofmtclean; schema verify passes (go run -tags=mage_schema ./magefiles -- verify).🤖 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.