Skip to content

Commit ef966d3

Browse files
committed
Clean up after adding LDX support
- Make write_policy_for_containerd_mounts more robust and remove write_policy_for_lxd_mounts since it is the same - Fix formatting based on "black" Signed-off-by: Vit Mojzis <[email protected]>
1 parent 68426e4 commit ef966d3

File tree

2 files changed

+11
-117
lines changed

2 files changed

+11
-117
lines changed

udica/parse.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ def json_is_containerd_format(json_rep):
6464

6565
def json_is_podman_format(json_rep):
6666
"""Check if the inspected file is in a format from podman."""
67-
return (
68-
isinstance(json_rep, list)
69-
and (
70-
"container=oci" in json_rep[0]["Config"]["Env"]
71-
or "container=podman" in json_rep[0]["Config"]["Env"]
72-
)
67+
return isinstance(json_rep, list) and (
68+
"container=oci" in json_rep[0]["Config"]["Env"]
69+
or "container=podman" in json_rep[0]["Config"]["Env"]
7370
)
7471

72+
7573
def json_is_lxd_format(json_rep):
7674
"""Check if the inspected file is in a format from LXD."""
7775
return (
@@ -85,7 +83,7 @@ def json_is_lxd_format(json_rep):
8583

8684
def get_engine_helper(data, ContainerEngine):
8785
engine = validate_container_engine(ContainerEngine)
88-
86+
8987
if engine == "-":
9088
json_rep = json.loads(data)
9189

@@ -273,6 +271,7 @@ def get_caps(self, data, opts):
273271
return opts["Caps"].split(",")
274272
return data[0]["Spec"]["process"]["capabilities"]["effective"]
275273

274+
276275
class LxdHelper(EngineHelper):
277276
def __init__(self):
278277
super().__init__(ENGINE_LXD)
@@ -311,7 +310,7 @@ def get_ports(self, data):
311310
if device["type"] == "proxy":
312311
port_info = {
313312
"portNumber": int(device["listen"].split(":")[-1]),
314-
"protocol": device["connect"].split(":")[0]
313+
"protocol": device["connect"].split(":")[0],
315314
}
316315
ports.append(port_info)
317316
return ports

udica/policy.py

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ def create_policy(
179179
# mounts
180180
if inspect_format == "CRI-O":
181181
write_policy_for_crio_mounts(mounts, policy)
182-
elif inspect_format == "containerd":
182+
elif inspect_format in ["containerd", "LXD"]:
183183
write_policy_for_containerd_mounts(mounts, policy)
184-
elif inspect_format == "LXD":
185-
write_policy_for_lxd_mounts(mounts, policy)
186184
else:
187185
write_policy_for_podman_mounts(mounts, policy)
188186

@@ -465,111 +463,6 @@ def write_policy_for_containerd_mounts(mounts, policy):
465463
# "nodev"
466464
# ]
467465
# }
468-
for item in sorted(mounts, key=lambda x: str(x["source"])):
469-
if not item["source"].find("/"):
470-
if item["source"] == LOG_CONTAINER and "ro" in item["options"]:
471-
policy.write(" (blockinherit log_container)\n")
472-
add_template("log_container")
473-
continue
474-
475-
if item["source"] == LOG_CONTAINER and "ro" not in item["options"]:
476-
policy.write(" (blockinherit log_rw_container)\n")
477-
add_template("log_container")
478-
continue
479-
480-
if item["source"] == HOME_CONTAINER and "ro" in item["options"]:
481-
policy.write(" (blockinherit home_container)\n")
482-
add_template("home_container")
483-
continue
484-
485-
if item["source"] == HOME_CONTAINER and "ro" not in item["options"]:
486-
policy.write(" (blockinherit home_rw_container)\n")
487-
add_template("home_container")
488-
continue
489-
490-
if item["source"] == TMP_CONTAINER and "ro" in item["options"]:
491-
policy.write(" (blockinherit tmp_container)\n")
492-
add_template("tmp_container")
493-
continue
494-
495-
if item["source"] == TMP_CONTAINER and "ro" not in item["options"]:
496-
policy.write(" (blockinherit tmp_rw_container)\n")
497-
add_template("tmp_container")
498-
continue
499-
500-
if item["source"] == CONFIG_CONTAINER and "ro" in item["options"]:
501-
policy.write(" (blockinherit config_container)\n")
502-
add_template("config_container")
503-
continue
504-
505-
if item["source"] == CONFIG_CONTAINER and "ro" not in item["options"]:
506-
policy.write(" (blockinherit config_rw_container)\n")
507-
add_template("config_container")
508-
continue
509-
510-
contexts = list_contexts(item["source"])
511-
for context in contexts:
512-
if "ro" not in item["options"]:
513-
policy.write(
514-
" (allow process "
515-
+ context
516-
+ " ( dir ( "
517-
+ perms.perm["dir_rw"]
518-
+ " ))) \n"
519-
)
520-
policy.write(
521-
" (allow process "
522-
+ context
523-
+ " ( file ( "
524-
+ perms.perm["file_rw"]
525-
+ " ))) \n"
526-
)
527-
policy.write(
528-
" (allow process "
529-
+ context
530-
+ " ( fifo_file ( "
531-
+ perms.perm["fifo_rw"]
532-
+ " ))) \n"
533-
)
534-
policy.write(
535-
" (allow process "
536-
+ context
537-
+ " ( sock_file ( "
538-
+ perms.perm["socket_rw"]
539-
+ " ))) \n"
540-
)
541-
if "ro" in item["options"]:
542-
policy.write(
543-
" (allow process "
544-
+ context
545-
+ " ( dir ( "
546-
+ perms.perm["dir_ro"]
547-
+ " ))) \n"
548-
)
549-
policy.write(
550-
" (allow process "
551-
+ context
552-
+ " ( file ( "
553-
+ perms.perm["file_ro"]
554-
+ " ))) \n"
555-
)
556-
policy.write(
557-
" (allow process "
558-
+ context
559-
+ " ( fifo_file ( "
560-
+ perms.perm["fifo_ro"]
561-
+ " ))) \n"
562-
)
563-
policy.write(
564-
" (allow process "
565-
+ context
566-
+ " ( sock_file ( "
567-
+ perms.perm["socket_ro"]
568-
+ " ))) \n"
569-
)
570-
571-
572-
def write_policy_for_lxd_mounts(mounts, policy):
573466
for item in sorted(mounts, key=lambda x: str(x["source"])):
574467
if not item["source"].find("/"):
575468
if item["source"] == LOG_CONTAINER and "ro" in item.get("options", []):
@@ -607,7 +500,9 @@ def write_policy_for_lxd_mounts(mounts, policy):
607500
add_template("config_container")
608501
continue
609502

610-
if item["source"] == CONFIG_CONTAINER and "ro" not in item.get("options", []):
503+
if item["source"] == CONFIG_CONTAINER and "ro" not in item.get(
504+
"options", []
505+
):
611506
policy.write(" (blockinherit config_rw_container)\n")
612507
add_template("config_container")
613508
continue

0 commit comments

Comments
 (0)