This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
image_checker.py
288 lines (248 loc) · 11.9 KB
/
image_checker.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# 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.
import argparse
import base64
import copy
import http
import logging
import os
import re
import sys
import requests
import yaml
#pylint: disable=wrong-import-position
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
from common.exceptions import ImageAuthenticationError, ImageCheckError, ImageNameError, UnknownError
import common.utils as utils
LOGGER = logging.getLogger(__name__)
# The workflow, refer to: https://docs.docker.com/registry/spec/auth/token/
# 1. send registry v2 request, if registry doesn't support v2 api, ignore image check
# 2. try to call v2 api to get image manifest. If return 401, do following steps
# 3. use WWW-Authenticate header returned from previous request to generate auth info
# 4. use generated auth info to get token
# 5. try to get image manifest with returned token. If succeed, the image is found in registry
BEARER_AUTH = "Bearer"
BASIC_AUTH = "Basic"
DEFAULT_REGISTRY = "https://index.docker.io/v2/"
def _get_registry_uri(uri) -> str:
ret_uri = uri.strip().rstrip("/")
if not ret_uri.startswith("http") and not ret_uri.startswith("https"):
ret_uri = "https://{}".format(ret_uri)
chunks = ret_uri.split('/')
api_version_str = chunks[-1]
if api_version_str in ("v1", "v2"):
ret_uri = "/".join(chunks[:-1])
ret_uri = ret_uri.rstrip("/") + "/v2/"
return ret_uri
# Parse the challenge field, refer to: https://tools.ietf.org/html/rfc6750#section-3
def _parse_auth_challenge(challenge) -> dict:
if not challenge.strip().startswith((BASIC_AUTH, BEARER_AUTH)):
LOGGER.info("Challenge not supported, ignore this")
return {}
auth_type = BASIC_AUTH if challenge.strip().startswith(
BASIC_AUTH) else BEARER_AUTH
challenge_dict = {auth_type: {}}
chunks = challenge.strip()[len(auth_type):].split(",")
for chunk in chunks:
pair = chunk.strip().split("=")
challenge_dict[auth_type][pair[0]] = pair[1].strip("\"")
return challenge_dict
class ImageChecker(): #pylint: disable=too-few-public-methods
"""
Class used to precheck docker image.
Notice: the image checker only works for docker registry which support v2 API and
enables https. For registry using v1 API or doesn't enable https. This check will passed,
and wrong image name may cause task hang.
Image checker will try to check image with best effort. If registry return unexpected
code such as 5xx/429, image checker will abort. We only failed the image checker when we make
sure the image is not exist or authentication failed.
"""
def __init__(self, job_config, secret):
prerequisites = job_config["prerequisites"]
task_role_name = os.getenv("PAI_CURRENT_TASK_ROLE_NAME")
task_role = job_config["taskRoles"][task_role_name]
docker_image_name = task_role["dockerImage"]
docker_images = list(
filter(lambda pre: pre["name"] == docker_image_name,
prerequisites))
assert len(docker_images) == 1
image_info = docker_images[0]
self._image_uri = image_info["uri"]
self._registry_uri = self._get_registry_from_image_uri(
image_info["uri"])
self._basic_auth_headers = {}
self._bearer_auth_headers = {}
self._registry_auth_type = BASIC_AUTH
self._timeout = 10
if "auth" in image_info and secret:
auth = image_info["auth"]
self._init_auth_info(auth, secret)
def _get_registry_from_image_uri(self, image_uri) -> str:
if self._is_default_domain_used():
return DEFAULT_REGISTRY
index = self._image_uri.find("/")
return _get_registry_uri(image_uri[:index])
def _init_auth_info(self, auth, secret) -> None:
if "registryuri" in auth:
registry_uri = _get_registry_uri(auth["registryuri"])
if self._is_default_domain_used(
) and registry_uri != DEFAULT_REGISTRY:
LOGGER.info(
"Using default registry for image %s, ignore auth info",
self._image_uri)
return
username = utils.render_string_with_secrets(
auth["username"], secret) if "username" in auth else ""
password = utils.render_string_with_secrets(
auth["password"], secret) if "password" in auth else ""
# Only set auth info if username/password present
if username and password:
basic_auth_token = base64.b64encode(
bytes("{}:{}".format(username, password), "utf8")).decode()
self._basic_auth_headers["Authorization"] = "{} {}".format(
BASIC_AUTH, basic_auth_token)
self._registry_uri = registry_uri
# Refer: https://github.com/docker/distribution/blob/a8371794149d1d95f1e846744b05c87f2f825e5a/reference/normalize.go#L91
def _is_default_domain_used(self) -> bool:
index = self._image_uri.find("/")
return index == -1 or all(ch not in [".", ":"]
for ch in self._image_uri[:index])
def _get_and_set_token(self, challenge) -> None:
if not challenge or BEARER_AUTH not in challenge:
LOGGER.info("Not using bearer token, use basic auth")
return
if "realm" not in challenge[BEARER_AUTH]:
LOGGER.warning("realm not in challenge, use basic auth")
return
url = challenge[BEARER_AUTH]["realm"]
parameters = copy.deepcopy(challenge[BEARER_AUTH])
del parameters["realm"]
resp = requests.get(url,
headers=self._basic_auth_headers,
params=parameters, timeout=self._timeout)
if resp.status_code == http.HTTPStatus.UNAUTHORIZED:
raise ImageAuthenticationError("Failed to get auth token")
if not resp.ok:
raise UnknownError("Unknown failure with resp code {}".format(
resp.status_code))
body = resp.json()
self._bearer_auth_headers["Authorization"] = "{} {}".format(
BEARER_AUTH, body["access_token"])
self._registry_auth_type = BEARER_AUTH
def _is_registry_v2_supportted(self) -> bool:
try:
resp = requests.head(self._registry_uri, timeout=self._timeout)
if resp.ok or resp.status_code == http.HTTPStatus.UNAUTHORIZED:
return True
return False
except (TimeoutError, ConnectionError):
return False
def _login_v2_registry(self, attempt_url) -> None:
if not self._is_registry_v2_supportted():
LOGGER.warning(
"Registry %s may not support v2 api, ignore image check",
self._registry_uri)
raise UnknownError("Failed to check registry v2 support")
resp = requests.head(attempt_url, timeout=self._timeout)
if resp.ok:
return
headers = resp.headers
if resp.status_code == http.HTTPStatus.UNAUTHORIZED and "Www-Authenticate" in headers:
challenge = _parse_auth_challenge(headers["Www-Authenticate"])
self._get_and_set_token(challenge)
return
if resp.status_code == http.HTTPStatus.UNAUTHORIZED:
raise ImageAuthenticationError("Failed to login registry")
LOGGER.error(
"Failed to login registry or get auth url, resp code is %d",
resp.status_code)
raise UnknownError("Unknown status when trying to login registry")
def _get_normalized_image_info(self) -> dict:
uri = self._image_uri
use_default_domain = self._is_default_domain_used()
if not use_default_domain:
assert "/" in self._image_uri
index = self._image_uri.find("/")
uri = self._image_uri[index + 1:]
uri_chunks = uri.split(":")
tag = "latest" if len(uri_chunks) == 1 else uri_chunks[1]
repository = uri_chunks[0]
if not re.fullmatch(r"[a-z\-_.0-9]+[\/a-z\-_.0-9]*",
repository) or not re.fullmatch(
r"[a-z\-_.0-9]+", tag):
raise ImageNameError("image uri {} is invalid".format(
self._image_uri))
repo_chunks = uri_chunks[0].split("/")
if len(repo_chunks) == 1 and use_default_domain:
return {"repo": "library/{}".format(repository), "tag": tag}
return {"repo": repository, "tag": tag}
@utils.enable_request_debug_log
def is_docker_image_accessible(self):
try:
image_info = self._get_normalized_image_info()
except ImageNameError:
LOGGER.error("docker image uri: %s is invalid",
self._image_uri,
exc_info=True)
return False
url = "{}{repo}/manifests/{tag}".format(self._registry_uri,
**image_info)
try:
self._login_v2_registry(url)
except ImageCheckError:
LOGGER.error("Login failed, username or password is incorrect",
exc_info=True)
return False
if self._registry_auth_type == BEARER_AUTH:
resp = requests.head(url, headers=self._bearer_auth_headers, timeout=self._timeout)
else:
resp = requests.head(url, headers=self._basic_auth_headers, timeout=self._timeout)
if resp.ok:
LOGGER.info("image %s found in registry", self._image_uri)
return True
if resp.status_code == http.HTTPStatus.NOT_FOUND or resp.status_code == http.HTTPStatus.UNAUTHORIZED:
LOGGER.error(
"image %s not found or user unauthorized, registry is %s, resp code is %d",
self._image_uri, self._registry_uri, resp.status_code)
return False
LOGGER.warning("resp with code %d, ignore image check",
resp.status_code)
raise UnknownError("Unknown response from registry")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("job_config", help="job config yaml")
parser.add_argument("secret_file", help="secret file path")
args = parser.parse_args()
LOGGER.info("get job config from %s", args.job_config)
with open(args.job_config) as config:
job_config = yaml.safe_load(config)
if not os.path.isfile(args.secret_file):
job_secret = None
else:
with open(args.secret_file) as f:
job_secret = yaml.safe_load(f.read())
LOGGER.info("Start checking docker image")
image_checker = ImageChecker(job_config, job_secret)
try:
if not image_checker.is_docker_image_accessible():
sys.exit(1)
except Exception: #pylint: disable=broad-except
LOGGER.warning("Failed to check image", exc_info=True)
if __name__ == "__main__":
utils.init_logger()
main()