|
| 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 volatile_config_contract |
| 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 %s 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": "volatile_config_contract.pending_rule", |
| 26 | + "msg": sprintf("Volatile include rule '%s' is pending activation (effective on: %s)", [config.value, effective_on]), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +# METADATA |
| 31 | +# title: Volatile config rule expiring soon |
| 32 | +# description: Warns when a volatile config rule will expire within threshold |
| 33 | +# custom: |
| 34 | +# short_name: expiring_rule |
| 35 | +# failure_msg: "Volatile %s rule '%s' expires soon (effective until: %s)" |
| 36 | +# solution: Review the volatile configuration rule and decide whether to extend it |
| 37 | +# collections: |
| 38 | +# - minimal |
| 39 | +warn contains result if { |
| 40 | + some source in object.get(input, ["policy_spec", "sources"], []) |
| 41 | + volatile_config := object.get(source, "volatileConfig", {}) |
| 42 | + some config in object.get(volatile_config, "include", []) |
| 43 | + _is_applicable(config) |
| 44 | + effective_until := object.get(config, "effectiveUntil", "") |
| 45 | + effective_until != "" |
| 46 | + not _is_past_date(effective_until) |
| 47 | + result := { |
| 48 | + "code": "volatile_config_contract.expiring_rule", |
| 49 | + "msg": sprintf("Volatile include rule '%s' expires soon (effective until: %s)", [config.value, effective_until]), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +# METADATA |
| 54 | +# title: Volatile config rule has no expiration |
| 55 | +# description: Warns when a volatile config rule has no effectiveUntil date |
| 56 | +# custom: |
| 57 | +# short_name: no_expiration |
| 58 | +# failure_msg: "Volatile %s rule '%s' has no expiration date set" |
| 59 | +# solution: Consider adding an effectiveUntil date |
| 60 | +# collections: |
| 61 | +# - minimal |
| 62 | +warn contains result if { |
| 63 | + some source in object.get(input, ["policy_spec", "sources"], []) |
| 64 | + volatile_config := object.get(source, "volatileConfig", {}) |
| 65 | + some config in object.get(volatile_config, "include", []) |
| 66 | + _is_applicable(config) |
| 67 | + object.get(config, "effectiveUntil", "") == "" |
| 68 | + effective_on := object.get(config, "effectiveOn", "") |
| 69 | + not _is_future_date(effective_on) |
| 70 | + result := { |
| 71 | + "code": "volatile_config_contract.no_expiration", |
| 72 | + "msg": sprintf("Volatile include rule '%s' has no expiration date set", [config.value]), |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +# Check if rule is applicable to current image (global or matching) |
| 77 | +_is_applicable(config) if { |
| 78 | + # Global rule - no image/component constraints |
| 79 | + object.get(config, "imageRef", "") == "" |
| 80 | + object.get(config, "imageUrl", "") == "" |
| 81 | + object.get(config, "imageDigest", "") == "" |
| 82 | + count(object.get(config, "componentNames", [])) == 0 |
| 83 | +} |
| 84 | + |
| 85 | +_is_applicable(config) if { |
| 86 | + # Match by component name |
| 87 | + component_names := object.get(config, "componentNames", []) |
| 88 | + count(component_names) > 0 |
| 89 | + input_component := object.get(input, "component_name", "") |
| 90 | + some name in component_names |
| 91 | + name == input_component |
| 92 | +} |
| 93 | + |
| 94 | +_is_applicable(config) if { |
| 95 | + # Match by imageDigest |
| 96 | + image_digest := object.get(config, "imageDigest", "") |
| 97 | + image_digest != "" |
| 98 | + _extract_digest(object.get(input, ["image", "ref"], "")) == image_digest |
| 99 | +} |
| 100 | + |
| 101 | +# Helper to extract digest from image ref |
| 102 | +_extract_digest(ref) := digest if { |
| 103 | + contains(ref, "@") |
| 104 | + parts := split(ref, "@") |
| 105 | + count(parts) == 2 |
| 106 | + digest := parts[1] |
| 107 | +} |
| 108 | + |
| 109 | +_extract_digest(ref) := "" if { |
| 110 | + not contains(ref, "@") |
| 111 | +} |
| 112 | + |
| 113 | +# Helper to check if date is in the future (simplified - just check it's set) |
| 114 | +_is_future_date(date_str) if { |
| 115 | + date_str != "" |
| 116 | + # Parse and compare - for testing we use a fixed "now" via effective_current_time_ns |
| 117 | + # In real usage this would use time.now_ns() |
| 118 | + date_ns := time.parse_rfc3339_ns(date_str) |
| 119 | + now_ns := _current_time_ns |
| 120 | + date_ns > now_ns |
| 121 | +} |
| 122 | + |
| 123 | +# Helper to check if date is in the past |
| 124 | +_is_past_date(date_str) if { |
| 125 | + date_str != "" |
| 126 | + date_ns := time.parse_rfc3339_ns(date_str) |
| 127 | + now_ns := _current_time_ns |
| 128 | + date_ns < now_ns |
| 129 | +} |
| 130 | + |
| 131 | +# Get current time - can be overridden in tests |
| 132 | +_current_time_ns := time.now_ns() |
0 commit comments