---
policy: /ce/ca/aws/ecr/repository-lifecycle
logic: /ce/ca/aws/ecr/repository-lifecycle/prod.logic.yaml
executionTime: 2025-10-04T12:03:06.228848491Z
generationMs: 80
executionMs: 820
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__lifecyclePolicyText__c').isEmpty()
      actual: extract('CA10__lifecyclePolicyText__c').isEmpty()
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/ecr/repository-lifecycle/policy.yaml
    md5Hash: 2A2577C836E61FA91B7C68B82A9E410B
    content: "---\nnames:\n  full: \"AWS ECR Repository Lifecycle Policy is not configured\"\
      \n  contextual: \"Repository Lifecycle Policy is not configured\"\ndescription:\
      \ >\n  Ensure that Amazon ECR repositories have a lifecycle policy configured\
      \ to \n  automatically manage and clean up unused container images, which helps\
      \ reduce \n  storage costs and maintain repository hygiene.\ntype: \"COMPLIANCE_POLICY\"\
      \ncategories:\n  - \"COST\"\n  - \"RELIABILITY\"\nframeworkMappings:\n  - \"\
      /frameworks/cloudaware/cost-efficiency-and-optimization/resource-optimization\"\
      \n  - \"/frameworks/aws-fsbp-v1.0.0/ecr/03\"\nsimilarPolicies:\n  awsSecurityHub:\n\
      \    - name: \"[ECR.3] ECR repositories should have at least one lifecycle policy\
      \ configured\"\n      url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/ecr-controls.html#ecr-3\"\
      \n  internal:\n    - \"dec-x-9a0607d9\""
  - path: /ce/ca/aws/ecr/repository-lifecycle/prod.logic.yaml
    md5Hash: C20109D91418F9E2D6846A12FABA6A3C
    content: |-
      ---
      inputType: "CA10__CaAwsEcrRepository__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAwsEcrRepository__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "The ECR repository does not have a lifecycle policy configured."
          remediationMessage: "Create a lifecycle policy to automatically manage container images."
          check:
            IS_EMPTY:
              arg:
                EXTRACT: "CA10__lifecyclePolicyText__c"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The ECR repository has a lifecycle policy configured."
  - path: /ce/ca/aws/ecr/repository-lifecycle/test-data.json
    md5Hash: 638DA1C0404BEF56F6458223BA06A87F
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-02T06:11:18Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-09-29T18:15:00Z",
              "CA10__lifecyclePolicyText__c": "{\"rules\":[{\"rulePriority\":1,\"description\":\"description\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":30},\"action\":{\"type\":\"expire\"}}]}"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__lifecyclePolicyText__c').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-02T06:11:18Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__lifecyclePolicyText__c": ""
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-02T06:11:18Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__lifecyclePolicyText__c": "{\"rules\":[{\"rulePriority\":1,\"description\":\"description\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":30},\"action\":{\"type\":\"expire\"}}]}"
          }
      ]
  - 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__lifecyclePolicyText__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsEcrRepository__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__lifecyclePolicyText__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-10-02T06:11:18Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2025-09-29T18:15:00Z"),
    "CA10__lifecyclePolicyText__c" : "{\"rules\":[{\"rulePriority\":1,\"description\":\"description\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":30},\"action\":{\"type\":\"expire\"}}]}"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-02T06:11:18Z")
    },
    "Id" : "test2",
    "CA10__lifecyclePolicyText__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-02T06:11:18Z")
    },
    "Id" : "test3",
    "CA10__lifecyclePolicyText__c" : "{\"rules\":[{\"rulePriority\":1,\"description\":\"description\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":30},\"action\":{\"type\":\"expire\"}}]}"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsEcrRepository__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__lifecyclePolicyText__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 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 extract3() { if (!this.out) { this.out = obj.CA10__lifecyclePolicyText__c; } return this.out; };
      references1.push('Lifecycle Policy Text JSON [obj.CA10__lifecyclePolicyText__c]: ' + obj.CA10__lifecyclePolicyText__c);
      if (BytesLib.isEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__lifecyclePolicyText__c').isEmpty()", currentStateMessage: "The ECR repository does not have a lifecycle policy configured.", currentStateReferences: references1.join('\n'), remediation: "Create a lifecycle policy to automatically manage container images.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The ECR repository has a lifecycle policy configured.", 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__lifecyclePolicyText__c AS CA10__lifecyclePolicyText__c,
          process_CA10__CaAwsEcrRepository__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__lifecyclePolicyText__c AS CA10__lifecyclePolicyText__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsEcrRepository__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
