-
Notifications
You must be signed in to change notification settings - Fork 97
/
docker_registry.py
214 lines (170 loc) · 8.37 KB
/
docker_registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python3
#
# Copyright (c) 2018 SUSE LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# This is a very basic client for the Docker Registry V2 API.
# It exists for a single reason: All clients either:
# - Don't work
# - Don't support uploading
# - Don't support multi-arch images (manifest lists)
# and some even all three.
import hashlib
import json
import os
import urllib.parse
import requests
class DockerRegistryClient():
def __init__(self, url, username, password, repository):
self.url = url
self.username = username
self.password = password
self.repository = repository
self.scopes = [f"repository:{repository}:pull,push,delete"]
self.token = None
class DockerRegistryError(Exception):
"""Some nicer display of docker registry errors"""
def __init__(self, errors):
self.errors = errors
def __str__(self):
ret = "Docker Registry errors:"
for error in self.errors:
ret += "\n" + str(error)
return ret
def _updateToken(self, www_authenticate):
bearer_parts = www_authenticate[len("Bearer "):].split(",")
bearer_dict = {}
for part in bearer_parts:
assignment = part.split('=')
bearer_dict[assignment[0]] = assignment[1].strip('"')
scope_param = "&scope=".join([""] + [urllib.parse.quote(scope) for scope in self.scopes])
response = requests.get(f"{bearer_dict['realm']}?service={bearer_dict['service']}{scope_param}",
auth=(self.username, self.password))
self.token = response.json()['token']
def doHttpCall(self, method, url, **kwargs):
"""This method wraps the requested method from the requests module to
add the token for authorization."""
try_update_token = True
# Relative to the host
if url.startswith("/"):
url = self.url + url
if "headers" not in kwargs:
kwargs['headers'] = {}
while True:
resp = None
if self.token is not None:
kwargs['headers']['Authorization'] = "Bearer " + self.token
methods = {'POST': requests.post,
'GET': requests.get,
'HEAD': requests.head,
'PUT': requests.put,
'DELETE': requests.delete}
if method not in methods:
return False
resp = methods[method](url, **kwargs)
if resp.status_code == 401 or resp.status_code == 403:
if try_update_token:
try_update_token = False
self._updateToken(resp.headers['Www-Authenticate'])
continue
if resp.status_code > 400 and resp.status_code < 404:
try:
errors = resp.json()['errors']
raise self.DockerRegistryError(errors)
except ValueError:
pass
return resp
def uploadManifest(self, content, reference=None):
"""Upload a manifest. Data is given as bytes in content, the digest/tag in reference.
If reference is None, the digest is computed and used as reference.
On success, the used reference is returned. False otherwise."""
content_json = json.loads(content.decode('utf-8'))
if "mediaType" not in content_json:
raise Exception("Invalid manifest")
if reference is None:
alg = hashlib.sha256()
alg.update(content)
reference = "sha256:" + alg.hexdigest()
resp = self.doHttpCall("PUT", f"/v2/{self.repository}/manifests/{reference}",
headers={'Content-Type': content_json['mediaType']},
data=content)
if resp.status_code != 201:
return False
return reference
def uploadManifestFile(self, filename, reference=None):
"""Upload a manifest. If the filename doesn't equal the digest, it's computed.
If reference is None, the digest is used. You can use the manifest's tag
for example.
On success, the used reference is returned. False otherwise."""
with open(filename, "rb") as manifest:
content = manifest.read()
if reference is None:
basename = os.path.basename(filename)
if basename.startswith("sha256:"):
reference = basename
if reference is None:
raise Exception("No reference determined")
return self.uploadManifest(content, reference)
def getManifest(self, reference):
"""Get a (json-parsed) manifest with the given reference (digest or tag).
If the manifest does not exist, return None. For other errors, False."""
resp = self.doHttpCall("GET", f"/v2/{self.repository}/manifests/{reference}",
headers={'Accept': "application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.docker.distribution.manifest.v2+json"}) # noqa: E501
if resp.status_code == 404:
return None
if resp.status_code != 200:
return False
return resp.json()
def getManifestDigest(self, reference):
"""Return the digest of the manifest with the given reference.
If the manifest doesn't exist or the request fails, it returns False."""
resp = self.doHttpCall("HEAD", f"/v2/{self.repository}/manifests/{reference}",
headers={'Accept': "application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.docker.distribution.manifest.v2+json"}) # noqa: E501
if resp.status_code != 200:
return False
return resp.headers['Docker-Content-Digest']
def deleteManifest(self, digest):
"""Delete the manifest with the given reference."""
resp = self.doHttpCall("DELETE", f"/v2/{self.repository}/manifests/{digest}")
return resp.status_code == 202
def uploadBlob(self, filename, digest=None):
"""Upload the blob with the given filename and digest. If digest is None,
the basename has to equal the digest.
Returns True if blob already exists or upload succeeded."""
if digest is None:
digest = os.path.basename(filename)
if not digest.startswith("sha256:"):
raise Exception("Invalid digest")
# Check whether the blob already exists - don't upload it needlessly.
stat_request = self.doHttpCall("HEAD", f"/v2/{self.repository}/blobs/{digest}")
if stat_request.status_code == 200 or stat_request.status_code == 307:
return True
# For now we can do a single upload call with everything inlined
# (which also means completely in ram, but currently it's never > 50 MiB)
content = None
with open(filename, "rb") as blob:
content = blob.read()
# First request an upload "slot", we get an URL we can PUT to back
upload_request = self.doHttpCall("POST", f"/v2/{self.repository}/blobs/uploads/")
if upload_request.status_code == 202:
location = upload_request.headers['Location']
upload = self.doHttpCall("PUT", location + "&digest=" + digest,
data=content)
return upload.status_code == 201
return False