fix(release): resolve image digests from Docker Hub
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 22m43s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish versioned installer release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 12s
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 22m43s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish versioned installer release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 12s
This commit is contained in:
1 parent
02783dd718
commit
aa141759a2
4 files changed
+94
-5
No files matched your search
@@ -165,10 +165,32 @@ else:
|
||||
self.wfile.write(payload)
|
||||
|
||||
def do_GET(self):
|
||||
path = urlsplit(self.path).path
|
||||
if path == '/token':
|
||||
expected = 'Basic ' + base64.b64encode(
|
||||
b'offline-user:' + DOCKER_TOKEN.encode()).decode('ascii')
|
||||
if self.headers.get('Authorization') != expected:
|
||||
self._json(401, {})
|
||||
return
|
||||
self._json(200, {'token': 'offline-registry-bearer'})
|
||||
return
|
||||
if path.startswith('/v2/') and '/manifests/sha-' in path:
|
||||
if self.headers.get('Authorization') != 'Bearer offline-registry-bearer':
|
||||
self._json(401, {})
|
||||
return
|
||||
if self.headers.get('Accept') is None:
|
||||
self._json(406, {})
|
||||
return
|
||||
digest = (GPU_AGENT_DIGEST if 'heimdall-agent-gpu' in path else
|
||||
AGENT_DIGEST if 'heimdall-agent' in path else SERVER_DIGEST)
|
||||
self.send_response(200)
|
||||
self.send_header('Docker-Content-Digest', digest)
|
||||
self.send_header('Content-Length', '0')
|
||||
self.end_headers()
|
||||
return
|
||||
if not self._authorized():
|
||||
self._json(401, {})
|
||||
return
|
||||
path = urlsplit(self.path).path
|
||||
commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip()
|
||||
if '/compare/' in path:
|
||||
self._json(200, {'commits': [{'sha': 'synthetic-merge-commit'}]})
|
||||
@@ -261,6 +283,8 @@ else:
|
||||
'EXPECTED_BUILD_SERVER_URL': origin,
|
||||
'DOCKERHUB_USERNAME': 'offline-user',
|
||||
'DOCKERHUB_TOKEN': DOCKER_TOKEN,
|
||||
'DOCKER_REGISTRY_TOKEN_URL': origin,
|
||||
'DOCKER_REGISTRY_URL': origin,
|
||||
'RELEASE_TOKEN': RELEASE_TOKEN,
|
||||
'RELEASE_SIGNING_KEY_B64': signing_key_b64,
|
||||
'OFFLINE_DOCKER_LOG': str(docker_log),
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Resolve the canonical digest for an immutable Docker Hub repository tag."""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
def endpoint(name, default, path):
|
||||
value = os.environ.get(name, default).rstrip('/')
|
||||
if not value.startswith('https://') or '@' in value:
|
||||
raise SystemExit('Registry endpoint must be HTTPS')
|
||||
return value + path
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
raise SystemExit('usage: dockerhub-tag-digest.py REPOSITORY IMMUTABLE_TAG')
|
||||
repository, tag = sys.argv[1:]
|
||||
if not re.fullmatch(r'[a-z0-9._-]+/[a-z0-9._-]+', repository):
|
||||
raise SystemExit('Invalid Docker Hub repository')
|
||||
if not re.fullmatch(r'sha-[0-9a-f]{40}', tag):
|
||||
raise SystemExit('Expected an immutable full-commit image tag')
|
||||
username = os.environ.get('DOCKERHUB_USERNAME', '')
|
||||
password = os.environ.get('DOCKERHUB_TOKEN', '')
|
||||
if not username or not password:
|
||||
raise SystemExit('Docker Hub credentials are required to resolve image digest')
|
||||
|
||||
token_url = endpoint('DOCKER_REGISTRY_TOKEN_URL', 'https://auth.docker.io',
|
||||
'/token?' + urlencode({'service': 'registry.docker.io',
|
||||
'scope': 'repository:' + repository + ':pull'}))
|
||||
registry_url = endpoint('DOCKER_REGISTRY_URL', 'https://registry-1.docker.io',
|
||||
'/v2/' + repository + '/manifests/' + tag)
|
||||
import base64
|
||||
credentials = base64.b64encode((username + ':' + password).encode()).decode('ascii')
|
||||
try:
|
||||
token_request = Request(token_url, headers={'Authorization': 'Basic ' + credentials,
|
||||
'Accept': 'application/json'})
|
||||
with urlopen(token_request, timeout=30) as response:
|
||||
token_data = json.load(response)
|
||||
bearer = token_data.get('token') or token_data.get('access_token')
|
||||
if not isinstance(bearer, str) or not bearer:
|
||||
raise SystemExit('Docker Hub did not return a registry bearer token')
|
||||
manifest_request = Request(registry_url, headers={
|
||||
'Authorization': 'Bearer ' + bearer,
|
||||
'Accept': ', '.join([
|
||||
'application/vnd.oci.image.index.v1+json',
|
||||
'application/vnd.docker.distribution.manifest.list.v2+json',
|
||||
'application/vnd.oci.image.manifest.v1+json',
|
||||
'application/vnd.docker.distribution.manifest.v2+json',
|
||||
]),
|
||||
})
|
||||
with urlopen(manifest_request, timeout=30) as response:
|
||||
digest = response.headers.get('Docker-Content-Digest', '')
|
||||
except (HTTPError, URLError, TimeoutError, OSError, ValueError):
|
||||
raise SystemExit('Unable to resolve immutable Docker Hub image digest')
|
||||
if not re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
|
||||
raise SystemExit('Docker Hub returned an invalid image digest')
|
||||
print(digest)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -67,9 +67,8 @@ for component in server agent agent-gpu; do
|
||||
docker pull "$image:sha-$sha"
|
||||
revision=$(docker image inspect "$image:sha-$sha" --format '{{index .Config.Labels "org.opencontainers.image.revision"}}')
|
||||
[[ "$revision" == "$sha" ]] || { echo 'Pulled image does not match the versioned source commit' >&2; exit 1; }
|
||||
digests=$(docker image inspect "$image:sha-$sha" --format '{{range .RepoDigests}}{{println .}}{{end}}')
|
||||
[[ "$(printf '%s\n' "$digests" | sed '/^$/d' | wc -l)" == 1 ]] || { echo 'Expected one registry digest for the immutable image tag' >&2; exit 1; }
|
||||
printf '%s\n' "$digests" >> artifacts/image-digests.txt
|
||||
digest=$(python3 scripts/ci/dockerhub-tag-digest.py "$image" "sha-$sha")
|
||||
printf '%s@%s\n' "$image" "$digest" >> artifacts/image-digests.txt
|
||||
done
|
||||
cp artifacts/image-digests.txt artifacts/packages/image-digests.txt
|
||||
(cd artifacts/packages && sha256sum image-digests.txt >> SHA256SUMS)
|
||||
|
||||
Reference in new issue
Block a user