Created
May 27, 2020 20:31
-
-
Save Midnighter/1dfedcab689d433210b13b2c8cc5ad93 to your computer and use it in GitHub Desktop.
Proof of concept interaction with the Google Container Registry.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%load_ext lab_black" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"import logging\n", | |
"from urllib.parse import urljoin\n", | |
"from urllib.error import HTTPError\n", | |
"from urllib.request import build_opener\n", | |
"from urllib.parse import urlencode, urljoin, urlsplit, urlunsplit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"logging.basicConfig(level=\"DEBUG\")\n", | |
"logging.getLogger(\"blib2to3\").setLevel(logging.WARNING)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image = \"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"service = \"gcr.io\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"authentication_url = \"https://gcr.io/v2/token\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"registry_url = \"https://gcr.io\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"output = !gcloud auth print-access-token\n", | |
"token = output[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"auth_opener = build_opener()\n", | |
"auth_opener.addheaders.extend(\n", | |
" [\n", | |
" (\"Accept\", \"application/vnd.docker.distribution.manifest.v2+json\"),\n", | |
" (\"Authorization\", f\"Bearer {token}\"),\n", | |
" ]\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"parts = urlsplit(authentication_url)\n", | |
"with auth_opener.open(\n", | |
" urlunsplit(\n", | |
" parts._replace(\n", | |
" query=urlencode({\"scope\": f\"repository:{image}:pull\", \"service\": service})\n", | |
" )\n", | |
" )\n", | |
") as response:\n", | |
" print(response.geturl())\n", | |
" print(response.status, response.reason)\n", | |
" data = json.loads(response.read())\n", | |
"access_token = data[\"token\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"opener = build_opener()\n", | |
"opener.addheaders.extend(\n", | |
" [\n", | |
" (\"Accept\", \"application/vnd.docker.distribution.manifest.v2+json\"),\n", | |
" (\"Authorization\", f\"Bearer {access_token}\"),\n", | |
" ]\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"parts = urlsplit(registry_url)\n", | |
"with opener.open(urlunsplit(parts._replace(path=\"/v2/\"))) as response:\n", | |
" print(response.geturl())\n", | |
" print(response.status, response.reason)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"digest = \"\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This call fails with 401 Unauthorized yet I can even open the requested URL in my browser and it works..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"parts = urlsplit(registry_url)\n", | |
"try:\n", | |
" with opener.open(\n", | |
" urlunsplit(parts._replace(path=f\"/v2/{image}/blobs/{digest}\"))\n", | |
" ) as response:\n", | |
" print(response.geturl())\n", | |
" print(response.status, response.reason)\n", | |
" data = json.loads(response.read())\n", | |
"except HTTPError as error:\n", | |
" print(error.geturl())\n", | |
" print(error.status, error.reason)\n", | |
" data = error.read()\n", | |
"data" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment