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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
import getpass
import hashlib
import io
import os
import re
import types
import urllib
import urllib.parse
import charliecloud as ch
## Hairy imports ##
# Requests is not bundled, so this noise makes the file parse and
# --version/--help work even if it’s not installed.
try:
import requests
import requests.auth
import requests.exceptions
except ImportError:
ch.depfails.append(("missing", 'Python module "requests"'))
# Mock up a requests.auth module so the rest of the file parses.
requests = types.ModuleType("requests")
requests.auth = types.ModuleType("requests.auth")
requests.auth.AuthBase = object
## Constants ##
# Content types for some stuff we care about.
# See: https://github.com/opencontainers/image-spec/blob/main/media-types.md
TYPES_MANIFEST = \
{"docker2": "application/vnd.docker.distribution.manifest.v2+json",
"oci1": "application/vnd.oci.image.manifest.v1+json"}
TYPES_INDEX = \
{"docker2": "application/vnd.docker.distribution.manifest.list.v2+json",
"oci1": "application/vnd.oci.image.index.v1+json"}
TYPE_CONFIG = "application/vnd.docker.container.image.v1+json"
TYPE_LAYER = "application/vnd.docker.image.rootfs.diff.tar.gzip"
## Globals ##
# Verify TLS certificates? Passed to requests.
tls_verify = True
# True if we talk to registries authenticated; false if anonymously.
auth_p = False
## Classes ##
class Auth(requests.auth.AuthBase):
# Every registry request has an “authorization object”. This starts as no
# authentication at all. If we get HTTP 401 Unauthorized, we try to
# “escalate” to a higher level of authorization; some classes have multiple
# escalators that we try in order. Escalation can fail either if
# authentication fails or there is nothing to escalate to.
#
# Class attributes:
#
# anon_p ...... True if the authorization object is anonymous; False if
# it needed authentication.
#
# escalators .. Sequence of classes we can escalate to. Empty if no
# escalation possible. This is actually a property rather
# than a class attribute because it needs to refer to
# classes that may not have been defined when the module is
# created, e.g. classes later in the file, or some can
# escalate to themselves.
#
# auth_p ...... True if appropriate for authenticated mode, False if
# anonymous (i.e., --auth or not, respectively). Everything
# must be one or the other.
#
# scheme ...... Auth scheme string (from WWW-Authenticate header) that
# this class matches.
__slots__ = ("auth_h_next",) # WWW-Authenticate header for next escalator
@classmethod
def authenticate(class_, creds, auth_d):
"""Authenticate using the given credentials and parsed WWW-Authenticate
dictionary. Return a new Auth object if successful, None if
not. The caller is responsible for dealing with the failure."""
...
def __eq__(self, other):
return (type(self) == type(other))
@property
def escalators(self):
...
def escalate(self, reg, res):
"""Escalate to a higher level of authorization. Use the WWW-Authenticate
header in failed response res if there is one."""
ch.VERBOSE("escalating from %s" % self)
assert (res.status_code == 401)
# Get authentication instructions.
if ("WWW-Authenticate" in res.headers):
auth_h = res.headers["WWW-Authenticate"]
elif (self.auth_h_next is not None):
auth_h = self.auth_h_next
else:
ch.FATAL("don’t know how to authenticate: WWW-Authenticate not found")
# We use two “undocumented (although very stable and frequently cited)”
# methods to parse the authentication response header (thanks Andy,
# i.e., @adrecord on GitHub).
(auth_scheme, auth_d) = auth_h.split(maxsplit=1)
auth_d = urllib.request.parse_keqv_list(
urllib.request.parse_http_list(auth_d))
ch.VERBOSE("WWW-Authenticate parsed: %s %s" % (auth_scheme, auth_d))
# Is escalation possible in principle?
if (len(self.escalators) == 0):
ch.FATAL("no further authentication possible, giving up")
# Try to escalate.
for class_ in self.escalators:
if (class_.scheme == auth_scheme):
if (class_.auth_p != auth_p):
ch.VERBOSE("skipping %s: auth mode mismatch" % class_.__name__)
else:
ch.VERBOSE("authenticating using %s" % class_.__name__)
auth = class_.authenticate(reg, auth_d)
if (auth is None):
ch.VERBOSE("authentication failed; trying next")
elif (auth == self):
ch.VERBOSE("authentication did not escalate; trying next")
else:
return auth # success!
ch.VERBOSE("no authentication left to try")
return None
class Auth_Basic(Auth):
anon_p = False
scheme = "Basic"
auth_p = True
__slots__ = ("basic")
@classmethod
def authenticate(class_, reg, auth_d):
# Note: Basic does not validate the credentials until we try to use it.
if ("realm" not in auth_d):
ch.FATAL("WWW-Authenticate missing realm")
(username, password) = reg.creds.get()
i = class_()
i.basic = requests.auth.HTTPBasicAuth(username, password)
return i
def __call__(self, *args, **kwargs):
return self.basic(*args, **kwargs)
def __eq__(self, other):
return super().__eq__(other) and (self.basic == other.basic)
def __str__(self):
return self.basic.__str__()
@property
def escalators(self):
return ()
class Auth_Bearer_IDed(Auth):
# https://stackoverflow.com/a/58055668
anon_p = False
scheme = "Bearer"
auth_p = True
variant = "IDed"
__slots__ = ("auth_d",
"token")
def __init__(self, token, auth_d):
self.token = token
self.auth_d = auth_d
@classmethod
def authenticate(class_, reg, auth_d):
# Registries and endpoints vary in what they put in WWW-Authenticate. We
# need realm because it’s the URL to use for a token. Otherwise, just
# give back all the keys we got.
for k in ("realm",):
if (k not in auth_d):
ch.FATAL("WWW-Authenticate missing key: %s" % k)
params = { (k,v) for (k,v) in auth_d.items() if k != "realm" }
# Request a Bearer token.
res = reg.request_raw("GET", auth_d["realm"], {200,401,403},
auth=class_.token_auth(reg.creds), params=params)
if (res.status_code != 200):
ch.VERBOSE("bearer token request rejected")
return None
# Create new instance.
i = class_(res.json()["token"], auth_d)
ch.VERBOSE("received bearer token: %s" % (i.token_short))
return i
@classmethod
def token_auth(class_, creds):
"""Return a requests.auth.AuthBase object used to authenticate the token
request."""
(username, password) = creds.get()
return requests.auth.HTTPBasicAuth(username, password)
def __call__(self, req):
req.headers["Authorization"] = "Bearer %s" % self.token
return req
def __eq__(self, other):
return super().__eq__(other) and (self.auth_d == other.auth_d)
def __str__(self):
return ("Bearer (%s) %s" % (self.__class__.__name__.split("_")[-1],
self.token_short))
@property
def escalators(self):
# One can escalate to an authenticated Bearer with a greater scope. I’m
# pretty sure this doesn’t create an infinite loop because eventually
# the token request will fail.
return (Auth_Bearer_IDed,)
@property
def token_short(self):
return ("%s..%s" % (self.token[:8], self.token[-8:]))
class Auth_Bearer_Anon(Auth_Bearer_IDed):
anon_p = True
scheme = "Bearer"
auth_p = False
__slots__ = ()
@classmethod
def token_auth(class_, creds):
# The way to get an anonymous Bearer token is to give no Basic auth
# header in the token request.
return None
@property
def escalators(self):
return (Auth_Bearer_IDed,)
class Auth_None(Auth):
anon_p = True
scheme = None
#auth_p = # not meaningful b/c we start here in both modes
def __call__(self, req):
return req
def __str__(self):
return "no authorization"
@property
def escalators(self):
return (Auth_Basic,
Auth_Bearer_Anon,
Auth_Bearer_IDed)
class Credentials:
__slots__ = ("password",
"username")
def __init__(self):
self.username = None
self.password = None
def get(self):
# If stored, return those.
if (self.username is not None):
username = self.username
password = self.password
else:
try:
# Otherwise, use environment variables.
username = os.environ["CH_IMAGE_USERNAME"]
password = os.environ["CH_IMAGE_PASSWORD"]
except KeyError:
# Finally, prompt the user.
# FIXME: This hangs in Bats despite sys.stdin.isatty() == True.
try:
username = input("\nUsername: ")
except KeyboardInterrupt:
ch.FATAL("authentication cancelled")
password = getpass.getpass("Password: ")
if (not ch.password_many):
# Remember the credentials.
self.username = username
self.password = password
return (username, password)
class HTTP:
"""Transfers image data to and from a remote image repository via HTTPS.
Note that ref refers to the *remote* image. Objects of this class have
no information about the local image."""
__slots__ = ("auth",
"creds",
"ref",
"session")
def __init__(self, ref):
# Need an image ref with all the defaults filled in.
self.ref = ref.canonical
self.auth = Auth_None()
self.creds = Credentials()
self.session = None
# This is commented out because it prints full request and response
# bodies to standard output (not stderr), which overwhelms the terminal.
# Normally, a better debugging approach if you need this is to sniff the
# connection using e.g. mitmproxy.
#if (verbose >= 2):
# http.client.HTTPConnection.debuglevel = 1
@staticmethod
def headers_log(hs):
"""Log the headers."""
# All headers first.
for h in hs:
if (h.lower() == "www-authenticate"):
f = ch.VERBOSE
else:
f = ch.DEBUG
f("%s: %s" % (h, hs[h]))
# Friendly message for Docker Hub rate limit.
pull_ct = period = left_ct = reason = "???" # keep as strings
if ("ratelimit-limit" in hs):
h = hs["ratelimit-limit"]
m = re.search(r"^(\d+);w=(\d+)$", h)
if (m is None):
ch.WARNING("can’t parse RateLimit-Limit: %s" % h)
else:
pull_ct = m[1]
period = str(int(m[2]) / 3600) # seconds to hours
if ("ratelimit-remaining" in hs):
h = hs["ratelimit-remaining"]
m = re.search(r"^(\d+);", h)
if (m is None):
ch.WARNING("can’t parse RateLimit-Remaining: %s" % h)
else:
left_ct = m[1]
if ("docker-ratelimit-source" in hs):
h = hs["docker-ratelimit-source"]
m = re.search(r"^[0-9.a-f:]+$", h) # IPv4 or IPv6
if (m is not None):
reason = m[0]
else:
m = re.search(r"^[0-9A-Fa-f-]+$", h) # user UUID
if (m is not None):
reason = "auth"
else:
# Overall limits yield HTTP 429 so warning seems legitimate?
ch.WARNING("can’t parse Docker-RateLimit-Source: %s" % h)
if (any(i != "???" for i in (pull_ct, period, left_ct))):
ch.INFO("Docker Hub rate limit: %s pulls left of %s per %s hours (%s)"
% (left_ct, pull_ct, period, reason))
@property
def _url_base(self):
return "https://%s:%d/v2/" % (self.ref.host, self.ref.port)
def _url_of(self, type_, address):
"Return an appropriate repository URL."
return self._url_base + "/".join((self.ref.path_full, type_, address))
def blob_exists_p(self, digest):
"""Return true if a blob with digest (hex string) exists in the
remote repository, false otherwise."""
# Gotchas:
#
# 1. HTTP 401 means both unauthorized *or* not found, I assume to avoid
# information leakage about the presence of stuff one isn’t allowed
# to see. By the time it gets here, we should be authenticated, so
# interpret it as not found.
#
# 2. Sometimes we get 301 Moved Permanently. It doesn’t bubble up to
# here because requests.request() follows redirects. However,
# requests.head() does not follow redirects, and it seems like a
# weird status, so I worry there is a gotcha I haven’t figured out.
url = self._url_of("blobs", "sha256:%s" % digest)
res = self.request("HEAD", url, {200,401,404})
return (res.status_code == 200)
def blob_to_file(self, digest, path, msg):
"GET the blob with hash digest and save it at path."
# /v2/library/hello-world/blobs/<layer-hash>
url = self._url_of("blobs", "sha256:" + digest)
sw = ch.Progress_Writer(path, msg)
self.request("GET", url, out=sw, hd=digest)
sw.close()
def blob_upload(self, digest, data, note=""):
"""Upload blob with hash digest to url. data is the data to upload, and
can be anything requests can handle; if it’s an open file, then it’s
wrapped in a Progress_Reader object. note is a string to prepend to
the log messages; default empty string."""
ch.INFO("%s%s: checking if already in repository" % (note, digest[:7]))
# 1. Check if blob already exists. If so, stop.
if (self.blob_exists_p(digest)):
ch.INFO("%s%s: already present" % (note, digest[:7]))
return
msg = "%s%s: not present, uploading" % (note, digest[:7])
if (isinstance(data, io.IOBase)):
data = ch.Progress_Reader(data, msg)
data.start()
else:
ch.INFO(msg)
# 2. Get upload URL for blob.
url = self._url_of("blobs", "uploads/")
res = self.request("POST", url, {202})
# 3. Upload blob. We do a “monolithic” upload (i.e., send all the
# content in a single PUT request) as opposed to a “chunked” upload
# (i.e., send data in multiple PATCH requests followed by a PUT request
# with no body).
url = self.location_parse(res)
res = self.request("PUT", url, {201}, data=data,
params={ "digest": "sha256:%s" % digest })
if (isinstance(data, ch.Progress_Reader)):
data.close()
# 4. Verify blob now exists.
if (not self.blob_exists_p(digest)):
ch.FATAL("blob just uploaded does not exist: %s" % digest[:7])
def close(self):
if (self.session is not None):
self.session.close()
def config_upload(self, config):
"Upload config (sequence of bytes)."
self.blob_upload(ch.bytes_hash(config), config, "config: ")
def escalate(self, res):
"Try to escalate authorization; return True if successful, else False."
auth = self.auth.escalate(self, res)
if (auth is None):
return False
else:
self.auth = auth
return True
def fatman_to_file(self, path, msg):
"""GET the manifest for self.image and save it at path. This seems to
have four possible results:
1. HTTP 200, and body is a fat manifest: image exists and is
architecture-aware.
2. HTTP 200, but body is a skinny manifest: image exists but is
not architecture-aware.
3. HTTP 401/404: image does not exist or is unauthorized.
4. HTTP 429: rate limit exceeded.
This method raises Image_Unavailable_Error in case 3. The caller is
responsible for distinguishing cases 1 and 2."""
url = self._url_of("manifests", self.ref.version)
pw = ch.Progress_Writer(path, msg)
# Including TYPES_MANIFEST avoids the server trying to convert its v2
# manifest to a v1 manifest, which currently fails for images
# Charliecloud pushes. The error in the test registry is “empty history
# when trying to create schema1 manifest”.
accept = ",".join( list(TYPES_INDEX.values())
+ list(TYPES_MANIFEST.values()))
res = self.request("GET", url, out=pw, statuses={200, 401, 404, 429},
headers={ "Accept" : accept })
pw.close()
if (res.status_code == 429):
if (self.auth.anon_p):
hint = "consider --auth"
else:
hint = None
ch.FATAL("registry rate limit exceeded (HTTP 429)", hint)
elif (res.status_code != 200):
ch.DEBUG(res.content)
raise ch.Image_Unavailable_Error()
def layer_from_file(self, digest, path, note=""):
"Upload gzipped tarball layer at path, which must have hash digest."
# NOTE: We don’t verify the digest b/c that means reading the whole file.
ch.VERBOSE("layer tarball: %s" % path)
fp = path.open("rb") # open file avoids reading it all into memory
self.blob_upload(digest, fp, note)
ch.close_(fp)
def location_parse(self, response):
(res_scheme, res_domain, res_path, res_query, res_fragment) \
= urllib.parse.urlsplit(response.url)
(loc_scheme, loc_domain, loc_path, loc_query, loc_fragment) \
= urllib.parse.urlsplit(response.headers["Location"])
if not loc_scheme:
loc_scheme = res_scheme
if not loc_domain:
loc_domain = res_domain
return urllib.parse.urlunsplit((loc_scheme, loc_domain,
loc_path, loc_query, loc_fragment))
def manifest_to_file(self, path, msg, digest=None):
"""GET skinny manifest for the image and save it at path. If digest is
given, use that to fetch the appropriate architecture; otherwise,
fetch the default manifest using the existing image reference."""
if (digest is None):
digest = self.ref.version
else:
digest = "sha256:" + digest
url = self._url_of("manifests", digest)
pw = ch.Progress_Writer(path, msg)
accept = ",".join(TYPES_MANIFEST.values())
res = self.request("GET", url, out=pw, statuses={200, 401, 404},
headers={ "Accept" : accept })
pw.close()
if (res.status_code == 200):
# Some registries give us a fat manifest when we ask for a skinny
# one, i.e., we get back a Content-Type not in our Accept header.
# This appears to RFC 9110 conformant [1], though I think that
# reflects both a mistake in the standard and an error in judgement
# by Docker Hub. “HTTP 406 Not Acceptable” would be a much better
# response here IMO.
#
# We try to work around this, but if that fails, better to give a
# clear error message rather than crashing later when we try to parse
# it. See MR !1938.
#
# [1]: https://datatracker.ietf.org/doc/html/rfc9110#section-12.1
if (res.headers["Content-Type"] not in TYPES_MANIFEST.values()):
ch.FATAL("invalid Content-Type for manifest: %s"
% res.headers["Content-Type"],
hint="likely a registry bug; try a different arch?")
else:
ch.DEBUG(res.content)
raise ch.Image_Unavailable_Error()
def manifest_upload(self, manifest):
"Upload manifest (sequence of bytes)."
# Note: The manifest is *not* uploaded as a blob. We just do one PUT.
ch.INFO("manifest: uploading")
url = self._url_of("manifests", self.ref.tag)
self.request("PUT", url, {201}, data=manifest,
headers={ "Content-Type": TYPES_MANIFEST["docker2"] })
def request(self, method, url, statuses={200}, out=None, hd=None, **kwargs):
"""Request url using method and return the response object. If statuses
is given, it is set of acceptable response status codes, defaulting
to {200}; any other response is a fatal error. If out is given,
response content will be streamed to this Progress_Writer object and
must be non-zero length. If hd is given, validate integrity of
downloaded data using expected hash digest.
Use current session if there is one, or start a new one if not. If
authentication fails (or isn’t initialized), then authenticate harder
and re-try the request."""
# Set up.
assert (out or hd is None), "digest only checked if streaming"
self.session_init_maybe()
ch.VERBOSE("auth: %s" % self.auth)
if (out is not None):
kwargs["stream"] = True
# Make the request.
while True:
res = self.request_raw(method, url, statuses | {401}, **kwargs)
if (res.status_code != 401):
break
else:
ch.VERBOSE("HTTP 401 unauthorized")
if (self.escalate(res)): # success
ch.VERBOSE("retrying with auth: %s" % self.auth)
elif (401 in statuses): # caller can deal with it
break
else:
ch.FATAL("unhandled authentication failure")
# Stream response if needed.
m = hashlib.sha256()
if (out is not None and res.status_code == 200):
try:
length = int(res.headers["Content-Length"])
except KeyError:
length = None
except ValueError:
ch.FATAL("invalid Content-Length in response")
out.start(length)
for chunk in res.iter_content(ch.HTTP_CHUNK_SIZE):
out.write(chunk)
m.update(chunk) # store downloaded hash digest
# Validate integrity of downloaded data
if (hd is not None and hd != m.hexdigest()):
ch.FATAL("registry streamed response content is invalid")
# Done.
return res
def request_raw(self, method, url, statuses, auth=None, **kwargs):
"""Request url using method. statuses is an iterable of acceptable
response status codes; any other response is a fatal error. Return
the requests.Response object.
Session must already exist. If auth arg given, use it; otherwise, use
object’s stored authentication if initialized; otherwise, use no
authentication."""
ch.VERBOSE("%s: %s" % (method, url))
if ("headers" in kwargs):
self.headers_log(kwargs["headers"])
if (auth is None):
auth = self.auth
try:
res = self.session.request(method, url, auth=auth, **kwargs)
ch.VERBOSE("response status: %d" % res.status_code)
self.headers_log(res.headers)
if (res.status_code not in statuses):
ch.FATAL("%s failed; expected status %s but got %d: %s"
% (method, statuses, res.status_code, res.reason))
except requests.exceptions.RequestException as x:
ch.FATAL("%s failed: %s" % (method, x))
return res
def session_init_maybe(self):
"Initialize session if it’s not initialized; otherwise do nothing."
if (self.session is None):
ch.VERBOSE("initializing session")
self.session = requests.Session()
self.session.verify = tls_verify
|