zun  4.0.0
About: OpenStack Zun is a Container Management service for OpenStack.
The "Train" series (latest release).
  Fossies Dox: zun-4.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

digest.py
Go to the documentation of this file.
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 # implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 
14 from zun.common.docker_image import regexp
15 
16 DigestRegexps = regexp.DigestRegexps
17 
18 
19 class InvalidDigest(Exception):
20  @classmethod
21  def default(cls):
22  return cls("invalid digest")
23 
24 
26  @classmethod
27  def default(cls):
28  return cls("unsupported digest algorithm")
29 
30 
32  @classmethod
33  def default(cls):
34  return cls("invalid checksum digest length")
35 
36 
37 DIGESTS_SIZE = {
38  'sha256': 32,
39  'sha384': 48,
40  'sha512': 64,
41 }
42 
43 
44 def validate_digest(digest):
45  matched = DigestRegexps.DIGEST_REGEXP_ANCHORED.match(digest)
46  if not matched:
47  raise InvalidDigest.default()
48 
49  i = digest.find(':')
50  # case: "sha256:" with no hex.
51  if i < 0 or ((i + 1) == len(digest)):
52  raise InvalidDigest.default()
53 
54  algorithm = digest[:i]
55  if algorithm not in DIGESTS_SIZE:
56  raise DigestUnsupported.default()
57 
58  if DIGESTS_SIZE[algorithm] * 2 != len(digest[i + 1:]):
59  raise DigestInvalidLength.default()
zun.common.docker_image.regexp.DigestRegexps
Definition: regexp.py:91
zun.common.docker_image
Definition: __init__.py:1
zun.common.docker_image.digest.validate_digest
def validate_digest(digest)
Definition: digest.py:44
zun.common.docker_image.digest.DigestInvalidLength
Definition: digest.py:31
zun.common.docker_image.digest.InvalidDigest
Definition: digest.py:19
zun.common.docker_image.digest.DigestUnsupported
Definition: digest.py:25
zun.common.docker_image.digest.DigestInvalidLength.default
def default(cls)
Definition: digest.py:33
zun.common.docker_image.digest.InvalidDigest.default
def default(cls)
Definition: digest.py:21
zun.common.docker_image.digest.DigestUnsupported.default
def default(cls)
Definition: digest.py:27