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 logging" | |
] | |
}, | |
{ | |
"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": [ | |
"import httpx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"registry_url = \"https://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": [ | |
"image = \"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"service = \"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": [ | |
"response = httpx.get(\n", | |
" authentication_url,\n", | |
" params={\"scope\": f\"repository:{image}:pull\", \"service\": service},\n", | |
" headers={\n", | |
" \"Authorization\": f\"Bearer {token}\",\n", | |
" \"Accept\": \"application/vnd.docker.distribution.manifest.v2+json\",\n", | |
" },\n", | |
")\n", | |
"access_token = response.json()[\"token\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with httpx.Client(\n", | |
" base_url=registry_url,\n", | |
" headers={\n", | |
" \"Authorization\": f\"Bearer {access_token}\",\n", | |
" \"Accept\": \"application/vnd.docker.distribution.manifest.v2+json\",\n", | |
" },\n", | |
") as client:\n", | |
" response = client.get(\"/v2/\")\n", | |
"response" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"digest = \"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with httpx.Client(\n", | |
" base_url=registry_url,\n", | |
" headers={\n", | |
" \"Authorization\": f\"Bearer {access_token}\",\n", | |
" \"Accept\": \"application/vnd.docker.distribution.manifest.v2+json\",\n", | |
" },\n", | |
") as client:\n", | |
" response = client.get(f\"/v2/{image}/blobs/{digest}\")\n", | |
"response" | |
] | |
} | |
], | |
"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