---
policy: /ce/ca/azure/subscription/bastion-host-exist
logic: /ce/ca/azure/subscription/bastion-host-exist/prod.logic.yaml
executionTime: 2025-04-24T23:46:13.877310666Z
rows:
  - id: test1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r.has(COMPLIANT)
      actual: CA10Z1__Azure_Virtual_Network_Bastion_Hosts__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/bastion-host-exist/policy.yaml
    md5Hash: 5487F17CCE1F52FB504BA25BA1885C16
    content: |
      ---
      names:
        full: Azure Subscription Bastion Host does not exist
        contextual: Bastion Host does not exist
      description: "The Azure Bastion service allows secure remote access to Azure Virtual\
        \ Machines over the Internet without exposing remote access   protocol ports and\
        \ services directly to the Internet. The Azure Bastion service provides this access\
        \ using TLS over 443/TCP, and subscribes to hardened configurations within an organization's\
        \ Azure Active Directory service."
      type: COMPLIANCE_POLICY
      categories:
        - SECURITY
      frameworkMappings:
        - "/frameworks/cis-azure-v2.1.0/07/01"
        - "/frameworks/cis-azure-v3.0.0/08/01"
        - "/frameworks/cloudaware/resource-security/secure-access"
      similarPolicies:
        cloudConformity:
          - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/azure/Network/bastion-host-exists.html
            name: Bastion Host in Use
  - path: /ce/ca/azure/subscription/bastion-host-exist/prod.logic.yaml
    md5Hash: 44A3EF68B82D6EC14003601AD0B9BA2B
    content: |-
      ---
      inputType: "CA10__CaAzureAccount__c"
      testData:
        - file: "test-data.json"
      conditions:
        - status: "COMPLIANT"
          currentStateMessage: "Bastion Host exists for this Subscription."
          check:
            RELATED_LIST_HAS:
              relationshipName: "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r"
              status: "COMPLIANT"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "Bastion Host doesn't exist for this Subscription."
      relatedLists:
        - relationshipName: "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r"
          conditions: []
          otherwise:
            status: "COMPLIANT"
            currentStateMessage: "Bastion Host is configured."
  - path: /ce/ca/azure/subscription/bastion-host-exist/test-data.json
    md5Hash: FBFC4E74DFB1A7929FB289411BE3E3A3
    content: |-
      [
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r.has(COMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test1",
              "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r": [
                  {
                      "Id": "test1_1",
                      "CA10Z1__disappearanceTime__c": null,
                      "CA10Z1__subscription__c": "test1"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test2",
              "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r": [
                  {
                      "Id": "test2_1",
                      "CA10Z1__disappearanceTime__c": "2024-06-13T04:17:29Z",
                      "CA10Z1__subscription__c": "test2"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test3",
              "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__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" : "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__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_CA10Z1__CaAzureBastionHost__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10Z1__disappearanceTime__c TIMESTAMP,
      CA10Z1__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",
    "CA10Z1__subscription__c" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test2_1",
    "CA10Z1__disappearanceTime__c" : new Date("2024-06-13T04:17:29Z"),
    "CA10Z1__subscription__c" : "test2"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureAccount__c(
      obj STRUCT<
          Id STRING,
          CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r ARRAY<STRUCT<
              Id STRING,
              CA10Z1__disappearanceTime__c TIMESTAMP,
              CA10Z1__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_CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r_COMPLIANT2 = 0;
      if (obj.CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r != null) {
          for (var i3 = 0; i3 < obj.CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r.length; i3++) {
              if (obj.CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r[i3].result.status == 'COMPLIANT') {
                  count_CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r_COMPLIANT2 += 1;
              }
          }
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Related list [CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r] ' + (count_CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r_COMPLIANT2 > 0 ? 'has' : 'does not have') + ' objects in COMPLIANT status');
      if (count_CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r_COMPLIANT2 > 0) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r.has(COMPLIANT)", currentStateMessage: "Bastion Host exists for this Subscription.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "Bastion Host doesn't exist for this Subscription.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r(
      obj STRUCT<
          Id STRING,
          CA10Z1__disappearanceTime__c TIMESTAMP,
          CA10Z1__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 [CA10Z1__disappearanceTime__c]: ' + obj.CA10Z1__disappearanceTime__c);
      if (obj.CA10Z1__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10Z1__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: "Bastion Host is 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,
          `CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r`.arr AS CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r,
          process_CA10__CaAzureAccount__c(
              STRUCT(
                  sObject.Id AS Id,
                  `CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r`.arr AS CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureAccount__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10Z1__subscription__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10Z1__disappearanceTime__c AS CA10Z1__disappearanceTime__c,
                      sObject.CA10Z1__subscription__c AS CA10Z1__subscription__c,
                      process_CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10Z1__disappearanceTime__c AS CA10Z1__disappearanceTime__c,
                              sObject.CA10Z1__subscription__c AS CA10Z1__subscription__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10Z1__CaAzureBastionHost__c()) AS sObject
          GROUP BY sObject.CA10Z1__subscription__c
      ) AS `CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r`
          ON sObject.Id = `CA10Z1__Azure_Virtual_Network_Bastion_Hosts__r`.CA10Z1__subscription__c
  ) sObject ON sObject.Id = expectedResult.Id;
