Skip to content

Commit 811e768

Browse files
authored
Merge pull request #1545 from jorris/ROK-923
Change git branch check from source to target
2 parents fedc71f + 9e5605d commit 811e768

File tree

6 files changed

+34
-23
lines changed

6 files changed

+34
-23
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= Git branch checks Package
22

3-
Check that the build was done from an expected git branch. The specific branches permitted are specified as a list of regexes in the `allowed_branch_patterns` rule data.
3+
Check that the build has an expected target git branch. The specific branches permitted are specified as a list of regexes in the `allowed_target_branch_patterns` rule data.
44

55
== Package Name
66

@@ -9,12 +9,12 @@ Check that the build was done from an expected git branch. The specific branches
99
== Rules Included
1010

1111
[#git_branch__git_branch]
12-
=== link:#git_branch__git_branch[Only allow builds from a trusted branch]
12+
=== link:#git_branch__git_branch[Builds have a trusted target branch]
1313

14-
Build must originate from a configured branch pattern (e.g., 'refs/heads/main')
14+
Build must target a configured branch pattern (e.g., 'c10s')
1515

1616
* Rule type: [rule-type-indicator failure]#FAILURE#
17-
* FAILURE message: `Build is from a branch %s which is not a trusted branch`
17+
* FAILURE message: `Build target is %s which is not a trusted target branch`
1818
* Code: `git_branch.git_branch`
1919
* Effective from: `2025-07-01T00:00:00Z`
2020
* https://github.com/conforma/policy/blob/{page-origin-refhash}/policy/release/git_branch/git_branch.rego#L14[Source, window="_blank"]

antora/docs/modules/ROOT/pages/release_policy.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Rules included:
227227
* xref:packages/release_cve.adoc#cve__unpatched_cve_blockers[CVE checks: Blocking unpatched CVE check]
228228
* xref:packages/release_cve.adoc#cve__cve_warnings[CVE checks: Non-blocking CVE check]
229229
* xref:packages/release_cve.adoc#cve__rule_data_provided[CVE checks: Rule data provided]
230-
* xref:packages/release_git_branch.adoc#git_branch__git_branch[Git branch checks: Only allow builds from a trusted branch]
230+
* xref:packages/release_git_branch.adoc#git_branch__git_branch[Git branch checks: Builds have a trusted target branch]
231231
* xref:packages/release_provenance_materials.adoc#provenance_materials__git_clone_source_matches_provenance[Provenance Materials: Git clone source matches materials provenance]
232232
* xref:packages/release_provenance_materials.adoc#provenance_materials__git_clone_task_found[Provenance Materials: Git clone task found]
233233
* xref:packages/release_rpm_pipeline.adoc#rpm_pipeline__invalid_pipeline[RPM Pipeline: Task version invalid_pipeline]
@@ -364,7 +364,7 @@ cve_leeway:
364364
a| Verify the attribute .predicate.buildDefinition.externalParameters of a SLSA Provenance v1.0 matches the expectation.
365365

366366
| xref:packages/release_git_branch.adoc[git_branch]
367-
a| Check that the build was done from an expected git branch. The specific branches permitted are specified as a list of regexes in the `allowed_branch_patterns` rule data.
367+
a| Check that the build has an expected target git branch. The specific branches permitted are specified as a list of regexes in the `allowed_target_branch_patterns` rule data.
368368

369369
| xref:packages/release_github_certificate.adoc[github_certificate]
370370
a| Verify attributes on the certificate involved in the image signature when using slsa-github-generator on GitHub Actions with Sigstore Fulcio

antora/docs/modules/ROOT/partials/release_policy_nav.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
**** xref:packages/release_external_parameters.adoc#external_parameters__pipeline_run_params_provided[PipelineRun params provided]
3636
**** xref:packages/release_external_parameters.adoc#external_parameters__restrict_shared_volumes[Restrict shared volumes]
3737
*** xref:packages/release_git_branch.adoc[Git branch checks]
38-
**** xref:packages/release_git_branch.adoc#git_branch__git_branch[Only allow builds from a trusted branch]
38+
**** xref:packages/release_git_branch.adoc#git_branch__git_branch[Builds have a trusted target branch]
3939
*** xref:packages/release_github_certificate.adoc[GitHub Certificate Checks]
4040
**** xref:packages/release_github_certificate.adoc#github_certificate__gh_workflow_extensions[GitHub Workflow Certificate Extensions]
4141
**** xref:packages/release_github_certificate.adoc#github_certificate__gh_workflow_name[GitHub Workflow Name]

example/data/rule_data.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
rule_data:
1919
# Usage: https://conforma.dev/docs/policy/packages/release_git_branch.html#git_branch__git_branch
20-
allowed_branch_patterns:
20+
allowed_target_branch_patterns:
2121
- ^refs/heads/main$
2222
- ^refs/heads/release-v[\d\.]+$
2323

policy/release/git_branch/git_branch.rego

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
# METADATA
33
# title: Git branch checks
44
# description: >-
5-
# Check that the build was done from an expected git branch. The
5+
# Check that the build has an expected target git branch. The
66
# specific branches permitted are specified as a list of regexes
7-
# in the `allowed_branch_patterns` rule data.
7+
# in the `allowed_target_branch_patterns` rule data.
88
#
99
package git_branch
1010

1111
import data.lib
1212
import rego.v1
1313

1414
# METADATA
15-
# title: Only allow builds from a trusted branch
16-
# description: Build must originate from a configured branch pattern (e.g., 'refs/heads/main')
15+
# title: Builds have a trusted target branch
16+
# description: Build must target a configured branch pattern (e.g., 'c10s')
1717
# custom:
1818
# short_name: git_branch
19-
# failure_msg: Build is from a branch %s which is not a trusted branch
19+
# failure_msg: Build target is %s which is not a trusted target branch
2020
# collections:
2121
# - redhat_rpms
2222
# effective_on: 2025-07-01
@@ -25,12 +25,12 @@ deny contains result if {
2525

2626
# Note that we're assuming that the annotation exists.
2727
# This will not produce a violation if the annotation is missing
28-
branch := task.invocation.environment.annotations["pipelinesascode.tekton.dev/source-branch"]
28+
branch := task.invocation.environment.annotations["build.appstudio.redhat.com/target_branch"]
2929
not matches_any(branch)
3030
result := lib.result_helper(rego.metadata.chain(), [branch])
3131
}
3232

3333
matches_any(branch) if {
34-
some pattern in lib.rule_data("allowed_branch_patterns")
34+
some pattern in lib.rule_data("allowed_target_branch_patterns")
3535
regex.match(pattern, branch)
3636
}

policy/release/git_branch/git_branch_test.rego

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import rego.v1
66

77
single_test_case(branch, expected_results) if {
88
# regal ignore:line-length
9-
mock_input := {"attestations": [{"statement": {"predicate": {"buildConfig": {"tasks": [{"invocation": {"environment": {"annotations": {"pipelinesascode.tekton.dev/source-branch": branch}}}}]}}}}]}
9+
mock_input := {"attestations": [{"statement": {"predicate": {"buildConfig": {"tasks": [{"invocation": {"environment": {"annotations": {"build.appstudio.redhat.com/target_branch": branch}}}}]}}}}]}
1010

11-
mock_rule_data := ["^refs/heads/main$", "^refs/heads/release-[23]$"]
11+
mock_rule_data := ["^c10s$", "^rhel-10.[0-9]+$", "^rhel-[0-9]+-main$", "branch[0-9]+-rhel-[0-9]+.[0-9]+.[0-9]+$"]
1212

1313
mock_tasks := mock_input.attestations[0].statement.predicate.buildConfig.tasks
1414

@@ -19,25 +19,36 @@ single_test_case(branch, expected_results) if {
1919
}
2020

2121
test_allow_with_main_branch if {
22-
single_test_case("refs/heads/main", [])
22+
single_test_case("rhel-9-main", [])
2323
}
2424

2525
test_allow_with_release_branch if {
26-
single_test_case("refs/heads/release-2", [])
26+
single_test_case("rhel-10.1", [])
27+
}
28+
29+
test_allow_with_c10s_branch if {
30+
single_test_case("c10s", [])
31+
}
32+
33+
test_allow_with_hotfixbranch if {
34+
single_test_case("kernel-5.14.0-570.42.1.el9_6-branch1-rhel-9.6.0", [])
35+
single_test_case("kernel-5.14.0-570.42.1.el9_6-branch1-rhel-9.6.0", [])
36+
single_test_case("kernel-5.14.0-570.42.1.el10_3-branch1-rhel-10.3.1", [])
37+
single_test_case("kernel-5.14.0-570.42.1.el11_2-branch13-rhel-11.2.9", [])
2738
}
2839

2940
test_deny_with_disallowed_branch if {
3041
expected := {{
3142
"code": "git_branch.git_branch",
32-
"msg": "Build is from a branch refs/heads/feature-branch which is not a trusted branch",
43+
"msg": "Build target is feature-branch which is not a trusted target branch",
3344
}}
34-
single_test_case("refs/heads/feature-branch", expected)
45+
single_test_case("feature-branch", expected)
3546
}
3647

3748
test_deny_with_unmatched_branch if {
3849
expected := {{
3950
"code": "git_branch.git_branch",
40-
"msg": "Build is from a branch refs/heads/release-1 which is not a trusted branch",
51+
"msg": "Build target is release-1 which is not a trusted target branch",
4152
}}
42-
single_test_case("refs/heads/release-1", expected)
53+
single_test_case("release-1", expected)
4354
}

0 commit comments

Comments
 (0)