---
policy: /ce/ca/aws/codebuild/project-bitbucket-source-url-contains-creds
logic: /ce/ca/aws/codebuild/project-bitbucket-source-url-contains-creds/prod.logic.yaml
executionTime: 2025-07-11T00:36:31.68155777Z
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: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__sourceType__c') != 'BITBUCKET'
      actual: extract('CA10__sourceType__c') != 'BITBUCKET'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__sourceLocation__c').contains('@')
      actual: extract('CA10__sourceLocation__c').contains('@')
    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/aws/codebuild/project-bitbucket-source-url-contains-creds/policy.yaml
    md5Hash: 2E96677C1BA4E939DF0F8E98035FEB14
    content: "---\nnames:\n  full: AWS CodeBuild Project Bitbucket Source Location\
      \ URL contains credentials\n  contextual: Project Bitbucket Source Location\
      \ URL contains credentials\ndescription: >\n  Ensures that AWS CodeBuild Project\
      \ Bitbucket source repository URLs do not \n  contain user credentials.\ntype:\
      \ COMPLIANCE_POLICY\ncategories:\n  - SECURITY\nframeworkMappings:\n  - \"/frameworks/cloudaware/resource-security/secure-access\"\
      \n  - \"/frameworks/aws-fsbp-v1.0.0/codebuild/01\"\nsimilarPolicies:\n  awsSecurityHub:\n\
      \    - name: \"[CodeBuild.1] CodeBuild Bitbucket source repository URLs should\
      \ not contain sensitive credentials\"\n      url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/codebuild-controls.html#codebuild-1\"\
      \n  internal:\n    - dec-x-cc74149f"
  - path: /ce/ca/aws/codebuild/project-bitbucket-source-url-contains-creds/prod.logic.yaml
    md5Hash: 89EEE3DBA5733838E14A4045BC6995D9
    content: |-
      ---
      inputType: "CA10__CaAwsCodeBuildProject__c"
      importExtracts:
        - file: /types/CA10__CaAwsCodeBuildProject__c/object.extracts.yaml
      testData:
        - file: "test-data.json"
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The source provider is not Bitbucket."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__sourceType__c"
              right:
                TEXT: "BITBUCKET"
        - status: "INCOMPLIANT"
          currentStateMessage: "Bitbucket source repository URL contains credentials."
          remediationMessage: "The repository URL should not contain credentials and use OAuth for authentication instead."
          check:
            CONTAINS:
              arg:
                EXTRACT: "CA10__sourceLocation__c"
              search:
                TEXT: "@"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "Bitbucket source repository URL does not contain sensitive credentials."
  - path: /ce/ca/aws/codebuild/project-bitbucket-source-url-contains-creds/test-data.json
    md5Hash: 574BCB12F26B8E36C79D80106280296D
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-07-08T18:56:31Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-06-24T21:52:49Z",
              "CA10__sourceType__c": "BITBUCKET",
              "CA10__sourceLocation__c": "username:password@bitbucket.org/workspace/repository.git"
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__sourceType__c') != 'BITBUCKET'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-07-08T18:56:31Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__sourceType__c": "sourceType",
              "CA10__sourceLocation__c": "sourceLocation"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__sourceLocation__c').contains('@')",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-07-08T18:56:31Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__sourceType__c": "BITBUCKET",
              "CA10__sourceLocation__c": "username:password@bitbucket.org/workspace/repository.git"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-07-08T18:56:31Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__sourceType__c": "BITBUCKET",
              "CA10__sourceLocation__c": "bitbucket.org/workspace/repository.git"
          }
      ]
  - path: /types/CA10__CaAwsCodeBuildProject__c/object.extracts.yaml
    md5Hash: D208E74BC8FC2D188EC746EC51BDE15A
    content: "---\nextracts:\n# Values: CODECOMMIT | CODEPIPELINE | GITHUB | GITLAB\
      \ | GITLAB_SELF_MANAGED | S3 | BITBUCKET | GITHUB_ENTERPRISE | NO_SOURCE\n#\
      \ Not Nullable. \n  - name: CA10__sourceType__c\n    value: \n      FIELD:\n\
      \        path: CA10__sourceType__c\n        undeterminedIf:\n          noAccessDelegate:\n\
      \            path: \"CA10__sourceType__c\"\n            currentStateMessage:\
      \ \"CodeBuild Project Source Type cannot be empty. Possible permissions issue\
      \ with codebuild:BatchGetBuilds\"\n# Nullable.\n  - name: CA10__sourceLocation__c\n\
      \    value: \n      FIELD:\n        path: CA10__sourceLocation__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" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__sourceType__c') != 'BITBUCKET'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__sourceLocation__c').contains('@')",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsCodeBuildProject__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__sourceType__c STRING,
      CA10__sourceLocation__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-07-08T18:56:31Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2025-06-24T21:52:49Z"),
    "CA10__sourceType__c" : "BITBUCKET",
    "CA10__sourceLocation__c" : "username:password@bitbucket.org/workspace/repository.git"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-08T18:56:31Z")
    },
    "Id" : "test2",
    "CA10__sourceType__c" : "sourceType",
    "CA10__sourceLocation__c" : "sourceLocation"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-08T18:56:31Z")
    },
    "Id" : "test3",
    "CA10__sourceType__c" : "BITBUCKET",
    "CA10__sourceLocation__c" : "username:password@bitbucket.org/workspace/repository.git"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-07-08T18:56:31Z")
    },
    "Id" : "test4",
    "CA10__sourceType__c" : "BITBUCKET",
    "CA10__sourceLocation__c" : "bitbucket.org/workspace/repository.git"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsCodeBuildProject__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__sourceType__c STRING,
          CA10__sourceLocation__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 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__sourceType__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__sourceType__c.delegatedTo(CA10__sourceType__c).isEmpty()", currentStateMessage: "CodeBuild Project Source Type cannot be empty. Possible permissions issue with codebuild:BatchGetBuilds", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__sourceType__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Source Type [obj.CA10__sourceType__c]: ' + obj.CA10__sourceType__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'BITBUCKET')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__sourceType__c') != 'BITBUCKET'", currentStateMessage: "The source provider is not Bitbucket.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__sourceLocation__c; } return this.out; };
      references1.push('Source Location [obj.CA10__sourceLocation__c]: ' + obj.CA10__sourceLocation__c);
      if (TextLib.contains(extract6.call(extract6), '@')) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__sourceLocation__c').contains('@')", currentStateMessage: "Bitbucket source repository URL contains credentials.", currentStateReferences: references1.join('\n'), remediation: "The repository URL should not contain credentials and use OAuth for authentication instead.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "Bitbucket source repository URL does not contain sensitive credentials.", 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__sourceType__c AS CA10__sourceType__c,
          sObject.CA10__sourceLocation__c AS CA10__sourceLocation__c,
          process_CA10__CaAwsCodeBuildProject__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__sourceType__c AS CA10__sourceType__c,
                  sObject.CA10__sourceLocation__c AS CA10__sourceLocation__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsCodeBuildProject__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
