---
policy: /ce/ca/azure/subscription/application-insights-configured
logic: /ce/ca/azure/subscription/application-insights-configured/prod.logic.yaml
executionTime: 2025-04-24T23:46:13.120032315Z
rows:
  - id: test1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10__Azure_Application_Insights_Components__r.has(COMPLIANT)
      actual: CA10__Azure_Application_Insights_Components__r.has(COMPLIANT)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/subscription/application-insights-configured/policy.yaml
    md5Hash: 0693EA0106DC79C22C2A68A20E8101E2
    content: |
      ---
      names:
        full: Azure Subscription Application Insights are not configured
        contextual: Application Insights are not configured
      description: "Application Insights within Azure act as an Application Performance\
        \ Monitoring solution providing valuable data into how well an application performs\
        \ and additional information   when performing incident response. The types of log\
        \ data collected include application metrics, telemetry data, and application trace\
        \ logging data providing organizations with detailed information about application\
        \ activity and application transactions."
      type: COMPLIANCE_POLICY
      categories:
        - RELIABILITY
        - PERFORMANCE
      frameworkMappings:
        - "/frameworks/cis-azure-v2.1.0/05/03/01"
        - "/frameworks/cis-azure-v3.0.0/06/03/01"
        - "/frameworks/cloudaware/logging-and-monitoring/logging-and-monitoring-configuration"
      similarPolicies:
        cloudConformity:
          - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/azure/Monitor/diagnostic-logs-supported-resources.html
            name: Enable Diagnostic Logs for the Supported Resources
  - path: /ce/ca/azure/subscription/application-insights-configured/prod.logic.yaml
    md5Hash: 33467AC5DC6B1ADEB06D39F5E467D580
    content: "---\ninputType: \"CA10__CaAzureAccount__c\"\ntestData:\n  - file: \"\
      test-data.json\"\nconditions:\n  - status: \"COMPLIANT\"\n    currentStateMessage:\
      \ \"Application Insights are configured and successfully provisioned.\"\n  \
      \  check:\n      RELATED_LIST_HAS:\n        relationshipName: \"CA10__Azure_Application_Insights_Components__r\"\
      \n        status: \"COMPLIANT\"\notherwise:\n  status: \"INCOMPLIANT\"\n  currentStateMessage:\
      \ \"Application Insights are not configured.\"\nrelatedLists:\n  - relationshipName:\
      \ \"CA10__Azure_Application_Insights_Components__r\"\n    conditions: []\n \
      \   otherwise: \n      status: \"COMPLIANT\"\n      currentStateMessage: \"\
      Application Insights are configured.\"\n"
  - path: /ce/ca/azure/subscription/application-insights-configured/test-data.json
    md5Hash: 6E381629E6A21E199FF7653D126FAD16
    content: |-
      [
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "CA10__Azure_Application_Insights_Components__r.has(COMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test1",
              "CA10__Azure_Application_Insights_Components__r": [
                  {
                      "Id": "test1_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__subscription__c": "test1"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test2",
              "CA10__Azure_Application_Insights_Components__r": [
                  {
                      "Id": "test2_1",
                      "CA10__disappearanceTime__c": "2024-06-13T04:17:29Z",
                      "CA10__subscription__c": "test2"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test3",
              "CA10__Azure_Application_Insights_Components__r": []
          }
      ]
script: |-
  CREATE TEMP FUNCTION mock_ExpectedResult()
  RETURNS ARRAY<STRUCT<
          Id STRING,
          expectedResult STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, runtimeError STRING>
      >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "Id" : "test1",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "CA10__Azure_Application_Insights_Components__r.has(COMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureAccount__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test3"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureApplicationInsightsComponent__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__subscription__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test1_1",
    "CA10__subscription__c" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test2_1",
    "CA10__disappearanceTime__c" : new Date("2024-06-13T04:17:29Z"),
    "CA10__subscription__c" : "test2"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureAccount__c(
      obj STRUCT<
          Id STRING,
          CA10__Azure_Application_Insights_Components__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__subscription__c STRING,
              result STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
          >>
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      if (false) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared()", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      var count_CA10__Azure_Application_Insights_Components__r_COMPLIANT2 = 0;
      if (obj.CA10__Azure_Application_Insights_Components__r != null) {
          for (var i3 = 0; i3 < obj.CA10__Azure_Application_Insights_Components__r.length; i3++) {
              if (obj.CA10__Azure_Application_Insights_Components__r[i3].result.status == 'COMPLIANT') {
                  count_CA10__Azure_Application_Insights_Components__r_COMPLIANT2 += 1;
              }
          }
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Related list [CA10__Azure_Application_Insights_Components__r] ' + (count_CA10__Azure_Application_Insights_Components__r_COMPLIANT2 > 0 ? 'has' : 'does not have') + ' objects in COMPLIANT status');
      if (count_CA10__Azure_Application_Insights_Components__r_COMPLIANT2 > 0) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "CA10__Azure_Application_Insights_Components__r.has(COMPLIANT)", currentStateMessage: "Application Insights are configured and successfully provisioned.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "Application Insights are not configured.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__Azure_Application_Insights_Components__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__subscription__c STRING
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Azure [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c);
      if (obj.CA10__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 100, conditionText: "otherwise", currentStateMessage: "Application Insights are configured.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;


  SELECT
    expectedResult.Id as Id,
    IF (
      IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '')
      AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1)
      AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '')
      AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''),
      "MATCH",
      "FAIL"
    ) as match,
    expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus,
    expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex,
    expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText,
    expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError
  FROM UNNEST(mock_ExpectedResult()) expectedResult
  LEFT JOIN (
      SELECT
          sObject.Id AS Id,
          `CA10__Azure_Application_Insights_Components__r`.arr AS CA10__Azure_Application_Insights_Components__r,
          process_CA10__CaAzureAccount__c(
              STRUCT(
                  sObject.Id AS Id,
                  `CA10__Azure_Application_Insights_Components__r`.arr AS CA10__Azure_Application_Insights_Components__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureAccount__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10__subscription__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__subscription__c AS CA10__subscription__c,
                      process_CA10__Azure_Application_Insights_Components__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__subscription__c AS CA10__subscription__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaAzureApplicationInsightsComponent__c()) AS sObject
          GROUP BY sObject.CA10__subscription__c
      ) AS `CA10__Azure_Application_Insights_Components__r`
          ON sObject.Id = `CA10__Azure_Application_Insights_Components__r`.CA10__subscription__c
  ) sObject ON sObject.Id = expectedResult.Id;
