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

Improve YAML Custom Policy for Checking Connection Dependencies #6951

Open
MatousVondal opened this issue Jan 15, 2025 · 0 comments
Open

Improve YAML Custom Policy for Checking Connection Dependencies #6951

MatousVondal opened this issue Jan 15, 2025 · 0 comments
Labels
contribution requested This is a great feature idea, but we will need a contribution to get it added to Checkov.

Comments

@MatousVondal
Copy link

Issue Description: I am trying to create a YAML custom policy to check the connection dependencies for all resources connected with azurerm_monitor_diagnostic_setting and ensure that all possible logs are enabled.

Current Implementation:

terraform

resource "azurerm_monitor_diagnostic_setting" "firewall" {
    name = "${local.project_prefix}-frwllogs${random_string.int[0].result}"
    
    target_resource_id = azurerm_firewall.firewall.id
    
    log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics.id
    
    log_analytics_destination_type = "Dedicated"
    
    enabled_log {
        category_group = "allLogs"
    }
    
    metric {
        category = "AllMetrics"
        enabled  = true
    }
}

yaml custom policy

metadata:
  id: "CUSTOM_DISGNOSTIC_SETTINGS_POLICY"
  name: "Audit diagnostic setting for selected resource types id:7f89b1eb-583c-429a-8828-af049802c1d9"
  category: "CONVENTION"
  severity: "HIGH"

scope:
  provider: "azure"

definition:

  and:

    - cond_type: "connection"
      resource_types:
        - "azurerm_monitor_diagnostic_setting"
      connected_resource_types:
        - "azurerm_virtual_network"
        - "azurerm_firewall"
        - "azurerm_network_security_group"
        - "azurerm_virtual_network_gateway"
        - "azurerm_public_ip"
        - "azurerm_linux_virtual_machine"
        - "azurerm_windows_virtual_machine"
        - "azurerm_network_interface"
        - "azurerm_application_gateway"
        - "azurerm_key_vault"
      operator: "exists"

    - cond_type: "attribute"
      resource_types:
        - "azurerm_monitor_diagnostic_setting"
      attribute: enabled_log.category_group
      operator: "equals"
      value: "allLogs"

    - cond_type: "attribute"
      resource_types:
        - "azurerm_monitor_diagnostic_setting"
      attribute: metric.category
      operator: "equals"
      value: "AllMetrics"

Problem: Currently, I have to explicitly define all resources where I want to control the existing connections. A better solution would be to use a negation of the connections for resources I do not want to check if diagnostic settings are enabled.

metadata:
  id: "OHLA_CUSTOM_DISGNOSTIC_SETTINGS_POLICY"
  name: "Audit diagnostic setting for selected resource types id:7f89b1eb-583c-429a-8828-af049802c1d9"
  category: "CONVENTION"
  severity: "HIGH"

scope:
  provider: "azure"

definition:

  and:
    - not:
        - cond_type: "connection"
          resource_types:
            - "azurerm_monitor_diagnostic_setting"
          connected_resource_types:
            - "azurerm_network_interface_security_group_association"
            - "azurerm_subnet_route_table_association"
            - "azurerm_network_security_group_security_rule"
            - "azurerm_network_security_rule"
            - "azurerm_monitor_diagnostic_setting"
            - "azurerm_virtual_network_peering"
            - "azurerm_route"
            - "azurerm_subnet"
            - "azurerm_private_dns_zone_virtual_network_link"
            - "azurerm_subnet_network_security_group_association"
          operator: "exists"

    - cond_type: "attribute"
      resource_types:
        - "azurerm_monitor_diagnostic_setting"
      attribute: enabled_log.category_group
      operator: "equals"
      value: "allLogs"

    - cond_type: "attribute"
      resource_types:
        - "azurerm_monitor_diagnostic_setting"
      attribute: metric.category
      operator: "equals"
      value: "AllMetrics"

Issue: The attempted solution with negation does not work. Is there a way to specify which connections I do not want to check and then check all other resources that fall under "all"?

@MatousVondal MatousVondal added the contribution requested This is a great feature idea, but we will need a contribution to get it added to Checkov. label Jan 15, 2025
@MatousVondal MatousVondal reopened this Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution requested This is a great feature idea, but we will need a contribution to get it added to Checkov.
Projects
None yet
Development

No branches or pull requests

1 participant