---
policy: /ce/ca/breeze/coverage/aws-ec2-instance
logic: /ce/ca/breeze/coverage/aws-ec2-instance/prod.logic.yaml
executionTime: 2025-05-03T12:05:29.502192808Z
rows:
  - id: test1
    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: test2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('breezeLastUpdate').isEmpty()
      actual: extract('breezeLastUpdate').isEmpty()
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('breezeLastUpdate').beyondLastDays(1)
      actual: extract('breezeLastUpdate').beyondLastDays(1)
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/breeze/coverage/aws-ec2-instance/policy.yaml
    md5Hash: 804638FF629D0C4A4DE375DEF74C7124
    content: |
      ---
      names:
        full: AWS EC2 Instance Should Have Breeze Agent Installed
        contextual: AWS EC2 Instance Should Have Breeze Agent Installed
      description: "Ensure that Breeze Agent is installed on the EC2 Instance. The Breeze\
        \ Agent streams OS-level data into Cloudaware CMDB and seamlessly enables other\
        \ Cloudaware modules such as Intrusion Detection (IDS), Vulnerability Scanning,\
        \ Patch Management, CIS Benchmarking, and Event Monitoring."
      type: BEST_PRACTICE
      categories:
        - RELIABILITY
  - path: /ce/ca/breeze/coverage/aws-ec2-instance/prod.logic.yaml
    md5Hash: 25BC0F87894B881756C3036D0430EE40
    content: |
      ---
      inputType: CA10__CaAwsInstance__c
      importExtracts:
        - file: /types/CA10__CaAwsInstance__c/breeze.extracts.yaml
      testData:
        - file: ../test-data-ca10.json
      conditions:
        - status: INCOMPLIANT
          currentStateMessage: Instance never had breeze agent installed
          remediationMessage: Install breeze agent on the instance
          check:
            IS_EMPTY:
              arg:
                EXTRACT: breezeLastUpdate
        - status: INCOMPLIANT
          currentStateMessage: Instance latest breeze report was more than a day ago
          remediationMessage: Re-install breeze agent on the instance and make sure it is reporting
          check:
            IS_BEYOND_LAST_DAYS:
              offsetDays: 1
              arg:
                EXTRACT: breezeLastUpdate
      otherwise:
        status: COMPLIANT
        currentStateMessage: Instance has breeze agent installed and has a recent report
  - path: /ce/ca/breeze/coverage/test-data-ca10.json
    md5Hash: 3F29F9B4DB309E9EEA69E663C01435DE
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-24T00:11:51Z"
              },
              "Id": "test1",
              "RecordTypeId": "rtId",
              "CA10__disappearanceTime__c": "2024-06-14T21:33:57Z",
              "CA10__breezeLastUpdate__c": null,
              "RecordType": {
                  "DeveloperName": "RecordType1",
                  "Id": "rtId"
              }
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "extract('breezeLastUpdate').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-24T00:11:51Z"
              },
              "Id": "test2",
              "RecordTypeId": "rtId",
              "CA10__disappearanceTime__c": null,
              "CA10__breezeLastUpdate__c": null,
              "RecordType": {
                  "DeveloperName": "RecordType1",
                  "Id": "rtId"
              }
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('breezeLastUpdate').beyondLastDays(1)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-24T00:11:51Z"
              },
              "Id": "test3",
              "RecordTypeId": "rtId",
              "CA10__disappearanceTime__c": null,
              "CA10__breezeLastUpdate__c": "2023-03-20T03:06:08Z",
              "RecordType": {
                  "DeveloperName": "RecordType1",
                  "Id": "rtId"
              }
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-24T00:11:51Z"
              },
              "Id": "test4",
              "RecordTypeId": "rtId",
              "CA10__disappearanceTime__c": null,
              "CA10__breezeLastUpdate__c": "2024-06-23T22:58:43Z",
              "RecordType": {
                  "DeveloperName": "RecordType1",
                  "Id": "rtId"
              }
          }
      ]
  - path: /types/CA10__CaAwsInstance__c/breeze.extracts.yaml
    md5Hash: 2E0FA731FC929EE1D3BA9374B84DF3A3
    content: "---\nextracts:\n  - name: breezeLastUpdate\n    value:\n      FIELD:\
      \ \n        # can be empty\n        # always accessible\n        path: CA10__breezeLastUpdate__c\n"
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" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('breezeLastUpdate').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('breezeLastUpdate').beyondLastDays(1)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__breezeLastUpdate__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-06-24T00:11:51Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2024-06-14T21:33:57Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-24T00:11:51Z")
    },
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-24T00:11:51Z")
    },
    "Id" : "test3",
    "CA10__breezeLastUpdate__c" : new Date("2023-03-20T03:06:08Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-24T00:11:51Z")
    },
    "Id" : "test4",
    "CA10__breezeLastUpdate__c" : new Date("2024-06-23T22:58:43Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__breezeLastUpdate__c TIMESTAMP
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      var todayMinus1Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-1 * 86400000);
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From AWS [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__breezeLastUpdate__c; } return this.out; };
      references1.push('Breeze: Last Update [obj.CA10__breezeLastUpdate__c]: ' + obj.CA10__breezeLastUpdate__c);
      if (IsEmptyLib.simpleIsEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('breezeLastUpdate').isEmpty()", currentStateMessage: "Instance never had breeze agent installed", currentStateReferences: references1.join('\n'), remediation: "Install breeze agent on the instance", runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__breezeLastUpdate__c; } return this.out; };
      if (extract6.call(extract6) != null && extract6.call(extract6).getTime() < todayMinus1Day) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('breezeLastUpdate').beyondLastDays(1)", currentStateMessage: "Instance latest breeze report was more than a day ago", currentStateReferences: references1.join('\n'), remediation: "Re-install breeze agent on the instance and make sure it is reporting", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "Instance has breeze agent installed and has a recent report", 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__breezeLastUpdate__c AS CA10__breezeLastUpdate__c,
          process_CA10__CaAwsInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__breezeLastUpdate__c AS CA10__breezeLastUpdate__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
