---
policy: /ce/ca/aws/opensearch/domain-instance-count
logic: /ce/ca/aws/opensearch/domain-instance-count/prod.logic.yaml
executionTime: 2025-12-27T12:02:05.750250802Z
generationMs: 59
executionMs: 604
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: 299
      actual: 299
    conditionText:
      expected: extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'false'
      actual: extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'false'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__clusterConfigInstanceCount__c') < number(3.0)
      actual: extract('CA10__clusterConfigInstanceCount__c') < number(3.0)
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 499
      actual: 499
    conditionText:
      expected: extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'true' &&
        extract('CA10__clusterConfigInstanceCount__c') >= number(3.0)
      actual: extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'true' && extract('CA10__clusterConfigInstanceCount__c')
        >= number(3.0)
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/opensearch/domain-instance-count/policy.yaml
    md5Hash: 2DF952BD621B987F445566B8E6B27CF8
    content: "---\nnames:\n  full: \"AWS OpenSearch Domain Instance Count is fewer\
      \ than three\"\n  contextual: \"Domain Instance Count is fewer than three\"\n\
      description: >\n  Ensure that Amazon OpenSearch Service domains are configured\
      \ for high availability. \n  This requires at least three data nodes and the\
      \ enablement of Zone Awareness \n  to distribute instances across multiple Availability\
      \ Zones.\ntype: \"COMPLIANCE_POLICY\"\ncategories:\n  - \"RELIABILITY\"\nframeworkMappings:\n\
      \  - \"/frameworks/cloudaware/resource-reliability/system-configuration\"\n\
      \  - \"/frameworks/aws-fsbp-v1.0.0/es/06\"\n  - \"/frameworks/aws-fsbp-v1.0.0/opensearch/06\"\
      \nsimilarPolicies:\n  awsSecurityHub:\n    - name: \"[Opensearch.6] OpenSearch\
      \ domains should have at least three data nodes\"\n      url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/opensearch-controls.html#opensearch-6\"\
      \n    - name: \"[ES.6] Elasticsearch domains should have at least three data\
      \ nodes\"\n      url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/es-controls.html#es-6\"\
      \n"
  - path: /ce/ca/aws/opensearch/domain-instance-count/prod.logic.yaml
    md5Hash: 5456E38187DFF7E4365B28A96E8C73C9
    content: |-
      ---
      inputType: "CA10__CaAwsElasticsearchDomain__c"
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10__CaAwsElasticsearchDomain__c/object.extracts.yaml"
      conditions:
        - status: "UNDETERMINED"
          currentStateMessage: "The instance count or zone awareness status is not populated in the CMDB."
          check:
            OR:
              args:
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__clusterConfigInstanceCount__c"
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__clusterConfigZoneAwarenessEnabled__c"
        - status: "INCOMPLIANT"
          currentStateMessage: "Zone Awareness is disabled for this OpenSearch domain."
          remediationMessage: "Enable Zone Awareness to distribute nodes across multiple Availability Zones."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__clusterConfigZoneAwarenessEnabled__c"
              right:
                TEXT: "false"
        - status: "INCOMPLIANT"
          currentStateMessage: "The OpenSearch Domain has fewer than 3 instances."
          remediationMessage: "Increase the instance count to at least 3 for high availability."
          check:
            LESS_THAN:
              left:
                EXTRACT: "CA10__clusterConfigInstanceCount__c"
              right:
                NUMBER: 3.0
        - status: "COMPLIANT"
          currentStateMessage: "The OpenSearch domain has at least 3 instances and Zone Awareness is enabled."
          check:
            AND:
              args:
                - IS_EQUAL:
                    left:
                      EXTRACT: "CA10__clusterConfigZoneAwarenessEnabled__c"
                    right:
                      TEXT: "true"
                - GREATER_THAN_EQUAL:
                    left:
                      EXTRACT: "CA10__clusterConfigInstanceCount__c"
                    right:
                      NUMBER: 3.0
      otherwise:
        status: "UNDETERMINED"
        currentStateMessage: "Unexpected values in the fields."
  - path: /ce/ca/aws/opensearch/domain-instance-count/test-data.json
    md5Hash: 5FC3DA87273D1BB4697BE87F074CD1A9
    content: |-
      [
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "conditionIndex": 99,
                  "status": "DISAPPEARED"
              },
              "context": {
                  "snapshotTime": "2025-12-23T10:29:21Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-08-17T07:38:43Z",
              "CA10__clusterConfigZoneAwarenessEnabled__c": "true",
              "CA10__clusterConfigInstanceCount__c": 2.0
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'false'",
                  "conditionIndex": 299,
                  "status": "INCOMPLIANT"
              },
              "context": {
                  "snapshotTime": "2025-12-23T10:29:21Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__clusterConfigZoneAwarenessEnabled__c": "false",
              "CA10__clusterConfigInstanceCount__c": 1.0
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "extract('CA10__clusterConfigInstanceCount__c') < number(3.0)",
                  "conditionIndex": 399,
                  "status": "INCOMPLIANT"
              },
              "context": {
                  "snapshotTime": "2025-12-23T10:29:21Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__clusterConfigZoneAwarenessEnabled__c": "true",
              "CA10__clusterConfigInstanceCount__c": 2.0
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'true' && extract('CA10__clusterConfigInstanceCount__c') >= number(3.0)",
                  "conditionIndex": 499,
                  "status": "COMPLIANT"
              },
              "context": {
                  "snapshotTime": "2025-12-23T10:29:21Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__clusterConfigZoneAwarenessEnabled__c": "true",
              "CA10__clusterConfigInstanceCount__c": 4.0
          }
      ]
  - path: /types/CA10__CaAwsElasticsearchDomain__c/object.extracts.yaml
    md5Hash: 0D5B1BE6E92CCF38D456A62B74D84E17
    content: |
      ---
      extracts:
      # Values: true | false (TEXT). Not nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__serviceSoftwareUpdateAvailable__c"
          value:
            FIELD:
              path: "CA10__serviceSoftwareUpdateAvailable__c"
      # Checkbox
        - name: "CA10__encryptionAtRestEnabled__c"
          value:
            FIELD:
              path: "CA10__encryptionAtRestEnabled__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__endpoint__c"
          value:
            FIELD:
              path: "CA10__endpoint__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__vpcEndpoints__c"
          value:
            FIELD:
              path: "CA10__vpcEndpoints__c"
      # Checkbox
        - name: "CA10__nodeToNodeEncryptionEnabled__c"
          value:
            FIELD:
              path: "CA10__nodeToNodeEncryptionEnabled__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__logPublishingOptionsJson__c"
          value:
            FIELD:
              returnType: "BYTES"
              path: "CA10__logPublishingOptionsJson__c"
        - name: "caJsonFrom__logPublishingOptionsJson__c"
          value:
            JSON_FROM:
              arg:
                EXTRACT: "CA10__logPublishingOptionsJson__c"
              undeterminedIf:
                isInvalid: "Log Publishing Options JSON is invalid."
      # Not nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__clusterConfigInstanceCount__c"
          value:
            FIELD:
              path: "CA10__clusterConfigInstanceCount__c"
      # Not nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__clusterConfigZoneAwarenessEnabled__c"
          value:
            FIELD:
              path: "CA10__clusterConfigZoneAwarenessEnabled__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__clusterConfigDedicatedMasterEnabled__c"
          value:
            FIELD:
              path: "CA10__clusterConfigDedicatedMasterEnabled__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__clusterConfigDedicatedMasterCount__c"
          value:
            FIELD:
              path: "CA10__clusterConfigDedicatedMasterCount__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__endpointOptionsEnforceHttps__c"
          value:
            FIELD:
              path: "CA10__endpointOptionsEnforceHttps__c"
      # Nullable. Can't have no access retrieved via es:DescribeDomains
        - name: "CA10__endpointOptionsTlsSecurityPolicy__c"
          value:
            FIELD:
              path: "CA10__endpointOptionsTlsSecurityPolicy__c"
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" : {
      "runtimeError" : null,
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "conditionIndex" : 99,
      "status" : "DISAPPEARED"
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'false'",
      "conditionIndex" : 299,
      "status" : "INCOMPLIANT"
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "extract('CA10__clusterConfigInstanceCount__c') < number(3.0)",
      "conditionIndex" : 399,
      "status" : "INCOMPLIANT"
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'true' && extract('CA10__clusterConfigInstanceCount__c') >= number(3.0)",
      "conditionIndex" : 499,
      "status" : "COMPLIANT"
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsElasticsearchDomain__c()
  RETURNS ARRAY<STRUCT<
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__clusterConfigInstanceCount__c FLOAT64,
      CA10__clusterConfigZoneAwarenessEnabled__c STRING,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-12-23T10:29:21Z")
    },
    "CA10__disappearanceTime__c" : new Date("2025-08-17T07:38:43Z"),
    "CA10__clusterConfigInstanceCount__c" : 2.0,
    "CA10__clusterConfigZoneAwarenessEnabled__c" : "true",
    "Id" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-23T10:29:21Z")
    },
    "CA10__clusterConfigInstanceCount__c" : 1.0,
    "CA10__clusterConfigZoneAwarenessEnabled__c" : "false",
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-23T10:29:21Z")
    },
    "CA10__clusterConfigInstanceCount__c" : 2.0,
    "CA10__clusterConfigZoneAwarenessEnabled__c" : "true",
    "Id" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-23T10:29:21Z")
    },
    "CA10__clusterConfigInstanceCount__c" : 4.0,
    "CA10__clusterConfigZoneAwarenessEnabled__c" : "true",
    "Id" : "test4"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsElasticsearchDomain__c(
      obj STRUCT<
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__clusterConfigInstanceCount__c FLOAT64,
          CA10__clusterConfigZoneAwarenessEnabled__c STRING,
          Id 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));
      };
      this.containsAll = function(arg, substrings) {
          if (substrings == null || substrings.length === 0) return false;
          let normalizedArg = this.normalize(arg);
          return substrings.every(sub => normalizedArg.includes(this.normalize(sub)));
      };
      this.containsAny = function(arg, substrings) {
          if (substrings == null || substrings.length === 0) return false;
          let normalizedArg = this.normalize(arg);
          return substrings.some(sub => normalizedArg.includes(this.normalize(sub)));
      };
  }();
  var IsEmptyLib = new function () {
      this.simpleIsEmpty = function(arg) {
          return arg == null;
      };
      this.simpleIsNotEmpty = function(arg) {
          return arg != null;
      };
  }();
      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__clusterConfigInstanceCount__c; } return this.out; };
      function extract6() { if (!this.out) { this.out = obj.CA10__clusterConfigZoneAwarenessEnabled__c; } return this.out; };
      references1.push('Cluster Config: Instance Count [obj.CA10__clusterConfigInstanceCount__c]: ' + obj.CA10__clusterConfigInstanceCount__c);
      references1.push('Cluster Config: Zone Awareness Enabled [obj.CA10__clusterConfigZoneAwarenessEnabled__c]: ' + obj.CA10__clusterConfigZoneAwarenessEnabled__c);
      if (IsEmptyLib.simpleIsEmpty(extract3.call(extract3)) || TextLib.isEmpty(extract6.call(extract6))) {
          return {status: 'UNDETERMINED', conditionIndex: 199, conditionText: "extract('CA10__clusterConfigInstanceCount__c').isEmpty() || extract('CA10__clusterConfigZoneAwarenessEnabled__c').isEmpty()", currentStateMessage: "The instance count or zone awareness status is not populated in the CMDB.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract9() { if (!this.out) { this.out = obj.CA10__clusterConfigZoneAwarenessEnabled__c; } return this.out; };
      if (TextLib.equal(extract9.call(extract9), 'false')) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'false'", currentStateMessage: "Zone Awareness is disabled for this OpenSearch domain.", currentStateReferences: references1.join('\n'), remediation: "Enable Zone Awareness to distribute nodes across multiple Availability Zones.", runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      function extract13() { if (!this.out) { this.out = obj.CA10__clusterConfigInstanceCount__c; } return this.out; };
      if (extract13.call(extract13) != null && 3.0 != null && extract13.call(extract13) < 3.0) {
          return {status: 'INCOMPLIANT', conditionIndex: 399, conditionText: "extract('CA10__clusterConfigInstanceCount__c') < number(3.0)", currentStateMessage: "The OpenSearch Domain has fewer than 3 instances.", currentStateReferences: references1.join('\n'), remediation: "Increase the instance count to at least 3 for high availability.", runtimeError: null};
      }
      // condition[4], conditionIndex:[400..499]
      function extract16() { if (!this.out) { this.out = obj.CA10__clusterConfigZoneAwarenessEnabled__c; } return this.out; };
      function extract20() { if (!this.out) { this.out = obj.CA10__clusterConfigInstanceCount__c; } return this.out; };
      if (TextLib.equal(extract16.call(extract16), 'true') && (extract20.call(extract20) != null && 3.0 != null && extract20.call(extract20) >= 3.0)) {
          return {status: 'COMPLIANT', conditionIndex: 499, conditionText: "extract('CA10__clusterConfigZoneAwarenessEnabled__c') == 'true' && extract('CA10__clusterConfigInstanceCount__c') >= number(3.0)", currentStateMessage: "The OpenSearch domain has at least 3 instances and Zone Awareness is enabled.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'UNDETERMINED', conditionIndex: 500, conditionText: "otherwise", currentStateMessage: "Unexpected values in the fields.", 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.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__clusterConfigInstanceCount__c AS CA10__clusterConfigInstanceCount__c,
          sObject.CA10__clusterConfigZoneAwarenessEnabled__c AS CA10__clusterConfigZoneAwarenessEnabled__c,
          sObject.Id AS Id,
          process_CA10__CaAwsElasticsearchDomain__c(
              STRUCT(
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__clusterConfigInstanceCount__c AS CA10__clusterConfigInstanceCount__c,
                  sObject.CA10__clusterConfigZoneAwarenessEnabled__c AS CA10__clusterConfigZoneAwarenessEnabled__c,
                  sObject.Id AS Id
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsElasticsearchDomain__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
