|
| 1 | +# Policy to validate volatile config schema contract between ec-cli and ec-policies. |
| 2 | +# This serves as an integration test to ensure the input schema is correctly populated. |
| 3 | +package main |
| 4 | + |
| 5 | +import rego.v1 |
| 6 | + |
| 7 | +# METADATA |
| 8 | +# title: Volatile config rule pending activation |
| 9 | +# description: Warns when a volatile config rule has effectiveOn in the future |
| 10 | +# custom: |
| 11 | +# short_name: pending_rule |
| 12 | +# failure_msg: "Volatile include rule '%s' is pending activation (effective on: %s)" |
| 13 | +# solution: Informational warning about pending volatile config rule |
| 14 | +# collections: |
| 15 | +# - minimal |
| 16 | +warn contains result if { |
| 17 | + some source in object.get(input, ["policy_spec", "sources"], []) |
| 18 | + volatile_config := object.get(source, "volatileConfig", {}) |
| 19 | + some config in object.get(volatile_config, "include", []) |
| 20 | + _is_applicable(config) |
| 21 | + effective_on := object.get(config, "effectiveOn", "") |
| 22 | + effective_on != "" |
| 23 | + _is_future_date(effective_on) |
| 24 | + result := { |
| 25 | + "code": "main.pending_rule", |
| 26 | + "collections": ["minimal"], |
| 27 | + "effective_on": "2022-01-01T00:00:00Z", |
| 28 | + "msg": sprintf("Volatile include rule '%s' is pending activation (effective on: %s)", [config.value, effective_on]), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +# METADATA |
| 33 | +# title: Volatile config rule expiring soon |
| 34 | +# description: Warns when a volatile config rule will expire within threshold |
| 35 | +# custom: |
| 36 | +# short_name: expiring_rule |
| 37 | +# failure_msg: "Volatile include rule '%s' expires soon (effective until: %s)" |
| 38 | +# solution: Review the volatile configuration rule and decide whether to extend it |
| 39 | +# collections: |
| 40 | +# - minimal |
| 41 | +warn contains result if { |
| 42 | + some source in object.get(input, ["policy_spec", "sources"], []) |
| 43 | + volatile_config := object.get(source, "volatileConfig", {}) |
| 44 | + some config in object.get(volatile_config, "include", []) |
| 45 | + _is_applicable(config) |
| 46 | + effective_until := object.get(config, "effectiveUntil", "") |
| 47 | + effective_until != "" |
| 48 | + not _is_past_date(effective_until) |
| 49 | + result := { |
| 50 | + "code": "main.expiring_rule", |
| 51 | + "collections": ["minimal"], |
| 52 | + "effective_on": "2022-01-01T00:00:00Z", |
| 53 | + "msg": sprintf("Volatile include rule '%s' expires soon (effective until: %s)", [config.value, effective_until]), |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +# METADATA |
| 58 | +# title: Volatile config rule has no expiration |
| 59 | +# description: Warns when a volatile config rule has no effectiveUntil date |
| 60 | +# custom: |
| 61 | +# short_name: no_expiration |
| 62 | +# failure_msg: "Volatile include rule '%s' has no expiration date set" |
| 63 | +# solution: Consider adding an effectiveUntil date |
| 64 | +# collections: |
| 65 | +# - minimal |
| 66 | +warn contains result if { |
| 67 | + some source in object.get(input, ["policy_spec", "sources"], []) |
| 68 | + volatile_config := object.get(source, "volatileConfig", {}) |
| 69 | + some config in object.get(volatile_config, "include", []) |
| 70 | + _is_applicable(config) |
| 71 | + object.get(config, "effectiveUntil", "") == "" |
| 72 | + effective_on := object.get(config, "effectiveOn", "") |
| 73 | + not _is_future_date(effective_on) |
| 74 | + result := { |
| 75 | + "code": "main.no_expiration", |
| 76 | + "collections": ["minimal"], |
| 77 | + "effective_on": "2022-01-01T00:00:00Z", |
| 78 | + "msg": sprintf("Volatile include rule '%s' has no expiration date set", [config.value]), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +# Check if rule is applicable to current image (global or matching) |
| 83 | +_is_applicable(config) if { |
| 84 | + # Global rule - no image/component constraints |
| 85 | + object.get(config, "imageRef", "") == "" |
| 86 | + object.get(config, "imageUrl", "") == "" |
| 87 | + object.get(config, "imageDigest", "") == "" |
| 88 | + count(object.get(config, "componentNames", [])) == 0 |
| 89 | +} |
| 90 | + |
| 91 | +_is_applicable(config) if { |
| 92 | + # Match by component name |
| 93 | + component_names := object.get(config, "componentNames", []) |
| 94 | + count(component_names) > 0 |
| 95 | + input_component := object.get(input, "component_name", "") |
| 96 | + some name in component_names |
| 97 | + name == input_component |
| 98 | +} |
| 99 | + |
| 100 | +_is_applicable(config) if { |
| 101 | + # Match by imageDigest |
| 102 | + image_digest := object.get(config, "imageDigest", "") |
| 103 | + image_digest != "" |
| 104 | + _extract_digest(object.get(input, ["image", "ref"], "")) == image_digest |
| 105 | +} |
| 106 | + |
| 107 | +# Helper to extract digest from image ref |
| 108 | +_extract_digest(ref) := digest if { |
| 109 | + contains(ref, "@") |
| 110 | + parts := split(ref, "@") |
| 111 | + count(parts) == 2 |
| 112 | + digest := parts[1] |
| 113 | +} |
| 114 | + |
| 115 | +_extract_digest(ref) := "" if { |
| 116 | + not contains(ref, "@") |
| 117 | +} |
| 118 | + |
| 119 | +# Helper to check if date is in the future |
| 120 | +_is_future_date(date_str) if { |
| 121 | + date_str != "" |
| 122 | + date_ns := time.parse_rfc3339_ns(date_str) |
| 123 | + now_ns := time.now_ns() |
| 124 | + date_ns > now_ns |
| 125 | +} |
| 126 | + |
| 127 | +# Helper to check if date is in the past |
| 128 | +_is_past_date(date_str) if { |
| 129 | + date_str != "" |
| 130 | + date_ns := time.parse_rfc3339_ns(date_str) |
| 131 | + now_ns := time.now_ns() |
| 132 | + date_ns < now_ns |
| 133 | +} |
0 commit comments