|
| 1 | +package tag_refs |
| 2 | + |
| 3 | +import rego.v1 |
| 4 | + |
| 5 | +# METADATA |
| 6 | +# custom: |
| 7 | +# short_name: count |
| 8 | +deny contains result if { |
| 9 | + refs := ec.oci.image_tag_refs(input.image.ref) |
| 10 | + count(refs) != 2 |
| 11 | + result := { |
| 12 | + "code": "tag_refs.count", |
| 13 | + "msg": sprintf("Expected 2 tag-based artifact references, got %d: %v", [count(refs), refs]), |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +# METADATA |
| 18 | +# custom: |
| 19 | +# short_name: format |
| 20 | +deny contains result if { |
| 21 | + refs := ec.oci.image_tag_refs(input.image.ref) |
| 22 | + not all_refs_valid_format(refs) |
| 23 | + result := { |
| 24 | + "code": "tag_refs.format", |
| 25 | + "msg": sprintf("Invalid tag reference format in: %v", [refs]), |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +# METADATA |
| 30 | +# custom: |
| 31 | +# short_name: sig_count |
| 32 | +deny contains result if { |
| 33 | + refs := ec.oci.image_tag_refs(input.image.ref) |
| 34 | + sig_count := count([ref | some ref in refs; contains(ref, ".sig")]) |
| 35 | + sig_count != 1 |
| 36 | + result := { |
| 37 | + "code": "tag_refs.sig_count", |
| 38 | + "msg": sprintf("Expected 1 .sig reference, got %d", [sig_count]), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +# METADATA |
| 43 | +# custom: |
| 44 | +# short_name: att_count |
| 45 | +deny contains result if { |
| 46 | + refs := ec.oci.image_tag_refs(input.image.ref) |
| 47 | + att_count := count([ref | some ref in refs; contains(ref, ".att")]) |
| 48 | + att_count != 1 |
| 49 | + result := { |
| 50 | + "code": "tag_refs.att_count", |
| 51 | + "msg": sprintf("Expected 1 .att reference, got %d", [att_count]), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +all_refs_valid_format(refs) if { |
| 56 | + every ref in refs { |
| 57 | + # Each ref should be a valid OCI reference with tag format: registry/repo:sha256-<hex>.<suffix> |
| 58 | + contains(ref, ":") |
| 59 | + contains(ref, "sha256-") |
| 60 | + # Split by : and get the last part (the tag) |
| 61 | + parts := split(ref, ":") |
| 62 | + tag_part := parts[count(parts) - 1] |
| 63 | + # Tag should start with sha256- and end with .sig or .att |
| 64 | + startswith(tag_part, "sha256-") |
| 65 | + valid_suffix(tag_part) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +valid_suffix(tag) if { |
| 70 | + endswith(tag, ".sig") |
| 71 | +} |
| 72 | + |
| 73 | +valid_suffix(tag) if { |
| 74 | + endswith(tag, ".att") |
| 75 | +} |
0 commit comments