This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
socket.rego
112 lines (89 loc) · 2.56 KB
/
socket.rego
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package socket
import future.keywords
default allow := false
input_labels_organized[name] contains value if {
some k, _ in input.labels
s := split(k, "=")
name := concat("", array.slice(s, 0, 1))
val := concat("=", array.slice(s, 1, count(s)))
name != val
value := val
}
replacements[key] := value if {
some name, values in input_labels_organized
key := concat("", ["[", name, "]"])
count(values) == 1
value := concat("", values)
}
input_remote_labels_organized[name] contains value if {
some k, _ in input.remote.labels
s := split(k, "=")
name := concat("", array.slice(s, 0, 1))
val := concat("=", array.slice(s, 1, count(s)))
name != val
value := val
}
egress_replacements[key] := value if {
some name, values in input_remote_labels_organized
key := concat("", ["[", name, "]"])
count(values) == 1
value := concat("", values)
}
replace_selectors(p, _) := policy if {
not p.certificate.workloadID
policy := p
}
replace_selectors(p, r) := policy if {
p.certificate.workloadID
wid := strings.replace_n(r, p.certificate.workloadID)
# TODO this regex stuff doesn't work on ARM on older, like 5.15 kernels
# spiffe_id_path_regex := `^(?:\/?(?:(?:[a-zA-Z0-9][a-zA-Z0-9-_]*[a-zA-Z0-9]|[a-zA-Z0-9]*)\.)*(?:[a-zA-Z0-9][a-zA-Z0-9-_]*[a-zA-Z0-9]|[a-zA-Z0-9]))*$`
# workloadIDIsValid := regex.match(wid, spiffe_id_path_regex)
policy := object.union(
p,
{"certificate": {
"workloadID": wid,
}},
)
}
policy_without_selectors(policy) := object.remove(policy, ["selectors"])
is_subset(super, sub) if {
sub_set := {value | some value, _ in sub}
super_set := {value | some value, _ in super}
c := sub_set & super_set
c == sub_set
}
matching_policies contains policy if {
some p in data.policies
some selectorset in p.selectors
is_subset(input.labels, selectorset)
policy := replace_selectors(
object.remove(
object.union(
p,
{"matched_selectors": selectorset},
),
["selectors"],
),
replacements,
)
}
matching_policies_wo_egresses contains policy if {
some p in matching_policies
policy := object.remove(p, ["egress"])
}
egresses contains result if {
some p in matching_policies
some k, egress in p.egress
some selectorset in egress.selectors
is_subset(input.remote.labels, selectorset)
e := replace_selectors(
object.union(
object.remove(egress, ["selectors"]),
{"matched_selectors": selectorset},
),
object.union(replacements, egress_replacements),
)
result := object.union(object.remove(p, ["egress", "selectors"]), {"egress": e})
}
allow := {"policies_with_egress": egresses, "policies": matching_policies_wo_egresses}