|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Moritz Sanft <58110325+msanft@users.noreply.github.com> |
| 3 | +Date: Mon, 20 Jan 2025 13:44:00 +0100 |
| 4 | +Subject: [PATCH] genpolicy: support dynamic annotations |
| 5 | + |
| 6 | +This adds support for handling annotations with dynamic keys to |
| 7 | +genpolicy. This is necessary for use-cases like GPU containers, where |
| 8 | +in-cluster components (i.e. post policy-generation) instrument |
| 9 | +containers with annotations with varying keys, like `cdi.k8s.io/vfioXY`, |
| 10 | +where `XY` corresponds to a dynamic ID. |
| 11 | +--- |
| 12 | + src/tools/genpolicy/genpolicy-settings.json | 8 ++- |
| 13 | + src/tools/genpolicy/rules.rego | 56 +++++++++++++++++++-- |
| 14 | + src/tools/genpolicy/src/policy.rs | 6 +++ |
| 15 | + 3 files changed, 64 insertions(+), 6 deletions(-) |
| 16 | + |
| 17 | +diff --git a/src/tools/genpolicy/genpolicy-settings.json b/src/tools/genpolicy/genpolicy-settings.json |
| 18 | +index 9b95f9f7462717d04f0b9ce685d97c0455f949da..7dac0e5e0585c25e324a39656d1a2dcfa12e7d96 100644 |
| 19 | +--- a/src/tools/genpolicy/genpolicy-settings.json |
| 20 | ++++ b/src/tools/genpolicy/genpolicy-settings.json |
| 21 | +@@ -309,7 +309,10 @@ |
| 22 | + "CAP_PERFMON", |
| 23 | + "CAP_BPF", |
| 24 | + "CAP_CHECKPOINT_RESTORE" |
| 25 | +- ] |
| 26 | ++ ], |
| 27 | ++ "dynamic_annotations": { |
| 28 | ++ "^cdi\\.k8s\\.io\\/vfio[0-9]{2}$": "^nvidia.com/gpu=[0-9]+$" |
| 29 | ++ } |
| 30 | + }, |
| 31 | + "kata_config": { |
| 32 | + "confidential_guest": false, |
| 33 | +@@ -333,7 +336,8 @@ |
| 34 | + "^AZURE_CLIENT_ID=[A-Fa-f0-9-]*$", |
| 35 | + "^AZURE_TENANT_ID=[A-Fa-f0-9-]*$", |
| 36 | + "^AZURE_FEDERATED_TOKEN_FILE=/var/run/secrets/azure/tokens/azure-identity-token$", |
| 37 | +- "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$" |
| 38 | ++ "^AZURE_AUTHORITY_HOST=https://login\\.microsoftonline\\.com/$", |
| 39 | ++ "^PCI_RESOURCE_NVIDIA_COM.*=[a-fA-F0-9:.-]*$" |
| 40 | + ] |
| 41 | + }, |
| 42 | + "CopyFileRequest": [ |
| 43 | +diff --git a/src/tools/genpolicy/rules.rego b/src/tools/genpolicy/rules.rego |
| 44 | +index 43cb19a56fe8ea5833708f0639c9e85ddd884cb3..271df2aebe05bd4bbd7aa396be24fb6fee0668bf 100644 |
| 45 | +--- a/src/tools/genpolicy/rules.rego |
| 46 | ++++ b/src/tools/genpolicy/rules.rego |
| 47 | +@@ -199,26 +199,31 @@ allow_anno(p_oci, i_oci) { |
| 48 | + } |
| 49 | + allow_anno(p_oci, i_oci) { |
| 50 | + print("allow_anno 2: p Annotations =", p_oci.Annotations) |
| 51 | ++ p_dynamic_annotations := policy_data.common.dynamic_annotations |
| 52 | ++ print("allow_anno 2: p Dynamic Annotations =", p_dynamic_annotations) |
| 53 | ++ |
| 54 | ++ i_annotations := i_oci.Annotations |
| 55 | + print("allow_anno 2: i Annotations =", i_oci.Annotations) |
| 56 | + |
| 57 | +- i_keys := object.keys(i_oci.Annotations) |
| 58 | ++ i_keys := object.keys(i_annotations) |
| 59 | + print("allow_anno 2: i keys =", i_keys) |
| 60 | + |
| 61 | + every i_key in i_keys { |
| 62 | +- allow_anno_key(i_key, p_oci) |
| 63 | ++ allow_anno_key(i_key, p_oci, p_dynamic_annotations) |
| 64 | ++ allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) |
| 65 | + } |
| 66 | + |
| 67 | + print("allow_anno 2: true") |
| 68 | + } |
| 69 | + |
| 70 | +-allow_anno_key(i_key, p_oci) { |
| 71 | ++allow_anno_key(i_key, p_oci, p_dynamic_annotations) { |
| 72 | + print("allow_anno_key 1: i key =", i_key) |
| 73 | + |
| 74 | + startswith(i_key, "io.kubernetes.cri.") |
| 75 | + |
| 76 | + print("allow_anno_key 1: true") |
| 77 | + } |
| 78 | +-allow_anno_key(i_key, p_oci) { |
| 79 | ++allow_anno_key(i_key, p_oci, p_dynamic_annotations) { |
| 80 | + print("allow_anno_key 2: i key =", i_key) |
| 81 | + |
| 82 | + some p_key, _ in p_oci.Annotations |
| 83 | +@@ -227,6 +232,49 @@ allow_anno_key(i_key, p_oci) { |
| 84 | + print("allow_anno_key 2: true") |
| 85 | + } |
| 86 | + |
| 87 | ++allow_anno_key(i_key, p_oci, p_dynamic_annotations) { |
| 88 | ++ print("allow_anno_key 3: i key =", i_key) |
| 89 | ++ |
| 90 | ++ some p_key, _ in p_dynamic_annotations |
| 91 | ++ regex.match(p_key, i_key) |
| 92 | ++ |
| 93 | ++ print("allow_anno_key 3: true") |
| 94 | ++} |
| 95 | ++ |
| 96 | ++ |
| 97 | ++# Account for containers without dynamic annotations |
| 98 | ++# at all.. |
| 99 | ++allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) { |
| 100 | ++ print("allow_dynamic_anno_value 1: i key =", i_key) |
| 101 | ++ |
| 102 | ++ not p_dynamic_annotations |
| 103 | ++ |
| 104 | ++ print("allow_dynamic_anno_value 1: true") |
| 105 | ++} |
| 106 | ++# ..for annotations which do not have a corresponding |
| 107 | ++# dynamic annotation set in the settings... |
| 108 | ++allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) { |
| 109 | ++ print("allow_dynamic_anno_value 2: i key =", i_key) |
| 110 | ++ |
| 111 | ++ every p_key, _ in p_dynamic_annotations { |
| 112 | ++ not regex.match(p_key, i_key) |
| 113 | ++ } |
| 114 | ++ |
| 115 | ++ print("allow_dynamic_anno_value 2: true") |
| 116 | ++} |
| 117 | ++# ...and check those which do. |
| 118 | ++allow_dynamic_anno_value(i_key, i_annotations, p_dynamic_annotations) { |
| 119 | ++ print("allow_dynamic_anno_value 3: i key =", i_key) |
| 120 | ++ |
| 121 | ++ some p_key, p_value in p_dynamic_annotations |
| 122 | ++ regex.match(p_key, i_key) |
| 123 | ++ |
| 124 | ++ i_value := i_annotations[i_key] |
| 125 | ++ regex.match(p_value, i_value) |
| 126 | ++ |
| 127 | ++ print("allow_dynamic_anno_value 3: true") |
| 128 | ++} |
| 129 | ++ |
| 130 | + # Get the value of the S_NAME_KEY annotation and |
| 131 | + # correlate it with other annotations and process fields. |
| 132 | + allow_by_anno(p_container, i_oci, i_storages) { |
| 133 | +diff --git a/src/tools/genpolicy/src/policy.rs b/src/tools/genpolicy/src/policy.rs |
| 134 | +index e2012bf6f2d80ffea678a38803d8e85f5369b9dc..80bb6a63b915fa021e60f2b1d60e4bb32b67ba19 100644 |
| 135 | +--- a/src/tools/genpolicy/src/policy.rs |
| 136 | ++++ b/src/tools/genpolicy/src/policy.rs |
| 137 | +@@ -392,6 +392,12 @@ pub struct CommonData { |
| 138 | + |
| 139 | + /// Default capabilities for a privileged container. |
| 140 | + pub privileged_caps: Vec<String>, |
| 141 | ++ |
| 142 | ++ /// Dynamic annotations contains arbitrary metadata for the container. |
| 143 | ++ /// It is different to `KataSpec.Annotations` in that it allows dynamic keys *and* |
| 144 | ++ /// values, and that they are checked for *all* keys, whereas `Annotations` |
| 145 | ++ /// only allows dynamic values, and only checks them for certain keys at all. |
| 146 | ++ pub dynamic_annotations: BTreeMap<String, String>, |
| 147 | + } |
| 148 | + |
| 149 | + /// Configuration from "kubectl config". |
0 commit comments