forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainers_from_vn2.py
More file actions
265 lines (234 loc) · 10.4 KB
/
containers_from_vn2.py
File metadata and controls
265 lines (234 loc) · 10.4 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import base64
from dataclasses import asdict
from hashlib import sha256
import json
from pathlib import Path
import re
from typing import Optional
import yaml
from azext_confcom import config
from azext_confcom.lib.platform import (
PRIVILEDGED_CAPABILITIES,
VN2_PRIVILEGED_MOUNTS,
VN2_WORKLOAD_IDENTITY_ENV_RULES,
VN2_WORKLOAD_IDENTITY_MOUNTS,
)
from azext_confcom.lib.policy import ContainerUser
from azext_confcom.lib.containers import (
from_image as container_from_image,
merge_containers,
)
def find_vn2_containers(vn2_template):
for key, value in vn2_template.items():
if key in ("containers", "initContainers"):
yield from value
elif isinstance(value, dict):
result = find_vn2_containers(value)
if result is not None:
yield from result
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
result = find_vn2_containers(item)
if result is not None:
yield from result
def vn2_container_env_rules(template: dict, container: dict, template_variables: dict):
for env_var in container.get("env", []):
if "value" in env_var:
is_special = re.match('^===VIRTUALNODE2.CC.THIM.(.+)===$', env_var.get('value'))
yield {
"pattern": f"{env_var.get('name')}={'.*' if is_special else env_var.get('value')}",
"strategy": "re2" if is_special else "string",
"required": False,
}
elif "valueFrom" in env_var:
if "configMapKeyRef" in env_var.get('valueFrom') or "secretKeyRef" in env_var.get('valueFrom'):
var_ref = (
env_var.get('valueFrom').get("configMapKeyRef", None) or
env_var.get('valueFrom').get("secretKeyRef", None)
)
yield {
"pattern": f"{env_var.get('name')}={template_variables[var_ref.get('name')][var_ref.get('key')]}",
"strategy": "string",
"required": False,
}
elif "fieldRef" in env_var.get('valueFrom'):
# Existing behaviour is to wildcard this, there is a correct implementation below
yield {
"pattern": f"{env_var.get('name')}=.*",
"strategy": "re2",
"required": False,
}
# value = template
# for part in env_var.get('valueFrom').get("fieldRef", {}).get("fieldPath", "").split("."):
# value = value.get(part, {})
# yield {
# "pattern": f"{env_var.get('name')}={value}",
# "strategy": "string",
# "required": False,
# })
elif "resourceFieldRef" in env_var.get('valueFrom'):
ref = env_var.get('valueFrom').get("resourceFieldRef", {})
ref_container_name = ref.get("containerName") or container.get("name")
ref_container = next(
(
c for c in template["spec"]["containers"]
if c.get("name") == ref_container_name
),
None,
)
if ref_container is None:
continue
value = ref_container.get("resources", {})
for part in ref["resource"].split("."):
value = value.get(part, {})
yield {
"pattern": f"{env_var.get('name')}={value}",
"strategy": "string",
"required": False,
}
def vn2_container_mounts(template: dict, container: dict) -> list[dict]:
volume_claim_access = {
v["metadata"]["name"]: v.get("spec", {}).get("accessModes", [])
for v in template.get("spec", {}).get("volumeClaimTemplates", [])
}
volume_defs = {
v["name"]: [k for k in v.keys() if k != "name"][0]
for v in template.get("spec", {}).get("volumes", [])
}
return [
{
"destination": m.get("mountPath"),
"options": [
"rbind",
"rshared",
"ro" if (
m.get("readOnly") or
"ReadOnlyMany" in volume_claim_access.get(m.get("name"), []) or
volume_defs.get(m.get("name")) in {"configMap", "secret", "downwardAPI", "projected"}
) else "rw"
],
"source": "sandbox:///tmp/atlas/emptydir/.+",
"type": "bind",
}
for m in container.get("volumeMounts", [])
]
def containers_from_vn2(
template: str,
container_name: Optional[str] = None
) -> str:
with Path(template).open("r") as f:
template_yaml = list(yaml.safe_load_all(f))
# Find containers matching the specified name (if provided)
template_containers = []
variables = {}
for doc in template_yaml:
if not isinstance(doc, dict):
continue
kind = doc.get("kind")
if kind == "ConfigMap":
variables[doc["metadata"]["name"]] = {
**doc.get("data", {}),
**{k: base64.b64decode(v).decode("utf-8") for k, v in doc.get("binaryData", {}).items()},
}
elif kind == "Secret":
variables[doc["metadata"]["name"]] = {
**{k: base64.b64decode(v).decode("utf-8") for k, v in doc.get("data", {}).items()},
**doc.get("stringData", {}),
}
elif kind in ["Pod", "Deployment", "StatefulSet", "DaemonSet", "Job", "CronJob", "ReplicaSet"]:
for container in find_vn2_containers(doc):
if container_name and container.get("name") != container_name:
continue
template_containers.append((container, doc))
if container_name:
if not template_containers:
raise AssertionError(f"No containers with name {container_name} found.")
if len(template_containers) > 1:
raise AssertionError(
f"Multiple containers with name {container_name} found."
)
elif not template_containers:
raise AssertionError("No containers found.")
container_defs = []
for template_container, template_doc in template_containers:
image_container_def = container_from_image(template_container.get("image"), platform="vn2")
template_container_def = {
"name": template_container.get("name"),
"command": template_container.get("command", []) + template_container.get("args", []),
"env_rules": (
[
{
"pattern": rule.get("pattern") or f"{rule.get('name')}={rule.get('value')}",
"strategy": rule.get("strategy", "string"),
"required": rule.get("required", False),
}
for rule in (
config.OPENGCS_ENV_RULES
+ config.FABRIC_ENV_RULES
+ config.MANAGED_IDENTITY_ENV_RULES
+ config.ENABLE_RESTART_ENV_RULE
+ config.VIRTUAL_NODE_ENV_RULES
)
]
+ list(vn2_container_env_rules(template_doc, template_container, variables))
),
"mounts": vn2_container_mounts(template_doc, template_container),
}
# Parse security context
security_context = (
template_doc.get("spec", {}).get("securityContext", {})
| template_container.get("securityContext", {})
)
if security_context.get("privileged", False):
template_container_def["allow_elevated"] = True
template_container_def["mounts"] += VN2_PRIVILEGED_MOUNTS
template_container_def["capabilities"] = PRIVILEDGED_CAPABILITIES
if security_context.get("runAsUser") or security_context.get("runAsGroup"):
template_container_def["user"] = asdict(ContainerUser())
if security_context.get("runAsUser"):
template_container_def["user"]["user_idname"] = {
"pattern": str(security_context.get("runAsUser")),
"strategy": "id",
}
if security_context.get("runAsGroup"):
template_container_def["user"]["group_idnames"] = [{
"pattern": str(security_context.get("runAsGroup")),
"strategy": "id",
}]
if security_context.get("seccompProfile"):
template_container_def["seccomp_profile_sha256"] = sha256(
base64.b64decode(security_context.get("seccompProfile"))
).hexdigest()
if security_context.get("allowPrivilegeEscalation") is False:
template_container_def["no_new_privileges"] = True
# Check for workload identity
labels = template_doc.get("metadata", {}).get("labels", {}) or {}
if labels.get("azure.workload.identity/use", "false") == "true":
template_container_def["env_rules"].extend(VN2_WORKLOAD_IDENTITY_ENV_RULES)
template_container_def["mounts"].extend(VN2_WORKLOAD_IDENTITY_MOUNTS)
exec_processes = [
{
"command": process.get("exec", {}).get("command", []),
"signals": []
}
for process in [
template_container.get("livenessProbe"),
template_container.get("readinessProbe"),
template_container.get("startupProbe"),
template_container.get("lifecycle", {}).get("postStart"),
template_container.get("lifecycle", {}).get("preStop"),
]
if process is not None
]
if exec_processes:
template_container_def["exec_processes"] = exec_processes
container_defs.append(merge_containers(
image_container_def,
template_container_def,
))
return json.dumps(container_defs)