-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_policies.py
184 lines (164 loc) · 5.09 KB
/
gen_policies.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
This script creates a yaml and bash file for a policy. The bash script is
to label existing pods with the policy labels.
"""
import json
import sys
import os
# This is a template which is used when no ingress and no egress is allowed
# from/to a pod.
base_policy = "\
apiVersion: cilium.io/v2\n\
kind: CiliumNetworkPolicy\n\
metadata:\n\
name: {}\n\
spec:\n\
endpointSelector:\n\
matchLabels:\n\
id.pod: {}\n\
ingress:\n\
- {{}}\n\
egress:\n\
- {{}}\n\
"
# This is a template which is used when only ingress is allowed from a pod.
ingress_only = "\
apiVersion: cilium.io/v2\n\
kind: CiliumNetworkPolicy\n\
metadata:\n\
name: {}\n\
spec:\n\
endpointSelector:\n\
matchLabels:\n\
id.pod: {}\n\
ingress:\n\
- fromEndpoints:\n\
- matchLabels:\n\
id.pod: {}\n\
egress:\n\
- {{}}\n\
"
# This is a template which is used when only egress is allowed to a pod.
egress_only = "\
apiVersion: cilium.io/v2\n\
kind: CiliumNetworkPolicy\n\
metadata:\n\
name: {}\n\
spec:\n\
endpointSelector:\n\
matchLabels:\n\
id.pod: {}\n\
ingress:\n\
- {{}}\n\
egress:\n\
- toEndpoints:\n\
- matchLabels:\n\
id.pod: {}\n\
"
# This is a template which is used when both ingress and egress is allowed
# from/to a pod.
egress_ingress = "\
apiVersion: cilium.io/v2\n\
kind: CiliumNetworkPolicy\n\
metadata:\n\
name: {}\n\
spec:\n\
endpointSelector:\n\
matchLabels:\n\
id.pod: {}\n\
ingress:\n\
- fromEndpoints:\n\
- matchLabels:\n\
id.pod: {}\n\
egress:\n\
- toEndpoints:\n\
- matchLabels:\n\
id.pod: {}\n\
"
def add_egress(dictio, vm, pod, to):
d = dictio[vm]
if (pod not in d) and (to is None):
d[pod] = ([], [])
if pod not in d:
d[pod] = ([], [to])
elif (to is not None):
l = d[pod][1]
if to not in l:
l.append(to)
def add_ingress(dictio, vm, pod, from_):
d = dictio[vm]
if pod not in d:
d[pod] = ([from_], [])
else:
l = d[pod][0]
if from_ not in l:
l.append(from_)
def write_delimiter(out, first):
if (not first):
out.write("---\n")
return False
def gen_policy(scen, output, prefix):
vms = scen["VMs"]
policy_data = {}
for vm in vms:
policy_data[vm] = {}
names = scen["VMpodnames"]
for c, vm in enumerate(vms):
vm_pols = scen["policies"][c]
if len(vm_pols) == 0:
continue
for pol in vm_pols:
if len(pol[1]) == 0:
name1 = names[c][pol[0]]
add_egress(policy_data, c, name1, None)
else:
ingress_vm = pol[1][0]
ingress_pods = pol[1][1]
for pod in ingress_pods:
name1 = names[c][pol[0]]
name2 = names[ingress_vm][pod]
add_egress(policy_data, vm, name1, (vms[ingress_vm], name2))
add_ingress(policy_data, vms[ingress_vm], name2, (vm, name1))
# Forbid communication if no ingress/egress rules defined
first = True
with open(output, 'a') as out:
for vm, data in policy_data.items():
for pod, (ingress, egress) in data.items():
if (not ingress) and (not egress):
first = write_delimiter(out, first)
out.write(base_policy.format(f"{prefix}-{pod}-isolated", f"{prefix}-{pod}"))
else:
for ingress_rule in ingress:
other_vm = ingress_rule[0]
other_pod = ingress_rule[1]
first = write_delimiter(out, first)
if ingress_rule in egress:
out.write(egress_ingress.format(f"{prefix}-{pod}-egress-ingress-{other_pod}", f"{prefix}-{pod}", f"{prefix}-{other_pod}", "id.pod", f"{prefix}-{other_vm}-{other_pod}"))
egress.remove(ingress_rule)
else:
out.write(ingress_only.format(f"{prefix}-{pod}-ingress-{other_pod}", f"{prefix}-{pod}", f"{prefix}-{other_pod}"))
for other_vm, other_pod in egress:
first = write_delimiter(out, first)
out.write(egress_only.format(f"{prefix}-{pod}-egress-{other_pod}", f"{prefix}-{pod}", f"{prefix}-{other_pod}"))
return policy_data
if __name__ == "__main__":
if len(sys.argv) < 5:
print("Usage: python3 gen_policies.py <gen id> <pods.json> <scenario.json> <output.yaml>")
else:
prefix = sys.argv[1]
podfile = sys.argv[2]
file = sys.argv[3]
output = sys.argv[4]
try:
os.remove(output)
except OSError:
pass
with open(podfile) as pf:
with open(file) as f:
scen = json.load(f)
tmp = json.load(pf)
for key, val in tmp.items():
scen[key] = val
policy_data = gen_policy(scen, output, prefix)
# gen_pod_labels(scen, outputsh, policy_data, prefix)
# gen_pods(scen, output, policy_data)