Harbor REST API
1️⃣ + Harbor API 인증 방식 이해 (Token, Cookie)
Harbor는 REST API를 통해 사용자, 프로젝트, 이미지 레포지토리 등 다양한 자원을 제어할 수 있도록 지원합니다.
1. Harbor API 접근 방법
- Base URL: 대부분의 엔드포인트는
/api/v2.0/
또는/api/v2.0/projects
,/api/v2.0/users
와 같은 형식으로 구성되어 있습니다. - Swagger 문서:
https://<harbor-url>/swagger
또는/api/swagger.json
통해 문서 확인 가능
2. 인증 방식
Harbor의 API는 다음 두 가지 인증 방식을 사용합니다:
✅ Basic Auth (가장 일반적)
curl -u admin:Harbor12345 https://harbor.example.com/api/v2.0/projects
✅ Session Cookie 방식 (로그인 후 쿠키 저장)
curl -c cookies.txt -X POST "https://harbor.example.com/c/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "principal=admin&password=Harbor12345"
curl -b cookies.txt https://harbor.example.com/api/v2.0/projects
2️⃣ + 이미지 조회, 사용자 생성, 프로젝트 관리
API를 이용하면 Harbor UI에서 하던 모든 작업을 스크립트 기반 자동화로 대체할 수 있습니다.
1. 프로젝트 목록 조회
curl -u admin:Harbor12345 https://harbor.example.com/api/v2.0/projects
2. 사용자 생성
curl -u admin:Harbor12345 -X POST https://harbor.example.com/api/v2.0/users \
-H "Content-Type: application/json" \
-d '{
"username": "devuser",
"email": "devuser@example.com",
"password": "UserPass123"
}'
3. 이미지 리포지토리 조회
curl -u admin:Harbor12345 https://harbor.example.com/api/v2.0/projects/myproject/repositories
3️⃣ + 실습: Python 또는 Bash로 Harbor 자동화 스크립트 작성
🔧 예제: Python으로 Harbor 프로젝트 자동 생성 스크립트
import requests
from requests.auth import HTTPBasicAuth
HARBOR_URL = "https://harbor.example.com"
USERNAME = "admin"
PASSWORD = "Harbor12345"
def create_project(name):
url = f"{HARBOR_URL}/api/v2.0/projects"
data = {"project_name": name, "public": True}
response = requests.post(url, json=data, auth=HTTPBasicAuth(USERNAME, PASSWORD))
print(response.status_code, response.text)
create_project("ci-managed-project")
🔧 Bash로 사용자 리스트 가져오기
#!/bin/bash
HARBOR_URL="https://harbor.example.com"
AUTH="admin:Harbor12345"
curl -s -u $AUTH $HARBOR_URL/api/v2.0/users | jq
🔍 Harbor REST API 아키텍처
아래 그림은 Harbor REST API의 동작 흐름과 인증 구조를 나타냅니다.
+-------------------+ Basic Auth or Session
| Client (CLI) | ----------------------------->
+-------------------+ |
| \|/
| HTTP(S) API Request +--------------------+
+------------------------------------>| Harbor API Gateway |
+--------------------+
|
| Internal Logic (Controllers)
v
+---------------------------+
| Harbor Core Service Layer |
+---------------------------+
|
+------------------+-----------------+
| | |
Projects Users Repositories
Harbor API를 활용하면 단순 UI 작업을 넘어서 DevOps 파이프라인 자동화, 대규모 사용자 관리, 리소스 정책 제어 등 다양한 고급 시나리오에 적용 가능합니다.
마지막 수정일자