Skip to content

Commit

Permalink
Make sure each section of the inspect exists before accessing
Browse files Browse the repository at this point in the history
Fixes: containers#105,
       containers#103

Inspired by:
WellIDKRealy@0c56d98

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
  • Loading branch information
vmojzis committed Nov 9, 2021
1 parent 4265ea8 commit b00e403
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions udica/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
ENGINE_ALL = [ENGINE_PODMAN, ENGINE_CRIO, ENGINE_DOCKER]


# Decorator for verifying ghat 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.
Expand Down Expand Up @@ -91,19 +109,22 @@ def get_ports(self, data):

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():
Expand All @@ -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 []

Expand All @@ -138,18 +164,25 @@ def parse_inspect(self, data):
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):
Expand All @@ -161,6 +194,7 @@ def get_devices(self, data):
# bind mounting device on the container
return []

@getter_decorator
def get_mounts(self, data):
return data["status"]["mounts"]

Expand Down

0 comments on commit b00e403

Please sign in to comment.