---
policy: /ce/ca/snowflake/user/account-admin-default-role
logic: /ce/ca/snowflake/user/account-admin-default-role/prod.logic.yaml
executionTime: 2025-05-10T12:06:05.322305144Z
rows:
  - id: test1
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10SN__disappearanceTime__c)
      actual: isDisappeared(CA10SN__disappearanceTime__c)
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10SN__defaultRoleName__c') == 'ACCOUNTADMIN'
      actual: extract('CA10SN__defaultRoleName__c') == 'ACCOUNTADMIN'
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/snowflake/user/account-admin-default-role/policy.yaml
    md5Hash: 412FC392AA35476F7B5C65AEB0641E4F
    content: "names:\n  full: \"Snowflake User Default Role is ACCOUNTADMIN\"\n  contextual:\
      \ \"Default Role is ACCOUNTADMIN\"\ndescription: >\n  Ensure that the ACCOUNTADMIN\
      \ role is not set as the Default Role for Snowflake \n  Users to reduce risk\
      \ and prevent accidental use of broad administrative privileges.\ntype: \"COMPLIANCE_POLICY\"\
      \ncategories:\n  - \"SECURITY\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/identity-and-access-governance/user-account-management\""
  - path: /ce/ca/snowflake/user/account-admin-default-role/prod.logic.yaml
    md5Hash: E4A9E4AF33E679A16851047B52BDAE29
    content: |
      ---
      inputType: "CA10SN__CaSnowflakeUser__c"
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10SN__CaSnowflakeUser__c/object.extracts.yaml"
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "The Default Role for this User is ACCOUNTADMIN."
          remediationMessage: "Change the Default Role for the User to a less privileged role."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10SN__defaultRoleName__c"
              right:
                TEXT: "ACCOUNTADMIN"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The Default Role for this User is not ACCOUNTADMIN."
  - path: /ce/ca/snowflake/user/account-admin-default-role/test-data.json
    md5Hash: EA9BDDB9B47A0CAD5C37CF480D06169F
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10SN__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-11-20T20:12:30Z"
              },
              "Id": "test1",
              "CA10SN__disappearanceTime__c": "2024-11-20T17:14:43Z",
              "CA10SN__defaultRoleName__c": "ACCOUNTADMIN"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10SN__defaultRoleName__c') == 'ACCOUNTADMIN'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-11-20T20:12:30Z"
              },
              "Id": "test3",
              "CA10SN__disappearanceTime__c": null,
              "CA10SN__defaultRoleName__c": "ACCOUNTADMIN"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-11-20T20:12:30Z"
              },
              "Id": "test4",
              "CA10SN__disappearanceTime__c": null,
              "CA10SN__defaultRoleName__c": "SECURITYADMIN"
          }
      ]
  - path: /types/CA10SN__CaSnowflakeUser__c/object.extracts.yaml
    md5Hash: C6EB37BF1FD385E8938A301910EC1C1E
    content: "---\nextracts:\n  - name: \"CA10SN__passwordLastSetTime__c\"\n    value:\
      \ \n      FIELD:\n        path: \"CA10SN__passwordLastSetTime__c\"\n  - name:\
      \ \"CA10SN__hasPassword__c\"\n    value: \n      FIELD:\n        path: \"CA10SN__hasPassword__c\"\
      \n  - name: \"CA10SN__extAuthnDuo__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10SN__extAuthnDuo__c\"\n  - name: \"CA10SN__defaultRoleName__c\"\n   \
      \ value: \n      FIELD:\n        path: \"CA10SN__defaultRoleName__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" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10SN__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10SN__defaultRoleName__c') == 'ACCOUNTADMIN'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10SN__CaSnowflakeUser__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10SN__disappearanceTime__c TIMESTAMP,
      CA10SN__defaultRoleName__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-11-20T20:12:30Z")
    },
    "Id" : "test1",
    "CA10SN__disappearanceTime__c" : new Date("2024-11-20T17:14:43Z"),
    "CA10SN__defaultRoleName__c" : "ACCOUNTADMIN"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-11-20T20:12:30Z")
    },
    "Id" : "test3",
    "CA10SN__defaultRoleName__c" : "ACCOUNTADMIN"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-11-20T20:12:30Z")
    },
    "Id" : "test4",
    "CA10SN__defaultRoleName__c" : "SECURITYADMIN"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10SN__CaSnowflakeUser__c(
      obj STRUCT<
          Id STRING,
          CA10SN__disappearanceTime__c TIMESTAMP,
          CA10SN__defaultRoleName__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 Snowflake [CA10SN__disappearanceTime__c]: ' + obj.CA10SN__disappearanceTime__c);
      if (obj.CA10SN__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10SN__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.CA10SN__defaultRoleName__c; } return this.out; };
      references1.push('Default Role Name [obj.CA10SN__defaultRoleName__c]: ' + obj.CA10SN__defaultRoleName__c);
      if (TextLib.equal(extract3.call(extract3), 'ACCOUNTADMIN')) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10SN__defaultRoleName__c') == 'ACCOUNTADMIN'", currentStateMessage: "The Default Role for this User is ACCOUNTADMIN.", currentStateReferences: references1.join('\n'), remediation: "Change the Default Role for the User to a less privileged role.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The Default Role for this User is not ACCOUNTADMIN.", 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.CA10SN__disappearanceTime__c AS CA10SN__disappearanceTime__c,
          sObject.CA10SN__defaultRoleName__c AS CA10SN__defaultRoleName__c,
          process_CA10SN__CaSnowflakeUser__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10SN__disappearanceTime__c AS CA10SN__disappearanceTime__c,
                  sObject.CA10SN__defaultRoleName__c AS CA10SN__defaultRoleName__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10SN__CaSnowflakeUser__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
