---
policy: /ce/ca/aws/ecr/repository-image-tag-mutability
logic: /ce/ca/aws/ecr/repository-image-tag-mutability/prod.logic.yaml
executionTime: 2025-10-04T12:03:05.276831843Z
generationMs: 65
executionMs: 944
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('CA10__imageTagMutability__c') == 'MUTABLE'
      actual: extract('CA10__imageTagMutability__c') == 'MUTABLE'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: "setOfText(['IMMUTABLE', 'IMMUTABLE_WITH_EXCLUSION', 'MUTABLE_WITH_EXCLUSION']).contains(extract('CA10__imageTagMutability__c'))"
      actual: "setOfText(['IMMUTABLE', 'IMMUTABLE_WITH_EXCLUSION', 'MUTABLE_WITH_EXCLUSION']).contains(extract('CA10__imageTagMutability__c'))"
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/ecr/repository-image-tag-mutability/policy.yaml
    md5Hash: A5734FB56DE9F7783452C984EEBF39AA
    content: |-
      ---
      names:
        full: "AWS ECR Repository Image Tag Mutability is set to Mutable"
        contextual: "Repository Image Tag Mutability is set to Mutable"
      description: >
        Ensure that AWS ECR Repositories are configured with image tag immutability.
        Setting tags to immutable prevents image tags from being overwritten.
      type: "COMPLIANCE_POLICY"
      categories:
        - "RELIABILITY"
      frameworkMappings:
        - "/frameworks/cloudaware/resource-reliability/system-configuration"
        - "/frameworks/aws-fsbp-v1.0.0/ecr/02"
      similarPolicies:
        awsSecurityHub:
          - name: "[ECR.2] ECR private repositories should have tag immutability configured"
            url: "https://docs.aws.amazon.com/securityhub/latest/userguide/ecr-controls.html#ecr-2"
        internal:
          - "dec-x-767cce1f"
  - path: /ce/ca/aws/ecr/repository-image-tag-mutability/prod.logic.yaml
    md5Hash: 034A9EFBCBC5DEC28A4FEF7E33373F98
    content: |-
      ---
      inputType: "CA10__CaAwsEcrRepository__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAwsEcrRepository__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "Image tag mutability is set to MUTABLE, allowing image tags to be overwritten."
          remediationMessage: "Consider setting image tag mutability to IMMUTABLE to prevent overwriting image tags."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__imageTagMutability__c"
              right:
                TEXT: "MUTABLE"
        - status: "COMPLIANT"
          currentStateMessage: "Image tag mutability is set to have immutable tags."
          check:
            CONTAINS:
              arg:
                SET:
                  itemType: "TEXT"
                  items:
                    - "IMMUTABLE"
                    - "IMMUTABLE_WITH_EXCLUSION"
                    - "MUTABLE_WITH_EXCLUSION"
              search:
                EXTRACT: "CA10__imageTagMutability__c"
      otherwise:
        status: "UNDETERMINED"
        currentStateMessage: "Unexpected values in the field."
  - path: /ce/ca/aws/ecr/repository-image-tag-mutability/test-data.json
    md5Hash: EF55D2DC6A79314E56699E38611796D4
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-01T13:35:59Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-09-29T18:15:00Z",
              "CA10__imageTagMutability__c": "IMMUTABLE"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__imageTagMutability__c') == 'MUTABLE'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-01T13:35:59Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__imageTagMutability__c": "MUTABLE"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "setOfText(['IMMUTABLE', 'IMMUTABLE_WITH_EXCLUSION', 'MUTABLE_WITH_EXCLUSION']).contains(extract('CA10__imageTagMutability__c'))",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-01T13:35:59Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__imageTagMutability__c": "IMMUTABLE"
          }
      ]
  - path: /types/CA10__CaAwsEcrRepository__c/object.extracts.yaml
    md5Hash: B0BC5F2B3D6A5EE7EDEC92352F9935A7
    content: "---\nextracts:\n# Values: MUTABLE | IMMUTABLE | IMMUTABLE_WITH_EXCLUSION\
      \ | MUTABLE_WITH_EXCLUSION\n# Not nullable. Can't have no access, retrieved\
      \ via ecr:DescribeRepositories\n  - name: \"CA10__imageTagMutability__c\"\n\
      \    value:\n      FIELD:\n        path: \"CA10__imageTagMutability__c\"\n \
      \       undeterminedIf:\n          isEmpty: \"Corrupted data. Image Tag Mutability\
      \ cannot be empty.\"\n# Nullable. \n  - name: \"CA10__lifecyclePolicyText__c\"\
      \n    value:\n      FIELD:\n        path: \"CA10__lifecyclePolicyText__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(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__imageTagMutability__c') == 'MUTABLE'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "setOfText(['IMMUTABLE', 'IMMUTABLE_WITH_EXCLUSION', 'MUTABLE_WITH_EXCLUSION']).contains(extract('CA10__imageTagMutability__c'))",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsEcrRepository__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__imageTagMutability__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-10-01T13:35:59Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2025-09-29T18:15:00Z"),
    "CA10__imageTagMutability__c" : "IMMUTABLE"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-01T13:35:59Z")
    },
    "Id" : "test2",
    "CA10__imageTagMutability__c" : "MUTABLE"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-01T13:35:59Z")
    },
    "Id" : "test3",
    "CA10__imageTagMutability__c" : "IMMUTABLE"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsEcrRepository__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__imageTagMutability__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));
          };
          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 CollectionLib = new function () {
          this.parse = function(arg, separator, skipEmpty, skipDuplicates, sort, normalize) {
              if (arg == null) return [];
              let parts = arg.split(separator);
              return this.fromArray(parts, skipEmpty, skipDuplicates, sort, normalize);
          };
          this.fromArray = function(arr, skipEmpty, skipDuplicates, sort, normalize) {
              if (arr == null) return [];
              let result = [];
              let seen = new Set();
              arr.forEach(el => {
                  let normalized = normalize ? TextLib.normalize(el) : BytesLib.normalize(el);
                  if (!skipEmpty || (normalize ? TextLib.isNotEmpty(el) : BytesLib.isNotEmpty(el))) {
                      if (!skipDuplicates || !seen.has(normalized)) {
                          if (skipDuplicates) seen.add(normalized);
                          result.push(normalized);
                      }
                  }
              });
              if (sort) result.sort();
              return result;
          };
          this.equal = function(left, right) {
              left = left == null ? [] : left;
              right = right == null ? [] : right;
              if (left.length !== right.length) return false;
              for (let i = 0; i < left.length; i++) {
                  if (left[i] !== right[i]) return false;
              }
              return true;
          };
          this.notEqual = function(left, right) {
              return !this.equal(left, right);
          };
          this.size = function(collection) {
              return collection == null ? 0 : collection.length;
          };
          this.startsWith = function(collection, search, normalize) {
              if (collection == null || collection.length === 0) return false;
              let normalizedSearch = normalize ? TextLib.normalize(search) : BytesLib.normalize(search);
              return collection[0] === normalizedSearch;
          };
          this.endsWith = function(collection, search, normalize) {
              if (collection == null || collection.length === 0) return false;
              let normalizedSearch = normalize ? TextLib.normalize(search) : BytesLib.normalize(search);
              return collection[collection.length - 1] === normalizedSearch;
          };
          this.contains = function(collection, search, normalize) {
              if (collection == null || collection.length === 0) return false;
              return collection.includes(normalize ? TextLib.normalize(search) : BytesLib.normalize(search));
          };
          this.containsAll = function(collection, searchArray, normalize) {
              if (collection == null || collection.length === 0 || searchArray == null || searchArray.length === 0) return false;
              return searchArray.every(search => collection.includes(normalize ? TextLib.normalize(search) : BytesLib.normalize(search)));
          };
          this.containsAny = function(collection, searchArray, normalize) {
              if (collection == null || collection.length === 0 || searchArray == null || searchArray.length === 0) return false;
              return searchArray.some(search => collection.includes(normalize ? TextLib.normalize(search) : BytesLib.normalize(search)));
          };
      }();
      var BytesLib = new function () {
          this.normalize = function(arg) {
              return arg == null ? '' : arg;
          };
          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 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 fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__imageTagMutability__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__imageTagMutability__c.isEmpty()", currentStateMessage: "Corrupted data. Image Tag Mutability cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__imageTagMutability__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Image Tag Mutability [obj.CA10__imageTagMutability__c]: ' + obj.CA10__imageTagMutability__c);
      try {
          if (TextLib.equal(extract3.call(extract3), 'MUTABLE')) {
              return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__imageTagMutability__c') == 'MUTABLE'", currentStateMessage: "Image tag mutability is set to MUTABLE, allowing image tags to be overwritten.", currentStateReferences: references1.join('\n'), remediation: "Consider setting image tag mutability to IMMUTABLE to prevent overwriting image tags.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[2], conditionIndex:[200..299]
      function fieldChecked7() {
          if (TextLib.isEmpty(obj.CA10__imageTagMutability__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "CA10__imageTagMutability__c.isEmpty()", currentStateMessage: "Corrupted data. Image Tag Mutability cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__imageTagMutability__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      try {
          if (CollectionLib.contains(CollectionLib.fromArray(['IMMUTABLE', 'IMMUTABLE_WITH_EXCLUSION', 'MUTABLE_WITH_EXCLUSION'], true, true, true, true), extract6.call(extract6), true)) {
              return {status: 'COMPLIANT', conditionIndex: 299, conditionText: "setOfText(['IMMUTABLE', 'IMMUTABLE_WITH_EXCLUSION', 'MUTABLE_WITH_EXCLUSION']).contains(extract('CA10__imageTagMutability__c'))", currentStateMessage: "Image tag mutability is set to have immutable tags.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'UNDETERMINED', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "Unexpected values in the field.", 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__imageTagMutability__c AS CA10__imageTagMutability__c,
          process_CA10__CaAwsEcrRepository__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__imageTagMutability__c AS CA10__imageTagMutability__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsEcrRepository__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
