Skip to content

Commit 9e96e87

Browse files
author
Anna Rania
committed
feat(CLOUDDST-29067): Add OLM allowed resource kinds policy
1 parent ba35c61 commit 9e96e87

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

antora/docs/modules/ROOT/pages/packages/release_olm.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ Each image referenced by the OLM bundle should match an entry in the list of pre
4545
* Effective from: `2024-09-01T00:00:00Z`
4646
* https://github.com/conforma/policy/blob/{page-origin-refhash}/policy/release/olm/olm.rego#L304[Source, window="_blank"]
4747

48+
[#olm__allowed_resource_kinds]
49+
=== link:#olm__allowed_resource_kinds[OLM bundle image manifests contain only allowed resource kinds]
50+
51+
Every manifest in an OLM bundle must be of an allowed resource kind, as defined by the rule data key `allowed_olm_resource_kinds`.
52+
53+
*Solution*: Remove any unsupported OLM resource kinds in the bundle manifests.
54+
55+
* Rule type: [rule-type-indicator failure]#FAILURE#
56+
* FAILURE message: `The %q manifest kind is not in the list of OLM allowed resource kinds.`
57+
* Code: `olm.allowed_resource_kinds`
58+
* https://github.com/conforma/policy/blob/{page-origin-refhash}/policy/release/olm/olm.rego#L361[Source, window="_blank"]
59+
4860
[#olm__olm_bundle_multi_arch]
4961
=== link:#olm__olm_bundle_multi_arch[OLM bundle images are not multi-arch]
5062

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Rules included:
126126
* xref:packages/release_olm.adoc#olm__csv_semver_format[OLM: ClusterServiceVersion semver format]
127127
* xref:packages/release_olm.adoc#olm__feature_annotations_format[OLM: Feature annotations have expected value]
128128
* xref:packages/release_olm.adoc#olm__allowed_registries[OLM: Images referenced by OLM bundle are from allowed registries]
129+
* xref:packages/release_olm.adoc#olm__allowed_resource_kinds[OLM: OLM bundle image manifests contain only allowed resource kinds]
129130
* xref:packages/release_olm.adoc#olm__olm_bundle_multi_arch[OLM: OLM bundle images are not multi-arch]
130131
* xref:packages/release_olm.adoc#olm__allowed_registries_related[OLM: Related images references are from allowed registries]
131132
* xref:packages/release_olm.adoc#olm__required_olm_features_annotations_provided[OLM: Required OLM feature annotations list provided]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
**** xref:packages/release_olm.adoc#olm__csv_semver_format[ClusterServiceVersion semver format]
6060
**** xref:packages/release_olm.adoc#olm__feature_annotations_format[Feature annotations have expected value]
6161
**** xref:packages/release_olm.adoc#olm__allowed_registries[Images referenced by OLM bundle are from allowed registries]
62+
**** xref:packages/release_olm.adoc#olm__allowed_resource_kinds[OLM bundle image manifests contain only allowed resource kinds]
6263
**** xref:packages/release_olm.adoc#olm__olm_bundle_multi_arch[OLM bundle images are not multi-arch]
6364
**** xref:packages/release_olm.adoc#olm__allowed_registries_related[Related images references are from allowed registries]
6465
**** xref:packages/release_olm.adoc#olm__required_olm_features_annotations_provided[Required OLM feature annotations list provided]

policy/lib/rule_data.rego

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ rule_data_defaults := {
8989
"registry.access.redhat.com/",
9090
"registry.redhat.io/",
9191
],
92+
# Used in release/olm.rego
93+
# The NetworkPolicy resource kind is temporarily not allowed for all OCP versions until OLM releases a backport
94+
# More information here:https://groups.google.com/a/redhat.com/g/aos-devel/c/yaWHjkj-tuA/m/RZh2YSQgBgAJ
95+
"allowed_olm_resource_kinds": [
96+
"ClusterServiceVersion",
97+
"CustomResourceDefinition",
98+
"Secret",
99+
"ClusterRole",
100+
"ClusterRoleBinding",
101+
"ConfigMap",
102+
"ServiceAccount",
103+
"Service",
104+
"Role",
105+
"RoleBinding",
106+
"PrometheusRule",
107+
"ServiceMonitor",
108+
"PodDisruptionBudget",
109+
"PriorityClass",
110+
"VerticalPodAutoscaler",
111+
"ConsoleYAMLSample",
112+
"ConsoleQuickStart",
113+
"ConsoleCLIDownload",
114+
"ConsoleLink",
115+
"ConsolePlugin",
116+
],
92117
#
93118
# Used in release/hermetic_task/hermetic_task.rego
94119
"required_hermetic_tasks": [

policy/release/olm/olm.rego

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,30 @@ deny contains result if {
358358
result := lib.result_helper_with_term(rego.metadata.chain(), [input.image.ref], input.image.ref)
359359
}
360360

361+
# METADATA
362+
# title: OLM bundle image manifests contain only allowed resource kinds
363+
# description: >-
364+
# Every manifest in an OLM bundle must be of an allowed resource kind,
365+
# as defined by the rule data key `allowed_olm_resource_kinds`.
366+
# custom:
367+
# short_name: allowed_resource_kinds
368+
# failure_msg: The %q manifest kind is not in the list of OLM allowed resource kinds.
369+
# solution: >-
370+
# Remove any unsupported OLM resource kinds in the bundle manifests.
371+
# collections:
372+
# - redhat
373+
deny contains result if {
374+
some path, manifest in input.image.files
375+
376+
# Only consider files in the manifests directory (as defined by OLM label)
377+
manifest_dir := input.image.config.Labels[manifestv1]
378+
startswith(path, manifest_dir)
379+
380+
not manifest.kind in lib.rule_data("allowed_olm_resource_kinds")
381+
382+
result := lib.result_helper_with_term(rego.metadata.chain(), [manifest.kind], manifest.kind)
383+
}
384+
361385
_name(o) := n if {
362386
n := o.name
363387
} else := "unnamed"

policy/release/olm/olm_test.rego

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ manifest := {
9898
"metadata-with-empty-annotations": {"metadata": {"annotations": {}}},
9999
}
100100

101+
network_policy_manifest := {
102+
"apiVersion": "networking.k8s.io/v1",
103+
"kind": "NetworkPolicy",
104+
"metadata": {"name": "default-deny"},
105+
"spec": {"podSelector": {}, "policyTypes": ["Ingress", "Egress"]},
106+
}
107+
108+
service_manifest := {
109+
"apiVersion": "v1",
110+
"kind": "Service",
111+
"metadata": {"name": "simple-demo-operator-controller-manager-metrics-service"},
112+
"spec": {"ports": [{"port": 8443, "targetPort": 8443}]},
113+
}
114+
101115
# regal ignore:rule-length
102116
test_all_image_ref if {
103117
lib.assert_equal(
@@ -137,12 +151,14 @@ test_all_good if {
137151
lib.assert_empty(olm.deny) with input.image.files as {"manifests/csv.yaml": manifest}
138152
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
139153
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
154+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
140155
}
141156

142157
test_all_good_custom_dir if {
143158
lib.assert_empty(olm.deny) with input.image.files as {"other/csv.yaml": manifest}
144159
with input.image.config.Labels as {olm.manifestv1: "other/"}
145160
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
161+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
146162
}
147163

148164
test_related_img_unpinned if {
@@ -162,6 +178,7 @@ test_related_img_unpinned if {
162178
lib.assert_equal_results(olm.deny, expected) with input.image.files as {"manifests/csv.yaml": unpinned_manifest}
163179
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
164180
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
181+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
165182
}
166183

167184
test_feature_annotations_format if {
@@ -201,6 +218,7 @@ test_feature_annotations_format if {
201218
lib.assert_equal_results(olm.deny, expected) with input.image.files as {"manifests/csv.yaml": bad_manifest}
202219
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
203220
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
221+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
204222
}
205223

206224
test_feature_annotations_format_custom_rule_data if {
@@ -218,6 +236,7 @@ test_feature_annotations_format_custom_rule_data if {
218236
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
219237
with data.rule_data.required_olm_features_annotations as ["foo", "spam"]
220238
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
239+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
221240
}
222241

223242
test_required_olm_features_annotations_provided if {
@@ -231,6 +250,7 @@ test_required_olm_features_annotations_provided if {
231250
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
232251
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
233252
with data.rule_data.required_olm_features_annotations as []
253+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
234254

235255
d := [
236256
# Wrong type
@@ -268,6 +288,7 @@ test_required_olm_features_annotations_provided if {
268288
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
269289
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
270290
with data.rule_data.required_olm_features_annotations as d
291+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
271292
}
272293

273294
test_csv_semver_format_bad_semver if {
@@ -281,6 +302,7 @@ test_csv_semver_format_bad_semver if {
281302
lib.assert_equal_results(olm.deny, expected) with input.image.files as {"manifests/csv.yaml": csv}
282303
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
283304
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
305+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
284306
}
285307

286308
test_csv_semver_format_missing if {
@@ -294,6 +316,7 @@ test_csv_semver_format_missing if {
294316
lib.assert_equal_results(olm.deny, expected) with input.image.files as {"manifests/csv.yaml": csv}
295317
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
296318
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
319+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
297320
}
298321

299322
test_subscriptions_annotation_format if {
@@ -340,6 +363,7 @@ test_subscriptions_annotation_format if {
340363
lib.assert_equal_results(olm.deny, expected) with input.image.files as files
341364
with input.image.config.Labels as {olm.manifestv1: "m/"}
342365
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
366+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
343367
}
344368

345369
test_unpinned_snapshot_references_operator if {
@@ -353,6 +377,7 @@ test_unpinned_snapshot_references_operator if {
353377
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
354378
with ec.oci.image_manifest as `{"config": {"digest": "sha256:goat"}}`
355379
with input.image.ref as unpinned_component.containerImage
380+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
356381
}
357382

358383
test_unpinned_snapshot_references_different_input if {
@@ -373,6 +398,7 @@ test_unmapped_references_in_operator if {
373398
lib.assert_equal_results(olm.deny, expected) with input.snapshot.components as [component1]
374399
with input.image.files as {"manifests/csv.yaml": manifest}
375400
with data.rule_data as {"pipeline_intention": "release", "allowed_olm_image_registry_prefixes": ["registry.io"]}
401+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
376402
with ec.oci.image_manifest as _mock_image_partial
377403
with ec.oci.descriptor as mock_ec_oci_image_descriptor
378404
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
@@ -436,6 +462,7 @@ test_unmapped_references_none_found if {
436462
with input.image.files as {"manifests/csv.yaml": manifest}
437463
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
438464
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io"]
465+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
439466
}
440467

441468
test_allowed_registries if {
@@ -444,6 +471,7 @@ test_allowed_registries if {
444471
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io", "registry.redhat.io"]
445472
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
446473
with input.image.files as {"manifests/csv.yaml": manifest}
474+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
447475
}
448476

449477
test_bundle_image_index if {
@@ -457,6 +485,7 @@ test_bundle_image_index if {
457485

458486
lib.assert_equal_results(olm.deny, expected_deny) with data.rule_data.pipeline_intention as "release"
459487
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.io", "registry.redhat.io"]
488+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
460489
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
461490
with input.image.files as {"manifests/csv.yaml": manifest}
462491
with input.image.ref as pinned1
@@ -482,6 +511,7 @@ test_unallowed_registries if {
482511
# This expects failure as registry.io is not a member of allowed_olm_image_registry_prefixes
483512
lib.assert_equal_results(olm.deny, expected) with data.rule_data.pipeline_intention as "release"
484513
with data.rule_data.allowed_olm_image_registry_prefixes as ["registry.access.redhat.com", "registry.redhat.io"]
514+
with data.rule_data.allowed_olm_resource_kinds as ["ClusterServiceVersion"]
485515
with input.image.config.Labels as {olm.manifestv1: "manifests/"}
486516
with input.image.files as {"manifests/csv.yaml": manifest}
487517
}
@@ -604,3 +634,23 @@ test_image_ref_with_repo_only if {
604634
expected := "registry.io/repo"
605635
lib.assert_equal(olm._image_ref(img), expected)
606636
}
637+
638+
test_disallowed_olm_resource_kind if {
639+
expected := {{
640+
"code": "olm.allowed_resource_kinds",
641+
"msg": "The \"NetworkPolicy\" manifest kind is not in the list of OLM allowed resource kinds.",
642+
"term": "NetworkPolicy",
643+
}}
644+
645+
lib.assert_equal_results(olm.deny, expected) with input.image.config.Labels as {olm.manifestv1: "manifests/"}
646+
with input.image.files as {"manifests/networkpolicy.yaml": network_policy_manifest}
647+
with data.rule_data.allowed_olm_resource_kinds as ["foo", "bar"]
648+
}
649+
650+
test_allowed_olm_resource_kind if {
651+
expected_empty := {}
652+
653+
lib.assert_equal_results(olm.deny, expected_empty) with input.image.config.Labels as {olm.manifestv1: "manifests/"}
654+
with input.image.files as {"manifests/service.yaml": service_manifest}
655+
with data.rule_data.allowed_olm_resource_kinds as ["Service"]
656+
}

0 commit comments

Comments
 (0)