Debian Codegraph @

ansible_collections/community/okd/plugins/module_utils/openshift_docker_image.py

raw
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, division, print_function
4
5 __metaclass__ = type
6
7 import re
8
9
10 def convert_storage_to_bytes(value):
11 keys = {
12 "Ki": 1024,
13 "Mi": 1024 * 1024,
14 "Gi": 1024 * 1024 * 1024,
15 "Ti": 1024 * 1024 * 1024 * 1024,
16 "Pi": 1024 * 1024 * 1024 * 1024 * 1024,
17 "Ei": 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
18 }
19 for k in keys:
20 if value.endswith(k) or value.endswith(k[0]):
21 idx = value.find(k[0])
22 return keys.get(k) * int(value[:idx])
23 return int(value)
24
25
26 def is_valid_digest(digest):
27 digest_algorithm_size = dict(
28 sha256=64,
29 sha384=96,
30 sha512=128,
31 )
32
33 m = re.match(r"[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+", digest)
34 if not m:
35 return "Docker digest does not match expected format %s" % digest
36
37 idx = digest.find(":")
38 # case: "sha256:" with no hex.
39 if idx < 0 or idx == (len(digest) - 1):
40 return "Invalid docker digest %s, no hex value define" % digest
41
42 algorithm = digest[:idx]
43 if algorithm not in digest_algorithm_size:
44 return "Unsupported digest algorithm value %s for digest %s" % (
45 algorithm,
46 digest,
47 )
48
49 hex_value = digest[idx + 1:] # fmt: skip
50 if len(hex_value) != digest_algorithm_size.get(algorithm):
51 return "Invalid length for digest hex expected %d found %d (digest is %s)" % (
52 digest_algorithm_size.get(algorithm),
53 len(hex_value),
54 digest,
55 )
56
57
58 def parse_docker_image_ref(image_ref, module=None):
59 """
60 Docker Grammar Reference
61 Reference => name [ ":" tag ] [ "@" digest ]
62 name => [hostname '/'] component ['/' component]*
63 hostname => hostcomponent ['.' hostcomponent]* [':' port-number]
64 hostcomponent => /([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])/
65 port-number => /[0-9]+/
66 component => alpha-numeric [separator alpha-numeric]*
67 alpha-numeric => /[a-z0-9]+/
68 separator => /[_.]|__|[-]*/
69 """
70 idx = image_ref.find("/")
71
72 def _contains_any(src, values):
73 return any(x in src for x in values)
74
75 result = {"tag": None, "digest": None}
76 default_domain = "docker.io"
77 if idx < 0 or (
78 not _contains_any(image_ref[:idx], ":.") and image_ref[:idx] != "localhost"
79 ):
80 result["hostname"], remainder = default_domain, image_ref
81 else:
82 result["hostname"], remainder = image_ref[:idx], image_ref[idx + 1:] # fmt: skip
83
84 # Parse remainder information
85 idx = remainder.find("@")
86 if idx > 0 and len(remainder) > (idx + 1):
87 # docker image reference with digest
88 component, result["digest"] = remainder[:idx], remainder[idx + 1:] # fmt: skip
89 err = is_valid_digest(result["digest"])
90 if err:
91 if module:
92 module.fail_json(msg=err)
93 return None, err
94 else:
95 idx = remainder.find(":")
96 if idx > 0 and len(remainder) > (idx + 1):
97 # docker image reference with tag
98 component, result["tag"] = remainder[:idx], remainder[idx + 1:] # fmt: skip
99 else:
100 # name only
101 component = remainder
102 v = component.split("/")
103 namespace = None
104 if len(v) > 1:
105 namespace = v[0]
106 result.update({"namespace": namespace, "name": v[-1]})
107
108 return result, None