---
policy: /ce/ca/azure/virtual-network/unused-public-ip-address
logic: /ce/ca/azure/virtual-network/unused-public-ip-address/prod.logic.yaml
executionTime: 2025-05-10T12:04:35.686088151Z
rows:
  - id: 111
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: 222
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__associatedResourceId__c').isEmpty()
      actual: extract('CA10__associatedResourceId__c').isEmpty()
    runtimeError: {}
  - id: 333
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/virtual-network/unused-public-ip-address/policy.yaml
    md5Hash: 150EAB93D80E1FCB7D180411CD6E1B7B
    content: |
      ---
      names:
        full: "Azure Public IP Address is not associated with any resource"
        contextual: "Public IP Address is not associated with any resource"
      description: >
        Consider deleting public IP addresses that are not attached to a virtual machine,
        network interface, internet-facing load balancer, VPN gateway, or application gateway.
      categories:
        - "SECURITY"
        - "COST"
      type: "COMPLIANCE_POLICY"
      frameworkMappings:
        - "/frameworks/cloudaware/cost-efficiency-and-optimization/waste-reduction"
        - "/frameworks/cloudaware/resource-security/public-and-anonymous-access"
  - path: /ce/ca/azure/virtual-network/unused-public-ip-address/prod.logic.yaml
    md5Hash: C8690201421169F458F4C1A6C33FE426
    content: |-
      ---
      inputType: "CA10__CaAzurePublicIpAddress__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAzurePublicIpAddress__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "Public IP address is not attached to any resource."
          remediationMessage: "Consider removing the IP address."
          check:
            IS_EMPTY:
              arg:
                EXTRACT: "CA10__associatedResourceId__c"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "Public IP address attached to a resource."
  - path: /types/CA10__CaAzurePublicIpAddress__c/object.extracts.yaml
    md5Hash: 63DE41DAC859D89BDDBC034ED31704BB
    content: "---\nextracts:\n# Nullable.\n  - name: \"CA10__associatedResourceId__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__associatedResourceId__c\""
  - path: /ce/ca/azure/virtual-network/unused-public-ip-address/test-data.json
    md5Hash: 5D2FB49CB52E2D40A029A212B598D94F
    content: |-
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-26T11:02:25Z"
          },
          "Id": "111",
          "CA10__disappearanceTime__c": "2025-03-17T14:50:31Z",
          "CA10__associatedResourceId__c": "/subscriptions/la"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__associatedResourceId__c').isEmpty()",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-26T11:02:25Z"
          },
          "Id": "222",
          "CA10__disappearanceTime__c": null,
          "CA10__associatedResourceId__c": ""
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-26T11:02:25Z"
          },
          "Id": "333",
          "CA10__disappearanceTime__c": null,
          "CA10__associatedResourceId__c": "/subscriptions/la"
        }
      ]
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" : "111",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "222",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__associatedResourceId__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "333",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzurePublicIpAddress__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__associatedResourceId__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-03-26T11:02:25Z")
    },
    "Id" : "111",
    "CA10__disappearanceTime__c" : new Date("2025-03-17T14:50:31Z"),
    "CA10__associatedResourceId__c" : "/subscriptions/la"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-26T11:02:25Z")
    },
    "Id" : "222",
    "CA10__associatedResourceId__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-26T11:02:25Z")
    },
    "Id" : "333",
    "CA10__associatedResourceId__c" : "/subscriptions/la"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzurePublicIpAddress__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__associatedResourceId__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 TextLib = new function () {
          this.normalize = function(arg) {
              return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase();
          };
          this.isEmpty = function(arg) {
              return this.normalize(arg) == '';
          };
          this.isNotEmpty = function(arg) {
              return this.normalize(arg) != '';
          };
          this.equal = function(left, right) {
              return this.normalize(left) == this.normalize(right);
          };
          this.notEqual = function(left, right) {
              return this.normalize(left) != this.normalize(right);
          };
          this.startsWith = function(arg, substring) {
              return this.normalize(arg).startsWith(this.normalize(substring));
          };
          this.endsWith = function(arg, substring) {
              return this.normalize(arg).endsWith(this.normalize(substring));
          };
          this.contains = function(arg, substring) {
              return this.normalize(arg).includes(this.normalize(substring));
          };
      }();
      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};
      }
      // condition[1], conditionIndex:[100..199]
      function extract3() { if (!this.out) { this.out = obj.CA10__associatedResourceId__c; } return this.out; };
      references1.push('Associated Resource ID [obj.CA10__associatedResourceId__c]: ' + obj.CA10__associatedResourceId__c);
      if (TextLib.isEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__associatedResourceId__c').isEmpty()", currentStateMessage: "Public IP address is not attached to any resource.", currentStateReferences: references1.join('\n'), remediation: "Consider removing the IP address.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "Public IP address attached to a resource.", 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,
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__associatedResourceId__c AS CA10__associatedResourceId__c,
          process_CA10__CaAzurePublicIpAddress__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__associatedResourceId__c AS CA10__associatedResourceId__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzurePublicIpAddress__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
