udica-0.2.7

New release https://github.com/containers/udica/releases/tag/v0.2.7

- Add support for containerd via "nerdctl inspect"
- Avoid duplicate rules for accessing mounts and devices

Fixes:
  https://github.com/containers/udica/issues/90
  https://github.com/containers/udica/issues/7
Related:
  https://github.com/containers/udica/issues/84
This commit is contained in:
Vit Mojzis 2022-06-22 14:03:02 +02:00
parent b78fa64042
commit 879af9f4cc
5 changed files with 8 additions and 192 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@
/v0.2.4.tar.gz /v0.2.4.tar.gz
/v0.2.5.tar.gz /v0.2.5.tar.gz
/v0.2.6.tar.gz /v0.2.6.tar.gz
/v0.2.7.tar.gz

View File

@ -1,133 +0,0 @@
From dd05dbe742384dd22f4a63889c56cb75e4e2f571 Mon Sep 17 00:00:00 2001
From: Vit Mojzis <vmojzis@redhat.com>
Date: Tue, 9 Nov 2021 18:04:39 +0100
Subject: [PATCH] Make sure each section of the inspect exists before accessing
Fixes: https://github.com/containers/udica/issues/105,
https://github.com/containers/udica/issues/103
Inspired by:
https://github.com/WellIDKRealy/udica/commit/0c56d98b8c58a8a4ceb89b04d700c834c13778fd
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
---
udica/parse.py | 62 ++++++++++++++++++++++++++++++++++++++------------
1 file changed, 48 insertions(+), 14 deletions(-)
diff --git a/udica/parse.py b/udica/parse.py
index 0797095..59b3dc5 100644
--- a/udica/parse.py
+++ b/udica/parse.py
@@ -29,6 +29,24 @@ ENGINE_DOCKER = "docker"
ENGINE_ALL = [ENGINE_PODMAN, ENGINE_CRIO, ENGINE_DOCKER]
+# Decorator for verifying that getting value from "data" won't
+# result in Key error or Type error
+# e.g. in data[0]["HostConfig"]["Devices"]
+# missing "HostConfig" key in data[0] produces KeyError and
+# data[0]["HostConfig"] == none produces TypeError
+def getter_decorator(function):
+ # Verify that each element in path exists and return the corresponding value,
+ # otherwise return [] -- can be safely processed by iterators
+ def wrapper(self, data, *args):
+ try:
+ value = function(self, data, *args)
+ return value if value else []
+ except (KeyError, TypeError):
+ return []
+
+ return wrapper
+
+
def json_is_podman_or_docker_format(json_rep):
"""Check if the inspected file is in a format from docker or podman.
@@ -91,19 +109,22 @@ class EngineHelper(abc.ABC):
def get_caps(self, data, opts):
if opts["Caps"]:
- if opts["Caps"] == "None":
+ if opts["Caps"] in ["None", "none"]:
return []
return opts["Caps"].split(",")
return []
class PodmanDockerHelper(EngineHelper):
+ @getter_decorator
def get_devices(self, data):
return data[0]["HostConfig"]["Devices"]
+ @getter_decorator
def get_mounts(self, data):
return data[0]["Mounts"]
+ @getter_decorator
def get_ports(self, data):
ports = []
for key, value in data[0]["NetworkSettings"]["Ports"].items():
@@ -120,8 +141,13 @@ class PodmanHelper(PodmanDockerHelper):
def __init__(self):
super().__init__(ENGINE_PODMAN)
+ @getter_decorator
def get_caps(self, data, opts):
- if not opts["Caps"]:
+ if opts["Caps"]:
+ return (
+ opts["Caps"].split(",") if opts["Caps"] not in ["None", "none"] else []
+ )
+ else:
return data[0]["EffectiveCaps"]
return []
@@ -138,18 +164,25 @@ class DockerHelper(PodmanDockerHelper):
def adjust_json_from_docker(self, json_rep):
"""If the json comes from a docker call, we need to adjust it to make use
of it."""
-
- if not isinstance(json_rep[0]["NetworkSettings"]["Ports"], dict):
- raise Exception(
- "Error parsing docker engine inspection JSON structure, try to specify container engine using '--container-engine' parameter"
- )
-
- for item in json_rep[0]["Mounts"]:
- item["source"] = item["Source"]
- if item["Mode"] == "rw":
- item["options"] = "rw"
- if item["Mode"] == "ro":
- item["options"] = "ro"
+ try:
+ if not isinstance(json_rep[0]["NetworkSettings"]["Ports"], dict):
+ raise Exception(
+ "Error parsing docker engine inspection JSON structure, try to specify container engine using '--container-engine' parameter"
+ )
+ except (KeyError, TypeError):
+ # "Ports" not specified in given json file
+ pass
+
+ try:
+ for item in json_rep[0]["Mounts"]:
+ item["source"] = item["Source"]
+ if item["Mode"] == "rw":
+ item["options"] = "rw"
+ if item["Mode"] == "ro":
+ item["options"] = "ro"
+ except (KeyError, TypeError):
+ # "Mounts" not specified in given json file
+ pass
class CrioHelper(EngineHelper):
@@ -161,6 +194,7 @@ class CrioHelper(EngineHelper):
# bind mounting device on the container
return []
+ @getter_decorator
def get_mounts(self, data):
return data["status"]["mounts"]
--
2.30.2

View File

@ -1,54 +0,0 @@
From 2e1f70537bf556a0f2238104e4fb3cf8518fcfc4 Mon Sep 17 00:00:00 2001
From: Vit Mojzis <vmojzis@redhat.com>
Date: Fri, 29 Apr 2022 16:15:06 +0200
Subject: [PATCH] Improve label collection for mounts and devices
Catch exception triggered by selabel_lookup when it encounters file
context definition containing "<<none>>"
Real label of given path may differ from what selable_lookup
(matchpathcon) returns. Udica should allow access to both.
Fixes:
https://github.com/containers/udica/issues/98
https://github.com/containers/udica/issues/109
---
udica/policy.py | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/udica/policy.py b/udica/policy.py
index 07d957c..1d53e2a 100644
--- a/udica/policy.py
+++ b/udica/policy.py
@@ -67,12 +67,23 @@ def list_contexts(directory):
contexts.append(semanage.semanage_context_get_type(context))
selabel = selinux.selabel_open(selinux.SELABEL_CTX_FILE, None, 0)
- (rc, context) = selinux.selabel_lookup(selabel, directory, 0)
- if context == None:
- if exists(directory) == False:
- exit(1)
+ try:
+ (rc, context) = selinux.selabel_lookup(selabel, directory, 0)
+ except FileNotFoundError:
+ # File context definition containing "<<none>>" triggers exception
+ context = None
+ if context:
+ contexts.append(context.split(":")[2])
+
+ # Get the real label (ls -lZ) - may differ from what selabel_lookup returns
+ try:
context = selinux.getfilecon(directory)[1]
- contexts.append(context.split(":")[2])
+ except FileNotFoundError:
+ context = None
+
+ if context:
+ contexts.append(context.split(":")[2])
+
return contexts
--
2.35.1

View File

@ -1 +1 @@
SHA512 (v0.2.6.tar.gz) = 29295c9d95ecb15aed7e226d92a0b838046cf607e98956c66a83ad0f4ddf58b255586c24d407216ae9ddf5b11f502ae2b3a6822208d8dc8141124e68dac19265 SHA512 (v0.2.7.tar.gz) = 03ff95f80317d09f980f89a7c415f0aeb6de965e64e9179969d2361d6a62ad243987e0355453a2c4ce4f4086834ea651644ccaa7eff1d82926000373040fa21d

View File

@ -1,10 +1,8 @@
Summary: A tool for generating SELinux security policies for containers Summary: A tool for generating SELinux security policies for containers
Name: udica Name: udica
Version: 0.2.6 Version: 0.2.7
Release: 5%{?dist} Release: 1%{?dist}
Source0: https://github.com/containers/udica/archive/v%{version}.tar.gz Source0: https://github.com/containers/udica/archive/v%{version}.tar.gz
Patch0001: 0001-Make-sure-each-section-of-the-inspect-exists-before-.patch
Patch0002: 0002-Improve-label-collection-for-mounts-and-devices.patch
License: GPLv3+ License: GPLv3+
BuildArch: noarch BuildArch: noarch
Url: https://github.com/containers/udica Url: https://github.com/containers/udica
@ -60,6 +58,10 @@ install -m 0644 udica/man/man8/udica.8 %{buildroot}%{_mandir}/man8/udica.8
%endif %endif
%changelog %changelog
* Wed Jun 22 2022 Vit Mojzis <vmojzis@redhat.com> - 0.2.7-1
- Add support for containerd via "nerdctl inspect"
- Avoid duplicate rules for accessing mounts and devices
* Mon Jun 13 2022 Python Maint <python-maint@redhat.com> - 0.2.6-5 * Mon Jun 13 2022 Python Maint <python-maint@redhat.com> - 0.2.6-5
- Rebuilt for Python 3.11 - Rebuilt for Python 3.11