LoginSignup
0
0

More than 1 year has passed since last update.

さくらのクラウドのコンテナレジストリ(Lab)からリポジトリ情報を取得する

Last updated at Posted at 2022-05-29

前提

  • さくらのクラウドのコンテナレジストリ(Lab)を持っている
  • 「All」権限を持ったユーザーを持っている

curl編

を参考にさせてもらいました

  • Www-Authenticateを確認する
# curl -k -IL https://{コンテナレジストリのホスト名}/v2/_catalog
HTTP/2 401 
content-type: application/json; charset=utf-8
docker-distribution-api-version: registry/2.0
www-authenticate: Bearer realm="https://auth.sakuracr.jp/token/",service="{コンテナレジストリのホスト名}",scope="registry:catalog:*"
content-length: 145
date: Sun, 29 May 2022 03:44:09 GMT
  • www-authenticateで取得したservice,scopeを使用して、Tokenを生成する
# curl -u'username':'password' 'https://auth.sakuracr.jp/token/?service={コンテナレジストリのホスト名}&scope=registry:catalog:*'
{"token": "tokenが出てくる"}
  • Tokenを使用して、リポジトリのリストを取得する
curl -H "Authorization: Bearer {出てきたtoken}" https://{コンテナレジストリのホスト名}/v2/_catalog
{"repositories":["python"]}

Python3編

  • 必要なパッケージをインストール
pip install www-authenticate requests
  • コード
sample.py
import requests
from urllib.parse import urlencode
import www_authenticate


user_name = 'username'
user_pass = 'password'
registry_host = 'コンテナレジストリのホスト名'

catalog_url ='https://' + str(registry_host) + '/v2/_catalog'

# Www-Authenticateを確認する
response_unauthorized = requests.get(catalog_url)

# www-authenticateで取得したservice,scopeを使用して、Tokenを生成する
authenticate = www_authenticate.parse(response_unauthorized.headers['WWW-Authenticate'])

bearer_info = authenticate['bearer']
base_url = bearer_info['realm']
query = {
    'service': bearer_info['service'],
    'scope': bearer_info['scope']
    }
token_url_request = '%s?%s' % (base_url, urlencode(query))
token_url_response = requests.get(token_url_request, auth=(user_name, user_pass))
response_token = token_url_response.json()['token']

# Tokenを使用して、リポジトリのリストを取得する
headers = {
    'Authorization': f'Bearer {response_token}',
    }
response = requests.get(catalog_url, headers=headers).json()

print(response)
  • Yatta!!
# python sample.py 
{'repositories': ['python']}

TIPS

imageタグの一覧とか取得したい!って場合は
「/v2/_catalog」→「/v2/"image_name"/tags/list」とすると取得できる
詳細:HTTP API V2

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0