GitLab Deploy Token Generator
GitLab Deploy Token을 생성해 주는 제너레이터 얘기예요. 프로젝트나 그룹용 토큰을 만들어 주고, 만든 토큰으로 ExternalSecret이 이미지나 패키지를 가져올 수 있게 도와줘요.
출처: External Secrets
본문
GitLab Deploy Token 생성기는 프로젝트나 그룹을 위한 GitLab deploy token을 만들어요. deploy token은 프로젝트의 저장소, 컨테이너 레지스트리, 패키지 레지스트리에 대한 읽기 또는 쓰기 접근을 주기 때문에, 자동화에서 이미지나 패키지를 가져올 때 아주 적합해요.
생성된 시크릿에는 두 개의 키가 있어요:
username: deploy token의 사용자 이름 (spec.username값, 또는username을 생략했을 때 GitLab이 할당하는gitlab+deploy-token-{n}값)token: deploy token 값
인증 (Authentication)
생성기는 api scope와 대상 프로젝트에 대한 Maintainer 이상(대상 그룹이면 Owner) 역할을 가진 access token(개인·그룹·프로젝트)으로 GitLab API에 인증해요. 그 토큰을 Kubernetes 시크릿에 저장하고 spec.auth.token.secretRef로 참조하면 돼요.
kubectl create secret generic gitlab-api-token --from-literal=token=«redacted:glpat-…»
대상 (Target)
spec.projectID 또는 spec.groupID 중 정확히 하나만 설정해요. 둘 다 숫자 ID 또는 group/project 같은 URL-escape 되지 않은 경로를 받아요. 생성기가 API를 호출하기 전에 경로를 URL-escape 하므로 미리 인코딩하지 마세요. 둘 다 설정하거나 둘 다 없거나 빈 문자열이면 CRD가 거부해요.
스코프 (Scopes)
spec.scopes는 다음 중 하나 이상이 필요해요: read_repository, read_registry, write_registry, read_package_registry, write_package_registry. 프로젝트는 추가로 read_virtual_registry와 write_virtual_registry를 지원해요.
토큰 수명 (Token lifecycle)
GitLab deploy token은 영구적이라서, 단기 토큰과 달리 GitLab이 자동으로 가비지 컬렉션하지 않아요. 그래서 생성기는 생성된 토큰 ID를 생성기 상태에 기록해 두고, 값이 재생성될 때(리프레시)와 이를 참조하는 ExternalSecret이 삭제될 때 이전 토큰을 폐기해요. GitLab이 서버 쪽에서 토큰을 만료시키는 백스톱을 원하면 spec.expiresAt을 설정하세요.
예제 매니페스트 (Example Manifest)
# 1. Create a GitLab access token (personal, group, or project) with the `api`
# scope and at least the Maintainer role on the target project / group.
# https://docs.gitlab.com/api/deploy_tokens/
# 2. Store it in a Kubernetes secret, e.g.
# kubectl create secret generic gitlab-api-token --from-literal=token=glpat-xxxx
---
apiVersion: generators.external-secrets.io/v1alpha1
kind: GitlabDeployToken
metadata:
name: gitlab-deploy-token
spec:
url: "" # Optional, defaults to https://gitlab.com
projectID: "42" # Numeric ID or unescaped path e.g. group/project. Mutually exclusive with groupID.
# groupID: "7" # Use instead of projectID to create a group deploy token.
name: "eso-managed"
scopes:
- read_repository
- read_registry
expiresAt: "2027-01-01T00:00:00Z" # Optional
username: "eso" # Optional, GitLab defaults to gitlab+deploy-token-{n}
auth:
token:
secretRef:
name: gitlab-api-token
key: token
생성기를 참조하는 ExternalSecret 예시:
---
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: gitlab-deploy-token
spec:
refreshInterval: "1h"
target:
name: gitlab-deploy-token # Name for the secret to be created on the cluster
dataFrom:
- sourceRef:
generatorRef:
apiVersion: generators.external-secrets.io/v1alpha1
kind: GitlabDeployToken
name: gitlab-deploy-token
참고 (Notes)
- 인증에 사용하는 access token은 절대 대상 시크릿에 기록되지 않고, 오직 생성된 deploy token만 기록돼요.
- 리프레시할 때마다 새 deploy token이 생성되고 이전 것이 폐기되므로, 매
refreshInterval마다 토큰 값이 회전돼요.