feat: v3 Go rewrite — full artifact proxy with web UI, TUI, and Terraform provider
Complete rewrite of ArtifactAPI from Python/FastAPI to Go as a single binary. Core engine: - 10 package providers: generic, docker, helm, pypi, npm, rpm, alpine, puppet, terraform, goproxy — each with built-in mutable patterns - Content-addressable storage (SHA256 dedup across all remotes) - Three-tier caching: Redis (TTL/locks) → S3/MinIO (blobs) → upstream - Classifier with allowlist/blocklist per-remote (empty = allow all) - Circuit breaker, conditional revalidation, stale-on-error - Background garbage collection for orphaned blobs - Access logging to PostgreSQL API: - v1 proxy endpoints (backwards compatible) - v2 management API: CRUD remotes/virtuals, object browser, stats, health, SSE events, probe/test endpoint - Virtual repos with index merging (Helm YAML + PyPI HTML) Frontend (React + Vite, separate Dockerfile): - Dashboard with stats, health indicators, top remotes - Remotes list with type filter, remote detail with config/patterns - Object browser with pagination and evict - Test Remote page: probe any remote path, see headers/size/timing - Virtuals page with expandable member lists TUI (Bubble Tea): - Dashboard, remotes list/detail, object browser, virtuals - Vim-style navigation, artifactapi tui --endpoint <url> Infrastructure: - S3 client supports MinIO, Ceph RGW, AWS S3 (minio-go) - PostgreSQL schema with migrations - Docker Compose: API + UI + Postgres 17 + Redis 7 + MinIO - Makefile with Go version check, build/test/lint/fmt/e2e targets - Distroless Docker image (~15MB) Testing: - Unit tests for models, classifier, providers, mergers - E2E tests with testcontainers-go (real Postgres/Redis/MinIO) Terraform config: - All 40 production remotes + helm virtual as HCL - Provider repo: terraform-provider-artifactapi v0.0.1 (separate)
This commit is contained in:
+2
-61
@@ -1,61 +1,2 @@
|
|||||||
# Python
|
bin/
|
||||||
__pycache__/
|
terraform/
|
||||||
*.py[cod]
|
|
||||||
*$py.class
|
|
||||||
*.so
|
|
||||||
.Python
|
|
||||||
build/
|
|
||||||
develop-eggs/
|
|
||||||
dist/
|
|
||||||
downloads/
|
|
||||||
eggs/
|
|
||||||
.eggs/
|
|
||||||
lib/
|
|
||||||
lib64/
|
|
||||||
parts/
|
|
||||||
sdist/
|
|
||||||
var/
|
|
||||||
wheels/
|
|
||||||
*.egg-info/
|
|
||||||
.installed.cfg
|
|
||||||
*.egg
|
|
||||||
MANIFEST
|
|
||||||
|
|
||||||
# Virtual environment
|
|
||||||
.venv/
|
|
||||||
venv/
|
|
||||||
ENV/
|
|
||||||
env/
|
|
||||||
|
|
||||||
# IDE
|
|
||||||
.vscode/
|
|
||||||
.idea/
|
|
||||||
*.swp
|
|
||||||
*.swo
|
|
||||||
|
|
||||||
# Environment variables
|
|
||||||
.env
|
|
||||||
|
|
||||||
# Logs
|
|
||||||
*.log
|
|
||||||
|
|
||||||
# uv
|
|
||||||
uv.lock
|
|
||||||
|
|
||||||
# tox
|
|
||||||
.tox/
|
|
||||||
|
|
||||||
# pytest
|
|
||||||
.pytest_cache/
|
|
||||||
|
|
||||||
# pre-commit
|
|
||||||
.pre-commit-cache/
|
|
||||||
|
|
||||||
# ruff
|
|
||||||
.ruff_cache/
|
|
||||||
|
|
||||||
# Docker volumes
|
|
||||||
minio_data/
|
|
||||||
|
|
||||||
# Local configuration overrides
|
|
||||||
ca-bundle.pem
|
|
||||||
|
|||||||
+13
-16
@@ -1,23 +1,20 @@
|
|||||||
FROM git.unkin.net/unkin/almalinux9-base:latest
|
FROM golang:1.25-alpine AS builder
|
||||||
|
|
||||||
ARG VERSION=0.0.0.dev0
|
RUN apk add --no-cache git
|
||||||
|
|
||||||
COPY . /build
|
WORKDIR /build
|
||||||
|
|
||||||
RUN HATCH_VCS_PRETEND_VERSION=${VERSION} \
|
COPY go.mod go.sum ./
|
||||||
SETUPTOOLS_SCM_PRETEND_VERSION=${VERSION} \
|
RUN go mod download
|
||||||
uv build --wheel --directory /build && \
|
|
||||||
useradd -m -r -s /bin/sh appuser
|
|
||||||
|
|
||||||
USER appuser
|
COPY . .
|
||||||
RUN uv tool install --from /build/dist/*.whl artifactapi
|
|
||||||
|
|
||||||
USER root
|
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o artifactapi ./cmd/artifactapi
|
||||||
RUN rm -rf /build
|
|
||||||
|
FROM gcr.io/distroless/static-debian12:nonroot
|
||||||
|
|
||||||
|
COPY --from=builder /build/artifactapi /usr/local/bin/artifactapi
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD curl -f http://localhost:8000/health || exit 1
|
|
||||||
USER appuser
|
ENTRYPOINT ["artifactapi"]
|
||||||
ENV PATH="/home/appuser/.local/bin:$PATH"
|
|
||||||
WORKDIR /app
|
|
||||||
CMD ["artifactapi"]
|
|
||||||
|
|||||||
@@ -1,59 +1,49 @@
|
|||||||
.PHONY: build install dev clean test lint format pre-commit tox docker-build docker-up docker-down docker-logs docker-rebuild docker-clean docker-restart
|
.PHONY: build test lint fmt e2e docker docker-ui compose clean tidy check-go
|
||||||
|
|
||||||
build:
|
BINARY := bin/artifactapi
|
||||||
docker build -t artifactapi:dev .
|
MODULE := git.unkin.net/unkin/artifactapi
|
||||||
|
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "0.0.0-dev")
|
||||||
|
GO_VERSION_REQUIRED := 1.23
|
||||||
|
GO_VERSION_ACTUAL := $(shell go version | sed 's/go version go\([0-9]*\.[0-9]*\).*/\1/')
|
||||||
|
|
||||||
install: build
|
check-go:
|
||||||
|
@if [ "$$(printf '%s\n%s' "$(GO_VERSION_REQUIRED)" "$(GO_VERSION_ACTUAL)" | sort -V | head -1)" != "$(GO_VERSION_REQUIRED)" ]; then \
|
||||||
|
echo "ERROR: Go >= $(GO_VERSION_REQUIRED) required, found $(GO_VERSION_ACTUAL)"; exit 1; \
|
||||||
|
fi
|
||||||
|
|
||||||
docker-build: build
|
build: check-go tidy
|
||||||
|
go build -ldflags="-s -w" -o $(BINARY) ./cmd/artifactapi
|
||||||
|
|
||||||
dev: build
|
test: check-go
|
||||||
uv sync --dev
|
go test -race -count=1 ./pkg/... ./internal/...
|
||||||
|
|
||||||
|
lint: check-go
|
||||||
|
golangci-lint run ./...
|
||||||
|
go vet ./...
|
||||||
|
|
||||||
|
fmt: check-go
|
||||||
|
gofmt -w .
|
||||||
|
goimports -w .
|
||||||
|
|
||||||
|
e2e: check-go
|
||||||
|
TESTCONTAINERS_RYUK_DISABLED=true go test -tags=e2e -race -count=1 -timeout=5m ./e2e/...
|
||||||
|
|
||||||
|
docker:
|
||||||
|
docker build -t artifactapi:$(VERSION) .
|
||||||
|
|
||||||
|
docker-ui:
|
||||||
|
docker build -t artifactapi-ui:$(VERSION) -f ui/Dockerfile.ui ui/
|
||||||
|
|
||||||
|
compose:
|
||||||
|
docker compose up -d
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf .venv
|
rm -rf bin/
|
||||||
rm -rf build/
|
|
||||||
rm -rf dist/
|
|
||||||
rm -rf *.egg-info/
|
|
||||||
|
|
||||||
test:
|
tidy:
|
||||||
uvx --python 3.11 --with tox-uv tox
|
go mod tidy
|
||||||
|
|
||||||
tox:
|
|
||||||
uvx --python 3.11 --with tox-uv tox
|
|
||||||
|
|
||||||
pre-commit:
|
|
||||||
uvx --python 3.11 pre-commit run --all-files
|
|
||||||
|
|
||||||
lint:
|
|
||||||
uv run ruff check --fix .
|
|
||||||
|
|
||||||
format:
|
|
||||||
uv run ruff format .
|
|
||||||
|
|
||||||
run:
|
|
||||||
uv run python -m src.artifactapi.main
|
|
||||||
|
|
||||||
docker-up:
|
|
||||||
docker-compose up --build --force-recreate -d
|
|
||||||
|
|
||||||
docker-down:
|
|
||||||
docker-compose down
|
|
||||||
|
|
||||||
docker-logs:
|
|
||||||
docker-compose logs -f
|
|
||||||
|
|
||||||
docker-rebuild:
|
|
||||||
docker-compose build --no-cache
|
|
||||||
|
|
||||||
docker-clean:
|
|
||||||
docker-compose down -v --remove-orphans
|
|
||||||
docker system prune -f
|
|
||||||
|
|
||||||
docker-restart: docker-down docker-up
|
|
||||||
|
|
||||||
# Bump helpers — reads the latest semver tag and creates the next one.
|
# Bump helpers — reads the latest semver tag and creates the next one.
|
||||||
# If no tag exists yet, starts from v0.0.0.
|
|
||||||
_LATEST := $(shell git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$$' | head -1)
|
_LATEST := $(shell git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$$' | head -1)
|
||||||
_BASE := $(if $(_LATEST),$(_LATEST),v0.0.0)
|
_BASE := $(if $(_LATEST),$(_LATEST),v0.0.0)
|
||||||
_MAJ := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f1)
|
_MAJ := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f1)
|
||||||
|
|||||||
@@ -0,0 +1,880 @@
|
|||||||
|
# ArtifactAPI v3 — Go Rewrite Plan
|
||||||
|
|
||||||
|
## Context
|
||||||
|
|
||||||
|
ArtifactAPI is a production artifact proxy/cache serving ~42 remotes (Docker registries, Helm repos, RPM/Alpine repos, GitHub releases, PyPI, npm, Puppet Forge, Terraform registries, Go module proxies) across a Kubernetes cluster. The current Python (FastAPI) implementation works but has architectural debt: opaque hashed S3 paths, no UI for visibility, YAML config files that drift, no garbage collection, no access logging, and virtual repos limited to Helm only.
|
||||||
|
|
||||||
|
The v3 rewrite targets: a single Go binary (API + TUI), a separate React frontend (own Dockerfile), a Terraform provider (separate repo), content-addressable storage, and a cleaner data model that makes the cache inspectable and manageable.
|
||||||
|
|
||||||
|
**Repo**: Same repo (`git.unkin.net/unkin/artifactapi`), new branch.
|
||||||
|
**Module**: `git.unkin.net/unkin/artifactapi`
|
||||||
|
**Frontend**: React + Vite, separate Dockerfile, talks to API
|
||||||
|
**Terraform provider**: Separate repo (`terraform-provider-artifactapi`)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
|
```
|
||||||
|
┌───────────────────────────────────┐ ┌──────────────────────┐
|
||||||
|
│ Go Binary (API + TUI) │ │ Frontend Container │
|
||||||
|
│ │ │ │
|
||||||
|
│ ┌──────────┐ ┌───────────────┐ │ │ React + Vite SPA │
|
||||||
|
│ │ REST API │ │ Proxy Engine │ │◄───│ nginx / node serve │
|
||||||
|
│ │ /api/v2 │ │ /api/v1/... │ │ │ Dockerfile.ui │
|
||||||
|
│ │ │ │ /v2/... (OCI) │ │ └──────────────────────┘
|
||||||
|
│ └────┬─────┘ └──────┬────────┘ │
|
||||||
|
│ │ │ │ ┌──────────────────────┐
|
||||||
|
│ ┌────┴───────────────┴────────┐ │ │ Terraform Provider │
|
||||||
|
│ │ Data Layer │ │◄───│ (separate repo) │
|
||||||
|
│ │ PostgreSQL · Redis · S3 │ │ └──────────────────────┘
|
||||||
|
│ └─────────────────────────────┘ │
|
||||||
|
│ │ ┌──────────────────────┐
|
||||||
|
│ ┌─────────────────────────────┐ │ │ TUI (subcommand) │
|
||||||
|
│ │ artifactapi tui │──│───►│ artifactapi tui │
|
||||||
|
│ └─────────────────────────────┘ │ │ --endpoint <url> │
|
||||||
|
└───────────────────────────────────┘ └──────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
Three independent deployment units:
|
||||||
|
1. **Go binary** — API server + TUI subcommand (single `Dockerfile`)
|
||||||
|
2. **React frontend** — SPA served by nginx (`Dockerfile.ui`), talks to `/api/v2`
|
||||||
|
3. **Terraform provider** — separate repo, calls `/api/v2` CRUD
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Project Structure (Modular)
|
||||||
|
|
||||||
|
```
|
||||||
|
artifactapi/
|
||||||
|
├── cmd/
|
||||||
|
│ └── artifactapi/
|
||||||
|
│ └── main.go # entrypoint: serve / tui subcommands
|
||||||
|
│
|
||||||
|
├── pkg/ # PUBLIC — importable by terraform provider, CLI tools
|
||||||
|
│ ├── models/ # shared domain types
|
||||||
|
│ │ ├── remote.go # Remote, RemoteConfig, PackageType enum
|
||||||
|
│ │ ├── virtual.go # Virtual, VirtualConfig
|
||||||
|
│ │ ├── artifact.go # Artifact, Blob, AccessLogEntry
|
||||||
|
│ │ ├── local.go # LocalFile, LocalRepo
|
||||||
|
│ │ └── stats.go # RemoteStats, OverviewStats
|
||||||
|
│ └── client/ # typed Go API client (used by TUI + Terraform provider)
|
||||||
|
│ ├── client.go # Client struct, base HTTP
|
||||||
|
│ ├── remotes.go # remote CRUD methods
|
||||||
|
│ ├── virtuals.go # virtual CRUD methods
|
||||||
|
│ ├── objects.go # object browse/evict methods
|
||||||
|
│ └── stats.go # stats methods
|
||||||
|
│
|
||||||
|
├── internal/ # PRIVATE — server internals
|
||||||
|
│ ├── server/
|
||||||
|
│ │ ├── server.go # HTTP server setup, router
|
||||||
|
│ │ └── middleware.go # logging, recovery, request-id, access logging
|
||||||
|
│ │
|
||||||
|
│ ├── api/
|
||||||
|
│ │ ├── v1/ # proxy endpoints (v1 compat)
|
||||||
|
│ │ │ ├── proxy.go # GET /api/v1/remote/{name}/{path}
|
||||||
|
│ │ │ ├── docker.go # /v2/{name}/{path}
|
||||||
|
│ │ │ ├── virtual.go # GET /api/v1/virtual/{name}/{path}
|
||||||
|
│ │ │ └── local.go # CRUD /api/v1/local/{name}/{path}
|
||||||
|
│ │ └── v2/ # management API
|
||||||
|
│ │ ├── remotes.go # CRUD + stats
|
||||||
|
│ │ ├── virtuals.go # CRUD
|
||||||
|
│ │ ├── objects.go # browse/evict cached objects
|
||||||
|
│ │ ├── stats.go # overview, top-remotes
|
||||||
|
│ │ ├── events.go # SSE stream
|
||||||
|
│ │ └── health.go # health, metrics
|
||||||
|
│ │
|
||||||
|
│ ├── provider/ # package-type providers (registry protocol handlers)
|
||||||
|
│ │ ├── provider.go # Provider interface + registry
|
||||||
|
│ │ ├── generic/
|
||||||
|
│ │ │ ├── generic.go
|
||||||
|
│ │ │ └── generic_test.go
|
||||||
|
│ │ ├── docker/
|
||||||
|
│ │ │ ├── docker.go # OCI Distribution v2 via go-containerregistry
|
||||||
|
│ │ │ ├── auth.go # Bearer token fetch + cache
|
||||||
|
│ │ │ └── docker_test.go
|
||||||
|
│ │ ├── helm/
|
||||||
|
│ │ │ ├── helm.go # index rewriting via helm.sh/helm/v3/pkg/repo
|
||||||
|
│ │ │ ├── merger.go # virtual index merge
|
||||||
|
│ │ │ └── helm_test.go
|
||||||
|
│ │ ├── pypi/
|
||||||
|
│ │ │ ├── pypi.go # simple index HTML rewriting
|
||||||
|
│ │ │ ├── merger.go # virtual simple index merge
|
||||||
|
│ │ │ └── pypi_test.go
|
||||||
|
│ │ ├── npm/
|
||||||
|
│ │ │ ├── npm.go # metadata JSON rewriting
|
||||||
|
│ │ │ └── npm_test.go
|
||||||
|
│ │ ├── rpm/
|
||||||
|
│ │ │ ├── rpm.go # repodata patterns
|
||||||
|
│ │ │ └── rpm_test.go
|
||||||
|
│ │ ├── alpine/
|
||||||
|
│ │ │ ├── alpine.go # APKINDEX patterns
|
||||||
|
│ │ │ └── alpine_test.go
|
||||||
|
│ │ ├── puppet/
|
||||||
|
│ │ │ ├── puppet.go # file_uri JSON rewriting
|
||||||
|
│ │ │ └── puppet_test.go
|
||||||
|
│ │ ├── terraform/
|
||||||
|
│ │ │ ├── terraform.go # registry protocol, download URL rewriting
|
||||||
|
│ │ │ └── terraform_test.go
|
||||||
|
│ │ └── goproxy/
|
||||||
|
│ │ ├── goproxy.go # Go module proxy protocol (GOPROXY)
|
||||||
|
│ │ └── goproxy_test.go
|
||||||
|
│ │
|
||||||
|
│ ├── proxy/
|
||||||
|
│ │ ├── engine.go # core fetch-or-cache logic
|
||||||
|
│ │ ├── engine_test.go
|
||||||
|
│ │ ├── classifier.go # immutable vs mutable classification
|
||||||
|
│ │ ├── classifier_test.go
|
||||||
|
│ │ ├── revalidator.go # conditional HEAD requests (ETag/Last-Modified)
|
||||||
|
│ │ └── circuit.go # per-remote circuit breaker
|
||||||
|
│ │
|
||||||
|
│ ├── storage/
|
||||||
|
│ │ ├── s3.go # S3 client (minio-go — works with MinIO, Ceph, AWS)
|
||||||
|
│ │ ├── s3_test.go
|
||||||
|
│ │ ├── cas.go # content-addressable store logic
|
||||||
|
│ │ └── cas_test.go
|
||||||
|
│ │
|
||||||
|
│ ├── cache/
|
||||||
|
│ │ ├── redis.go # TTL management, fetch locks
|
||||||
|
│ │ ├── redis_test.go
|
||||||
|
│ │ └── lock.go # distributed lock abstraction
|
||||||
|
│ │
|
||||||
|
│ ├── database/
|
||||||
|
│ │ ├── postgres.go # connection pool, migration runner
|
||||||
|
│ │ ├── queries/ # SQL query files or sqlc-generated code
|
||||||
|
│ │ │ ├── remotes.sql.go
|
||||||
|
│ │ │ ├── virtuals.sql.go
|
||||||
|
│ │ │ ├── artifacts.sql.go
|
||||||
|
│ │ │ └── access_log.sql.go
|
||||||
|
│ │ └── migrations/ # golang-migrate SQL files
|
||||||
|
│ │ ├── 001_initial.up.sql
|
||||||
|
│ │ └── 001_initial.down.sql
|
||||||
|
│ │
|
||||||
|
│ ├── metrics/
|
||||||
|
│ │ └── prometheus.go # counters, gauges, histograms
|
||||||
|
│ │
|
||||||
|
│ ├── gc/
|
||||||
|
│ │ ├── gc.go # background garbage collection goroutine
|
||||||
|
│ │ └── gc_test.go
|
||||||
|
│ │
|
||||||
|
│ ├── tui/
|
||||||
|
│ │ ├── app.go # Bubble Tea main model
|
||||||
|
│ │ ├── views/
|
||||||
|
│ │ │ ├── dashboard.go
|
||||||
|
│ │ │ ├── remotes.go
|
||||||
|
│ │ │ ├── objects.go
|
||||||
|
│ │ │ └── virtuals.go
|
||||||
|
│ │ └── components/
|
||||||
|
│ │ ├── table.go
|
||||||
|
│ │ └── statusbar.go
|
||||||
|
│ │
|
||||||
|
│ └── config/
|
||||||
|
│ └── env.go # environment variable parsing + validation
|
||||||
|
│
|
||||||
|
├── ui/ # React frontend — SEPARATE DOCKERFILE
|
||||||
|
│ ├── src/
|
||||||
|
│ │ ├── App.tsx
|
||||||
|
│ │ ├── pages/
|
||||||
|
│ │ │ ├── Dashboard.tsx
|
||||||
|
│ │ │ ├── Remotes.tsx
|
||||||
|
│ │ │ ├── RemoteDetail.tsx
|
||||||
|
│ │ │ ├── Virtuals.tsx
|
||||||
|
│ │ │ └── Objects.tsx
|
||||||
|
│ │ ├── components/
|
||||||
|
│ │ │ ├── RemoteTable.tsx
|
||||||
|
│ │ │ ├── ObjectBrowser.tsx
|
||||||
|
│ │ │ ├── StatsCard.tsx
|
||||||
|
│ │ │ └── EventFeed.tsx
|
||||||
|
│ │ └── api/
|
||||||
|
│ │ └── client.ts # typed API client
|
||||||
|
│ ├── package.json
|
||||||
|
│ ├── vite.config.ts
|
||||||
|
│ ├── tsconfig.json
|
||||||
|
│ ├── Dockerfile.ui # multi-stage: node build → nginx
|
||||||
|
│ └── nginx.conf # proxy /api/* to backend, serve SPA
|
||||||
|
│
|
||||||
|
├── e2e/ # end-to-end integration tests
|
||||||
|
│ ├── e2e_test.go # TestMain spins up docker-compose stack
|
||||||
|
│ ├── proxy_test.go # proxy through real remotes
|
||||||
|
│ ├── docker_test.go # Docker v2 protocol e2e
|
||||||
|
│ ├── management_test.go # v2 API CRUD
|
||||||
|
│ ├── virtual_test.go # virtual repo merge e2e
|
||||||
|
│ └── docker-compose.e2e.yml # postgres + redis + minio for tests
|
||||||
|
│
|
||||||
|
├── go.mod
|
||||||
|
├── go.sum
|
||||||
|
├── Makefile
|
||||||
|
├── Dockerfile # Go binary (API server + TUI)
|
||||||
|
├── Dockerfile.ui # symlink or copy → ui/Dockerfile.ui
|
||||||
|
└── docker-compose.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
### Key Modularisation Decisions
|
||||||
|
|
||||||
|
- **`pkg/models/`** — Shared domain types importable by the Terraform provider and any external tooling. No dependencies on internal packages
|
||||||
|
- **`pkg/client/`** — Typed Go API client used by both the TUI and the Terraform provider. Depends only on `pkg/models/` and stdlib
|
||||||
|
- **`internal/provider/`** — Each package type is its own subpackage with isolated tests. A provider registry maps `PackageType → Provider`
|
||||||
|
- **`internal/database/queries/`** — Use [sqlc](https://sqlc.dev/) to generate type-safe query functions from SQL, or hand-written query files
|
||||||
|
- **`e2e/`** — Separate test binary that spins up a real docker-compose stack
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Go Ecosystem Libraries
|
||||||
|
|
||||||
|
Prefer existing, maintained Go modules over writing protocol handlers from scratch.
|
||||||
|
|
||||||
|
### Package-Type Libraries
|
||||||
|
|
||||||
|
| Package Type | Go Module | What It Gives Us |
|
||||||
|
|---|---|---|
|
||||||
|
| **Docker/OCI** | `github.com/google/go-containerregistry` | Full Registry v2/OCI client: manifest parsing, auth challenges, blob operations. `pkg/registry` can implement a v2 server. Reference: `github.com/regclient/regclient` |
|
||||||
|
| **Helm** | `helm.sh/helm/v3/pkg/repo` | Parse/generate `index.yaml`, `IndexFile`/`ChartVersion` types, URL entries. Used directly for merge |
|
||||||
|
| **Terraform** | `github.com/hashicorp/terraform-registry-address` | Provider/module address parsing, `ForRegistryProtocol()` URL generation. Protocol spec: provider registry protocol v1 |
|
||||||
|
| **Go Modules** | `github.com/goproxy/goproxy` | Minimalist GOPROXY protocol handler, implements full spec as `http.Handler`. Handles `/@v/list`, `/@v/{v}.info`, `/@v/{v}.mod`, `/@v/{v}.zip`, `/@latest` |
|
||||||
|
| **RPM** | `rs3.io/go/rpm/repomd` | Parse `repomd.xml`, `primary.xml` with proper XML namespace handling |
|
||||||
|
| **Alpine** | `gitlab.alpinelinux.org/alpine/go` | Official Alpine library: parse APKINDEX, `.apk` files |
|
||||||
|
| **PyPI** | stdlib `golang.org/x/net/html` | No dedicated Go PyPI library exists. Parse simple index HTML with `x/net/html`, extract `<a>` tags. Minimal — the rewriting is just href replacement |
|
||||||
|
| **npm** | stdlib `encoding/json` | npm metadata is JSON — parse with stdlib, rewrite `dist.tarball` URLs. No special library needed |
|
||||||
|
| **Puppet Forge** | stdlib `encoding/json` | Forge API is JSON — parse and rewrite `file_uri` fields. Community lib `github.com/johnmccabe/go-puppetforge` exists but is thin; stdlib suffices |
|
||||||
|
|
||||||
|
### Infrastructure Libraries
|
||||||
|
|
||||||
|
| Purpose | Go Module | Why This One |
|
||||||
|
|---|---|---|
|
||||||
|
| **HTTP router** | `github.com/go-chi/chi/v5` | Lightweight, stdlib `http.Handler` compatible, middleware chain |
|
||||||
|
| **PostgreSQL** | `github.com/jackc/pgx/v5` | Pure Go, connection pooling, COPY support, prepared statements |
|
||||||
|
| **SQL generation** | `github.com/sqlc-dev/sqlc` | Generate type-safe Go from SQL queries — no ORM, no reflection |
|
||||||
|
| **Redis** | `github.com/redis/go-redis/v9` | Full Redis client, pipelining, pub/sub |
|
||||||
|
| **S3 (MinIO/Ceph/AWS)** | `github.com/minio/minio-go/v7` | Native S3-compatible client. Works with MinIO, Ceph RGW, AWS S3, any S3-compatible backend out of the box. Lighter than aws-sdk-go-v2, purpose-built for S3 compat |
|
||||||
|
| **DB migrations** | `github.com/golang-migrate/migrate/v4` | SQL file-based migrations, CLI + library |
|
||||||
|
| **Prometheus** | `github.com/prometheus/client_golang` | Counters, gauges, histograms |
|
||||||
|
| **TUI** | `github.com/charmbracelet/bubbletea` | Elm-architecture TUI framework |
|
||||||
|
| **TUI styling** | `github.com/charmbracelet/lipgloss` | Terminal styling |
|
||||||
|
| **TUI components** | `github.com/charmbracelet/bubbles` | Table, text input, spinner, etc. |
|
||||||
|
| **Structured logging** | `log/slog` (stdlib) | Go 1.21+ structured logging, zero dependencies |
|
||||||
|
| **Testing** | `github.com/stretchr/testify` | Assertions + require for unit tests |
|
||||||
|
| **Test containers** | `github.com/testcontainers/testcontainers-go` | Spin up Postgres/Redis/MinIO in e2e tests |
|
||||||
|
|
||||||
|
### S3 Client: Multi-Backend Support
|
||||||
|
|
||||||
|
Using `minio-go/v7` as the S3 client because it natively supports:
|
||||||
|
- **MinIO** — primary development/production target
|
||||||
|
- **Ceph RGW** — S3-compatible via endpoint config
|
||||||
|
- **AWS S3** — via region + credential config
|
||||||
|
- **Any S3-compatible** — GCS (interop mode), Wasabi, DigitalOcean Spaces, etc.
|
||||||
|
|
||||||
|
No abstraction layer needed — `minio-go` handles endpoint differences internally. Config:
|
||||||
|
```go
|
||||||
|
client, _ := minio.New(endpoint, &minio.Options{
|
||||||
|
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||||
|
Secure: useTLS,
|
||||||
|
Region: region, // optional, for AWS
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Data Layer
|
||||||
|
|
||||||
|
### PostgreSQL Schema
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- Remotes: managed exclusively by Terraform
|
||||||
|
CREATE TABLE remotes (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
package_type TEXT NOT NULL, -- generic, docker, helm, pypi, npm, rpm, alpine, puppet, terraform, goproxy
|
||||||
|
base_url TEXT NOT NULL,
|
||||||
|
description TEXT DEFAULT '',
|
||||||
|
username TEXT DEFAULT '',
|
||||||
|
password TEXT DEFAULT '',
|
||||||
|
immutable_ttl INTEGER DEFAULT 0,
|
||||||
|
mutable_ttl INTEGER DEFAULT 3600,
|
||||||
|
check_mutable BOOLEAN DEFAULT TRUE,
|
||||||
|
immutable_patterns TEXT[] DEFAULT '{}', -- user-defined immutable patterns
|
||||||
|
mutable_patterns TEXT[] DEFAULT '{}', -- user-defined mutable patterns (merged with provider built-ins)
|
||||||
|
allowlist TEXT[] DEFAULT '{}', -- if empty, allow all paths; if non-empty, only matching paths proxied
|
||||||
|
blocklist TEXT[] DEFAULT '{}', -- always denied, checked before allowlist
|
||||||
|
ban_tags_enabled BOOLEAN DEFAULT FALSE,
|
||||||
|
ban_tags TEXT[] DEFAULT '{}',
|
||||||
|
quarantine_enabled BOOLEAN DEFAULT FALSE,
|
||||||
|
quarantine_days INTEGER DEFAULT 3,
|
||||||
|
stale_on_error BOOLEAN DEFAULT TRUE,
|
||||||
|
releases_remote TEXT DEFAULT '', -- terraform type: name of CDN remote for download URL rewriting
|
||||||
|
managed_by TEXT DEFAULT '', -- 'terraform' or empty
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Virtual repositories
|
||||||
|
CREATE TABLE virtuals (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
package_type TEXT NOT NULL,
|
||||||
|
description TEXT DEFAULT '',
|
||||||
|
members TEXT[] NOT NULL,
|
||||||
|
managed_by TEXT DEFAULT '',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Content-addressable blob storage tracking
|
||||||
|
CREATE TABLE blobs (
|
||||||
|
content_hash TEXT PRIMARY KEY,
|
||||||
|
s3_key TEXT NOT NULL,
|
||||||
|
size_bytes BIGINT NOT NULL,
|
||||||
|
content_type TEXT DEFAULT 'application/octet-stream',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Artifact metadata: maps (remote, path) → content blob
|
||||||
|
CREATE TABLE artifacts (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
remote_name TEXT NOT NULL REFERENCES remotes(name) ON DELETE CASCADE,
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
content_hash TEXT NOT NULL REFERENCES blobs(content_hash),
|
||||||
|
upstream_etag TEXT DEFAULT '',
|
||||||
|
upstream_last_modified TIMESTAMPTZ,
|
||||||
|
first_seen_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
last_fetched_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
last_accessed_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
fetch_count BIGINT DEFAULT 1,
|
||||||
|
access_count BIGINT DEFAULT 1,
|
||||||
|
UNIQUE(remote_name, path)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_artifacts_remote ON artifacts(remote_name);
|
||||||
|
CREATE INDEX idx_artifacts_last_accessed ON artifacts(last_accessed_at);
|
||||||
|
|
||||||
|
-- Local file uploads
|
||||||
|
CREATE TABLE local_files (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
repo_name TEXT NOT NULL,
|
||||||
|
file_path TEXT NOT NULL,
|
||||||
|
content_hash TEXT NOT NULL REFERENCES blobs(content_hash),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
UNIQUE(repo_name, file_path)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Access log (append-only, powers dashboards)
|
||||||
|
CREATE TABLE access_log (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
remote_name TEXT NOT NULL,
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
cache_hit BOOLEAN NOT NULL,
|
||||||
|
size_bytes BIGINT DEFAULT 0,
|
||||||
|
upstream_ms INTEGER DEFAULT 0,
|
||||||
|
client_ip TEXT DEFAULT '',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_access_log_remote_time ON access_log(remote_name, created_at);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Redis Usage (Ephemeral Only)
|
||||||
|
|
||||||
|
| Key pattern | Type | TTL | Purpose |
|
||||||
|
|---|---|---|---|
|
||||||
|
| `ttl:{remote}:{path}` | STRING | remote's immutable/mutable TTL | Artifact freshness — existence = still fresh |
|
||||||
|
| `lock:{remote}:{path}` | STRING (NX) | 30s | Fetch lock — prevents thundering herd |
|
||||||
|
| `etag:{remote}:{path}` | STRING | same as TTL key | Cached ETag for conditional revalidation |
|
||||||
|
| `circuit:{remote}` | STRING | configurable | Circuit breaker — consecutive failure count |
|
||||||
|
|
||||||
|
Losing Redis = all TTLs expire = next request re-validates upstream. No data loss.
|
||||||
|
|
||||||
|
### S3 Layout (Content-Addressable)
|
||||||
|
|
||||||
|
```
|
||||||
|
artifacts-bucket/
|
||||||
|
├── blobs/sha256/{content_hash} # immutable CAS blobs
|
||||||
|
├── indexes/{remote}/{path} # mutable index files (helm, pypi, rpm, etc.)
|
||||||
|
├── indexes/{virtual}/{path} # merged virtual indexes
|
||||||
|
└── local/{repo}/{path} # user uploads (CAS-backed via blobs table)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Terraform Remote Type (New in v2)
|
||||||
|
|
||||||
|
The `terraform` package type proxies the Terraform Provider Registry Protocol:
|
||||||
|
|
||||||
|
- **URL construction**: prepends `/v1/providers/` to request paths
|
||||||
|
- **Built-in mutable pattern**: `[^/]+/[^/]+/versions$` (version listings change over time)
|
||||||
|
- **Built-in immutable pattern**: `[^/]+/[^/]+/[^/]+/download/[^/]+/[^/]+$` (per-version download info is fixed)
|
||||||
|
- **Response rewriting**: download info JSON — rewrites `download_url`, `shasums_url`, `shasums_signature_url` to route through a companion `releases_remote` (e.g., `hashicorp-releases` generic remote)
|
||||||
|
- **Config**: requires `releases_remote` field pointing to the CDN remote that serves the actual binaries
|
||||||
|
|
||||||
|
Uses `github.com/hashicorp/terraform-registry-address` for address parsing and protocol-compliant URL generation.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Go Module Proxy Remote Type (New)
|
||||||
|
|
||||||
|
The `goproxy` package type implements the GOPROXY protocol (Go module proxy):
|
||||||
|
|
||||||
|
| Endpoint | Mutability | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `{module}/@v/list` | Mutable | Plain text list of known versions |
|
||||||
|
| `{module}/@latest` | Mutable | JSON metadata for latest version |
|
||||||
|
| `{module}/@v/{version}.info` | Immutable | JSON version metadata (`Version`, `Time`) |
|
||||||
|
| `{module}/@v/{version}.mod` | Immutable | `go.mod` file for that version |
|
||||||
|
| `{module}/@v/{version}.zip` | Immutable | Source archive for that version |
|
||||||
|
|
||||||
|
- **No URL rewriting needed** — responses are self-contained (no embedded URLs)
|
||||||
|
- **Config**: `base_url` points to upstream proxy (e.g., `https://proxy.golang.org`)
|
||||||
|
- **Client usage**: set `GOPROXY=https://artifactapi.example.com/api/v1/remote/goproxy`
|
||||||
|
- Uses `github.com/goproxy/goproxy` for protocol handling
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Allowlist / Blocklist / Automatic Mutable Patterns
|
||||||
|
|
||||||
|
### Access Control (Per-Remote)
|
||||||
|
|
||||||
|
| Field | Default | Behavior |
|
||||||
|
|---|---|---|
|
||||||
|
| `blocklist` | `[]` (empty) | If a path matches any blocklist pattern → **403 Forbidden**. Checked first |
|
||||||
|
| `allowlist` | `[]` (empty) | If empty → **allow everything**. If non-empty → only matching paths are proxied; everything else → **403** |
|
||||||
|
|
||||||
|
Evaluation order: blocklist → allowlist → proxy. No allowlist + no blocklist = open proxy (default).
|
||||||
|
|
||||||
|
### Automatic Mutable Patterns (Per-Provider Built-ins)
|
||||||
|
|
||||||
|
Each provider declares built-in mutable patterns that are **always merged** with user-defined `mutable_patterns`. Users never need to configure these — the provider knows which paths change over time.
|
||||||
|
|
||||||
|
| Provider | Built-in Mutable Patterns | Rationale |
|
||||||
|
|---|---|---|
|
||||||
|
| **generic** | *(none)* | No convention for what's mutable |
|
||||||
|
| **docker** | `/manifests/(?!sha256:)[^/]+$`, `/tags/list$` | Tag manifests change; digest manifests don't |
|
||||||
|
| **helm** | `index\.yaml$` | Chart index changes when new charts are published |
|
||||||
|
| **pypi** | `simple/` | Package index pages change with new releases |
|
||||||
|
| **npm** | `^[^/]+$` (package metadata, not `.tgz`) | Package metadata changes; tarballs are immutable |
|
||||||
|
| **rpm** | `repomd\.xml$`, `repodata/.*`, `Packages\.gz$` | Repo metadata rebuilt on every publish |
|
||||||
|
| **alpine** | `APKINDEX\.tar\.gz$` | Package index rebuilt on every publish |
|
||||||
|
| **puppet** | `^v3/modules/`, `^v3/releases` | Module metadata changes with new releases |
|
||||||
|
| **terraform** | `[^/]+/[^/]+/versions$` | Provider version listings grow over time |
|
||||||
|
| **goproxy** | `@v/list$`, `@latest$` | Version list and latest pointer change |
|
||||||
|
|
||||||
|
These are returned by `Provider.BuiltinMutablePatterns()` and merged at classification time:
|
||||||
|
```
|
||||||
|
effective_mutable = provider.BuiltinMutablePatterns() ∪ remote.mutable_patterns
|
||||||
|
```
|
||||||
|
|
||||||
|
If a path matches `effective_mutable` → use `mutable_ttl`. If it matches `remote.immutable_patterns` → use `immutable_ttl`. Immutable patterns take precedence over mutable when both match.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API Design
|
||||||
|
|
||||||
|
### v1 Proxy Endpoints (Backwards Compatible)
|
||||||
|
|
||||||
|
| Method | Path | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `GET` | `/api/v1/remote/{name}/{path}` | Proxy/cache artifact |
|
||||||
|
| `GET` | `/api/v1/virtual/{name}/{path}` | Virtual repo proxy |
|
||||||
|
| `GET/HEAD` | `/v2/{name}/{path}` | Docker Registry v2 |
|
||||||
|
| `GET` | `/v2/` | Docker v2 ping |
|
||||||
|
| `GET/PUT/HEAD/DELETE` | `/api/v1/local/{name}/{path}` | Local repo CRUD |
|
||||||
|
|
||||||
|
### v2 Management API (New)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v2/remotes → [{name, package_type, base_url, description, stats}]
|
||||||
|
GET /api/v2/remotes/{name} → {full config + stats + health}
|
||||||
|
POST /api/v2/remotes → create remote (Terraform provider)
|
||||||
|
PUT /api/v2/remotes/{name} → update remote (Terraform provider)
|
||||||
|
DELETE /api/v2/remotes/{name} → delete remote — cascades artifacts, GC cleans S3
|
||||||
|
|
||||||
|
GET /api/v2/virtuals → [{name, package_type, members, stats}]
|
||||||
|
GET /api/v2/virtuals/{name} → {full config + member details}
|
||||||
|
POST /api/v2/virtuals → create virtual
|
||||||
|
PUT /api/v2/virtuals/{name} → update virtual
|
||||||
|
DELETE /api/v2/virtuals/{name} → delete virtual
|
||||||
|
|
||||||
|
GET /api/v2/remotes/{name}/objects → paginated objects
|
||||||
|
?q=pattern&sort=size|accessed|age&page=1&per_page=50
|
||||||
|
DELETE /api/v2/remotes/{name}/objects/{path} → evict specific cached object
|
||||||
|
DELETE /api/v2/remotes/{name}/cache → flush cache
|
||||||
|
?type=all|indexes|blobs
|
||||||
|
|
||||||
|
GET /api/v2/stats → overview stats
|
||||||
|
GET /api/v2/stats/top-remotes → top remotes by size/requests/hit-rate
|
||||||
|
|
||||||
|
GET /api/v2/health → {status, postgres, redis, s3, uptime}
|
||||||
|
GET /metrics → Prometheus format
|
||||||
|
GET /api/v2/events → SSE stream
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Proxy Engine
|
||||||
|
|
||||||
|
### Request Flow
|
||||||
|
|
||||||
|
```
|
||||||
|
Client Request
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
Classify (immutable/mutable/denied)
|
||||||
|
│
|
||||||
|
├── blocklist match → 403
|
||||||
|
├── allowlist non-empty + no match → 403
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
Check Redis TTL key
|
||||||
|
│
|
||||||
|
├── exists (fresh) → serve from S3, log access
|
||||||
|
│
|
||||||
|
├── missing (expired or uncached)
|
||||||
|
│ │
|
||||||
|
│ ▼
|
||||||
|
│ Acquire fetch lock (Redis SETNX, 30s TTL)
|
||||||
|
│ │
|
||||||
|
│ ├── lock acquired
|
||||||
|
│ │ ├── mutable + check_mutable + have ETag → HEAD upstream
|
||||||
|
│ │ │ ├── 304 → refresh TTL, serve from S3
|
||||||
|
│ │ │ └── changed → full fetch
|
||||||
|
│ │ └── full fetch from upstream
|
||||||
|
│ │ → provider.RewriteResponse() if needed
|
||||||
|
│ │ → CAS store (hash → check blobs → upload if new)
|
||||||
|
│ │ → upsert artifact in Postgres
|
||||||
|
│ │ → set Redis TTL + release lock
|
||||||
|
│ │ → on upstream error + stale_on_error → refresh TTL, serve stale
|
||||||
|
│ │
|
||||||
|
│ └── lock not acquired → poll S3 briefly, serve if another pod fetched it
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
Stream response from S3, log access
|
||||||
|
```
|
||||||
|
|
||||||
|
### Circuit Breaker
|
||||||
|
|
||||||
|
Per-remote, tracked in Redis. Closed → Open (after N failures) → Half-open (after cooldown). Exposed via `GET /api/v2/remotes/{name}` health field.
|
||||||
|
|
||||||
|
### Content-Addressable Storage
|
||||||
|
|
||||||
|
1. Stream upstream → temp file, compute SHA256 inline
|
||||||
|
2. Check `blobs` table for hash
|
||||||
|
3. Exists → skip S3 upload, upsert `artifacts` row only
|
||||||
|
4. New → upload to `blobs/sha256/{hash}`, insert both rows
|
||||||
|
|
||||||
|
### Garbage Collection
|
||||||
|
|
||||||
|
Background goroutine (configurable interval, default 1h):
|
||||||
|
1. Orphaned blobs: delete S3 objects whose `content_hash` has no referencing `artifacts` or `local_files` rows
|
||||||
|
2. Cold artifacts: optional per-remote, delete artifacts not accessed in N days
|
||||||
|
3. Remote deletion: `ON DELETE CASCADE` handles Postgres; GC sweeps orphaned blobs
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Package Providers
|
||||||
|
|
||||||
|
### Provider Interface
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Provider interface {
|
||||||
|
Type() models.PackageType
|
||||||
|
BuiltinMutablePatterns() []*regexp.Regexp
|
||||||
|
BuiltinImmutablePatterns() []*regexp.Regexp
|
||||||
|
ContentType(path string) string
|
||||||
|
UpstreamURL(remote models.Remote, path string) string
|
||||||
|
RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error)
|
||||||
|
AuthHeaders(ctx context.Context, remote models.Remote) (http.Header, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndexMerger interface {
|
||||||
|
MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Provider Registry
|
||||||
|
|
||||||
|
```go
|
||||||
|
var registry = map[models.PackageType]Provider{
|
||||||
|
models.PackageGeneric: &generic.Provider{},
|
||||||
|
models.PackageDocker: &docker.Provider{},
|
||||||
|
models.PackageHelm: &helm.Provider{},
|
||||||
|
models.PackagePyPI: &pypi.Provider{},
|
||||||
|
models.PackageNPM: &npm.Provider{},
|
||||||
|
models.PackageRPM: &rpm.Provider{},
|
||||||
|
models.PackageAlpine: &alpine.Provider{},
|
||||||
|
models.PackagePuppet: &puppet.Provider{},
|
||||||
|
models.PackageTerraform: &terraform.Provider{},
|
||||||
|
models.PackageGoProxy: &goproxy.Provider{},
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(t models.PackageType) (Provider, error) { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
Each provider lives in its own subpackage under `internal/provider/` with its own `_test.go`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing Strategy
|
||||||
|
|
||||||
|
### Unit Tests
|
||||||
|
|
||||||
|
Every package gets `_test.go` files alongside the source. Run with `go test ./...`.
|
||||||
|
|
||||||
|
| Package | What's Tested |
|
||||||
|
|---|---|
|
||||||
|
| `internal/provider/docker/` | Auth token parsing/caching, manifest classification, tag banning, URL construction, blob key generation |
|
||||||
|
| `internal/provider/helm/` | `index.yaml` parsing (using `helm.sh/helm/v3/pkg/repo`), URL rewriting, index merging |
|
||||||
|
| `internal/provider/pypi/` | Simple index HTML parsing, URL rewriting, index merging |
|
||||||
|
| `internal/provider/npm/` | Metadata JSON rewriting (`dist.tarball` URLs) |
|
||||||
|
| `internal/provider/terraform/` | Registry URL construction, download info JSON rewriting, `releases_remote` URL extraction |
|
||||||
|
| `internal/provider/rpm/` | Mutable pattern matching (repodata) |
|
||||||
|
| `internal/provider/alpine/` | Mutable pattern matching (APKINDEX) |
|
||||||
|
| `internal/provider/puppet/` | `file_uri` JSON rewriting |
|
||||||
|
| `internal/proxy/` | Classifier (immutable vs mutable vs denied), circuit breaker state transitions, revalidator logic |
|
||||||
|
| `internal/storage/` | CAS key generation, dedup detection, S3 operation mocking |
|
||||||
|
| `internal/cache/` | Redis TTL set/check, fetch lock acquire/release/contention |
|
||||||
|
| `internal/gc/` | Orphan detection queries, cold artifact selection |
|
||||||
|
| `pkg/models/` | Model validation, PackageType enum |
|
||||||
|
| `pkg/client/` | API client request/response serialization |
|
||||||
|
|
||||||
|
### End-to-End Tests
|
||||||
|
|
||||||
|
Located in `e2e/`. Use `testcontainers-go` to spin up real Postgres, Redis, and MinIO containers. The test binary starts the actual `artifactapi` server against these backends.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// e2e/e2e_test.go
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
// Start postgres, redis, minio via testcontainers-go
|
||||||
|
// Run migrations
|
||||||
|
// Start artifactapi server on random port
|
||||||
|
// Run tests
|
||||||
|
// Tear down
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
| Test File | What's Tested |
|
||||||
|
|---|---|
|
||||||
|
| `e2e/proxy_test.go` | Proxy a real GitHub release through generic remote, verify S3 storage, verify Redis TTL, verify Postgres artifact row, verify cache hit on second request |
|
||||||
|
| `e2e/docker_test.go` | Pull a real image manifest + blob through Docker v2 proxy, verify blob deduplication, tag banning |
|
||||||
|
| `e2e/management_test.go` | Full CRUD lifecycle: create remote via v2 API, proxy through it, list objects, evict object, flush cache, delete remote |
|
||||||
|
| `e2e/virtual_test.go` | Create two helm remotes + virtual, fetch merged index, verify priority ordering |
|
||||||
|
| `e2e/terraform_test.go` | Proxy terraform provider version listing + download info, verify URL rewriting to releases_remote |
|
||||||
|
| `e2e/goproxy_test.go` | Proxy Go module `@v/list`, `.info`, `.mod`, `.zip` through GOPROXY remote, verify mutable vs immutable classification |
|
||||||
|
| `e2e/gc_test.go` | Create artifact, delete remote, trigger GC, verify S3 blob cleaned up |
|
||||||
|
|
||||||
|
### Code Quality
|
||||||
|
|
||||||
|
- `gofmt` / `goimports` — enforced in CI, run on save
|
||||||
|
- `golangci-lint` — comprehensive linter suite (staticcheck, errcheck, govet, etc.)
|
||||||
|
- `go vet ./...` — run in CI
|
||||||
|
- Makefile targets: `make test`, `make lint`, `make e2e`, `make fmt`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Terraform Provider (Separate Repo)
|
||||||
|
|
||||||
|
**Repo**: `terraform-provider-artifactapi`
|
||||||
|
**Uses**: `pkg/client/` and `pkg/models/` from the main module
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provider "artifactapi" {
|
||||||
|
endpoint = "https://artifactapi.k8s.syd1.au.unkin.net"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "artifactapi_remote" "terraform_registry" {
|
||||||
|
name = "terraform-registry"
|
||||||
|
package_type = "terraform"
|
||||||
|
base_url = "https://registry.terraform.io"
|
||||||
|
description = "Terraform provider registry"
|
||||||
|
releases_remote = artifactapi_remote.hashicorp_releases.name
|
||||||
|
|
||||||
|
immutable_patterns = [
|
||||||
|
"[^/]+/[^/]+/[^/]+/download/[^/]+/[^/]+$",
|
||||||
|
]
|
||||||
|
|
||||||
|
cache {
|
||||||
|
immutable_ttl = 0
|
||||||
|
mutable_ttl = 300
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "artifactapi_remote" "hashicorp_releases" {
|
||||||
|
name = "hashicorp-releases"
|
||||||
|
package_type = "generic"
|
||||||
|
base_url = "https://releases.hashicorp.com"
|
||||||
|
|
||||||
|
immutable_patterns = [
|
||||||
|
".*\\.zip$",
|
||||||
|
".*SHA256SUMS(\\.sig)?$",
|
||||||
|
]
|
||||||
|
|
||||||
|
cache {
|
||||||
|
immutable_ttl = 0
|
||||||
|
mutable_ttl = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "artifactapi_virtual" "helm" {
|
||||||
|
name = "helm"
|
||||||
|
package_type = "helm"
|
||||||
|
description = "All helm repos merged"
|
||||||
|
members = [
|
||||||
|
artifactapi_remote.jetstack.name,
|
||||||
|
artifactapi_remote.hashicorp_helm.name,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Web UI (React + Vite — Separate Container)
|
||||||
|
|
||||||
|
### Deployment
|
||||||
|
|
||||||
|
Separate `Dockerfile.ui`: multi-stage build (node → nginx). Served as its own container/pod. nginx proxies `/api/*` to the Go backend.
|
||||||
|
|
||||||
|
### Pages
|
||||||
|
|
||||||
|
| Route | Content |
|
||||||
|
|---|---|
|
||||||
|
| `/` | Dashboard: total objects, storage used, dedup savings, bandwidth saved, top remotes chart, live SSE event feed, health indicators |
|
||||||
|
| `/remotes` | Remote table: name, type, description, object count, size, hit rate, health. Filter by type, sort any column |
|
||||||
|
| `/remotes/:name` | Config (read-only, "Managed by Terraform" badge), stats, object browser with search/sort/evict, flush actions |
|
||||||
|
| `/virtuals` | Virtual table: name, type, members, merged object count |
|
||||||
|
| `/virtuals/:name` | Member list with individual stats |
|
||||||
|
|
||||||
|
All config is read-only — managed by Terraform.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## TUI (Bubble Tea — Subcommand)
|
||||||
|
|
||||||
|
`artifactapi tui --endpoint http://localhost:8000` or via `ARTIFACTAPI_ENDPOINT` env.
|
||||||
|
|
||||||
|
Uses `pkg/client/` for all API calls (same client as Terraform provider).
|
||||||
|
|
||||||
|
| View | Key bindings |
|
||||||
|
|---|---|
|
||||||
|
| Dashboard | summary stats, top remotes |
|
||||||
|
| Remotes list | `j`/`k` navigate, `/` filter, `Enter` detail |
|
||||||
|
| Remote detail | config + stats, `Enter` → object browser |
|
||||||
|
| Object browser | `/` search, `d` evict, `f` flush |
|
||||||
|
| Virtuals | `j`/`k`, `Enter` detail |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Improvements Over v2
|
||||||
|
|
||||||
|
| Area | v2 (Python) | v3 (Go) |
|
||||||
|
|---|---|---|
|
||||||
|
| S3 paths | Hashed, opaque | Content-addressed CAS |
|
||||||
|
| Config | YAML files, mtime reload | Terraform via API |
|
||||||
|
| Package types | 8 types | 10 types (+ terraform, goproxy) |
|
||||||
|
| Virtual repos | Helm only | Helm + PyPI, extensible |
|
||||||
|
| Deduplication | Docker blobs only | All types via CAS |
|
||||||
|
| Revalidation | Opt-in flag | Default for all mutable |
|
||||||
|
| Access logging | None | Per-artifact in Postgres |
|
||||||
|
| GC | None | Background goroutine |
|
||||||
|
| Upstream health | Per-request | Circuit breaker |
|
||||||
|
| S3 backends | MinIO only | MinIO, Ceph, AWS (minio-go) |
|
||||||
|
| UI | None | Web dashboard + TUI |
|
||||||
|
| Binary | Python + venv | Static Go binary |
|
||||||
|
| Frontend | N/A | Separate container (React) |
|
||||||
|
| Testing | Mocked unit tests | Unit + e2e with real backends |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Implementation Phases
|
||||||
|
|
||||||
|
### Phase 1: Core Engine + Models
|
||||||
|
- Go module, Makefile (`make build test lint fmt e2e`), Dockerfile, docker-compose
|
||||||
|
- `pkg/models/` — all domain types
|
||||||
|
- PostgreSQL schema + migrations
|
||||||
|
- S3 storage layer with CAS (`minio-go/v7`)
|
||||||
|
- Redis cache layer (TTL, locks)
|
||||||
|
- Proxy engine: fetch-or-cache, classifier, revalidator
|
||||||
|
- Generic + Docker providers (most complexity: OCI auth, CAS, tag banning)
|
||||||
|
- Health + metrics endpoints
|
||||||
|
- Unit tests for all packages
|
||||||
|
- **Milestone**: proxy Docker + generic, cache in S3, track in Postgres
|
||||||
|
|
||||||
|
### Phase 2: All Providers
|
||||||
|
- Helm (using `helm.sh/helm/v3/pkg/repo`)
|
||||||
|
- PyPI (stdlib `x/net/html`)
|
||||||
|
- npm (stdlib `encoding/json`)
|
||||||
|
- RPM (using `rs3.io/go/rpm/repomd`)
|
||||||
|
- Alpine (using `gitlab.alpinelinux.org/alpine/go`)
|
||||||
|
- Puppet Forge (stdlib `encoding/json`)
|
||||||
|
- Terraform (using `hashicorp/terraform-registry-address`)
|
||||||
|
- Go Modules / GOPROXY (using `github.com/goproxy/goproxy`)
|
||||||
|
- Unit tests per provider
|
||||||
|
- **Milestone**: feature parity with v2 + goproxy
|
||||||
|
|
||||||
|
### Phase 3: Management API + Virtual Repos + GC
|
||||||
|
- `pkg/client/` — shared Go API client
|
||||||
|
- v2 CRUD endpoints
|
||||||
|
- Virtual repo engine: `IndexMerger` for Helm + PyPI
|
||||||
|
- Circuit breaker
|
||||||
|
- Access logging middleware
|
||||||
|
- GC goroutine
|
||||||
|
- **Milestone**: full API, virtuals, GC
|
||||||
|
|
||||||
|
### Phase 4: End-to-End Tests
|
||||||
|
- `e2e/` test suite with `testcontainers-go`
|
||||||
|
- Proxy, Docker, management, virtual, terraform, GC tests
|
||||||
|
- CI pipeline: `make e2e`
|
||||||
|
- **Milestone**: comprehensive e2e coverage
|
||||||
|
|
||||||
|
### Phase 5: Terraform Provider
|
||||||
|
- Separate repo: `terraform-provider-artifactapi`
|
||||||
|
- Imports `pkg/client/` and `pkg/models/`
|
||||||
|
- `artifactapi_remote` + `artifactapi_virtual` resources + data sources
|
||||||
|
- Import support
|
||||||
|
- **Milestone**: manage all config via Terraform
|
||||||
|
|
||||||
|
### Phase 6: Web UI
|
||||||
|
- React + Vite in `ui/`
|
||||||
|
- `Dockerfile.ui` (multi-stage → nginx)
|
||||||
|
- Dashboard, remotes, objects, virtuals pages
|
||||||
|
- SSE event feed
|
||||||
|
- **Milestone**: full web UI in separate container
|
||||||
|
|
||||||
|
### Phase 7: TUI
|
||||||
|
- Bubble Tea in `internal/tui/`
|
||||||
|
- Uses `pkg/client/`
|
||||||
|
- Dashboard, remotes, objects, virtuals views
|
||||||
|
- **Milestone**: TUI feature parity with web UI
|
||||||
|
|
||||||
|
### Phase 8: Migration + Cutover
|
||||||
|
- Migration tool: v2 YAML → Terraform HCL + `terraform import` commands
|
||||||
|
- S3 rehash script: `{remote}/{hash16}/{file}` → `blobs/sha256/{content_hash}`
|
||||||
|
- Parallel run, response comparison
|
||||||
|
- Cutover
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Makefile Targets
|
||||||
|
|
||||||
|
```makefile
|
||||||
|
.PHONY: build test lint fmt e2e docker docker-ui
|
||||||
|
|
||||||
|
build: ## Build Go binary
|
||||||
|
go build -o bin/artifactapi ./cmd/artifactapi
|
||||||
|
|
||||||
|
test: ## Run unit tests
|
||||||
|
go test ./...
|
||||||
|
|
||||||
|
lint: ## Run golangci-lint + go vet
|
||||||
|
golangci-lint run ./...
|
||||||
|
go vet ./...
|
||||||
|
|
||||||
|
fmt: ## Format code (gofmt + goimports)
|
||||||
|
gofmt -w .
|
||||||
|
goimports -w .
|
||||||
|
|
||||||
|
e2e: ## Run end-to-end tests (requires Docker)
|
||||||
|
go test -tags=e2e -count=1 -timeout=5m ./e2e/...
|
||||||
|
|
||||||
|
docker: ## Build API server Docker image
|
||||||
|
docker build -t artifactapi .
|
||||||
|
|
||||||
|
docker-ui: ## Build frontend Docker image
|
||||||
|
docker build -t artifactapi-ui -f ui/Dockerfile.ui ui/
|
||||||
|
|
||||||
|
compose: ## Start full stack (API + UI + Postgres + Redis + MinIO)
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
@@ -1,582 +1,167 @@
|
|||||||
# Artifact Storage System
|
# ArtifactAPI
|
||||||
|
|
||||||
FastAPI caching proxy that downloads and stores files from remote sources in S3-compatible storage.
|
Caching proxy for package repositories. Single Go binary, 10 package types, content-addressable storage, managed by Terraform.
|
||||||
|
|
||||||
## Features
|
## Quick Start
|
||||||
|
|
||||||
- Remote definitions via `remotes.yaml` — generic HTTP, Alpine APK, RPM, Docker, PyPI, npm, Helm, Puppet Forge, Terraform/OpenTofu registry
|
```bash
|
||||||
- Virtual repositories — merge multiple remotes of the same package type into a single unified index
|
# Start backing services
|
||||||
- Immutable/mutable caching model with per-remote TTLs
|
docker compose up -d postgres redis minio
|
||||||
- Conditional revalidation (`If-None-Match` / `If-Modified-Since`) on TTL expiry
|
|
||||||
- Stale-on-upstream-error: refreshes TTL when backend is unreachable rather than evicting
|
# Build and run
|
||||||
- URL rewriting for PyPI simple index, npm metadata, and Helm `index.yaml`
|
make build
|
||||||
- Access control via regex patterns — unmatched paths return 403
|
./bin/artifactapi
|
||||||
- Docker tag banning — block named tags (e.g. `latest`) while allowing digest pulls
|
|
||||||
|
# Frontend (separate container or dev server)
|
||||||
|
cd ui && npm install && npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
API: `http://localhost:8000` | Frontend: `http://localhost:5173`
|
||||||
|
|
||||||
|
## Package Types
|
||||||
|
|
||||||
|
| Type | Mutable (auto-detected) | Immutable (auto-detected) |
|
||||||
|
|---|---|---|
|
||||||
|
| `generic` | nothing | everything |
|
||||||
|
| `docker` | tag manifests, `/tags/list` | blobs, digest manifests |
|
||||||
|
| `helm` | `index.yaml` | `.tgz` charts |
|
||||||
|
| `pypi` | `simple/*` index pages | `.whl`, `.tar.gz` |
|
||||||
|
| `npm` | package metadata | `.tgz` tarballs |
|
||||||
|
| `rpm` | `repomd.xml`, `repodata/*` | `.rpm` |
|
||||||
|
| `alpine` | `APKINDEX.tar.gz` | `.apk` |
|
||||||
|
| `puppet` | `v3/modules/*`, `v3/releases*` | `.tar.gz` |
|
||||||
|
| `terraform` | `*/versions` | `*/download/*/*` |
|
||||||
|
| `goproxy` | `@v/list`, `@latest` | `.info`, `.mod`, `.zip` |
|
||||||
|
|
||||||
|
Providers classify paths automatically. Users only configure what to proxy and TTLs.
|
||||||
|
|
||||||
|
## Terraform
|
||||||
|
|
||||||
|
Remotes and virtuals are managed by Terraform. Each package type has its own resource:
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
resource "artifactapi_remote_generic" "github" {
|
||||||
|
name = "github"
|
||||||
|
base_url = "https://github.com"
|
||||||
|
|
||||||
|
immutable_ttl = 0
|
||||||
|
mutable_ttl = 7200
|
||||||
|
|
||||||
|
patterns = [
|
||||||
|
"ducaale/xh/.*/xh-.*-x86_64-unknown-linux-musl.tar.gz$",
|
||||||
|
"mikefarah/yq/.*/yq_linux_amd64$",
|
||||||
|
]
|
||||||
|
|
||||||
|
mutable_patterns = [
|
||||||
|
".*/archive/refs/heads/.*\\.tar\\.gz$",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "artifactapi_remote_docker" "dockerhub" {
|
||||||
|
name = "dockerhub"
|
||||||
|
base_url = "https://registry-1.docker.io"
|
||||||
|
|
||||||
|
immutable_ttl = 0
|
||||||
|
mutable_ttl = 300
|
||||||
|
ban_tags_enabled = true
|
||||||
|
ban_tags = ["latest"]
|
||||||
|
|
||||||
|
patterns = [
|
||||||
|
"^library/postgres",
|
||||||
|
"^library/redis",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "artifactapi_remote_helm" "jetstack" {
|
||||||
|
name = "jetstack"
|
||||||
|
base_url = "https://charts.jetstack.io"
|
||||||
|
|
||||||
|
immutable_ttl = 0
|
||||||
|
mutable_ttl = 3600
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "artifactapi_virtual" "helm" {
|
||||||
|
name = "helm"
|
||||||
|
package_type = "helm"
|
||||||
|
members = [artifactapi_remote_helm.jetstack.name]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Provider: [terraform-provider-artifactapi](../terraform-provider-artifactapi)
|
||||||
|
|
||||||
|
## Access Control
|
||||||
|
|
||||||
|
| Field | Default | Behaviour |
|
||||||
|
|---|---|---|
|
||||||
|
| `patterns` | empty (proxy all) | If set, only matching paths are proxied. Acts as allowlist. |
|
||||||
|
| `blocklist` | empty | Matching paths always denied. Checked first. |
|
||||||
|
| `mutable_patterns` | empty | Override: force paths to mutable TTL. |
|
||||||
|
| `immutable_patterns` | empty | Override: force paths to immutable TTL. |
|
||||||
|
|
||||||
|
No patterns + no blocklist = open proxy. Provider handles mutability classification automatically.
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### Proxy (v1)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v1/remote/{name}/{path} Proxy/cache artifact
|
||||||
|
GET /api/v1/virtual/{name}/{path} Virtual repo (merged index)
|
||||||
|
GET /v2/{name}/{path} Docker Registry v2
|
||||||
|
```
|
||||||
|
|
||||||
|
### Management (v2)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET/POST /api/v2/remotes List / create remotes
|
||||||
|
GET/PUT/DELETE /api/v2/remotes/{name} Read / update / delete remote
|
||||||
|
GET/DELETE /api/v2/remotes/{name}/objects Browse / evict cached objects
|
||||||
|
GET /api/v2/stats Overview stats
|
||||||
|
GET /api/v2/health Service health
|
||||||
|
POST /api/v2/probe Test a remote (fetch without streaming to client)
|
||||||
|
GET /api/v2/events SSE event stream
|
||||||
|
```
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
```
|
```
|
||||||
client → /api/v1/remote/{remote}/{path}
|
PostgreSQL ─── config (remotes, virtuals), artifact metadata, access log
|
||||||
↓
|
Redis ─── TTL keys, fetch locks, circuit breaker state
|
||||||
Redis: mutable TTL check
|
S3/MinIO ─── content-addressable blob storage (blobs/sha256/{hash})
|
||||||
↓ miss / expired
|
|
||||||
S3: object exists?
|
|
||||||
↓ no
|
|
||||||
upstream remote → S3 + PostgreSQL metadata
|
|
||||||
↓
|
|
||||||
response (X-Artifact-Source: cache|remote)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Docker Registry traffic uses the `/v2/{remote}/{path}` endpoint implementing the Docker Registry HTTP API v2.
|
S3 client supports MinIO, Ceph RGW, and AWS S3 (via minio-go).
|
||||||
|
|
||||||
### Code layout
|
## Environment Variables
|
||||||
|
|
||||||
```
|
| Variable | Default | Description |
|
||||||
src/artifactapi/
|
|
||||||
├── main.py — FastAPI app + thin route declarations only
|
|
||||||
├── config.py — ConfigManager (loads remotes.yaml)
|
|
||||||
├── metrics.py — Prometheus + Redis metrics
|
|
||||||
├── docker_auth.py — backwards-compat shim → auth/docker.py
|
|
||||||
├── artifact/ — route handler implementations
|
|
||||||
│ ├── proxy.py — GET /api/v1/remote (remote proxy, cache, revalidation)
|
|
||||||
│ ├── virtual.py — GET /api/v1/virtual (virtual repo index merging)
|
|
||||||
│ ├── local.py — PUT/HEAD/DELETE /api/v1/remote (local repos)
|
|
||||||
│ ├── docker.py — /v2/ Docker Registry v2 proxy
|
|
||||||
│ ├── discovery.py — /api/v1/artifacts discovery + bulk cache
|
|
||||||
│ └── flush.py — PUT /cache/flush
|
|
||||||
├── auth/
|
|
||||||
│ ├── __init__.py — re-exports Docker auth helpers
|
|
||||||
│ └── docker.py — Bearer token fetching + in-memory cache
|
|
||||||
├── cache/
|
|
||||||
│ ├── __init__.py — re-exports RedisCache
|
|
||||||
│ └── redis.py — RedisCache (TTL keys, ETag metadata)
|
|
||||||
├── database/
|
|
||||||
│ ├── __init__.py — re-exports DatabaseManager
|
|
||||||
│ └── postgres.py — DatabaseManager (artifact + local-file tables)
|
|
||||||
├── storage/
|
|
||||||
│ ├── __init__.py — re-exports S3Storage
|
|
||||||
│ └── s3.py — S3Storage (MinIO/S3 abstraction)
|
|
||||||
└── remote/
|
|
||||||
├── __init__.py
|
|
||||||
├── base.py — content-type detection
|
|
||||||
├── generic.py — generic HTTP remotes
|
|
||||||
├── helm.py — Helm index.yaml URL rewriting
|
|
||||||
├── npm.py — npm metadata URL rewriting
|
|
||||||
├── puppet.py — Puppet Forge JSON URL rewriting
|
|
||||||
├── python.py — PyPI URL construction + HTML rewriting
|
|
||||||
├── rpm.py — RPM remotes
|
|
||||||
└── terraform.py — Terraform/OpenTofu registry URL construction + download URL rewriting
|
|
||||||
```
|
|
||||||
|
|
||||||
## API Endpoints
|
|
||||||
|
|
||||||
| Method | Path | Description |
|
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| `GET` | `/api/v1/remote/{remote}/{path}` | Fetch artifact (auto-cache on miss) |
|
| `LISTEN_ADDR` | `:8000` | Server listen address |
|
||||||
| `GET` | `/api/v1/virtual/{virtual}/{path}` | Fetch from virtual (merged) repository |
|
| `DBHOST` | `localhost` | PostgreSQL host |
|
||||||
| `GET` | `/api/v1/local/{local}/{path}` | Download from local repository |
|
| `DBPORT` | `5432` | PostgreSQL port |
|
||||||
| `PUT` | `/api/v1/local/{local}/{path}` | Upload to local repository |
|
| `DBUSER` | `artifacts` | PostgreSQL user |
|
||||||
| `HEAD` | `/api/v1/local/{local}/{path}` | Check existence (local) |
|
| `DBPASS` | | PostgreSQL password |
|
||||||
| `DELETE` | `/api/v1/local/{local}/{path}` | Delete from local repository |
|
| `DBNAME` | `artifacts` | PostgreSQL database |
|
||||||
| `GET` | `/v2/{remote}/{path}` | Docker Registry v2 proxy |
|
| `REDIS_URL` | `redis://localhost:6379` | Redis URL |
|
||||||
| `PUT` | `/cache/flush` | Flush cache entries |
|
| `MINIO_ENDPOINT` | `localhost:9000` | S3 endpoint |
|
||||||
| `GET` | `/health` | Health check |
|
| `MINIO_ACCESS_KEY` | | S3 access key |
|
||||||
| `GET` | `/config` | View loaded configuration |
|
| `MINIO_SECRET_KEY` | | S3 secret key |
|
||||||
| `GET` | `/` | API info and available remotes |
|
| `MINIO_BUCKET` | `artifacts` | S3 bucket |
|
||||||
|
| `MINIO_SECURE` | `false` | Use HTTPS for S3 |
|
||||||
|
| `MINIO_REGION` | | S3 region (AWS) |
|
||||||
|
|
||||||
## Configuration
|
## Development
|
||||||
|
|
||||||
Runtime settings come from environment variables; remote definitions live in one or more YAML files pointed to by `CONFIG_PATH`.
|
|
||||||
|
|
||||||
### Environment Variables
|
|
||||||
|
|
||||||
| Variable | Description |
|
|
||||||
|---|---|
|
|
||||||
| `CONFIG_PATH` | Path to a config YAML file **or** a directory of YAML files |
|
|
||||||
| `DBHOST`, `DBPORT`, `DBUSER`, `DBPASS`, `DBNAME` | PostgreSQL connection |
|
|
||||||
| `REDIS_URL` | Redis URL (e.g. `redis://localhost:6379`) |
|
|
||||||
| `MINIO_ENDPOINT` | MinIO/S3 endpoint |
|
|
||||||
| `MINIO_ACCESS_KEY` | S3 access key |
|
|
||||||
| `MINIO_SECRET_KEY` | S3 secret key |
|
|
||||||
| `MINIO_BUCKET` | S3 bucket name |
|
|
||||||
| `MINIO_SECURE` | Use HTTPS (`true`/`false`) |
|
|
||||||
|
|
||||||
### Split configuration
|
|
||||||
|
|
||||||
`CONFIG_PATH` accepts three forms:
|
|
||||||
|
|
||||||
**Single file** (original behaviour):
|
|
||||||
```
|
|
||||||
CONFIG_PATH=/etc/artifactapi/remotes.yaml
|
|
||||||
```
|
|
||||||
|
|
||||||
**Directory** — all `*.yaml` / `*.yml` files in the directory are loaded and merged alphabetically. `remotes` keys are merged across files; later files win on conflict:
|
|
||||||
```
|
|
||||||
CONFIG_PATH=/etc/artifactapi/conf.d/
|
|
||||||
```
|
|
||||||
|
|
||||||
**Main file + `config_dir`** — the main file holds global settings and a `config_dir` pointer; each file in that directory contributes its own `remotes`. Relative `config_dir` paths are resolved relative to the main file:
|
|
||||||
```yaml
|
|
||||||
# /etc/artifactapi/config.yaml
|
|
||||||
config_dir: conf.d # or an absolute path
|
|
||||||
|
|
||||||
# s3/redis/database settings go here (or in env vars)
|
|
||||||
remotes: {} # optional base remotes
|
|
||||||
```
|
|
||||||
|
|
||||||
### Configuration structure
|
|
||||||
|
|
||||||
Repositories are declared under three top-level keys matching their type:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes: # proxy (caching) remotes
|
|
||||||
remote-name:
|
|
||||||
base_url: "https://example.com"
|
|
||||||
package: "generic" # generic, alpine, rpm, docker, pypi, npm, helm, puppet, terraform
|
|
||||||
description: "..."
|
|
||||||
immutable_patterns: # regex — cached forever
|
|
||||||
- ".*\\.tar\\.gz$"
|
|
||||||
mutable_patterns: # regex — expire after mutable_ttl
|
|
||||||
- "index\\.yaml$"
|
|
||||||
check_mutable_updates: false # send HEAD (If-None-Match) on TTL expiry
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0 # 0 = indefinitely
|
|
||||||
mutable_ttl: 3600
|
|
||||||
|
|
||||||
virtuals: # virtual (merged-index) repositories
|
|
||||||
virtual-name:
|
|
||||||
package: "helm"
|
|
||||||
members:
|
|
||||||
- remote-a
|
|
||||||
- remote-b
|
|
||||||
|
|
||||||
locals: # local upload repositories (no base_url)
|
|
||||||
local-name:
|
|
||||||
package: "generic"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 0
|
|
||||||
```
|
|
||||||
|
|
||||||
## Remote Types
|
|
||||||
|
|
||||||
### generic
|
|
||||||
|
|
||||||
Arbitrary HTTP file servers — GitHub releases, HashiCorp, custom servers.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
github:
|
|
||||||
base_url: "https://github.com"
|
|
||||||
package: "generic"
|
|
||||||
immutable_patterns:
|
|
||||||
- "gruntwork-io/terragrunt/.*terragrunt_linux_amd64.*"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
|
|
||||||
github-archive:
|
|
||||||
base_url: "https://github.com"
|
|
||||||
package: "generic"
|
|
||||||
immutable_patterns:
|
|
||||||
- ".*/archive/refs/tags/.*\\.tar\\.gz$" # tag archives never change
|
|
||||||
mutable_patterns:
|
|
||||||
- ".*/archive/refs/heads/main\\.tar\\.gz$" # branch archives can change
|
|
||||||
check_mutable_updates: true
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 86400
|
|
||||||
```
|
|
||||||
|
|
||||||
Access: `GET /api/v1/remote/github/owner/repo/releases/download/v1.0/binary.tar.gz`
|
|
||||||
|
|
||||||
### alpine
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
alpine:
|
|
||||||
base_url: "https://dl-cdn.alpinelinux.org"
|
|
||||||
package: "alpine"
|
|
||||||
immutable_patterns:
|
|
||||||
- ".*/x86_64/.*\\.apk$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 7200
|
|
||||||
```
|
|
||||||
|
|
||||||
`APKINDEX.tar.gz` is a built-in mutable pattern — no `mutable_patterns` entry needed.
|
|
||||||
|
|
||||||
### rpm
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
almalinux:
|
|
||||||
base_url: "https://mirror.example.com/almalinux"
|
|
||||||
package: "rpm"
|
|
||||||
immutable_patterns:
|
|
||||||
- ".*/x86_64/.*\\.rpm$"
|
|
||||||
- ".*/noarch/.*\\.rpm$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 7200
|
|
||||||
```
|
|
||||||
|
|
||||||
`repomd.xml` and `repodata/` metadata files are built-in mutable patterns.
|
|
||||||
|
|
||||||
### docker
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
dockerhub:
|
|
||||||
base_url: "https://registry-1.docker.io"
|
|
||||||
package: "docker"
|
|
||||||
# username / password optional for public images
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 300
|
|
||||||
|
|
||||||
ghcr:
|
|
||||||
base_url: "https://ghcr.io"
|
|
||||||
package: "docker"
|
|
||||||
username: "your-github-username"
|
|
||||||
password: "ghp_your_pat" # read:packages scope
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 300
|
|
||||||
```
|
|
||||||
|
|
||||||
Tag manifests and `/tags/list` are built-in mutable patterns. Digest-addressed blobs are immutable.
|
|
||||||
|
|
||||||
#### Banning tags
|
|
||||||
|
|
||||||
Set `ban_tags_enabled: true` and list named tags in `ban_tags` to block specific tag references. Requests for a banned tag return `403`. Digest-addressed pulls (`sha256:…`) are never blocked, so images already in use can still be referenced by digest.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
dockerhub:
|
|
||||||
base_url: "https://registry-1.docker.io"
|
|
||||||
package: "docker"
|
|
||||||
ban_tags_enabled: true
|
|
||||||
ban_tags:
|
|
||||||
- latest # force pinned tags in CI/CD
|
|
||||||
- edge
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 300
|
|
||||||
```
|
|
||||||
|
|
||||||
`ban_tags_enabled` defaults to `false`. Setting it to `true` with an empty `ban_tags` list has no effect.
|
|
||||||
|
|
||||||
For RKE2/containerd, configure `/etc/rancher/rke2/registries.yaml`:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
mirrors:
|
|
||||||
docker.io:
|
|
||||||
endpoint:
|
|
||||||
- "https://artifacts.example.com"
|
|
||||||
rewrite:
|
|
||||||
"^(.*)$": "dockerhub/$1"
|
|
||||||
ghcr.io:
|
|
||||||
endpoint:
|
|
||||||
- "https://artifacts.example.com"
|
|
||||||
rewrite:
|
|
||||||
"^(.*)$": "ghcr/$1"
|
|
||||||
```
|
|
||||||
|
|
||||||
### pypi
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
pypi:
|
|
||||||
base_url: "https://files.pythonhosted.org"
|
|
||||||
package: "pypi"
|
|
||||||
check_mutable_updates: true
|
|
||||||
immutable_patterns:
|
|
||||||
- "packages/.*\\.whl$"
|
|
||||||
- "packages/.*\\.whl\\.metadata$"
|
|
||||||
- "packages/.*\\.tar\\.gz$"
|
|
||||||
- "packages/.*\\.zip$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 600
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Note**: Simple index requests (`/simple/{package}/`) are always fetched from `https://pypi.org`, regardless of `base_url`. This is hardcoded — `base_url` only controls where package files are downloaded from. For self-hosted registries (Gitea, Nexus) where both index and files share the same host, set `base_url` to that host and the override does not apply.
|
|
||||||
|
|
||||||
URLs in simple index HTML are rewritten to route package file downloads back through the same remote.
|
|
||||||
|
|
||||||
Configure uv:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
# /etc/uv/uv.toml or ~/.config/uv/uv.toml
|
|
||||||
[[index]]
|
|
||||||
url = "https://artifacts.example.com/api/v1/remote/pypi/simple"
|
|
||||||
default = true
|
|
||||||
```
|
|
||||||
|
|
||||||
### npm
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
npm:
|
|
||||||
base_url: "https://registry.npmjs.org"
|
|
||||||
package: "npm"
|
|
||||||
check_mutable_updates: true
|
|
||||||
immutable_patterns:
|
|
||||||
- "\.tgz$"
|
|
||||||
mutable_patterns:
|
|
||||||
- "^(?!.*\.tgz$).*"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 600
|
|
||||||
```
|
|
||||||
|
|
||||||
`dist.tarball` URLs in package metadata JSON are rewritten to route tarball downloads back through the same remote.
|
|
||||||
|
|
||||||
Configure npm / yarn / pnpm:
|
|
||||||
|
|
||||||
```ini
|
|
||||||
# .npmrc or ~/.npmrc
|
|
||||||
registry=https://artifacts.example.com/api/v1/remote/npm/
|
|
||||||
```
|
|
||||||
|
|
||||||
### helm
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
hashicorp-helm:
|
|
||||||
base_url: "https://helm.releases.hashicorp.com"
|
|
||||||
package: "helm"
|
|
||||||
check_mutable_updates: true
|
|
||||||
immutable_patterns:
|
|
||||||
- "\\.tgz$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 3600
|
|
||||||
```
|
|
||||||
|
|
||||||
`index.yaml` is a built-in mutable pattern. Chart URLs inside `index.yaml` are rewritten to route tarball downloads back through the same remote.
|
|
||||||
|
|
||||||
Configure Helm:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
helm repo add hashicorp https://artifacts.example.com/api/v1/remote/hashicorp-helm
|
make build # Build binary
|
||||||
helm repo update
|
make test # Unit tests
|
||||||
|
make e2e # E2E tests (needs Docker)
|
||||||
|
make lint # golangci-lint + go vet
|
||||||
|
make fmt # gofmt + goimports
|
||||||
```
|
```
|
||||||
|
|
||||||
### puppet
|
### TUI
|
||||||
|
|
||||||
Proxy for [Puppet Forge](https://forge.puppet.com) (forgeapi.puppet.com). Module metadata is cached as mutable; versioned module tarballs are cached as immutable.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
puppet-forge:
|
|
||||||
base_url: "https://forgeapi.puppet.com"
|
|
||||||
package: "puppet"
|
|
||||||
check_mutable_updates: true
|
|
||||||
immutable_patterns:
|
|
||||||
- "^v3/files/.*\\.tar\\.gz$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0 # module tarballs cached indefinitely
|
|
||||||
mutable_ttl: 600 # module metadata refreshed after 10 minutes
|
|
||||||
```
|
|
||||||
|
|
||||||
`v3/modules/` and `v3/releases` are built-in mutable patterns — module metadata pages expire after `mutable_ttl` and are re-fetched on the next request.
|
|
||||||
|
|
||||||
**URL rewriting**: the proxy rewrites `file_uri` fields in Forge JSON responses from relative paths (`/v3/files/…`) to absolute proxy URLs. g10k resolves download URLs with Go's `url.ResolveReference`, so an absolute `file_uri` overrides the forge base entirely — tarballs download straight from the proxy without a second hop.
|
|
||||||
|
|
||||||
**Client configuration — g10k**: set `forge_base_url` in the g10k config file:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# g10k.yaml
|
|
||||||
cachedir: /tmp/g10k
|
|
||||||
forge_base_url: https://artifacts.example.com/api/v1/remote/puppet-forge
|
|
||||||
sources:
|
|
||||||
control:
|
|
||||||
remote: git@git.example.com:puppet/control.git
|
|
||||||
basedir: /etc/puppetlabs/code/environments
|
|
||||||
```
|
|
||||||
|
|
||||||
Alternatively, set the URL per-Puppetfile with the `forge.baseUrl` directive (works with `-puppetfile` mode and does not require a config file):
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
forge.baseUrl https://artifacts.example.com/api/v1/remote/puppet-forge
|
|
||||||
|
|
||||||
mod 'puppetlabs-stdlib', '9.7.0'
|
|
||||||
mod 'puppetlabs-inifile', '6.2.0'
|
|
||||||
```
|
|
||||||
|
|
||||||
### terraform
|
|
||||||
|
|
||||||
Proxy for [Terraform](https://registry.terraform.io) / [OpenTofu](https://opentofu.org) provider registries using the [Registry Protocol](https://developer.hashicorp.com/terraform/internals/provider-registry-protocol). Provider version listings are mutable; per-version download info is immutable.
|
|
||||||
|
|
||||||
Two remotes are needed: one for the registry API and one for the release CDN (where the actual `.zip` binaries live):
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
terraform-registry:
|
|
||||||
base_url: "https://registry.terraform.io"
|
|
||||||
package: "terraform"
|
|
||||||
releases_remote: "hashicorp-releases" # name of the CDN remote below
|
|
||||||
immutable_patterns:
|
|
||||||
- "[^/]+/[^/]+/[^/]+/download/[^/]+/[^/]+$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0 # per-version download info cached indefinitely
|
|
||||||
mutable_ttl: 300 # provider version lists refreshed after 5 minutes
|
|
||||||
|
|
||||||
hashicorp-releases:
|
|
||||||
base_url: "https://releases.hashicorp.com"
|
|
||||||
package: "generic"
|
|
||||||
immutable_patterns:
|
|
||||||
- ".*\\.zip$"
|
|
||||||
- ".*SHA256SUMS(\\.sig)?$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 0
|
|
||||||
```
|
|
||||||
|
|
||||||
`{namespace}/{type}/versions` is a built-in mutable pattern — the version list expires after `mutable_ttl` and is re-fetched on the next request.
|
|
||||||
|
|
||||||
**URL rewriting**: the `download_url`, `shasums_url`, and `shasums_signature_url` fields in per-version download info JSON are rewritten from `releases.hashicorp.com` to point at the remote named by `releases_remote`, so Terraform fetches binaries through the proxy.
|
|
||||||
|
|
||||||
**Client configuration**: redirect Terraform's provider registry lookup via `.terraformrc` without changing any provider source addresses in your Terraform code:
|
|
||||||
|
|
||||||
```hcl
|
|
||||||
# ~/.terraformrc (or /etc/terraform.rc, or TF_CLI_CONFIG_FILE)
|
|
||||||
host "registry.terraform.io" {
|
|
||||||
services = {
|
|
||||||
"providers.v1" = "http://artifacts.example.com/api/v1/remote/terraform-registry/"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
With this in place, `terraform init` / `tofu init` fetches provider metadata from the proxy and downloads zips from the `hashicorp-releases` remote. No changes to `.tf` files are needed.
|
|
||||||
|
|
||||||
### virtual
|
|
||||||
|
|
||||||
A virtual repository presents a single unified index built from multiple member remotes of the same package type. Clients configure one endpoint and get access to all member remotes transparently.
|
|
||||||
|
|
||||||
All members must share the same `package` type as the virtual repo. Currently supported package types: `helm`.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
helm-hashicorp:
|
|
||||||
base_url: "https://helm.releases.hashicorp.com"
|
|
||||||
package: "helm"
|
|
||||||
immutable_patterns:
|
|
||||||
- "\\.tgz$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 3600
|
|
||||||
|
|
||||||
helm-bitnami:
|
|
||||||
base_url: "https://charts.bitnami.com/bitnami"
|
|
||||||
package: "helm"
|
|
||||||
immutable_patterns:
|
|
||||||
- "\\.tgz$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 3600
|
|
||||||
|
|
||||||
virtuals:
|
|
||||||
helm-all:
|
|
||||||
package: "helm"
|
|
||||||
members:
|
|
||||||
- helm-hashicorp # listed first = highest priority
|
|
||||||
- helm-bitnami
|
|
||||||
```
|
|
||||||
|
|
||||||
**How it works:**
|
|
||||||
|
|
||||||
1. A request for the package index triggers a parallel fetch of each member's index from S3 cache, falling back to upstream if not yet cached.
|
|
||||||
2. Member indexes are merged into a single index with URL rewriting so artifact download URLs continue to resolve through the individual member remote.
|
|
||||||
3. The merged index is cached in Redis with a TTL equal to the minimum `mutable_ttl` across all members.
|
|
||||||
|
|
||||||
**Priority / conflict resolution:**
|
|
||||||
|
|
||||||
When the same artifact name and version appears in more than one member, the member listed **first** in `members` wins. Subsequent members contribute only artifacts not already present.
|
|
||||||
|
|
||||||
**Partial failures:**
|
|
||||||
|
|
||||||
If a member is unreachable and has no cached index, it is skipped and a warning is logged. The merged index is still served from available members. If *no* members can be reached, the request returns `502`.
|
|
||||||
|
|
||||||
**Caching:**
|
|
||||||
|
|
||||||
The merged index is cached using `min(mutable_ttl)` across all members. Each member's raw index is cached in S3 under its own remote key; the virtual handler reuses those copies when available. On rebuild, each member's parsed index is also stored as a compact msgpack file (`index.msgpack`) alongside the raw YAML, eliminating the YAML parse cost on subsequent rebuilds.
|
|
||||||
|
|
||||||
**Helm example:**
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
helm repo add all https://artifacts.example.com/api/v1/virtual/helm-all
|
./bin/artifactapi tui --endpoint http://localhost:8000
|
||||||
helm repo update
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Chart tarball URLs in the merged `index.yaml` are rewritten to point at the individual member remote (e.g. `…/api/v1/remote/helm-hashicorp/vault-0.27.0.tgz`), so downloads bypass the virtual endpoint entirely.
|
|
||||||
|
|
||||||
### local
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
locals:
|
|
||||||
local-generic:
|
|
||||||
package: "generic"
|
|
||||||
description: "Local file repository"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 0
|
|
||||||
```
|
|
||||||
|
|
||||||
No `base_url`. Files are uploaded via `PUT /api/v1/local/{name}/{path}` and downloaded via `GET /api/v1/local/{name}/{path}`.
|
|
||||||
|
|
||||||
## Caching Model
|
|
||||||
|
|
||||||
### Immutable patterns
|
|
||||||
|
|
||||||
Files matching `immutable_patterns` are cached for `immutable_ttl` seconds (0 = indefinitely). Use for versioned release artifacts that never change once published.
|
|
||||||
|
|
||||||
**Access control**: only paths matching an immutable or mutable pattern are served; all others return 403. Omitting `immutable_patterns` entirely allows all paths from that remote.
|
|
||||||
|
|
||||||
### Mutable patterns
|
|
||||||
|
|
||||||
Files matching `mutable_patterns` expire after `mutable_ttl` seconds and are re-fetched on the next request. Mutable files are always served regardless of `immutable_patterns`.
|
|
||||||
|
|
||||||
Each package type has built-in defaults that are merged with any user-defined `mutable_patterns`:
|
|
||||||
|
|
||||||
| Package type | Built-in mutable patterns |
|
|
||||||
|---|---|
|
|
||||||
| `alpine` | `APKINDEX\.tar\.gz$` |
|
|
||||||
| `rpm` | `repomd\.xml$`, `repodata/` metadata variants, `Packages\.gz$` |
|
|
||||||
| `docker` | Tag manifests (non-digest refs), `/tags/list` |
|
|
||||||
| `pypi` | `simple/` (per-package and top-level index pages) |
|
|
||||||
| `helm` | `index\.yaml$` |
|
|
||||||
| `puppet` | `^v3/modules/`, `^v3/releases` |
|
|
||||||
| `terraform` | `[^/]+/[^/]+/versions$` |
|
|
||||||
| `npm` | *(none built-in — define via `mutable_patterns`)* |
|
|
||||||
| `generic` | *(none)* |
|
|
||||||
|
|
||||||
### Conditional revalidation
|
|
||||||
|
|
||||||
Set `check_mutable_updates: true` to send `HEAD` with `If-None-Match` / `If-Modified-Since` on TTL expiry. A 304 response refreshes the TTL without re-downloading. Only applies to user-defined `mutable_patterns` — built-in patterns are always re-fetched unconditionally.
|
|
||||||
|
|
||||||
### Stale-on-upstream-error
|
|
||||||
|
|
||||||
When a mutable file expires and the upstream is unreachable (connection refused, DNS failure, timeout), the cached copy is kept and its TTL refreshed. HTTP error responses (4xx, 5xx) are not treated as network failures and proceed with normal expiry.
|
|
||||||
|
|
||||||
### Quarantine (supply-chain protection)
|
|
||||||
|
|
||||||
Set `quarantine_new: true` and `quarantine_days: N` on a remote to block immutable artifacts published within the last N days. Requests return `404` until the quarantine period expires, giving time to detect malicious packages before they are consumed.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
remotes:
|
|
||||||
pypi:
|
|
||||||
base_url: "https://files.pythonhosted.org"
|
|
||||||
package: "pypi"
|
|
||||||
quarantine_new: true
|
|
||||||
quarantine_days: 3 # block packages published in the last 3 days
|
|
||||||
immutable_patterns:
|
|
||||||
- "packages/.*\\.whl$"
|
|
||||||
- "packages/.*\\.tar\\.gz$"
|
|
||||||
cache:
|
|
||||||
immutable_ttl: 0
|
|
||||||
mutable_ttl: 600
|
|
||||||
```
|
|
||||||
|
|
||||||
The upstream `Last-Modified` response header is used as the publish date proxy. Artifacts that have no `Last-Modified` header are allowed through (fail-open). Mutable files (index pages, tag manifests) are never quarantined.
|
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/config"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/server"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/tui"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) > 1 && os.Args[1] == "tui" {
|
||||||
|
endpoint := os.Getenv("ARTIFACTAPI_ENDPOINT")
|
||||||
|
if endpoint == "" {
|
||||||
|
endpoint = "http://localhost:8000"
|
||||||
|
}
|
||||||
|
for i, arg := range os.Args {
|
||||||
|
if arg == "--endpoint" && i+1 < len(os.Args) {
|
||||||
|
endpoint = os.Args[i+1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app := tui.New(endpoint)
|
||||||
|
if err := app.Run(); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "tui error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := config.Load()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to load config", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
srv, err := server.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to create server", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := srv.Run(ctx); err != nil {
|
||||||
|
slog.Error("server exited with error", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
-55
@@ -1,31 +1,22 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
artifactapi:
|
artifactapi:
|
||||||
build:
|
build: .
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
args:
|
|
||||||
- VERSION=2.2.2.dev0
|
|
||||||
ports:
|
ports:
|
||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
volumes:
|
|
||||||
- ./examples/single-file/remotes.yaml:/app/remotes.yaml:ro,z
|
|
||||||
- ./ca-bundle.pem:/app/ca-bundle.pem:ro,z
|
|
||||||
environment:
|
environment:
|
||||||
- CONFIG_PATH=/app/remotes.yaml
|
LISTEN_ADDR: ":8000"
|
||||||
- DBHOST=postgres
|
DBHOST: postgres
|
||||||
- DBPORT=5432
|
DBPORT: "5432"
|
||||||
- DBUSER=artifacts
|
DBUSER: artifacts
|
||||||
- DBPASS=artifacts123
|
DBPASS: artifacts123
|
||||||
- DBNAME=artifacts
|
DBNAME: artifacts
|
||||||
- REDIS_URL=redis://redis:6379
|
DBSSL: disable
|
||||||
- MINIO_ENDPOINT=minio:9000
|
REDIS_URL: redis://redis:6379
|
||||||
- MINIO_ACCESS_KEY=minioadmin
|
MINIO_ENDPOINT: minio:9000
|
||||||
- MINIO_SECRET_KEY=minioadmin
|
MINIO_ACCESS_KEY: minioadmin
|
||||||
- MINIO_BUCKET=artifacts
|
MINIO_SECRET_KEY: minioadmin
|
||||||
- MINIO_SECURE=false
|
MINIO_BUCKET: artifacts
|
||||||
- REQUESTS_CA_BUNDLE=/app/ca-bundle.pem
|
MINIO_SECURE: "false"
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
@@ -34,27 +25,35 @@ services:
|
|||||||
minio:
|
minio:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
|
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8000/health"]
|
||||||
interval: 30s
|
interval: 10s
|
||||||
timeout: 10s
|
timeout: 5s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
minio:
|
ui:
|
||||||
image: minio/minio:latest
|
build:
|
||||||
|
context: ui
|
||||||
|
dockerfile: Dockerfile.ui
|
||||||
ports:
|
ports:
|
||||||
- "9000:9000"
|
- "8080:80"
|
||||||
- "9001:9001"
|
depends_on:
|
||||||
|
- artifactapi
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:17-alpine
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
environment:
|
environment:
|
||||||
MINIO_ROOT_USER: minioadmin
|
POSTGRES_USER: artifacts
|
||||||
MINIO_ROOT_PASSWORD: minioadmin
|
POSTGRES_PASSWORD: artifacts123
|
||||||
command: server /data --console-address ":9001"
|
POSTGRES_DB: artifacts
|
||||||
volumes:
|
volumes:
|
||||||
- minio_data:/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
|
test: ["CMD-SHELL", "pg_isready -U artifacts -d artifacts"]
|
||||||
interval: 30s
|
interval: 5s
|
||||||
timeout: 20s
|
timeout: 5s
|
||||||
retries: 3
|
retries: 5
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:7-alpine
|
image: redis:7-alpine
|
||||||
@@ -65,27 +64,28 @@ services:
|
|||||||
command: redis-server --save 20 1
|
command: redis-server --save 20 1
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "redis-cli", "ping"]
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
interval: 30s
|
interval: 5s
|
||||||
timeout: 10s
|
timeout: 5s
|
||||||
retries: 3
|
retries: 5
|
||||||
|
|
||||||
postgres:
|
minio:
|
||||||
image: postgres:15-alpine
|
image: minio/minio:latest
|
||||||
ports:
|
command: server /data --console-address ":9001"
|
||||||
- "5432:5432"
|
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: artifacts
|
MINIO_ROOT_USER: minioadmin
|
||||||
POSTGRES_USER: artifacts
|
MINIO_ROOT_PASSWORD: minioadmin
|
||||||
POSTGRES_PASSWORD: artifacts123
|
ports:
|
||||||
|
- "9000:9000"
|
||||||
|
- "9001:9001"
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- minio_data:/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U artifacts -d artifacts"]
|
test: ["CMD", "mc", "ready", "local"]
|
||||||
interval: 30s
|
interval: 5s
|
||||||
timeout: 10s
|
timeout: 5s
|
||||||
retries: 3
|
retries: 5
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
minio_data:
|
|
||||||
redis_data:
|
|
||||||
postgres_data:
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
|
minio_data:
|
||||||
|
|||||||
+137
@@ -0,0 +1,137 @@
|
|||||||
|
//go:build e2e
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/testcontainers/testcontainers-go"
|
||||||
|
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||||
|
tcredis "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||||
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/config"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var baseURL string
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
pgContainer, err := tcpostgres.Run(ctx,
|
||||||
|
"postgres:17-alpine",
|
||||||
|
tcpostgres.WithDatabase("artifacts"),
|
||||||
|
tcpostgres.WithUsername("artifacts"),
|
||||||
|
tcpostgres.WithPassword("artifacts123"),
|
||||||
|
testcontainers.WithWaitStrategy(
|
||||||
|
wait.ForListeningPort("5432/tcp").WithStartupTimeout(30*time.Second),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("postgres: %v", err)
|
||||||
|
}
|
||||||
|
defer pgContainer.Terminate(ctx)
|
||||||
|
|
||||||
|
redisContainer, err := tcredis.Run(ctx,
|
||||||
|
"redis:7-alpine",
|
||||||
|
testcontainers.WithWaitStrategy(
|
||||||
|
wait.ForListeningPort("6379/tcp").WithStartupTimeout(30*time.Second),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("redis: %v", err)
|
||||||
|
}
|
||||||
|
defer redisContainer.Terminate(ctx)
|
||||||
|
|
||||||
|
minioContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||||
|
ContainerRequest: testcontainers.ContainerRequest{
|
||||||
|
Image: "minio/minio:latest",
|
||||||
|
ExposedPorts: []string{"9000/tcp"},
|
||||||
|
Cmd: []string{"server", "/data"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"MINIO_ROOT_USER": "minioadmin",
|
||||||
|
"MINIO_ROOT_PASSWORD": "minioadmin",
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForHTTP("/minio/health/live").WithPort("9000/tcp").WithStartupTimeout(30 * time.Second),
|
||||||
|
},
|
||||||
|
Started: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("minio: %v", err)
|
||||||
|
}
|
||||||
|
defer minioContainer.Terminate(ctx)
|
||||||
|
|
||||||
|
pgHost, _ := pgContainer.Host(ctx)
|
||||||
|
pgPort, _ := pgContainer.MappedPort(ctx, "5432/tcp")
|
||||||
|
redisHost, _ := redisContainer.Host(ctx)
|
||||||
|
redisPort, _ := redisContainer.MappedPort(ctx, "6379/tcp")
|
||||||
|
minioHost, _ := minioContainer.Host(ctx)
|
||||||
|
minioPort, _ := minioContainer.MappedPort(ctx, "9000/tcp")
|
||||||
|
|
||||||
|
os.Setenv("DBHOST", pgHost)
|
||||||
|
os.Setenv("DBPORT", pgPort.Port())
|
||||||
|
os.Setenv("DBUSER", "artifacts")
|
||||||
|
os.Setenv("DBPASS", "artifacts123")
|
||||||
|
os.Setenv("DBNAME", "artifacts")
|
||||||
|
os.Setenv("DBSSL", "disable")
|
||||||
|
os.Setenv("REDIS_URL", fmt.Sprintf("redis://%s:%s", redisHost, redisPort.Port()))
|
||||||
|
os.Setenv("MINIO_ENDPOINT", fmt.Sprintf("%s:%s", minioHost, minioPort.Port()))
|
||||||
|
os.Setenv("MINIO_ACCESS_KEY", "minioadmin")
|
||||||
|
os.Setenv("MINIO_SECRET_KEY", "minioadmin")
|
||||||
|
os.Setenv("MINIO_BUCKET", "artifacts-test")
|
||||||
|
os.Setenv("MINIO_SECURE", "false")
|
||||||
|
os.Setenv("LISTEN_ADDR", ":0")
|
||||||
|
|
||||||
|
cfg, err := config.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("config: %v", err)
|
||||||
|
}
|
||||||
|
cfg.ListenAddr = "127.0.0.1:0"
|
||||||
|
|
||||||
|
srv, err := server.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
srvCtx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
addr := startServer(srvCtx, srv)
|
||||||
|
baseURL = "http://" + addr
|
||||||
|
|
||||||
|
code := m.Run()
|
||||||
|
cancel()
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func startServer(ctx context.Context, srv *server.Server) string {
|
||||||
|
ln, err := findListener()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("listener: %v", err)
|
||||||
|
}
|
||||||
|
addr := ln.Addr().String()
|
||||||
|
|
||||||
|
go srv.RunOnListener(ctx, ln)
|
||||||
|
|
||||||
|
deadline := time.Now().Add(5 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
resp, err := http.Get("http://" + addr + "/health")
|
||||||
|
if err == nil && resp.StatusCode == 200 {
|
||||||
|
resp.Body.Close()
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
if resp != nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
log.Fatalf("server did not start in time at %s", addr)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
//go:build e2e
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func findListener() (net.Listener, error) {
|
||||||
|
return net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiURL(path string) string {
|
||||||
|
return baseURL + path
|
||||||
|
}
|
||||||
|
|
||||||
|
func createRemote(t *testing.T, body string) {
|
||||||
|
t.Helper()
|
||||||
|
resp, err := http.Post(apiURL("/api/v2/remotes"), "application/json", strings.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create remote: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
|
b, _ := io.ReadAll(resp.Body)
|
||||||
|
t.Fatalf("create remote: status %d: %s", resp.StatusCode, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteRemote(t *testing.T, name string) {
|
||||||
|
t.Helper()
|
||||||
|
req, _ := http.NewRequest(http.MethodDelete, apiURL("/api/v2/remotes/"+name), nil)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("delete remote: %v", err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getJSON(t *testing.T, url string) map[string]any {
|
||||||
|
t.Helper()
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GET %s: %v", url, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
b, _ := io.ReadAll(resp.Body)
|
||||||
|
t.Fatalf("GET %s: status %d: %s", url, resp.StatusCode, b)
|
||||||
|
}
|
||||||
|
var result map[string]any
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||||
|
t.Fatalf("decode: %v", err)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBody(t *testing.T, url string) ([]byte, int) {
|
||||||
|
t.Helper()
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GET %s: %v", url, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
b, _ := io.ReadAll(resp.Body)
|
||||||
|
return b, resp.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func getString(t *testing.T, url string) string {
|
||||||
|
t.Helper()
|
||||||
|
b, status := getBody(t, url)
|
||||||
|
if status != http.StatusOK {
|
||||||
|
t.Fatalf("GET %s: status %d: %s", url, status, b)
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertStatus(t *testing.T, url string, wantStatus int) {
|
||||||
|
t.Helper()
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GET %s: %v", url, err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != wantStatus {
|
||||||
|
t.Errorf("GET %s: got %d, want %d", url, resp.StatusCode, wantStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteRequest(t *testing.T, url string) int {
|
||||||
|
t.Helper()
|
||||||
|
req, _ := http.NewRequest(http.MethodDelete, url, nil)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("DELETE %s: %v", url, err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
return resp.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustFormat(format string, args ...any) string {
|
||||||
|
return fmt.Sprintf(format, args...)
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
//go:build e2e
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHealth(t *testing.T) {
|
||||||
|
result := getJSON(t, apiURL("/health"))
|
||||||
|
if result["status"] != "ok" {
|
||||||
|
t.Errorf("expected ok, got %v", result["status"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoot(t *testing.T) {
|
||||||
|
result := getJSON(t, apiURL("/"))
|
||||||
|
if result["name"] != "artifactapi" {
|
||||||
|
t.Errorf("expected artifactapi, got %v", result["name"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoteCRUD(t *testing.T) {
|
||||||
|
createRemote(t, `{
|
||||||
|
"name": "test-generic",
|
||||||
|
"package_type": "generic",
|
||||||
|
"base_url": "https://example.com",
|
||||||
|
"description": "test remote",
|
||||||
|
"mutable_ttl": 600,
|
||||||
|
"check_mutable": true,
|
||||||
|
"stale_on_error": true
|
||||||
|
}`)
|
||||||
|
defer deleteRemote(t, "test-generic")
|
||||||
|
|
||||||
|
remote := getJSON(t, apiURL("/api/v2/remotes/test-generic"))
|
||||||
|
if remote["name"] != "test-generic" {
|
||||||
|
t.Errorf("expected test-generic, got %v", remote["name"])
|
||||||
|
}
|
||||||
|
if remote["package_type"] != "generic" {
|
||||||
|
t.Errorf("expected generic, got %v", remote["package_type"])
|
||||||
|
}
|
||||||
|
|
||||||
|
req, _ := http.NewRequest(http.MethodPut, apiURL("/api/v2/remotes/test-generic"),
|
||||||
|
strings.NewReader(`{
|
||||||
|
"package_type": "generic",
|
||||||
|
"base_url": "https://updated.example.com",
|
||||||
|
"description": "updated",
|
||||||
|
"mutable_ttl": 300,
|
||||||
|
"check_mutable": true,
|
||||||
|
"stale_on_error": true
|
||||||
|
}`))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("update: status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
updated := getJSON(t, apiURL("/api/v2/remotes/test-generic"))
|
||||||
|
if updated["base_url"] != "https://updated.example.com" {
|
||||||
|
t.Errorf("expected updated URL, got %v", updated["base_url"])
|
||||||
|
}
|
||||||
|
|
||||||
|
status := deleteRequest(t, apiURL("/api/v2/remotes/test-generic"))
|
||||||
|
if status != http.StatusNoContent {
|
||||||
|
t.Errorf("delete: got %d, want 204", status)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertStatus(t, apiURL("/api/v2/remotes/test-generic"), http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoteList(t *testing.T) {
|
||||||
|
createRemote(t, `{"name":"list-a","package_type":"generic","base_url":"https://a.example.com","stale_on_error":true}`)
|
||||||
|
createRemote(t, `{"name":"list-b","package_type":"helm","base_url":"https://b.example.com","stale_on_error":true}`)
|
||||||
|
defer deleteRemote(t, "list-a")
|
||||||
|
defer deleteRemote(t, "list-b")
|
||||||
|
|
||||||
|
resp, err := http.Get(apiURL("/api/v2/remotes"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
var remotes []map[string]any
|
||||||
|
json.Unmarshal(body, &remotes)
|
||||||
|
|
||||||
|
if len(remotes) < 2 {
|
||||||
|
t.Fatalf("expected at least 2 remotes, got %d", len(remotes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVirtualCRUD(t *testing.T) {
|
||||||
|
createRemote(t, `{"name":"virt-member-a","package_type":"helm","base_url":"https://a.example.com","stale_on_error":true}`)
|
||||||
|
createRemote(t, `{"name":"virt-member-b","package_type":"helm","base_url":"https://b.example.com","stale_on_error":true}`)
|
||||||
|
defer deleteRemote(t, "virt-member-a")
|
||||||
|
defer deleteRemote(t, "virt-member-b")
|
||||||
|
|
||||||
|
resp, err := http.Post(apiURL("/api/v2/virtuals"), "application/json",
|
||||||
|
strings.NewReader(`{
|
||||||
|
"name": "test-virtual",
|
||||||
|
"package_type": "helm",
|
||||||
|
"description": "test virtual",
|
||||||
|
"members": ["virt-member-a", "virt-member-b"]
|
||||||
|
}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
|
t.Fatalf("create virtual: status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
virt := getJSON(t, apiURL("/api/v2/virtuals/test-virtual"))
|
||||||
|
if virt["name"] != "test-virtual" {
|
||||||
|
t.Errorf("expected test-virtual, got %v", virt["name"])
|
||||||
|
}
|
||||||
|
|
||||||
|
status := deleteRequest(t, apiURL("/api/v2/virtuals/test-virtual"))
|
||||||
|
if status != http.StatusNoContent {
|
||||||
|
t.Errorf("delete virtual: got %d, want 204", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStatsEndpoint(t *testing.T) {
|
||||||
|
result := getJSON(t, apiURL("/api/v2/stats"))
|
||||||
|
if _, ok := result["total_remotes"]; !ok {
|
||||||
|
t.Error("expected total_remotes in stats")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHealthV2(t *testing.T) {
|
||||||
|
result := getJSON(t, apiURL("/api/v2/health"))
|
||||||
|
if result["status"] != "ok" {
|
||||||
|
t.Errorf("expected ok, got %v", result["status"])
|
||||||
|
}
|
||||||
|
if result["postgres"] != "ok" {
|
||||||
|
t.Errorf("expected postgres ok, got %v", result["postgres"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidPackageType(t *testing.T) {
|
||||||
|
resp, err := http.Post(apiURL("/api/v2/remotes"), "application/json",
|
||||||
|
strings.NewReader(`{"name":"bad","package_type":"bogus","base_url":"https://x.com"}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusBadRequest {
|
||||||
|
t.Errorf("expected 400 for invalid package type, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
//go:build e2e
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProxyUnknownRemote(t *testing.T) {
|
||||||
|
assertStatus(t, apiURL("/api/v1/remote/nonexistent/some/path"), http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProxyBlocklist(t *testing.T) {
|
||||||
|
createRemote(t, `{
|
||||||
|
"name": "blocklist-test",
|
||||||
|
"package_type": "generic",
|
||||||
|
"base_url": "https://example.com",
|
||||||
|
"blocklist": ["\\.exe$"],
|
||||||
|
"stale_on_error": true
|
||||||
|
}`)
|
||||||
|
defer deleteRemote(t, "blocklist-test")
|
||||||
|
|
||||||
|
assertStatus(t, apiURL("/api/v1/remote/blocklist-test/malware.exe"), http.StatusForbidden)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProxyPatterns(t *testing.T) {
|
||||||
|
createRemote(t, `{
|
||||||
|
"name": "patterns-test",
|
||||||
|
"package_type": "generic",
|
||||||
|
"base_url": "https://example.com",
|
||||||
|
"patterns": ["^releases/"],
|
||||||
|
"stale_on_error": true
|
||||||
|
}`)
|
||||||
|
defer deleteRemote(t, "patterns-test")
|
||||||
|
|
||||||
|
assertStatus(t, apiURL("/api/v1/remote/patterns-test/uploads/file.tar.gz"), http.StatusForbidden)
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
module git.unkin.net/unkin/artifactapi
|
||||||
|
|
||||||
|
go 1.25.9
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/charmbracelet/bubbletea v1.3.10
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0
|
||||||
|
github.com/go-chi/chi/v5 v5.3.0
|
||||||
|
github.com/jackc/pgx/v5 v5.10.0
|
||||||
|
github.com/minio/minio-go/v7 v7.2.0
|
||||||
|
github.com/redis/go-redis/v9 v9.20.0
|
||||||
|
github.com/testcontainers/testcontainers-go v0.42.0
|
||||||
|
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0
|
||||||
|
github.com/testcontainers/testcontainers-go/modules/redis v0.42.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
dario.cat/mergo v1.0.2 // indirect
|
||||||
|
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
|
||||||
|
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/charmbracelet/colorprofile v0.4.3 // indirect
|
||||||
|
github.com/charmbracelet/x/ansi v0.11.7 // indirect
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.15 // indirect
|
||||||
|
github.com/charmbracelet/x/term v0.2.2 // indirect
|
||||||
|
github.com/clipperhouse/displaywidth v0.11.0 // indirect
|
||||||
|
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||||
|
github.com/containerd/errdefs v1.0.0 // indirect
|
||||||
|
github.com/containerd/errdefs/pkg v0.3.0 // indirect
|
||||||
|
github.com/containerd/log v0.1.0 // indirect
|
||||||
|
github.com/containerd/platforms v0.2.1 // indirect
|
||||||
|
github.com/cpuguy83/dockercfg v0.3.2 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/distribution/reference v0.6.0 // indirect
|
||||||
|
github.com/docker/go-connections v0.6.0 // indirect
|
||||||
|
github.com/docker/go-units v0.5.0 // indirect
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/ebitengine/purego v0.10.0 // indirect
|
||||||
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||||
|
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||||
|
github.com/go-logr/logr v1.4.3 // indirect
|
||||||
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
|
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||||
|
github.com/klauspost/compress v1.18.6 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
|
||||||
|
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
|
||||||
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||||
|
github.com/magiconair/properties v1.8.10 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.23 // indirect
|
||||||
|
github.com/mdelapenya/tlscert v0.2.0 // indirect
|
||||||
|
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||||
|
github.com/minio/md5-simd v1.1.2 // indirect
|
||||||
|
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||||
|
github.com/moby/go-archive v0.2.0 // indirect
|
||||||
|
github.com/moby/moby/api v1.54.1 // indirect
|
||||||
|
github.com/moby/moby/client v0.4.0 // indirect
|
||||||
|
github.com/moby/patternmatcher v0.6.1 // indirect
|
||||||
|
github.com/moby/sys/sequential v0.6.0 // indirect
|
||||||
|
github.com/moby/sys/user v0.4.0 // indirect
|
||||||
|
github.com/moby/sys/userns v0.1.0 // indirect
|
||||||
|
github.com/moby/term v0.5.2 // indirect
|
||||||
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||||
|
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||||
|
github.com/muesli/termenv v0.16.0 // indirect
|
||||||
|
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||||
|
github.com/opencontainers/image-spec v1.1.1 // indirect
|
||||||
|
github.com/philhofer/fwd v1.2.0 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/rs/xid v1.6.0 // indirect
|
||||||
|
github.com/shirou/gopsutil/v4 v4.26.3 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.9.4 // indirect
|
||||||
|
github.com/stretchr/testify v1.11.1 // indirect
|
||||||
|
github.com/tinylib/msgp v1.6.1 // indirect
|
||||||
|
github.com/tklauser/go-sysconf v0.3.16 // indirect
|
||||||
|
github.com/tklauser/numcpus v0.11.0 // indirect
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||||
|
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||||
|
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||||
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
|
||||||
|
go.opentelemetry.io/otel v1.41.0 // indirect
|
||||||
|
go.opentelemetry.io/otel/metric v1.41.0 // indirect
|
||||||
|
go.opentelemetry.io/otel/trace v1.41.0 // indirect
|
||||||
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
|
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||||
|
golang.org/x/crypto v0.51.0 // indirect
|
||||||
|
golang.org/x/net v0.53.0 // indirect
|
||||||
|
golang.org/x/sync v0.20.0 // indirect
|
||||||
|
golang.org/x/sys v0.44.0 // indirect
|
||||||
|
golang.org/x/text v0.37.0 // indirect
|
||||||
|
gopkg.in/ini.v1 v1.67.2 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,247 @@
|
|||||||
|
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||||
|
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||||
|
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
|
||||||
|
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
|
||||||
|
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg=
|
||||||
|
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||||
|
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||||
|
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
|
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||||
|
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
|
||||||
|
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
|
||||||
|
github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=
|
||||||
|
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
|
||||||
|
github.com/charmbracelet/x/ansi v0.11.7 h1:kzv1kJvjg2S3r9KHo8hDdHFQLEqn4RBCb39dAYC84jI=
|
||||||
|
github.com/charmbracelet/x/ansi v0.11.7/go.mod h1:9qGpnAVYz+8ACONkZBUWPtL7lulP9No6p1epAihUZwQ=
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.15 h1:ur3pZy0o6z/R7EylET877CBxaiE1Sp1GMxoFPAIztPI=
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.15/go.mod h1:J1YVbR7MUuEGIFPCaaZ96KDl5NoS0DAWkskup+mOY+Q=
|
||||||
|
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
|
||||||
|
github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
|
||||||
|
github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8=
|
||||||
|
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
|
||||||
|
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
|
||||||
|
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
|
||||||
|
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
|
||||||
|
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
|
||||||
|
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
|
||||||
|
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
|
||||||
|
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
||||||
|
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
|
||||||
|
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
|
||||||
|
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
|
||||||
|
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
|
||||||
|
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
|
||||||
|
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||||
|
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
|
||||||
|
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||||
|
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
|
||||||
|
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
|
||||||
|
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||||
|
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||||
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/ebitengine/purego v0.10.0 h1:QIw4xfpWT6GWTzaW5XEKy3HXoqrJGx1ijYHzTF0/ISU=
|
||||||
|
github.com/ebitengine/purego v0.10.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||||
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||||
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
||||||
|
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||||
|
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||||
|
github.com/go-chi/chi/v5 v5.3.0 h1:halUjDxhshgXHMrao5bB8eNBXo/rnzwr8m5m36glehM=
|
||||||
|
github.com/go-chi/chi/v5 v5.3.0/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
|
||||||
|
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||||
|
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||||
|
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
|
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||||
|
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||||
|
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
||||||
|
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||||
|
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||||
|
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
|
||||||
|
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao=
|
||||||
|
github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||||
|
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||||
|
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
|
||||||
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
||||||
|
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
|
||||||
|
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
||||||
|
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||||
|
github.com/mattn/go-runewidth v0.0.23 h1:7ykA0T0jkPpzSvMS5i9uoNn2Xy3R383f9HDx3RybWcw=
|
||||||
|
github.com/mattn/go-runewidth v0.0.23/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||||
|
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
|
||||||
|
github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
|
||||||
|
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||||
|
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||||
|
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||||
|
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||||
|
github.com/minio/minio-go/v7 v7.2.0 h1:RCJM0R1XOsRs+A3x3UCaf3ZYbByDaLjFeAi+YCQEPhs=
|
||||||
|
github.com/minio/minio-go/v7 v7.2.0/go.mod h1:EU9hENAStx/xXduNdrGO5e4X5vk19NtgB+RIPjZO8o0=
|
||||||
|
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||||
|
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||||
|
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
|
||||||
|
github.com/moby/go-archive v0.2.0/go.mod h1:mNeivT14o8xU+5q1YnNrkQVpK+dnNe/K6fHqnTg4qPU=
|
||||||
|
github.com/moby/moby/api v1.54.1 h1:TqVzuJkOLsgLDDwNLmYqACUuTehOHRGKiPhvH8V3Nn4=
|
||||||
|
github.com/moby/moby/api v1.54.1/go.mod h1:+RQ6wluLwtYaTd1WnPLykIDPekkuyD/ROWQClE83pzs=
|
||||||
|
github.com/moby/moby/client v0.4.0 h1:S+2XegzHQrrvTCvF6s5HFzcrywWQmuVnhOXe2kiWjIw=
|
||||||
|
github.com/moby/moby/client v0.4.0/go.mod h1:QWPbvWchQbxBNdaLSpoKpCdf5E+WxFAgNHogCWDoa7g=
|
||||||
|
github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U=
|
||||||
|
github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
|
||||||
|
github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU=
|
||||||
|
github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko=
|
||||||
|
github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs=
|
||||||
|
github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
|
||||||
|
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
|
||||||
|
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
|
||||||
|
github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ=
|
||||||
|
github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc=
|
||||||
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
||||||
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||||
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
|
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
|
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||||
|
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||||
|
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||||
|
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||||
|
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
||||||
|
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
||||||
|
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||||
|
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
|
||||||
|
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||||
|
github.com/redis/go-redis/v9 v9.20.0 h1:WnQYxLkgO2xiXTCJY0ldIiI8dNqCDlQAG+AtaH7a2a0=
|
||||||
|
github.com/redis/go-redis/v9 v9.20.0/go.mod h1:v/M13XI1PVCDcm01VtPFOADfZtHf8YW3baQf57KlIkA=
|
||||||
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||||
|
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||||
|
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||||
|
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||||
|
github.com/shirou/gopsutil/v4 v4.26.3 h1:2ESdQt90yU3oXF/CdOlRCJxrP+Am1aBYubTMTfxJ1qc=
|
||||||
|
github.com/shirou/gopsutil/v4 v4.26.3/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
|
||||||
|
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
|
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
|
||||||
|
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/testcontainers/testcontainers-go v0.42.0 h1:He3IhTzTZOygSXLJPMX7n44XtK+qhjat1nI9cneBbUY=
|
||||||
|
github.com/testcontainers/testcontainers-go v0.42.0/go.mod h1:vZjdY1YmUA1qEForxOIOazfsrdyORJAbhi0bp8plN30=
|
||||||
|
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0 h1:GCbb1ndrF7OTDiIvxXyItaDab4qkzTFJ48LKFdM7EIo=
|
||||||
|
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0/go.mod h1:IRPBaI8jXdrNfD0e4Zm7Fbcgaz5shKxOQv4axiL09xs=
|
||||||
|
github.com/testcontainers/testcontainers-go/modules/redis v0.42.0 h1:id/6LH8ZeDrtAUVSuNvZUAJ1kVpb82y1pr9yweAWsRg=
|
||||||
|
github.com/testcontainers/testcontainers-go/modules/redis v0.42.0/go.mod h1:uF0jI8FITagQpBNOgweGBmPf6rP4K0SeL1XFPbsZSSY=
|
||||||
|
github.com/tinylib/msgp v1.6.1 h1:ESRv8eL3u+DNHUoSAAQRE50Hm162zqAnBoGv9PzScPY=
|
||||||
|
github.com/tinylib/msgp v1.6.1/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||||
|
github.com/tklauser/go-sysconf v0.3.16 h1:frioLaCQSsF5Cy1jgRBrzr6t502KIIwQ0MArYICU0nA=
|
||||||
|
github.com/tklauser/go-sysconf v0.3.16/go.mod h1:/qNL9xxDhc7tx3HSRsLWNnuzbVfh3e7gh/BmM179nYI=
|
||||||
|
github.com/tklauser/numcpus v0.11.0 h1:nSTwhKH5e1dMNsCdVBukSZrURJRoHbSEQjdEbY+9RXw=
|
||||||
|
github.com/tklauser/numcpus v0.11.0/go.mod h1:z+LwcLq54uWZTX0u/bGobaV34u6V7KNlTZejzM6/3MQ=
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||||
|
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||||
|
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||||
|
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||||
|
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||||
|
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||||
|
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||||
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
|
||||||
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0/go.mod h1:69uWxva0WgAA/4bu2Yy70SLDBwZXuQ6PbBpbsa5iZrQ=
|
||||||
|
go.opentelemetry.io/otel v1.41.0 h1:YlEwVsGAlCvczDILpUXpIpPSL/VPugt7zHThEMLce1c=
|
||||||
|
go.opentelemetry.io/otel v1.41.0/go.mod h1:Yt4UwgEKeT05QbLwbyHXEwhnjxNO6D8L5PQP51/46dE=
|
||||||
|
go.opentelemetry.io/otel/metric v1.41.0 h1:rFnDcs4gRzBcsO9tS8LCpgR0dxg4aaxWlJxCno7JlTQ=
|
||||||
|
go.opentelemetry.io/otel/metric v1.41.0/go.mod h1:xPvCwd9pU0VN8tPZYzDZV/BMj9CM9vs00GuBjeKhJps=
|
||||||
|
go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY=
|
||||||
|
go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg=
|
||||||
|
go.opentelemetry.io/otel/sdk/metric v1.35.0 h1:1RriWBmCKgkeHEhM7a2uMjMUfP7MsOF5JpUCaEqEI9o=
|
||||||
|
go.opentelemetry.io/otel/sdk/metric v1.35.0/go.mod h1:is6XYCUMpcKi+ZsOvfluY5YstFnhW0BidkR+gL+qN+w=
|
||||||
|
go.opentelemetry.io/otel/trace v1.41.0 h1:Vbk2co6bhj8L59ZJ6/xFTskY+tGAbOnCtQGVVa9TIN0=
|
||||||
|
go.opentelemetry.io/otel/trace v1.41.0/go.mod h1:U1NU4ULCoxeDKc09yCWdWe+3QoyweJcISEVa1RBzOis=
|
||||||
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
|
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||||
|
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||||
|
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
|
||||||
|
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
|
||||||
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||||
|
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
|
||||||
|
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
|
||||||
|
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||||
|
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
|
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
||||||
|
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
|
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
|
||||||
|
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
|
||||||
|
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||||
|
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/ini.v1 v1.67.2 h1:JtOSMb9OuaCZKr7h5D/h6iii14sK0hLbplTc6frx4Ss=
|
||||||
|
gopkg.in/ini.v1 v1.67.2/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
||||||
|
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
||||||
|
pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk=
|
||||||
|
pgregory.net/rapid v1.2.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package v1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/virtual"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProxyHandler struct {
|
||||||
|
engine *proxy.Engine
|
||||||
|
virtualEngine *virtual.Engine
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewProxyHandler(engine *proxy.Engine, virtualEngine *virtual.Engine, db *database.DB) *ProxyHandler {
|
||||||
|
return &ProxyHandler{engine: engine, virtualEngine: virtualEngine, db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ProxyHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/remote/{remoteName}/*", h.handleProxy)
|
||||||
|
r.Get("/virtual/{virtualName}/*", h.handleVirtual)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ProxyHandler) handleProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
|
remoteName := chi.URLParam(r, "remoteName")
|
||||||
|
path := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
remote, err := h.db.GetRemote(r.Context(), remoteName)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("remote %q not found", remoteName), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prov, err := provider.Get(remote.PackageType)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("no provider for %q", remote.PackageType), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := h.engine.Fetch(r.Context(), *remote, path, prov)
|
||||||
|
if err != nil {
|
||||||
|
var proxyErr *proxy.ProxyError
|
||||||
|
if errors.As(err, &proxyErr) {
|
||||||
|
http.Error(w, proxyErr.Message, proxyErr.Status)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
slog.Error("proxy fetch failed", "remote", remoteName, "path", path, "error", err)
|
||||||
|
http.Error(w, "bad gateway", http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer result.Reader.Close()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", result.ContentType)
|
||||||
|
w.Header().Set("X-Artifact-Source", result.Source)
|
||||||
|
if result.Size > 0 {
|
||||||
|
w.Header().Set("X-Artifact-Size", fmt.Sprintf("%d", result.Size))
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
io.Copy(w, result.Reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ProxyHandler) handleVirtual(w http.ResponseWriter, r *http.Request) {
|
||||||
|
virtualName := chi.URLParam(r, "virtualName")
|
||||||
|
path := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
virt, err := h.db.GetVirtual(r.Context(), virtualName)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("virtual %q not found", virtualName), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyBaseURL := fmt.Sprintf("%s://%s", scheme(r), r.Host)
|
||||||
|
|
||||||
|
body, contentType, err := h.virtualEngine.Fetch(r.Context(), *virt, path, proxyBaseURL)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("virtual fetch failed", "virtual", virtualName, "path", path, "error", err)
|
||||||
|
http.Error(w, "bad gateway", http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
w.Header().Set("X-Artifact-Source", "virtual")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func scheme(r *http.Request) string {
|
||||||
|
if r.TLS != nil {
|
||||||
|
return "https"
|
||||||
|
}
|
||||||
|
if fwd := r.Header.Get("X-Forwarded-Proto"); fwd != "" {
|
||||||
|
return fwd
|
||||||
|
}
|
||||||
|
return "http"
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EventsHandler struct{}
|
||||||
|
|
||||||
|
func NewEventsHandler() *EventsHandler {
|
||||||
|
return &EventsHandler{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *EventsHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", h.stream)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *EventsHandler) stream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
flusher, ok := w.(http.Flusher)
|
||||||
|
if !ok {
|
||||||
|
http.Error(w, "streaming not supported", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
w.Header().Set("X-Accel-Buffering", "no")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
flusher.Flush()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-r.Context().Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
fmt.Fprintf(w, ": keepalive\n\n")
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/cache"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HealthHandler struct {
|
||||||
|
db *database.DB
|
||||||
|
cache *cache.Redis
|
||||||
|
store *storage.S3
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHealthHandler(db *database.DB, c *cache.Redis, s *storage.S3) *HealthHandler {
|
||||||
|
return &HealthHandler{db: db, cache: c, store: s}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HealthHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", h.health)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HealthHandler) health(w http.ResponseWriter, r *http.Request) {
|
||||||
|
status := map[string]string{
|
||||||
|
"status": "ok",
|
||||||
|
"postgres": "ok",
|
||||||
|
"redis": "ok",
|
||||||
|
"s3": "ok",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.db.Pool.Ping(r.Context()); err != nil {
|
||||||
|
status["postgres"] = "error"
|
||||||
|
status["status"] = "degraded"
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, status)
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ObjectsHandler struct {
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewObjectsHandler(db *database.DB) *ObjectsHandler {
|
||||||
|
return &ObjectsHandler{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ObjectsHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", h.list)
|
||||||
|
r.Delete("/*", h.evict)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ObjectsHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
remoteName := chi.URLParam(r, "name")
|
||||||
|
limit, _ := strconv.Atoi(r.URL.Query().Get("per_page"))
|
||||||
|
if limit <= 0 || limit > 100 {
|
||||||
|
limit = 50
|
||||||
|
}
|
||||||
|
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
||||||
|
if page <= 0 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
offset := (page - 1) * limit
|
||||||
|
|
||||||
|
artifacts, err := h.db.ListArtifacts(r.Context(), remoteName, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, artifacts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ObjectsHandler) evict(w http.ResponseWriter, r *http.Request) {
|
||||||
|
remoteName := chi.URLParam(r, "name")
|
||||||
|
path := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
if err := h.db.DeleteArtifact(r.Context(), remoteName, path); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("evict failed: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProbeHandler struct {
|
||||||
|
engine *proxy.Engine
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewProbeHandler(engine *proxy.Engine, db *database.DB) *ProbeHandler {
|
||||||
|
return &ProbeHandler{engine: engine, db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ProbeHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Post("/", h.probe)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type probeRequest struct {
|
||||||
|
Remote string `json:"remote"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type probeResponse struct {
|
||||||
|
Status int `json:"status"`
|
||||||
|
Source string `json:"source,omitempty"`
|
||||||
|
ContentType string `json:"content_type,omitempty"`
|
||||||
|
SizeBytes int64 `json:"size_bytes"`
|
||||||
|
Headers map[string]string `json:"headers,omitempty"`
|
||||||
|
DurationMS int64 `json:"duration_ms"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ProbeHandler) probe(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var req probeRequest
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Remote == "" || req.Path == "" {
|
||||||
|
http.Error(w, "remote and path are required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remote, err := h.db.GetRemote(r.Context(), req.Remote)
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusOK, probeResponse{
|
||||||
|
Status: 404,
|
||||||
|
Error: fmt.Sprintf("remote %q not found", req.Remote),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prov, err := provider.Get(remote.PackageType)
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusOK, probeResponse{
|
||||||
|
Status: 500,
|
||||||
|
Error: fmt.Sprintf("no provider for %q", remote.PackageType),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
result, err := h.engine.Fetch(ctx, *remote, req.Path, prov)
|
||||||
|
duration := time.Since(start).Milliseconds()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusOK, probeResponse{
|
||||||
|
Status: 502,
|
||||||
|
DurationMS: duration,
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
io.Copy(io.Discard, result.Reader)
|
||||||
|
result.Reader.Close()
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, probeResponse{
|
||||||
|
Status: 200,
|
||||||
|
Source: result.Source,
|
||||||
|
ContentType: result.ContentType,
|
||||||
|
SizeBytes: result.Size,
|
||||||
|
DurationMS: duration,
|
||||||
|
Headers: map[string]string{
|
||||||
|
"X-Artifact-Source": result.Source,
|
||||||
|
"X-Artifact-Size": fmt.Sprintf("%d", result.Size),
|
||||||
|
"Content-Type": result.ContentType,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RemotesHandler struct {
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRemotesHandler(db *database.DB) *RemotesHandler {
|
||||||
|
return &RemotesHandler{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RemotesHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", h.list)
|
||||||
|
r.Post("/", h.create)
|
||||||
|
r.Get("/{name}", h.get)
|
||||||
|
r.Put("/{name}", h.update)
|
||||||
|
r.Delete("/{name}", h.del)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RemotesHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
remotes, err := h.db.ListRemotes(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, remotes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RemotesHandler) get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
remote, err := h.db.GetRemote(r.Context(), name)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("remote %q not found", name), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RemotesHandler) create(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var remote models.Remote
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&remote); err != nil {
|
||||||
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !remote.PackageType.Valid() {
|
||||||
|
http.Error(w, fmt.Sprintf("invalid package type: %q", remote.PackageType), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := h.db.CreateRemote(r.Context(), &remote); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusCreated, remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RemotesHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
var remote models.Remote
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&remote); err != nil {
|
||||||
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
remote.Name = name
|
||||||
|
if err := h.db.UpdateRemote(r.Context(), &remote); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RemotesHandler) del(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
if err := h.db.DeleteRemote(r.Context(), name); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
json.NewEncoder(w).Encode(v)
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StatsHandler struct {
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStatsHandler(db *database.DB) *StatsHandler {
|
||||||
|
return &StatsHandler{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *StatsHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", h.overview)
|
||||||
|
r.Get("/top-remotes", h.topRemotes)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *StatsHandler) overview(w http.ResponseWriter, r *http.Request) {
|
||||||
|
stats, err := h.db.GetOverviewStats(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, stats)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *StatsHandler) topRemotes(w http.ResponseWriter, r *http.Request) {
|
||||||
|
remotes, err := h.db.GetTopRemotes(r.Context(), 10)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, remotes)
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VirtualsHandler struct {
|
||||||
|
db *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewVirtualsHandler(db *database.DB) *VirtualsHandler {
|
||||||
|
return &VirtualsHandler{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *VirtualsHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", h.list)
|
||||||
|
r.Post("/", h.create)
|
||||||
|
r.Get("/{name}", h.get)
|
||||||
|
r.Put("/{name}", h.update)
|
||||||
|
r.Delete("/{name}", h.del)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *VirtualsHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
virtuals, err := h.db.ListVirtuals(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, virtuals)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *VirtualsHandler) get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
virt, err := h.db.GetVirtual(r.Context(), name)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("virtual %q not found", name), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, virt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *VirtualsHandler) create(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var virt models.Virtual
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&virt); err != nil {
|
||||||
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := h.db.CreateVirtual(r.Context(), &virt); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusCreated, virt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *VirtualsHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
var virt models.Virtual
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&virt); err != nil {
|
||||||
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
virt.Name = name
|
||||||
|
if err := h.db.UpdateVirtual(r.Context(), &virt); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, virt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *VirtualsHandler) del(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
if err := h.db.DeleteVirtual(r.Context(), name); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BasicHeaders(remote models.Remote) http.Header {
|
||||||
|
h := http.Header{}
|
||||||
|
if remote.Username != "" {
|
||||||
|
h.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(
|
||||||
|
[]byte(remote.Username+":"+remote.Password),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
Vendored
+105
@@ -0,0 +1,105 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Redis struct {
|
||||||
|
client *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedis(url string) (*Redis, error) {
|
||||||
|
opts, err := redis.ParseURL(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("parse redis url: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := redis.NewClient(opts)
|
||||||
|
|
||||||
|
if err := client.Ping(context.Background()).Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("ping redis: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Redis{client: client}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) Close() error {
|
||||||
|
return r.client.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) SetTTL(ctx context.Context, remote, path string, ttl time.Duration) error {
|
||||||
|
key := fmt.Sprintf("ttl:%s:%s", remote, path)
|
||||||
|
return r.client.Set(ctx, key, "1", ttl).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) CheckTTL(ctx context.Context, remote, path string) (bool, error) {
|
||||||
|
key := fmt.Sprintf("ttl:%s:%s", remote, path)
|
||||||
|
exists, err := r.client.Exists(ctx, key).Result()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return exists > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) AcquireLock(ctx context.Context, remote, path string, ttl time.Duration) (bool, error) {
|
||||||
|
key := fmt.Sprintf("lock:%s:%s", remote, path)
|
||||||
|
ok, err := r.client.SetNX(ctx, key, "1", ttl).Result()
|
||||||
|
return ok, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) ReleaseLock(ctx context.Context, remote, path string) error {
|
||||||
|
key := fmt.Sprintf("lock:%s:%s", remote, path)
|
||||||
|
return r.client.Del(ctx, key).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) SetETag(ctx context.Context, remote, path, etag string, ttl time.Duration) error {
|
||||||
|
key := fmt.Sprintf("etag:%s:%s", remote, path)
|
||||||
|
return r.client.Set(ctx, key, etag, ttl).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) GetETag(ctx context.Context, remote, path string) (string, error) {
|
||||||
|
key := fmt.Sprintf("etag:%s:%s", remote, path)
|
||||||
|
val, err := r.client.Get(ctx, key).Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return val, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) IncrCircuitFailure(ctx context.Context, remote string, cooldown time.Duration) (int64, error) {
|
||||||
|
key := fmt.Sprintf("circuit:%s", remote)
|
||||||
|
pipe := r.client.Pipeline()
|
||||||
|
incr := pipe.Incr(ctx, key)
|
||||||
|
pipe.Expire(ctx, key, cooldown)
|
||||||
|
_, err := pipe.Exec(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return incr.Val(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) ResetCircuit(ctx context.Context, remote string) error {
|
||||||
|
key := fmt.Sprintf("circuit:%s", remote)
|
||||||
|
return r.client.Del(ctx, key).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) GetCircuitFailures(ctx context.Context, remote string) (int64, error) {
|
||||||
|
key := fmt.Sprintf("circuit:%s", remote)
|
||||||
|
val, err := r.client.Get(ctx, key).Int64()
|
||||||
|
if err == redis.Nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return val, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Redis) FlushRemote(ctx context.Context, remote string) error {
|
||||||
|
iter := r.client.Scan(ctx, 0, fmt.Sprintf("*:%s:*", remote), 100).Iterator()
|
||||||
|
for iter.Next(ctx) {
|
||||||
|
r.client.Del(ctx, iter.Val())
|
||||||
|
}
|
||||||
|
return iter.Err()
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
ListenAddr string
|
||||||
|
|
||||||
|
DBHost string
|
||||||
|
DBPort int
|
||||||
|
DBUser string
|
||||||
|
DBPass string
|
||||||
|
DBName string
|
||||||
|
DBSSL string
|
||||||
|
|
||||||
|
RedisURL string
|
||||||
|
|
||||||
|
S3Endpoint string
|
||||||
|
S3AccessKey string
|
||||||
|
S3SecretKey string
|
||||||
|
S3Bucket string
|
||||||
|
S3Secure bool
|
||||||
|
S3Region string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) DatabaseDSN() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"postgres://%s:%s@%s:%d/%s?sslmode=%s",
|
||||||
|
c.DBUser, c.DBPass, c.DBHost, c.DBPort, c.DBName, c.DBSSL,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load() (*Config, error) {
|
||||||
|
dbPort, err := strconv.Atoi(getenv("DBPORT", "5432"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid DBPORT: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s3Secure, _ := strconv.ParseBool(getenv("MINIO_SECURE", "false"))
|
||||||
|
|
||||||
|
cfg := &Config{
|
||||||
|
ListenAddr: getenv("LISTEN_ADDR", ":8000"),
|
||||||
|
|
||||||
|
DBHost: getenv("DBHOST", "localhost"),
|
||||||
|
DBPort: dbPort,
|
||||||
|
DBUser: getenv("DBUSER", "artifacts"),
|
||||||
|
DBPass: getenv("DBPASS", ""),
|
||||||
|
DBName: getenv("DBNAME", "artifacts"),
|
||||||
|
DBSSL: getenv("DBSSL", "disable"),
|
||||||
|
|
||||||
|
RedisURL: getenv("REDIS_URL", "redis://localhost:6379"),
|
||||||
|
|
||||||
|
S3Endpoint: getenv("MINIO_ENDPOINT", "localhost:9000"),
|
||||||
|
S3AccessKey: getenv("MINIO_ACCESS_KEY", ""),
|
||||||
|
S3SecretKey: getenv("MINIO_SECRET_KEY", ""),
|
||||||
|
S3Bucket: getenv("MINIO_BUCKET", "artifacts"),
|
||||||
|
S3Secure: s3Secure,
|
||||||
|
S3Region: getenv("MINIO_REGION", ""),
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getenv(key, fallback string) string {
|
||||||
|
if v := os.Getenv(key); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (db *DB) UpsertBlob(ctx context.Context, contentHash, s3Key string, sizeBytes int64, contentType string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
INSERT INTO blobs (content_hash, s3_key, size_bytes, content_type)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (content_hash) DO NOTHING
|
||||||
|
`, contentHash, s3Key, sizeBytes, contentType)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) UpsertArtifact(ctx context.Context, remoteName, path, contentHash, upstreamETag string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
INSERT INTO artifacts (remote_name, path, content_hash, upstream_etag)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (remote_name, path) DO UPDATE SET
|
||||||
|
content_hash = EXCLUDED.content_hash,
|
||||||
|
upstream_etag = EXCLUDED.upstream_etag,
|
||||||
|
last_fetched_at = NOW(),
|
||||||
|
fetch_count = artifacts.fetch_count + 1
|
||||||
|
`, remoteName, path, contentHash, upstreamETag)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetArtifact(ctx context.Context, remoteName, path string) (*models.Artifact, error) {
|
||||||
|
row := db.Pool.QueryRow(ctx, `
|
||||||
|
SELECT a.id, a.remote_name, a.path, a.content_hash, a.upstream_etag,
|
||||||
|
a.upstream_last_modified, a.first_seen_at, a.last_fetched_at,
|
||||||
|
a.last_accessed_at, a.fetch_count, a.access_count,
|
||||||
|
b.size_bytes, b.content_type
|
||||||
|
FROM artifacts a
|
||||||
|
JOIN blobs b ON a.content_hash = b.content_hash
|
||||||
|
WHERE a.remote_name = $1 AND a.path = $2
|
||||||
|
`, remoteName, path)
|
||||||
|
|
||||||
|
var a models.Artifact
|
||||||
|
err := row.Scan(
|
||||||
|
&a.ID, &a.RemoteName, &a.Path, &a.ContentHash, &a.UpstreamETag,
|
||||||
|
&a.UpstreamLastModified, &a.FirstSeenAt, &a.LastFetchedAt,
|
||||||
|
&a.LastAccessedAt, &a.FetchCount, &a.AccessCount,
|
||||||
|
&a.SizeBytes, &a.ContentType,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &a, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) TouchArtifactAccess(ctx context.Context, remoteName, path string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
UPDATE artifacts SET
|
||||||
|
last_accessed_at = NOW(),
|
||||||
|
access_count = access_count + 1
|
||||||
|
WHERE remote_name = $1 AND path = $2
|
||||||
|
`, remoteName, path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListArtifacts(ctx context.Context, remoteName string, limit, offset int) ([]models.Artifact, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT a.id, a.remote_name, a.path, a.content_hash, a.upstream_etag,
|
||||||
|
a.upstream_last_modified, a.first_seen_at, a.last_fetched_at,
|
||||||
|
a.last_accessed_at, a.fetch_count, a.access_count,
|
||||||
|
b.size_bytes, b.content_type
|
||||||
|
FROM artifacts a
|
||||||
|
JOIN blobs b ON a.content_hash = b.content_hash
|
||||||
|
WHERE a.remote_name = $1
|
||||||
|
ORDER BY a.path
|
||||||
|
LIMIT $2 OFFSET $3
|
||||||
|
`, remoteName, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var artifacts []models.Artifact
|
||||||
|
for rows.Next() {
|
||||||
|
var a models.Artifact
|
||||||
|
if err := rows.Scan(
|
||||||
|
&a.ID, &a.RemoteName, &a.Path, &a.ContentHash, &a.UpstreamETag,
|
||||||
|
&a.UpstreamLastModified, &a.FirstSeenAt, &a.LastFetchedAt,
|
||||||
|
&a.LastAccessedAt, &a.FetchCount, &a.AccessCount,
|
||||||
|
&a.SizeBytes, &a.ContentType,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
artifacts = append(artifacts, a)
|
||||||
|
}
|
||||||
|
return artifacts, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteArtifact(ctx context.Context, remoteName, path string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `DELETE FROM artifacts WHERE remote_name = $1 AND path = $2`, remoteName, path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) InsertAccessLog(ctx context.Context, remoteName, path string, cacheHit bool, sizeBytes int64, upstreamMS int, clientIP string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
INSERT INTO access_log (remote_name, path, cache_hit, size_bytes, upstream_ms, client_ip)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
`, remoteName, path, cacheHit, sizeBytes, upstreamMS, clientIP)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) FindOrphanedBlobs(ctx context.Context) ([]models.Blob, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT b.content_hash, b.s3_key, b.size_bytes, b.content_type, b.created_at
|
||||||
|
FROM blobs b
|
||||||
|
WHERE b.content_hash NOT IN (
|
||||||
|
SELECT content_hash FROM artifacts
|
||||||
|
UNION
|
||||||
|
SELECT content_hash FROM local_files
|
||||||
|
)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var blobs []models.Blob
|
||||||
|
for rows.Next() {
|
||||||
|
var b models.Blob
|
||||||
|
if err := rows.Scan(&b.ContentHash, &b.S3Key, &b.SizeBytes, &b.ContentType, &b.CreatedAt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
blobs = append(blobs, b)
|
||||||
|
}
|
||||||
|
return blobs, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteBlob(ctx context.Context, contentHash string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `DELETE FROM blobs WHERE content_hash = $1`, contentHash)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteColdArtifacts(ctx context.Context, remoteName string, olderThan time.Duration) (int64, error) {
|
||||||
|
cutoff := time.Now().Add(-olderThan)
|
||||||
|
tag, err := db.Pool.Exec(ctx, `
|
||||||
|
DELETE FROM artifacts
|
||||||
|
WHERE remote_name = $1 AND last_accessed_at < $2
|
||||||
|
`, remoteName, cutoff)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return tag.RowsAffected(), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
Pool *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(dsn string) (*DB, error) {
|
||||||
|
pool, err := pgxpool.New(context.Background(), dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("connect to postgres: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := pool.Ping(context.Background()); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
return nil, fmt.Errorf("ping postgres: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db := &DB{Pool: pool}
|
||||||
|
if err := db.migrate(); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
return nil, fmt.Errorf("run migrations: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Close() {
|
||||||
|
db.Pool.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) migrate() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
CREATE TABLE IF NOT EXISTS remotes (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
package_type TEXT NOT NULL,
|
||||||
|
base_url TEXT NOT NULL,
|
||||||
|
description TEXT DEFAULT '',
|
||||||
|
username TEXT DEFAULT '',
|
||||||
|
password TEXT DEFAULT '',
|
||||||
|
immutable_ttl INTEGER DEFAULT 0,
|
||||||
|
mutable_ttl INTEGER DEFAULT 3600,
|
||||||
|
check_mutable BOOLEAN DEFAULT TRUE,
|
||||||
|
patterns TEXT[] DEFAULT '{}',
|
||||||
|
blocklist TEXT[] DEFAULT '{}',
|
||||||
|
mutable_patterns TEXT[] DEFAULT '{}',
|
||||||
|
immutable_patterns TEXT[] DEFAULT '{}',
|
||||||
|
ban_tags_enabled BOOLEAN DEFAULT FALSE,
|
||||||
|
ban_tags TEXT[] DEFAULT '{}',
|
||||||
|
quarantine_enabled BOOLEAN DEFAULT FALSE,
|
||||||
|
quarantine_days INTEGER DEFAULT 3,
|
||||||
|
stale_on_error BOOLEAN DEFAULT TRUE,
|
||||||
|
releases_remote TEXT DEFAULT '',
|
||||||
|
managed_by TEXT DEFAULT '',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS virtuals (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
package_type TEXT NOT NULL,
|
||||||
|
description TEXT DEFAULT '',
|
||||||
|
members TEXT[] NOT NULL,
|
||||||
|
managed_by TEXT DEFAULT '',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS blobs (
|
||||||
|
content_hash TEXT PRIMARY KEY,
|
||||||
|
s3_key TEXT NOT NULL,
|
||||||
|
size_bytes BIGINT NOT NULL,
|
||||||
|
content_type TEXT DEFAULT 'application/octet-stream',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS artifacts (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
remote_name TEXT NOT NULL REFERENCES remotes(name) ON DELETE CASCADE,
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
content_hash TEXT NOT NULL REFERENCES blobs(content_hash),
|
||||||
|
upstream_etag TEXT DEFAULT '',
|
||||||
|
upstream_last_modified TIMESTAMPTZ,
|
||||||
|
first_seen_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
last_fetched_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
last_accessed_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
fetch_count BIGINT DEFAULT 1,
|
||||||
|
access_count BIGINT DEFAULT 1,
|
||||||
|
UNIQUE(remote_name, path)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_artifacts_remote ON artifacts(remote_name);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_artifacts_last_accessed ON artifacts(last_accessed_at);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS local_files (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
repo_name TEXT NOT NULL,
|
||||||
|
file_path TEXT NOT NULL,
|
||||||
|
content_hash TEXT NOT NULL REFERENCES blobs(content_hash),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
UNIQUE(repo_name, file_path)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS access_log (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
remote_name TEXT NOT NULL,
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
cache_hit BOOLEAN NOT NULL,
|
||||||
|
size_bytes BIGINT DEFAULT 0,
|
||||||
|
upstream_ms INTEGER DEFAULT 0,
|
||||||
|
client_ip TEXT DEFAULT '',
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_access_log_remote_time ON access_log(remote_name, created_at);
|
||||||
|
`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
const remoteCols = `name, package_type, base_url, description, username, password,
|
||||||
|
immutable_ttl, mutable_ttl, check_mutable,
|
||||||
|
patterns, blocklist, mutable_patterns, immutable_patterns,
|
||||||
|
ban_tags_enabled, ban_tags,
|
||||||
|
quarantine_enabled, quarantine_days, stale_on_error,
|
||||||
|
releases_remote, managed_by, created_at, updated_at`
|
||||||
|
|
||||||
|
func scanRemote(scanner interface{ Scan(...any) error }, r *models.Remote) error {
|
||||||
|
return scanner.Scan(
|
||||||
|
&r.Name, &r.PackageType, &r.BaseURL, &r.Description, &r.Username, &r.Password,
|
||||||
|
&r.ImmutableTTL, &r.MutableTTL, &r.CheckMutable,
|
||||||
|
&r.Patterns, &r.Blocklist, &r.MutablePatterns, &r.ImmutablePatterns,
|
||||||
|
&r.BanTagsEnabled, &r.BanTags,
|
||||||
|
&r.QuarantineEnabled, &r.QuarantineDays, &r.StaleOnError,
|
||||||
|
&r.ReleasesRemote, &r.ManagedBy, &r.CreatedAt, &r.UpdatedAt,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetRemote(ctx context.Context, name string) (*models.Remote, error) {
|
||||||
|
row := db.Pool.QueryRow(ctx, `SELECT `+remoteCols+` FROM remotes WHERE name = $1`, name)
|
||||||
|
var r models.Remote
|
||||||
|
if err := scanRemote(row, &r); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListRemotes(ctx context.Context) ([]models.Remote, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `SELECT `+remoteCols+` FROM remotes ORDER BY name`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var remotes []models.Remote
|
||||||
|
for rows.Next() {
|
||||||
|
var r models.Remote
|
||||||
|
if err := scanRemote(rows, &r); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
remotes = append(remotes, r)
|
||||||
|
}
|
||||||
|
return remotes, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) CreateRemote(ctx context.Context, r *models.Remote) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
INSERT INTO remotes (
|
||||||
|
name, package_type, base_url, description, username, password,
|
||||||
|
immutable_ttl, mutable_ttl, check_mutable,
|
||||||
|
patterns, blocklist, mutable_patterns, immutable_patterns,
|
||||||
|
ban_tags_enabled, ban_tags,
|
||||||
|
quarantine_enabled, quarantine_days, stale_on_error,
|
||||||
|
releases_remote, managed_by
|
||||||
|
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20)
|
||||||
|
`,
|
||||||
|
r.Name, r.PackageType, r.BaseURL, r.Description, r.Username, r.Password,
|
||||||
|
r.ImmutableTTL, r.MutableTTL, r.CheckMutable,
|
||||||
|
r.Patterns, r.Blocklist, r.MutablePatterns, r.ImmutablePatterns,
|
||||||
|
r.BanTagsEnabled, r.BanTags,
|
||||||
|
r.QuarantineEnabled, r.QuarantineDays, r.StaleOnError,
|
||||||
|
r.ReleasesRemote, r.ManagedBy,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) UpdateRemote(ctx context.Context, r *models.Remote) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
UPDATE remotes SET
|
||||||
|
package_type=$2, base_url=$3, description=$4, username=$5, password=$6,
|
||||||
|
immutable_ttl=$7, mutable_ttl=$8, check_mutable=$9,
|
||||||
|
patterns=$10, blocklist=$11, mutable_patterns=$12, immutable_patterns=$13,
|
||||||
|
ban_tags_enabled=$14, ban_tags=$15,
|
||||||
|
quarantine_enabled=$16, quarantine_days=$17, stale_on_error=$18,
|
||||||
|
releases_remote=$19, managed_by=$20, updated_at=NOW()
|
||||||
|
WHERE name=$1
|
||||||
|
`,
|
||||||
|
r.Name, r.PackageType, r.BaseURL, r.Description, r.Username, r.Password,
|
||||||
|
r.ImmutableTTL, r.MutableTTL, r.CheckMutable,
|
||||||
|
r.Patterns, r.Blocklist, r.MutablePatterns, r.ImmutablePatterns,
|
||||||
|
r.BanTagsEnabled, r.BanTags,
|
||||||
|
r.QuarantineEnabled, r.QuarantineDays, r.StaleOnError,
|
||||||
|
r.ReleasesRemote, r.ManagedBy,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteRemote(ctx context.Context, name string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `DELETE FROM remotes WHERE name = $1`, name)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (db *DB) GetOverviewStats(ctx context.Context) (*models.OverviewStats, error) {
|
||||||
|
var stats models.OverviewStats
|
||||||
|
|
||||||
|
err := db.Pool.QueryRow(ctx, `SELECT COUNT(*) FROM remotes`).Scan(&stats.TotalRemotes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Pool.QueryRow(ctx, `SELECT COALESCE(COUNT(*), 0), COALESCE(SUM(b.size_bytes), 0)
|
||||||
|
FROM artifacts a JOIN blobs b ON a.content_hash = b.content_hash`).
|
||||||
|
Scan(&stats.TotalObjects, &stats.TotalBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Pool.QueryRow(ctx, `
|
||||||
|
SELECT COALESCE(
|
||||||
|
(SELECT COUNT(*) FROM artifacts) - (SELECT COUNT(DISTINCT content_hash) FROM artifacts),
|
||||||
|
0
|
||||||
|
)`).Scan(&stats.TotalBlobsDeduped)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &stats, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type RemoteStatRow struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ObjectCount int64 `json:"object_count"`
|
||||||
|
TotalBytes int64 `json:"total_bytes"`
|
||||||
|
Requests30d int64 `json:"requests_30d"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetTopRemotes(ctx context.Context, limit int) ([]RemoteStatRow, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT r.name,
|
||||||
|
COALESCE(a.cnt, 0) AS object_count,
|
||||||
|
COALESCE(a.total_bytes, 0) AS total_bytes,
|
||||||
|
COALESCE(l.req_count, 0) AS requests_30d
|
||||||
|
FROM remotes r
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT remote_name, COUNT(*) AS cnt, SUM(b.size_bytes) AS total_bytes
|
||||||
|
FROM artifacts a JOIN blobs b ON a.content_hash = b.content_hash
|
||||||
|
GROUP BY remote_name
|
||||||
|
) a ON r.name = a.remote_name
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT remote_name, COUNT(*) AS req_count
|
||||||
|
FROM access_log
|
||||||
|
WHERE created_at > NOW() - INTERVAL '30 days'
|
||||||
|
GROUP BY remote_name
|
||||||
|
) l ON r.name = l.remote_name
|
||||||
|
ORDER BY COALESCE(a.total_bytes, 0) DESC
|
||||||
|
LIMIT $1
|
||||||
|
`, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var result []RemoteStatRow
|
||||||
|
for rows.Next() {
|
||||||
|
var r RemoteStatRow
|
||||||
|
if err := rows.Scan(&r.Name, &r.ObjectCount, &r.TotalBytes, &r.Requests30d); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, r)
|
||||||
|
}
|
||||||
|
return result, rows.Err()
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (db *DB) GetVirtual(ctx context.Context, name string) (*models.Virtual, error) {
|
||||||
|
row := db.Pool.QueryRow(ctx, `
|
||||||
|
SELECT name, package_type, description, members, managed_by, created_at, updated_at
|
||||||
|
FROM virtuals WHERE name = $1
|
||||||
|
`, name)
|
||||||
|
|
||||||
|
var v models.Virtual
|
||||||
|
err := row.Scan(&v.Name, &v.PackageType, &v.Description, &v.Members, &v.ManagedBy, &v.CreatedAt, &v.UpdatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListVirtuals(ctx context.Context) ([]models.Virtual, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT name, package_type, description, members, managed_by, created_at, updated_at
|
||||||
|
FROM virtuals ORDER BY name
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var virtuals []models.Virtual
|
||||||
|
for rows.Next() {
|
||||||
|
var v models.Virtual
|
||||||
|
if err := rows.Scan(&v.Name, &v.PackageType, &v.Description, &v.Members, &v.ManagedBy, &v.CreatedAt, &v.UpdatedAt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
virtuals = append(virtuals, v)
|
||||||
|
}
|
||||||
|
return virtuals, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) CreateVirtual(ctx context.Context, v *models.Virtual) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
INSERT INTO virtuals (name, package_type, description, members, managed_by)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
`, v.Name, v.PackageType, v.Description, v.Members, v.ManagedBy)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) UpdateVirtual(ctx context.Context, v *models.Virtual) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
UPDATE virtuals SET
|
||||||
|
package_type=$2, description=$3, members=$4, managed_by=$5, updated_at=NOW()
|
||||||
|
WHERE name=$1
|
||||||
|
`, v.Name, v.PackageType, v.Description, v.Members, v.ManagedBy)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteVirtual(ctx context.Context, name string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `DELETE FROM virtuals WHERE name = $1`, name)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package gc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Collector struct {
|
||||||
|
db *database.DB
|
||||||
|
store *storage.S3
|
||||||
|
interval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(db *database.DB, store *storage.S3, interval time.Duration) *Collector {
|
||||||
|
return &Collector{db: db, store: store, interval: interval}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) Run(ctx context.Context) {
|
||||||
|
slog.Info("gc started", "interval", c.interval)
|
||||||
|
ticker := time.NewTicker(c.interval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
slog.Info("gc stopped")
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
c.sweep(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Collector) sweep(ctx context.Context) {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
orphaned, err := c.db.FindOrphanedBlobs(ctx)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("gc: find orphaned blobs", "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deleted := 0
|
||||||
|
for _, blob := range orphaned {
|
||||||
|
if err := c.store.Delete(ctx, blob.S3Key); err != nil {
|
||||||
|
slog.Warn("gc: delete s3 object", "key", blob.S3Key, "error", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := c.db.DeleteBlob(ctx, blob.ContentHash); err != nil {
|
||||||
|
slog.Warn("gc: delete blob row", "hash", blob.ContentHash, "error", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
deleted++
|
||||||
|
}
|
||||||
|
|
||||||
|
if deleted > 0 || len(orphaned) > 0 {
|
||||||
|
slog.Info("gc sweep complete",
|
||||||
|
"orphaned_found", len(orphaned),
|
||||||
|
"deleted", deleted,
|
||||||
|
"duration_ms", time.Since(start).Milliseconds(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package gc_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/gc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
c := gc.New(nil, nil, 1*time.Hour)
|
||||||
|
if c == nil {
|
||||||
|
t.Fatal("expected non-nil collector")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package alpine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageAlpine }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if strings.HasSuffix(path, "APKINDEX.tar.gz") {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.HasSuffix(path, ".apk") {
|
||||||
|
return "application/vnd.android.package-archive"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(path, ".tar.gz") {
|
||||||
|
return "application/gzip"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(_ []byte, _ models.Remote, _ string) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package docker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
tagManifestRe = regexp.MustCompile(`/manifests/[^/]+$`)
|
||||||
|
digestManifestRe = regexp.MustCompile(`/manifests/sha256:[0-9a-fA-F]+$`)
|
||||||
|
tagsListRe = regexp.MustCompile(`/tags/list$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageDocker }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if tagsListRe.MatchString(path) {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
if tagManifestRe.MatchString(path) && !digestManifestRe.MatchString(path) {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.Contains(path, "/blobs/") {
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
if strings.Contains(path, "/manifests/") {
|
||||||
|
return "application/vnd.docker.distribution.manifest.v2+json"
|
||||||
|
}
|
||||||
|
return "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/v2/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(_ []byte, _ models.Remote, _ string) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
h := http.Header{}
|
||||||
|
if remote.Username != "" && remote.Password != "" {
|
||||||
|
h.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(remote.Username+":"+remote.Password)))
|
||||||
|
}
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package docker_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/docker"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProvider_Type(t *testing.T) {
|
||||||
|
p := &docker.Provider{}
|
||||||
|
if p.Type() != models.PackageDocker {
|
||||||
|
t.Errorf("expected docker, got %q", p.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_Classify(t *testing.T) {
|
||||||
|
p := &docker.Provider{}
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
want provider.Mutability
|
||||||
|
}{
|
||||||
|
{"library/nginx/manifests/latest", provider.Mutable},
|
||||||
|
{"library/nginx/manifests/v1.25", provider.Mutable},
|
||||||
|
{"library/nginx/manifests/sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", provider.Immutable},
|
||||||
|
{"library/nginx/tags/list", provider.Mutable},
|
||||||
|
{"library/nginx/blobs/sha256:abc123", provider.Immutable},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.Classify(tt.path); got != tt.want {
|
||||||
|
t.Errorf("Classify(%q) = %v, want %v", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_UpstreamURL(t *testing.T) {
|
||||||
|
p := &docker.Provider{}
|
||||||
|
got := p.UpstreamURL(models.Remote{BaseURL: "https://registry-1.docker.io"}, "library/nginx/manifests/latest")
|
||||||
|
want := "https://registry-1.docker.io/v2/library/nginx/manifests/latest"
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_ContentType(t *testing.T) {
|
||||||
|
p := &docker.Provider{}
|
||||||
|
if p.ContentType("x/blobs/sha256:abc") != "application/octet-stream" {
|
||||||
|
t.Error("blobs should be octet-stream")
|
||||||
|
}
|
||||||
|
if p.ContentType("x/manifests/latest") != "application/vnd.docker.distribution.manifest.v2+json" {
|
||||||
|
t.Error("manifests should be manifest type")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageGeneric }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(_ string) provider.Mutability {
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
var contentTypeMap = map[string]string{
|
||||||
|
".tar.gz": "application/gzip",
|
||||||
|
".tgz": "application/gzip",
|
||||||
|
".gz": "application/gzip",
|
||||||
|
".zip": "application/zip",
|
||||||
|
".whl": "application/zip",
|
||||||
|
".exe": "application/x-msdownload",
|
||||||
|
".rpm": "application/x-rpm",
|
||||||
|
".xml": "application/xml",
|
||||||
|
".yaml": "text/yaml",
|
||||||
|
".yml": "text/yaml",
|
||||||
|
".json": "application/json",
|
||||||
|
".sig": "application/octet-stream",
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(filePath string) string {
|
||||||
|
lower := strings.ToLower(filePath)
|
||||||
|
if strings.HasSuffix(lower, ".tar.gz") {
|
||||||
|
return "application/gzip"
|
||||||
|
}
|
||||||
|
ext := path.Ext(lower)
|
||||||
|
if ct, ok := contentTypeMap[ext]; ok {
|
||||||
|
return ct
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, reqPath string) string {
|
||||||
|
base := strings.TrimRight(remote.BaseURL, "/")
|
||||||
|
return base + "/" + strings.TrimLeft(reqPath, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(_ []byte, _ models.Remote, _ string) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
h := http.Header{}
|
||||||
|
if remote.Username != "" {
|
||||||
|
h.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(remote.Username+":"+remote.Password)))
|
||||||
|
}
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package generic_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/generic"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProvider_Type(t *testing.T) {
|
||||||
|
p := &generic.Provider{}
|
||||||
|
if p.Type() != models.PackageGeneric {
|
||||||
|
t.Errorf("expected generic, got %q", p.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_Classify_AllImmutable(t *testing.T) {
|
||||||
|
p := &generic.Provider{}
|
||||||
|
paths := []string{"file.tar.gz", "path/to/binary", "index.html", "data.json"}
|
||||||
|
for _, path := range paths {
|
||||||
|
if p.Classify(path) != provider.Immutable {
|
||||||
|
t.Errorf("generic should classify %q as immutable", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_ContentType(t *testing.T) {
|
||||||
|
p := &generic.Provider{}
|
||||||
|
tests := []struct{ path, want string }{
|
||||||
|
{"file.tar.gz", "application/gzip"},
|
||||||
|
{"file.tgz", "application/gzip"},
|
||||||
|
{"file.zip", "application/zip"},
|
||||||
|
{"file.rpm", "application/x-rpm"},
|
||||||
|
{"file.json", "application/json"},
|
||||||
|
{"file.unknown", "application/octet-stream"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.ContentType(tt.path); got != tt.want {
|
||||||
|
t.Errorf("ContentType(%q) = %q, want %q", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_UpstreamURL(t *testing.T) {
|
||||||
|
p := &generic.Provider{}
|
||||||
|
got := p.UpstreamURL(models.Remote{BaseURL: "https://example.com/repo"}, "path/to/file.tar.gz")
|
||||||
|
want := "https://example.com/repo/path/to/file.tar.gz"
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_AuthHeaders_BasicAuth(t *testing.T) {
|
||||||
|
p := &generic.Provider{}
|
||||||
|
h, _ := p.AuthHeaders(context.Background(), models.Remote{Username: "user", Password: "pass"})
|
||||||
|
if h.Get("Authorization") != "Basic dXNlcjpwYXNz" {
|
||||||
|
t.Errorf("unexpected auth header: %q", h.Get("Authorization"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_AuthHeaders_NoAuth(t *testing.T) {
|
||||||
|
p := &generic.Provider{}
|
||||||
|
h, _ := p.AuthHeaders(context.Background(), models.Remote{})
|
||||||
|
if h.Get("Authorization") != "" {
|
||||||
|
t.Error("expected no auth header")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package goproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageGoProxy }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if strings.HasSuffix(path, "/@v/list") || strings.HasSuffix(path, "/@latest") {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.HasSuffix(path, ".zip") {
|
||||||
|
return "application/zip"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(path, ".mod") {
|
||||||
|
return "text/plain"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(path, ".info") {
|
||||||
|
return "application/json"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(path, "/list") {
|
||||||
|
return "text/plain"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(_ []byte, _ models.Remote, _ string) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package goproxy_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/goproxy"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProvider_Type(t *testing.T) {
|
||||||
|
p := &goproxy.Provider{}
|
||||||
|
if p.Type() != models.PackageGoProxy {
|
||||||
|
t.Errorf("expected goproxy, got %q", p.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_Classify(t *testing.T) {
|
||||||
|
p := &goproxy.Provider{}
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
want provider.Mutability
|
||||||
|
}{
|
||||||
|
{"golang.org/x/net/@v/list", provider.Mutable},
|
||||||
|
{"golang.org/x/net/@latest", provider.Mutable},
|
||||||
|
{"golang.org/x/net/@v/v0.1.0.info", provider.Immutable},
|
||||||
|
{"golang.org/x/net/@v/v0.1.0.mod", provider.Immutable},
|
||||||
|
{"golang.org/x/net/@v/v0.1.0.zip", provider.Immutable},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.Classify(tt.path); got != tt.want {
|
||||||
|
t.Errorf("Classify(%q) = %v, want %v", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_ContentType(t *testing.T) {
|
||||||
|
p := &goproxy.Provider{}
|
||||||
|
tests := []struct{ path, want string }{
|
||||||
|
{"m/@v/v1.0.0.zip", "application/zip"},
|
||||||
|
{"m/@v/v1.0.0.mod", "text/plain"},
|
||||||
|
{"m/@v/v1.0.0.info", "application/json"},
|
||||||
|
{"m/@v/list", "text/plain"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.ContentType(tt.path); got != tt.want {
|
||||||
|
t.Errorf("ContentType(%q) = %q, want %q", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package helm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageHelm }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if strings.HasSuffix(path, "index.yaml") || strings.HasSuffix(path, "index.yml") {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.HasSuffix(path, ".tgz") || strings.HasSuffix(path, ".tar.gz") {
|
||||||
|
return "application/gzip"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
|
||||||
|
return "text/yaml"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error) {
|
||||||
|
if proxyBaseURL == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
content := string(body)
|
||||||
|
baseURL := strings.TrimRight(remote.BaseURL, "/")
|
||||||
|
proxyURL := strings.TrimRight(proxyBaseURL, "/") + "/api/v1/remote/" + remote.Name
|
||||||
|
rewritten := strings.ReplaceAll(content, baseURL, proxyURL)
|
||||||
|
if rewritten == content {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return []byte(rewritten), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package helm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/helm"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProvider_Type(t *testing.T) {
|
||||||
|
p := &helm.Provider{}
|
||||||
|
if p.Type() != models.PackageHelm {
|
||||||
|
t.Errorf("expected helm, got %q", p.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_Classify(t *testing.T) {
|
||||||
|
p := &helm.Provider{}
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
want provider.Mutability
|
||||||
|
}{
|
||||||
|
{"index.yaml", provider.Mutable},
|
||||||
|
{"index.yml", provider.Mutable},
|
||||||
|
{"chart-1.0.tgz", provider.Immutable},
|
||||||
|
{"charts/nginx-1.0.tgz", provider.Immutable},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.Classify(tt.path); got != tt.want {
|
||||||
|
t.Errorf("Classify(%q) = %v, want %v", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_RewriteResponse(t *testing.T) {
|
||||||
|
p := &helm.Provider{}
|
||||||
|
body := []byte("urls:\n- https://charts.example.com/chart-1.0.tgz")
|
||||||
|
remote := models.Remote{Name: "helm-test", BaseURL: "https://charts.example.com"}
|
||||||
|
rewritten, err := p.RewriteResponse(body, remote, "https://proxy.example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if rewritten == nil {
|
||||||
|
t.Fatal("expected rewrite")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(rewritten), "proxy.example.com/api/v1/remote/helm-test") {
|
||||||
|
t.Errorf("expected proxy URL in body: %s", rewritten)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package npm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageNPM }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if strings.HasSuffix(path, ".tgz") {
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.HasSuffix(path, ".tgz") {
|
||||||
|
return "application/gzip"
|
||||||
|
}
|
||||||
|
return "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error) {
|
||||||
|
if proxyBaseURL == "" || !json.Valid(body) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
content := string(body)
|
||||||
|
baseURL := strings.TrimRight(remote.BaseURL, "/")
|
||||||
|
proxyURL := strings.TrimRight(proxyBaseURL, "/") + "/api/v1/remote/" + remote.Name
|
||||||
|
rewritten := strings.ReplaceAll(content, baseURL, proxyURL)
|
||||||
|
if rewritten == content {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return []byte(rewritten), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mutability int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Immutable Mutability = iota
|
||||||
|
Mutable
|
||||||
|
)
|
||||||
|
|
||||||
|
type Provider interface {
|
||||||
|
Type() models.PackageType
|
||||||
|
Classify(path string) Mutability
|
||||||
|
ContentType(path string) string
|
||||||
|
UpstreamURL(remote models.Remote, path string) string
|
||||||
|
RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error)
|
||||||
|
AuthHeaders(ctx context.Context, remote models.Remote) (http.Header, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndexMerger interface {
|
||||||
|
MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type MemberIndex struct {
|
||||||
|
RemoteName string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var registry = map[models.PackageType]Provider{}
|
||||||
|
|
||||||
|
func Register(p Provider) {
|
||||||
|
registry[p.Type()] = p
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(t models.PackageType) (Provider, error) {
|
||||||
|
p, ok := registry[t]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("no provider registered for package type %q", t)
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func All() map[models.PackageType]Provider {
|
||||||
|
return registry
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package puppet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackagePuppet }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if strings.HasPrefix(path, "v3/modules/") || strings.HasPrefix(path, "v3/releases") {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.HasSuffix(path, ".tar.gz") {
|
||||||
|
return "application/gzip"
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(path, "v3/") {
|
||||||
|
return "application/json"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error) {
|
||||||
|
if proxyBaseURL == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
content := string(body)
|
||||||
|
proxyURL := strings.TrimRight(proxyBaseURL, "/") + "/api/v1/remote/" + remote.Name
|
||||||
|
content = strings.ReplaceAll(content, `"/v3/files/`, `"`+proxyURL+`/v3/files/`)
|
||||||
|
baseURL := strings.TrimRight(remote.BaseURL, "/")
|
||||||
|
content = strings.ReplaceAll(content, baseURL, proxyURL)
|
||||||
|
return []byte(content), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package pypi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackagePyPI }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if strings.Contains(path, "simple/") {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
lower := strings.ToLower(path)
|
||||||
|
if strings.HasSuffix(lower, ".whl") || strings.HasSuffix(lower, ".zip") {
|
||||||
|
return "application/zip"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(lower, ".tar.gz") {
|
||||||
|
return "application/gzip"
|
||||||
|
}
|
||||||
|
if strings.Contains(path, "simple/") {
|
||||||
|
return "text/html"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
if strings.HasPrefix(path, "simple/") {
|
||||||
|
return "https://pypi.org/" + path
|
||||||
|
}
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error) {
|
||||||
|
if proxyBaseURL == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
content := string(body)
|
||||||
|
proxyURL := strings.TrimRight(proxyBaseURL, "/") + "/api/v1/remote/" + remote.Name + "/"
|
||||||
|
content = strings.ReplaceAll(content, "https://files.pythonhosted.org/", proxyURL)
|
||||||
|
content = strings.ReplaceAll(content, "../../", proxyURL)
|
||||||
|
return []byte(content), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package rpm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var mutableRe = []*regexp.Regexp{
|
||||||
|
regexp.MustCompile(`repomd\.xml$`),
|
||||||
|
regexp.MustCompile(`repodata/`),
|
||||||
|
regexp.MustCompile(`Packages\.gz$`),
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageRPM }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
for _, re := range mutableRe {
|
||||||
|
if re.MatchString(path) {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
if strings.HasSuffix(path, ".rpm") {
|
||||||
|
return "application/x-rpm"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(path, ".xml") || strings.HasSuffix(path, ".xml.gz") || strings.HasSuffix(path, ".xml.xz") {
|
||||||
|
return "application/xml"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(_ []byte, _ models.Remote, _ string) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package rpm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/rpm"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProvider_Type(t *testing.T) {
|
||||||
|
p := &rpm.Provider{}
|
||||||
|
if p.Type() != models.PackageRPM {
|
||||||
|
t.Errorf("expected rpm, got %q", p.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_Classify(t *testing.T) {
|
||||||
|
p := &rpm.Provider{}
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
want provider.Mutability
|
||||||
|
}{
|
||||||
|
{"repomd.xml", provider.Mutable},
|
||||||
|
{"repodata/primary.xml.gz", provider.Mutable},
|
||||||
|
{"Packages.gz", provider.Mutable},
|
||||||
|
{"package-1.0.rpm", provider.Immutable},
|
||||||
|
{"RPM-GPG-KEY-almalinux", provider.Immutable},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.Classify(tt.path); got != tt.want {
|
||||||
|
t.Errorf("Classify(%q) = %v, want %v", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package terraform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
provider.Register(&Provider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var versionsRe = regexp.MustCompile(`[^/]+/[^/]+/versions$`)
|
||||||
|
|
||||||
|
type Provider struct{}
|
||||||
|
|
||||||
|
func (p *Provider) Type() models.PackageType { return models.PackageTerraform }
|
||||||
|
|
||||||
|
func (p *Provider) Classify(path string) provider.Mutability {
|
||||||
|
if versionsRe.MatchString(path) {
|
||||||
|
return provider.Mutable
|
||||||
|
}
|
||||||
|
return provider.Immutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ContentType(path string) string {
|
||||||
|
lower := strings.ToLower(path)
|
||||||
|
if strings.HasSuffix(lower, ".zip") {
|
||||||
|
return "application/zip"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(lower, ".sig") {
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
return "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UpstreamURL(remote models.Remote, path string) string {
|
||||||
|
return strings.TrimRight(remote.BaseURL, "/") + "/v1/providers/" + strings.TrimLeft(path, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) RewriteResponse(body []byte, remote models.Remote, proxyBaseURL string) ([]byte, error) {
|
||||||
|
if remote.ReleasesRemote == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if !json.Valid(body) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var data map[string]any
|
||||||
|
if err := json.Unmarshal(body, &data); err != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
changed := false
|
||||||
|
for _, field := range []string{"download_url", "shasums_url", "shasums_signature_url"} {
|
||||||
|
if val, ok := data[field].(string); ok && val != "" {
|
||||||
|
rewritten := rewriteDownloadURL(val, remote.ReleasesRemote, proxyBaseURL)
|
||||||
|
if rewritten != val {
|
||||||
|
data[field] = rewritten
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !changed {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return json.Marshal(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rewriteDownloadURL(originalURL, releasesRemote, proxyBaseURL string) string {
|
||||||
|
parsed, err := url.Parse(originalURL)
|
||||||
|
if err != nil || proxyBaseURL == "" {
|
||||||
|
return originalURL
|
||||||
|
}
|
||||||
|
return strings.TrimRight(proxyBaseURL, "/") + "/api/v1/remote/" + releasesRemote + parsed.Path
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
|
return auth.BasicHeaders(remote), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package terraform_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/terraform"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProvider_Type(t *testing.T) {
|
||||||
|
p := &terraform.Provider{}
|
||||||
|
if p.Type() != models.PackageTerraform {
|
||||||
|
t.Errorf("expected terraform, got %q", p.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_Classify(t *testing.T) {
|
||||||
|
p := &terraform.Provider{}
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
want provider.Mutability
|
||||||
|
}{
|
||||||
|
{"hashicorp/vault/versions", provider.Mutable},
|
||||||
|
{"hashicorp/vault/0.28.0/download/linux/amd64", provider.Immutable},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := p.Classify(tt.path); got != tt.want {
|
||||||
|
t.Errorf("Classify(%q) = %v, want %v", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProvider_RewriteResponse_DownloadInfo(t *testing.T) {
|
||||||
|
p := &terraform.Provider{}
|
||||||
|
remote := models.Remote{Name: "tf", ReleasesRemote: "hashicorp-releases"}
|
||||||
|
body, _ := json.Marshal(map[string]any{
|
||||||
|
"download_url": "https://releases.hashicorp.com/terraform-provider-vault/0.28.0/file.zip",
|
||||||
|
"shasums_url": "https://releases.hashicorp.com/terraform-provider-vault/0.28.0/SHA256SUMS",
|
||||||
|
})
|
||||||
|
rewritten, err := p.RewriteResponse(body, remote, "https://proxy")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if rewritten == nil {
|
||||||
|
t.Fatal("expected rewrite")
|
||||||
|
}
|
||||||
|
var result map[string]any
|
||||||
|
json.Unmarshal(rewritten, &result)
|
||||||
|
if !strings.Contains(result["download_url"].(string), "proxy/api/v1/remote/hashicorp-releases") {
|
||||||
|
t.Errorf("download_url not rewritten: %s", result["download_url"])
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/cache"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultCircuitThreshold = 5
|
||||||
|
defaultCircuitCooldown = 60 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type CircuitBreaker struct {
|
||||||
|
cache *cache.Redis
|
||||||
|
threshold int64
|
||||||
|
cooldown time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCircuitBreaker(c *cache.Redis) *CircuitBreaker {
|
||||||
|
return &CircuitBreaker{
|
||||||
|
cache: c,
|
||||||
|
threshold: defaultCircuitThreshold,
|
||||||
|
cooldown: defaultCircuitCooldown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *CircuitBreaker) IsOpen(ctx context.Context, remote string) bool {
|
||||||
|
failures, err := cb.cache.GetCircuitFailures(ctx, remote)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return failures >= cb.threshold
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *CircuitBreaker) RecordFailure(ctx context.Context, remote string) {
|
||||||
|
cb.cache.IncrCircuitFailure(ctx, remote, cb.cooldown)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *CircuitBreaker) RecordSuccess(ctx context.Context, remote string) {
|
||||||
|
cb.cache.ResetCircuit(ctx, remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *CircuitBreaker) Health(ctx context.Context, remote string) models.RemoteHealth {
|
||||||
|
failures, err := cb.cache.GetCircuitFailures(ctx, remote)
|
||||||
|
if err != nil {
|
||||||
|
return models.RemoteHealth{Status: "unknown"}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case failures == 0:
|
||||||
|
return models.RemoteHealth{Status: "healthy", ConsecutiveFailures: int(failures)}
|
||||||
|
case failures < cb.threshold:
|
||||||
|
return models.RemoteHealth{Status: "degraded", ConsecutiveFailures: int(failures)}
|
||||||
|
default:
|
||||||
|
return models.RemoteHealth{Status: "down", ConsecutiveFailures: int(failures)}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package proxy_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCircuitBreaker_New(t *testing.T) {
|
||||||
|
cb := proxy.NewCircuitBreaker(nil)
|
||||||
|
if cb == nil {
|
||||||
|
t.Fatal("expected non-nil circuit breaker")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Classification int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ClassImmutable Classification = iota
|
||||||
|
ClassMutable
|
||||||
|
ClassDenied
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c Classification) String() string {
|
||||||
|
switch c {
|
||||||
|
case ClassImmutable:
|
||||||
|
return "immutable"
|
||||||
|
case ClassMutable:
|
||||||
|
return "mutable"
|
||||||
|
case ClassDenied:
|
||||||
|
return "denied"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Classifier struct {
|
||||||
|
provider provider.Provider
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClassifier(p provider.Provider) *Classifier {
|
||||||
|
return &Classifier{provider: p}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Classifier) Classify(remote models.Remote, path string) Classification {
|
||||||
|
if matchesAny(path, compilePatterns(remote.Blocklist)) {
|
||||||
|
return ClassDenied
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(remote.Patterns) > 0 && !matchesAny(path, compilePatterns(remote.Patterns)) {
|
||||||
|
return ClassDenied
|
||||||
|
}
|
||||||
|
|
||||||
|
if matchesAny(path, compilePatterns(remote.ImmutablePatterns)) {
|
||||||
|
return ClassImmutable
|
||||||
|
}
|
||||||
|
|
||||||
|
if matchesAny(path, compilePatterns(remote.MutablePatterns)) {
|
||||||
|
return ClassMutable
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.provider.Classify(path) == provider.Mutable {
|
||||||
|
return ClassMutable
|
||||||
|
}
|
||||||
|
|
||||||
|
return ClassImmutable
|
||||||
|
}
|
||||||
|
|
||||||
|
func compilePatterns(patterns []string) []*regexp.Regexp {
|
||||||
|
compiled := make([]*regexp.Regexp, 0, len(patterns))
|
||||||
|
for _, p := range patterns {
|
||||||
|
if re, err := regexp.Compile(p); err == nil {
|
||||||
|
compiled = append(compiled, re)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return compiled
|
||||||
|
}
|
||||||
|
|
||||||
|
func matchesAny(path string, patterns []*regexp.Regexp) bool {
|
||||||
|
for _, re := range patterns {
|
||||||
|
if re.MatchString(path) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
package proxy_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/docker"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/generic"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/helm"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider/rpm"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestClassifier_EmptyPatternsAllowsAll(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&generic.Provider{})
|
||||||
|
remote := models.Remote{Name: "test"}
|
||||||
|
if c.Classify(remote, "any/path") == proxy.ClassDenied {
|
||||||
|
t.Error("empty patterns should allow all paths")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_PatternsActAsAllowlist(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&generic.Provider{})
|
||||||
|
remote := models.Remote{
|
||||||
|
Name: "test",
|
||||||
|
Patterns: []string{`^releases/`},
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "releases/v1.0/app.tar.gz") == proxy.ClassDenied {
|
||||||
|
t.Error("path matching patterns should be allowed")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "uploads/other.tar.gz") != proxy.ClassDenied {
|
||||||
|
t.Error("path not matching patterns should be denied")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_BlocklistDenies(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&generic.Provider{})
|
||||||
|
remote := models.Remote{
|
||||||
|
Name: "test",
|
||||||
|
Blocklist: []string{`\.exe$`},
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "malware.exe") != proxy.ClassDenied {
|
||||||
|
t.Error("blocklist match should deny")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "legit.tar.gz") == proxy.ClassDenied {
|
||||||
|
t.Error("non-blocked path should be allowed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_BlocklistBeforePatterns(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&generic.Provider{})
|
||||||
|
remote := models.Remote{
|
||||||
|
Name: "test",
|
||||||
|
Patterns: []string{`^releases/`},
|
||||||
|
Blocklist: []string{`releases/v0\.1/`},
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "releases/v0.1/app.tar.gz") != proxy.ClassDenied {
|
||||||
|
t.Error("blocklist should take priority")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_GenericAllImmutable(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&generic.Provider{})
|
||||||
|
remote := models.Remote{Name: "test"}
|
||||||
|
if c.Classify(remote, "any/file.tar.gz") != proxy.ClassImmutable {
|
||||||
|
t.Error("generic provider should classify everything as immutable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_GenericMutableOverride(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&generic.Provider{})
|
||||||
|
remote := models.Remote{
|
||||||
|
Name: "test",
|
||||||
|
MutablePatterns: []string{`/archive/refs/heads/`},
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "repo/archive/refs/heads/main.tar.gz") != proxy.ClassMutable {
|
||||||
|
t.Error("mutable_patterns should override provider default")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "repo/releases/v1.0.tar.gz") != proxy.ClassImmutable {
|
||||||
|
t.Error("non-mutable path should stay immutable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_ImmutableOverride(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&helm.Provider{})
|
||||||
|
remote := models.Remote{
|
||||||
|
Name: "test",
|
||||||
|
ImmutablePatterns: []string{`special-index\.yaml$`},
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "special-index.yaml") != proxy.ClassImmutable {
|
||||||
|
t.Error("immutable_patterns should force immutable even for normally mutable paths")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_HelmAutoClassifies(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&helm.Provider{})
|
||||||
|
remote := models.Remote{Name: "test"}
|
||||||
|
if c.Classify(remote, "index.yaml") != proxy.ClassMutable {
|
||||||
|
t.Error("helm should auto-classify index.yaml as mutable")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "chart-1.0.tgz") != proxy.ClassImmutable {
|
||||||
|
t.Error("helm should auto-classify .tgz as immutable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_DockerAutoClassifies(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&docker.Provider{})
|
||||||
|
remote := models.Remote{Name: "test"}
|
||||||
|
if c.Classify(remote, "library/nginx/manifests/latest") != proxy.ClassMutable {
|
||||||
|
t.Error("docker should classify tag manifest as mutable")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "library/nginx/manifests/sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890") != proxy.ClassImmutable {
|
||||||
|
t.Error("docker should classify digest manifest as immutable")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "library/nginx/blobs/sha256:abc") != proxy.ClassImmutable {
|
||||||
|
t.Error("docker should classify blobs as immutable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClassifier_RPMAutoClassifies(t *testing.T) {
|
||||||
|
c := proxy.NewClassifier(&rpm.Provider{})
|
||||||
|
remote := models.Remote{Name: "test"}
|
||||||
|
if c.Classify(remote, "repodata/primary.xml.gz") != proxy.ClassMutable {
|
||||||
|
t.Error("rpm should classify repodata as mutable")
|
||||||
|
}
|
||||||
|
if c.Classify(remote, "packages/foo-1.0.rpm") != proxy.ClassImmutable {
|
||||||
|
t.Error("rpm should classify .rpm as immutable")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,341 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/cache"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/storage"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
const fetchLockTTL = 30 * time.Second
|
||||||
|
|
||||||
|
type Engine struct {
|
||||||
|
db *database.DB
|
||||||
|
cache *cache.Redis
|
||||||
|
store *storage.S3
|
||||||
|
cas *storage.CAS
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEngine(db *database.DB, c *cache.Redis, s *storage.S3) *Engine {
|
||||||
|
return &Engine{
|
||||||
|
db: db,
|
||||||
|
cache: c,
|
||||||
|
store: s,
|
||||||
|
cas: storage.NewCAS(s),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FetchResult struct {
|
||||||
|
Reader io.ReadCloser
|
||||||
|
ContentType string
|
||||||
|
Size int64
|
||||||
|
Source string // "cache" or "remote"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) Fetch(ctx context.Context, remote models.Remote, path string, prov provider.Provider) (*FetchResult, error) {
|
||||||
|
classifier := NewClassifier(prov)
|
||||||
|
class := classifier.Classify(remote, path)
|
||||||
|
|
||||||
|
if class == ClassDenied {
|
||||||
|
return nil, &ProxyError{Status: http.StatusForbidden, Message: "access denied"}
|
||||||
|
}
|
||||||
|
|
||||||
|
ttl := e.ttlFor(remote, class)
|
||||||
|
|
||||||
|
fresh, err := e.cache.CheckTTL(ctx, remote.Name, path)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("redis check failed, treating as miss", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fresh {
|
||||||
|
result, err := e.serveFromStore(ctx, remote, path)
|
||||||
|
if err == nil {
|
||||||
|
result.Source = "cache"
|
||||||
|
go e.logAccess(remote.Name, path, true, result.Size, 0)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
slog.Warn("cache hit but S3 miss, re-fetching", "remote", remote.Name, "path", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
locked, err := e.cache.AcquireLock(ctx, remote.Name, path, fetchLockTTL)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("lock acquire failed", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !locked {
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
result, err := e.serveFromStore(ctx, remote, path)
|
||||||
|
if err == nil {
|
||||||
|
result.Source = "cache"
|
||||||
|
go e.logAccess(remote.Name, path, true, result.Size, 0)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if locked {
|
||||||
|
defer e.cache.ReleaseLock(ctx, remote.Name, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if class == ClassMutable && remote.CheckMutable {
|
||||||
|
etag, _ := e.cache.GetETag(ctx, remote.Name, path)
|
||||||
|
if etag != "" {
|
||||||
|
notModified, err := e.checkUpstream(ctx, remote, path, etag, prov)
|
||||||
|
if err == nil && notModified {
|
||||||
|
_ = e.cache.SetTTL(ctx, remote.Name, path, ttl)
|
||||||
|
_ = e.cache.SetETag(ctx, remote.Name, path, etag, ttl)
|
||||||
|
result, err := e.serveFromStore(ctx, remote, path)
|
||||||
|
if err == nil {
|
||||||
|
result.Source = "cache"
|
||||||
|
go e.logAccess(remote.Name, path, true, result.Size, 0)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
result, err := e.fetchFromUpstream(ctx, remote, path, prov, class, ttl)
|
||||||
|
upstreamMS := int(time.Since(start).Milliseconds())
|
||||||
|
if err != nil {
|
||||||
|
if remote.StaleOnError && isNetworkError(err) {
|
||||||
|
_ = e.cache.SetTTL(ctx, remote.Name, path, ttl)
|
||||||
|
stale, serr := e.serveFromStore(ctx, remote, path)
|
||||||
|
if serr == nil {
|
||||||
|
slog.Warn("serving stale on upstream error", "remote", remote.Name, "path", path, "error", err)
|
||||||
|
stale.Source = "cache"
|
||||||
|
go e.logAccess(remote.Name, path, true, stale.Size, 0)
|
||||||
|
return stale, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
go e.logAccess(remote.Name, path, false, result.Size, upstreamMS)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) fetchFromUpstream(ctx context.Context, remote models.Remote, path string, prov provider.Provider, class Classification, ttl time.Duration) (*FetchResult, error) {
|
||||||
|
url := prov.UpstreamURL(remote, path)
|
||||||
|
|
||||||
|
authHeaders, err := prov.AuthHeaders(ctx, remote)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("auth headers: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
for k, vv := range authHeaders {
|
||||||
|
for _, v := range vv {
|
||||||
|
req.Header.Add(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, &UpstreamError{Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
resp.Body.Close()
|
||||||
|
return nil, &ProxyError{Status: resp.StatusCode, Message: fmt.Sprintf("upstream returned %d", resp.StatusCode)}
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read upstream body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rewritten, err := prov.RewriteResponse(body, remote, "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("rewrite response: %w", err)
|
||||||
|
}
|
||||||
|
if rewritten != nil {
|
||||||
|
body = rewritten
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := prov.ContentType(path)
|
||||||
|
if ct := resp.Header.Get("Content-Type"); ct != "" && contentType == "application/octet-stream" {
|
||||||
|
contentType = ct
|
||||||
|
}
|
||||||
|
|
||||||
|
if class == ClassMutable {
|
||||||
|
s3Key := storage.IndexKey(remote.Name, path)
|
||||||
|
if err := e.store.Upload(ctx, s3Key, bytesReader(body), int64(len(body)), contentType); err != nil {
|
||||||
|
return nil, fmt.Errorf("upload index: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
etag := resp.Header.Get("ETag")
|
||||||
|
_ = e.cache.SetTTL(ctx, remote.Name, path, ttl)
|
||||||
|
if etag != "" {
|
||||||
|
_ = e.cache.SetETag(ctx, remote.Name, path, etag, ttl)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
hash := sha256Hash(body)
|
||||||
|
s3Key := storage.BlobKey(hash)
|
||||||
|
|
||||||
|
exists, _ := e.store.Exists(ctx, s3Key)
|
||||||
|
if !exists {
|
||||||
|
if err := e.store.Upload(ctx, s3Key, bytesReader(body), int64(len(body)), contentType); err != nil {
|
||||||
|
return nil, fmt.Errorf("upload blob: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contentHash := fmt.Sprintf("sha256:%s", hash)
|
||||||
|
if err := e.db.UpsertBlob(ctx, contentHash, s3Key, int64(len(body)), contentType); err != nil {
|
||||||
|
slog.Warn("upsert blob failed", "error", err)
|
||||||
|
}
|
||||||
|
if err := e.db.UpsertArtifact(ctx, remote.Name, path, contentHash, resp.Header.Get("ETag")); err != nil {
|
||||||
|
slog.Warn("upsert artifact failed", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = e.cache.SetTTL(ctx, remote.Name, path, ttl)
|
||||||
|
if etag := resp.Header.Get("ETag"); etag != "" {
|
||||||
|
_ = e.cache.SetETag(ctx, remote.Name, path, etag, ttl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &FetchResult{
|
||||||
|
Reader: io.NopCloser(bytesReader(body)),
|
||||||
|
ContentType: contentType,
|
||||||
|
Size: int64(len(body)),
|
||||||
|
Source: "remote",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) serveFromStore(ctx context.Context, remote models.Remote, path string) (*FetchResult, error) {
|
||||||
|
artifact, err := e.db.GetArtifact(ctx, remote.Name, path)
|
||||||
|
if err == nil && artifact != nil {
|
||||||
|
reader, info, err := e.store.Download(ctx, artifact.ContentHash[len("sha256:"):])
|
||||||
|
if err == nil {
|
||||||
|
_ = e.db.TouchArtifactAccess(ctx, remote.Name, path)
|
||||||
|
return &FetchResult{
|
||||||
|
Reader: reader,
|
||||||
|
ContentType: info.ContentType,
|
||||||
|
Size: info.Size,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
s3Key := storage.BlobKey(artifact.ContentHash[len("sha256:"):])
|
||||||
|
reader, info, err = e.store.Download(ctx, s3Key)
|
||||||
|
if err == nil {
|
||||||
|
_ = e.db.TouchArtifactAccess(ctx, remote.Name, path)
|
||||||
|
return &FetchResult{
|
||||||
|
Reader: reader,
|
||||||
|
ContentType: info.ContentType,
|
||||||
|
Size: info.Size,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s3Key := storage.IndexKey(remote.Name, path)
|
||||||
|
reader, info, err := e.store.Download(ctx, s3Key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("not in store: %w", err)
|
||||||
|
}
|
||||||
|
return &FetchResult{
|
||||||
|
Reader: reader,
|
||||||
|
ContentType: info.ContentType,
|
||||||
|
Size: info.Size,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) checkUpstream(ctx context.Context, remote models.Remote, path, etag string, prov provider.Provider) (bool, error) {
|
||||||
|
url := prov.UpstreamURL(remote, path)
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
req.Header.Set("If-None-Match", etag)
|
||||||
|
|
||||||
|
authHeaders, err := prov.AuthHeaders(ctx, remote)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for k, vv := range authHeaders {
|
||||||
|
for _, v := range vv {
|
||||||
|
req.Header.Add(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return false, &UpstreamError{Err: err}
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
return resp.StatusCode == http.StatusNotModified, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) ttlFor(remote models.Remote, class Classification) time.Duration {
|
||||||
|
switch class {
|
||||||
|
case ClassImmutable:
|
||||||
|
if remote.ImmutableTTL == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return time.Duration(remote.ImmutableTTL) * time.Second
|
||||||
|
default:
|
||||||
|
return time.Duration(remote.MutableTTL) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) logAccess(remoteName, path string, cacheHit bool, size int64, upstreamMS int) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_ = e.db.InsertAccessLog(ctx, remoteName, path, cacheHit, size, upstreamMS, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func sha256Hash(data []byte) string {
|
||||||
|
h := sha256.Sum256(data)
|
||||||
|
return hex.EncodeToString(h[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func bytesReader(data []byte) io.Reader {
|
||||||
|
return io.NewSectionReader(readerAt(data), 0, int64(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
type readerAt []byte
|
||||||
|
|
||||||
|
func (r readerAt) ReadAt(p []byte, off int64) (n int, err error) {
|
||||||
|
if off >= int64(len(r)) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
n = copy(p, r[off:])
|
||||||
|
if off+int64(n) >= int64(len(r)) {
|
||||||
|
err = io.EOF
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProxyError struct {
|
||||||
|
Status int
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ProxyError) Error() string { return e.Message }
|
||||||
|
|
||||||
|
type UpstreamError struct {
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *UpstreamError) Error() string { return fmt.Sprintf("upstream error: %v", e.Err) }
|
||||||
|
func (e *UpstreamError) Unwrap() error { return e.Err }
|
||||||
|
|
||||||
|
func isNetworkError(err error) bool {
|
||||||
|
if _, ok := err.(*UpstreamError); ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
func cors(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStructuredLogger() func(next http.Handler) http.Handler {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
slog.Info("request",
|
||||||
|
"method", r.Method,
|
||||||
|
"path", r.URL.Path,
|
||||||
|
"status", ww.Status(),
|
||||||
|
"bytes", ww.BytesWritten(),
|
||||||
|
"duration_ms", time.Since(start).Milliseconds(),
|
||||||
|
"remote", r.RemoteAddr,
|
||||||
|
"request_id", middleware.GetReqID(r.Context()),
|
||||||
|
)
|
||||||
|
}()
|
||||||
|
|
||||||
|
next.ServeHTTP(ww, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
|
||||||
|
v1 "git.unkin.net/unkin/artifactapi/internal/api/v1"
|
||||||
|
v2 "git.unkin.net/unkin/artifactapi/internal/api/v2"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/cache"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/config"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/gc"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/alpine"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/docker"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/generic"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/goproxy"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/helm"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/npm"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/puppet"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/pypi"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/rpm"
|
||||||
|
_ "git.unkin.net/unkin/artifactapi/internal/provider/terraform"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/storage"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/virtual"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
cfg *config.Config
|
||||||
|
router chi.Router
|
||||||
|
db *database.DB
|
||||||
|
cache *cache.Redis
|
||||||
|
store *storage.S3
|
||||||
|
engine *proxy.Engine
|
||||||
|
virtEngine *virtual.Engine
|
||||||
|
gc *gc.Collector
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(cfg *config.Config) (*Server, error) {
|
||||||
|
db, err := database.New(cfg.DatabaseDSN())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
redis, err := cache.NewRedis(cfg.RedisURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("redis: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s3, err := storage.NewS3(cfg.S3Endpoint, cfg.S3AccessKey, cfg.S3SecretKey, cfg.S3Bucket, cfg.S3Secure, cfg.S3Region)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("s3: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
engine := proxy.NewEngine(db, redis, s3)
|
||||||
|
virtEngine := virtual.NewEngine(db, engine)
|
||||||
|
collector := gc.New(db, s3, 1*time.Hour)
|
||||||
|
|
||||||
|
s := &Server{
|
||||||
|
cfg: cfg,
|
||||||
|
db: db,
|
||||||
|
cache: redis,
|
||||||
|
store: s3,
|
||||||
|
engine: engine,
|
||||||
|
virtEngine: virtEngine,
|
||||||
|
gc: collector,
|
||||||
|
}
|
||||||
|
|
||||||
|
s.router = s.routes()
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.Use(middleware.RequestID)
|
||||||
|
r.Use(middleware.RealIP)
|
||||||
|
r.Use(NewStructuredLogger())
|
||||||
|
r.Use(middleware.Recoverer)
|
||||||
|
|
||||||
|
r.Use(cors)
|
||||||
|
|
||||||
|
r.Get("/health", s.handleHealth)
|
||||||
|
r.Get("/", s.handleRoot)
|
||||||
|
|
||||||
|
proxyHandler := v1.NewProxyHandler(s.engine, s.virtEngine, s.db)
|
||||||
|
r.Mount("/api/v1", proxyHandler.Routes())
|
||||||
|
|
||||||
|
remotesHandler := v2.NewRemotesHandler(s.db)
|
||||||
|
virtualsHandler := v2.NewVirtualsHandler(s.db)
|
||||||
|
healthHandler := v2.NewHealthHandler(s.db, s.cache, s.store)
|
||||||
|
statsHandler := v2.NewStatsHandler(s.db)
|
||||||
|
eventsHandler := v2.NewEventsHandler()
|
||||||
|
probeHandler := v2.NewProbeHandler(s.engine, s.db)
|
||||||
|
|
||||||
|
r.Route("/api/v2", func(r chi.Router) {
|
||||||
|
r.Mount("/remotes", remotesHandler.Routes())
|
||||||
|
r.Mount("/virtuals", virtualsHandler.Routes())
|
||||||
|
r.Mount("/health", healthHandler.Routes())
|
||||||
|
r.Mount("/stats", statsHandler.Routes())
|
||||||
|
r.Mount("/events", eventsHandler.Routes())
|
||||||
|
r.Mount("/probe", probeHandler.Routes())
|
||||||
|
|
||||||
|
r.Route("/remotes/{name}/objects", func(r chi.Router) {
|
||||||
|
objHandler := v2.NewObjectsHandler(s.db)
|
||||||
|
r.Get("/", objHandler.Routes().ServeHTTP)
|
||||||
|
r.Delete("/*", objHandler.Routes().ServeHTTP)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprint(w, `{"status":"ok"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprint(w, `{"name":"artifactapi","version":"3.0.0-dev"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) newHTTPServer() *http.Server {
|
||||||
|
return &http.Server{
|
||||||
|
Addr: s.cfg.ListenAddr,
|
||||||
|
Handler: s.router,
|
||||||
|
ReadTimeout: 30 * time.Second,
|
||||||
|
WriteTimeout: 300 * time.Second,
|
||||||
|
IdleTimeout: 120 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Run(ctx context.Context) error {
|
||||||
|
go s.gc.Run(ctx)
|
||||||
|
|
||||||
|
httpServer := s.newHTTPServer()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
slog.Info("shutting down server")
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_ = httpServer.Shutdown(shutdownCtx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
slog.Info("starting server", "addr", s.cfg.ListenAddr)
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) RunOnListener(ctx context.Context, ln net.Listener) error {
|
||||||
|
go s.gc.Run(ctx)
|
||||||
|
|
||||||
|
httpServer := s.newHTTPServer()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_ = httpServer.Shutdown(shutdownCtx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
slog.Info("starting server", "addr", ln.Addr().String())
|
||||||
|
if err := httpServer.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CAS struct {
|
||||||
|
s3 *S3
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCAS(s3 *S3) *CAS {
|
||||||
|
return &CAS{s3: s3}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CASResult struct {
|
||||||
|
ContentHash string
|
||||||
|
S3Key string
|
||||||
|
SizeBytes int64
|
||||||
|
AlreadyExists bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CAS) Store(ctx context.Context, reader io.Reader, contentType string) (*CASResult, error) {
|
||||||
|
tmp, err := os.CreateTemp("", "artifact-*")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tmp.Name())
|
||||||
|
defer tmp.Close()
|
||||||
|
|
||||||
|
hasher := sha256.New()
|
||||||
|
size, err := io.Copy(io.MultiWriter(tmp, hasher), reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("write temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||||
|
s3Key := BlobKey(hash)
|
||||||
|
|
||||||
|
exists, err := c.s3.Exists(ctx, s3Key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("check blob exists: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||||
|
return nil, fmt.Errorf("seek temp file: %w", err)
|
||||||
|
}
|
||||||
|
if err := c.s3.Upload(ctx, s3Key, tmp, size, contentType); err != nil {
|
||||||
|
return nil, fmt.Errorf("upload blob: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &CASResult{
|
||||||
|
ContentHash: fmt.Sprintf("sha256:%s", hash),
|
||||||
|
S3Key: s3Key,
|
||||||
|
SizeBytes: size,
|
||||||
|
AlreadyExists: exists,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func BlobKey(hash string) string {
|
||||||
|
return fmt.Sprintf("blobs/sha256/%s", hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IndexKey(remote, path string) string {
|
||||||
|
return fmt.Sprintf("indexes/%s/%s", remote, path)
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/minio/minio-go/v7"
|
||||||
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||||
|
)
|
||||||
|
|
||||||
|
type S3 struct {
|
||||||
|
client *minio.Client
|
||||||
|
bucket string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewS3(endpoint, accessKey, secretKey, bucket string, secure bool, region string) (*S3, error) {
|
||||||
|
opts := &minio.Options{
|
||||||
|
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||||
|
Secure: secure,
|
||||||
|
}
|
||||||
|
if region != "" {
|
||||||
|
opts.Region = region
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := minio.New(endpoint, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create s3 client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s := &S3{client: client, bucket: bucket}
|
||||||
|
|
||||||
|
if err := s.ensureBucket(context.Background()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S3) ensureBucket(ctx context.Context) error {
|
||||||
|
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("check bucket: %w", err)
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{}); err != nil {
|
||||||
|
return fmt.Errorf("create bucket: %w", err)
|
||||||
|
}
|
||||||
|
slog.Info("created bucket", "bucket", s.bucket)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S3) Upload(ctx context.Context, key string, reader io.Reader, size int64, contentType string) error {
|
||||||
|
_, err := s.client.PutObject(ctx, s.bucket, key, reader, size, minio.PutObjectOptions{
|
||||||
|
ContentType: contentType,
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S3) Download(ctx context.Context, key string) (io.ReadCloser, *minio.ObjectInfo, error) {
|
||||||
|
obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := obj.Stat()
|
||||||
|
if err != nil {
|
||||||
|
obj.Close()
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj, &info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S3) Exists(ctx context.Context, key string) (bool, error) {
|
||||||
|
_, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
resp := minio.ToErrorResponse(err)
|
||||||
|
if resp.Code == "NoSuchKey" {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S3) Delete(ctx context.Context, key string) error {
|
||||||
|
return s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S3) Stat(ctx context.Context, key string) (*minio.ObjectInfo, error) {
|
||||||
|
info, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &info, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,305 @@
|
|||||||
|
package tui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/client"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type view int
|
||||||
|
|
||||||
|
const (
|
||||||
|
viewDashboard view = iota
|
||||||
|
viewRemotes
|
||||||
|
viewRemoteDetail
|
||||||
|
viewObjects
|
||||||
|
viewVirtuals
|
||||||
|
)
|
||||||
|
|
||||||
|
type model struct {
|
||||||
|
client *client.Client
|
||||||
|
view view
|
||||||
|
width int
|
||||||
|
height int
|
||||||
|
err error
|
||||||
|
loading bool
|
||||||
|
|
||||||
|
stats *models.OverviewStats
|
||||||
|
remotes []models.Remote
|
||||||
|
virtuals []models.Virtual
|
||||||
|
objects []models.Artifact
|
||||||
|
|
||||||
|
selectedRemote string
|
||||||
|
cursor int
|
||||||
|
page int
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(endpoint string) *model {
|
||||||
|
return &model{
|
||||||
|
client: client.New(endpoint),
|
||||||
|
view: viewDashboard,
|
||||||
|
loading: true,
|
||||||
|
page: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) Run() error {
|
||||||
|
p := tea.NewProgram(m, tea.WithAltScreen())
|
||||||
|
_, err := p.Run()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) Init() tea.Cmd {
|
||||||
|
return m.loadDashboard()
|
||||||
|
}
|
||||||
|
|
||||||
|
type dashboardLoaded struct {
|
||||||
|
stats *models.OverviewStats
|
||||||
|
remotes []models.Remote
|
||||||
|
virtuals []models.Virtual
|
||||||
|
}
|
||||||
|
|
||||||
|
type remotesLoaded struct{ remotes []models.Remote }
|
||||||
|
type virtualsLoaded struct{ virtuals []models.Virtual }
|
||||||
|
type objectsLoaded struct{ objects []models.Artifact }
|
||||||
|
type errMsg struct{ err error }
|
||||||
|
|
||||||
|
func (m *model) loadDashboard() tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
ctx := context.Background()
|
||||||
|
stats, err := m.client.Stats(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return errMsg{err}
|
||||||
|
}
|
||||||
|
remotes, _ := m.client.ListRemotes(ctx)
|
||||||
|
virtuals, _ := m.client.ListVirtuals(ctx)
|
||||||
|
return dashboardLoaded{stats: stats, remotes: remotes, virtuals: virtuals}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) loadRemotes() tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
remotes, err := m.client.ListRemotes(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return errMsg{err}
|
||||||
|
}
|
||||||
|
return remotesLoaded{remotes}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) loadVirtuals() tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
virtuals, err := m.client.ListVirtuals(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return errMsg{err}
|
||||||
|
}
|
||||||
|
return virtualsLoaded{virtuals}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) loadObjects() tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
objects, err := m.client.ListObjects(context.Background(), m.selectedRemote, m.page, 30)
|
||||||
|
if err != nil {
|
||||||
|
return errMsg{err}
|
||||||
|
}
|
||||||
|
return objectsLoaded{objects}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.WindowSizeMsg:
|
||||||
|
m.width = msg.Width
|
||||||
|
m.height = msg.Height
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case tea.KeyMsg:
|
||||||
|
return m.handleKey(msg)
|
||||||
|
|
||||||
|
case dashboardLoaded:
|
||||||
|
m.loading = false
|
||||||
|
m.stats = msg.stats
|
||||||
|
m.remotes = msg.remotes
|
||||||
|
m.virtuals = msg.virtuals
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case remotesLoaded:
|
||||||
|
m.loading = false
|
||||||
|
m.remotes = msg.remotes
|
||||||
|
m.cursor = 0
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case virtualsLoaded:
|
||||||
|
m.loading = false
|
||||||
|
m.virtuals = msg.virtuals
|
||||||
|
m.cursor = 0
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case objectsLoaded:
|
||||||
|
m.loading = false
|
||||||
|
m.objects = msg.objects
|
||||||
|
m.cursor = 0
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case errMsg:
|
||||||
|
m.loading = false
|
||||||
|
m.err = msg.err
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg.String() {
|
||||||
|
case "q", "ctrl+c":
|
||||||
|
if m.view == viewDashboard {
|
||||||
|
return m, tea.Quit
|
||||||
|
}
|
||||||
|
m.view = viewDashboard
|
||||||
|
m.cursor = 0
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadDashboard()
|
||||||
|
|
||||||
|
case "esc":
|
||||||
|
switch m.view {
|
||||||
|
case viewRemoteDetail, viewObjects:
|
||||||
|
m.view = viewRemotes
|
||||||
|
m.cursor = 0
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadRemotes()
|
||||||
|
case viewRemotes, viewVirtuals:
|
||||||
|
m.view = viewDashboard
|
||||||
|
m.cursor = 0
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadDashboard()
|
||||||
|
default:
|
||||||
|
return m, tea.Quit
|
||||||
|
}
|
||||||
|
|
||||||
|
case "1":
|
||||||
|
m.view = viewDashboard
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadDashboard()
|
||||||
|
|
||||||
|
case "2":
|
||||||
|
m.view = viewRemotes
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadRemotes()
|
||||||
|
|
||||||
|
case "3":
|
||||||
|
m.view = viewVirtuals
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadVirtuals()
|
||||||
|
|
||||||
|
case "j", "down":
|
||||||
|
m.cursor++
|
||||||
|
m.clampCursor()
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case "k", "up":
|
||||||
|
if m.cursor > 0 {
|
||||||
|
m.cursor--
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case "enter":
|
||||||
|
return m.handleEnter()
|
||||||
|
|
||||||
|
case "r":
|
||||||
|
m.loading = true
|
||||||
|
switch m.view {
|
||||||
|
case viewDashboard:
|
||||||
|
return m, m.loadDashboard()
|
||||||
|
case viewRemotes:
|
||||||
|
return m, m.loadRemotes()
|
||||||
|
case viewVirtuals:
|
||||||
|
return m, m.loadVirtuals()
|
||||||
|
case viewObjects:
|
||||||
|
return m, m.loadObjects()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) handleEnter() (tea.Model, tea.Cmd) {
|
||||||
|
switch m.view {
|
||||||
|
case viewRemotes:
|
||||||
|
if m.cursor < len(m.remotes) {
|
||||||
|
m.selectedRemote = m.remotes[m.cursor].Name
|
||||||
|
m.view = viewRemoteDetail
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
case viewRemoteDetail:
|
||||||
|
m.view = viewObjects
|
||||||
|
m.page = 1
|
||||||
|
m.loading = true
|
||||||
|
return m, m.loadObjects()
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) clampCursor() {
|
||||||
|
max := 0
|
||||||
|
switch m.view {
|
||||||
|
case viewRemotes:
|
||||||
|
max = len(m.remotes) - 1
|
||||||
|
case viewVirtuals:
|
||||||
|
max = len(m.virtuals) - 1
|
||||||
|
case viewObjects:
|
||||||
|
max = len(m.objects) - 1
|
||||||
|
}
|
||||||
|
if m.cursor > max {
|
||||||
|
m.cursor = max
|
||||||
|
}
|
||||||
|
if m.cursor < 0 {
|
||||||
|
m.cursor = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) View() string {
|
||||||
|
if m.loading {
|
||||||
|
return m.chrome("Loading...")
|
||||||
|
}
|
||||||
|
if m.err != nil {
|
||||||
|
return m.chrome(errStyle.Render(fmt.Sprintf("Error: %v", m.err)))
|
||||||
|
}
|
||||||
|
|
||||||
|
var body string
|
||||||
|
switch m.view {
|
||||||
|
case viewDashboard:
|
||||||
|
body = m.viewDashboard()
|
||||||
|
case viewRemotes:
|
||||||
|
body = m.viewRemotesList()
|
||||||
|
case viewRemoteDetail:
|
||||||
|
body = m.viewRemoteDetail()
|
||||||
|
case viewObjects:
|
||||||
|
body = m.viewObjectsList()
|
||||||
|
case viewVirtuals:
|
||||||
|
body = m.viewVirtualsList()
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.chrome(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) chrome(body string) string {
|
||||||
|
nav := navStyle.Render(
|
||||||
|
"[1] Dashboard [2] Remotes [3] Virtuals │ [r] Refresh [q] Quit",
|
||||||
|
)
|
||||||
|
return lipgloss.JoinVertical(lipgloss.Left, body, "", nav)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
titleStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12"))
|
||||||
|
navStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
|
||||||
|
errStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
|
||||||
|
mutedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
|
||||||
|
selStyle = lipgloss.NewStyle().Background(lipgloss.Color("4")).Foreground(lipgloss.Color("15"))
|
||||||
|
)
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
package tui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/tui/views"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m *model) viewDashboard() string {
|
||||||
|
return titleStyle.Render("ArtifactAPI Dashboard") + "\n\n" +
|
||||||
|
views.RenderDashboard(m.stats, len(m.remotes), len(m.virtuals)) +
|
||||||
|
"\n\n" + mutedStyle.Render("Press [2] for remotes, [3] for virtuals")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) viewRemotesList() string {
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString(titleStyle.Render("Remotes") + "\n\n")
|
||||||
|
|
||||||
|
if len(m.remotes) == 0 {
|
||||||
|
sb.WriteString(mutedStyle.Render("No remotes configured"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, r := range m.remotes {
|
||||||
|
line := fmt.Sprintf(" %-25s %-12s %s", r.Name, r.PackageType, r.Description)
|
||||||
|
if i == m.cursor {
|
||||||
|
sb.WriteString(selStyle.Render(line))
|
||||||
|
} else {
|
||||||
|
sb.WriteString(line)
|
||||||
|
}
|
||||||
|
sb.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.WriteString("\n" + mutedStyle.Render("j/k navigate · enter detail · esc back"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) viewRemoteDetail() string {
|
||||||
|
var r *remoteView
|
||||||
|
for i := range m.remotes {
|
||||||
|
if m.remotes[i].Name == m.selectedRemote {
|
||||||
|
r = &remoteView{m.remotes[i]}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r == nil {
|
||||||
|
return mutedStyle.Render("Remote not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString(titleStyle.Render(r.Name) + "\n\n")
|
||||||
|
sb.WriteString(fmt.Sprintf(" Type: %s\n", r.PackageType))
|
||||||
|
sb.WriteString(fmt.Sprintf(" Base URL: %s\n", r.BaseURL))
|
||||||
|
sb.WriteString(fmt.Sprintf(" Description: %s\n", r.Description))
|
||||||
|
sb.WriteString(fmt.Sprintf(" Immutable TTL: %s\n", ttlStr(r.ImmutableTTL)))
|
||||||
|
sb.WriteString(fmt.Sprintf(" Mutable TTL: %ds\n", r.MutableTTL))
|
||||||
|
sb.WriteString(fmt.Sprintf(" Revalidation: %v\n", r.CheckMutable))
|
||||||
|
sb.WriteString(fmt.Sprintf(" Stale on Error: %v\n", r.StaleOnError))
|
||||||
|
|
||||||
|
if len(r.Patterns) > 0 {
|
||||||
|
sb.WriteString(fmt.Sprintf(" Patterns: %s\n", strings.Join(r.Patterns, ", ")))
|
||||||
|
}
|
||||||
|
if len(r.Blocklist) > 0 {
|
||||||
|
sb.WriteString(fmt.Sprintf(" Blocklist: %s\n", strings.Join(r.Blocklist, ", ")))
|
||||||
|
}
|
||||||
|
if r.ManagedBy != "" {
|
||||||
|
sb.WriteString(fmt.Sprintf(" Managed by: %s\n", r.ManagedBy))
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.WriteString("\n" + mutedStyle.Render("enter → browse objects · esc back"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) viewObjectsList() string {
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString(titleStyle.Render(fmt.Sprintf("Objects: %s (page %d)", m.selectedRemote, m.page)) + "\n\n")
|
||||||
|
|
||||||
|
if len(m.objects) == 0 {
|
||||||
|
sb.WriteString(mutedStyle.Render("No cached objects"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, a := range m.objects {
|
||||||
|
size := views.FormatBytes(a.SizeBytes)
|
||||||
|
line := fmt.Sprintf(" %-50s %10s %5d hits", truncate(a.Path, 50), size, a.AccessCount)
|
||||||
|
if i == m.cursor {
|
||||||
|
sb.WriteString(selStyle.Render(line))
|
||||||
|
} else {
|
||||||
|
sb.WriteString(line)
|
||||||
|
}
|
||||||
|
sb.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.WriteString("\n" + mutedStyle.Render("j/k navigate · esc back"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *model) viewVirtualsList() string {
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString(titleStyle.Render("Virtual Repositories") + "\n\n")
|
||||||
|
|
||||||
|
if len(m.virtuals) == 0 {
|
||||||
|
sb.WriteString(mutedStyle.Render("No virtual repositories configured"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range m.virtuals {
|
||||||
|
line := fmt.Sprintf(" %-25s %-12s %d members %s",
|
||||||
|
v.Name, v.PackageType, len(v.Members), v.Description)
|
||||||
|
if i == m.cursor {
|
||||||
|
sb.WriteString(selStyle.Render(line))
|
||||||
|
} else {
|
||||||
|
sb.WriteString(line)
|
||||||
|
}
|
||||||
|
sb.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.WriteString("\n" + mutedStyle.Render("j/k navigate · esc back"))
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
type remoteView struct {
|
||||||
|
models.Remote
|
||||||
|
}
|
||||||
|
|
||||||
|
func ttlStr(ttl int) string {
|
||||||
|
if ttl == 0 {
|
||||||
|
return "forever"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%ds", ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func truncate(s string, max int) string {
|
||||||
|
if len(s) <= max {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return s[:max-3] + "..."
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package views
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FormatBytes(bytes int64) string {
|
||||||
|
if bytes == 0 {
|
||||||
|
return "0 B"
|
||||||
|
}
|
||||||
|
units := []string{"B", "KB", "MB", "GB", "TB"}
|
||||||
|
i := 0
|
||||||
|
b := float64(bytes)
|
||||||
|
for b >= 1024 && i < len(units)-1 {
|
||||||
|
b /= 1024
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
if i == 0 {
|
||||||
|
return fmt.Sprintf("%.0f %s", b, units[i])
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.1f %s", b, units[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderDashboard(stats *models.OverviewStats, remoteCount, virtualCount int) string {
|
||||||
|
if stats == nil {
|
||||||
|
return "No stats available"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"╭─ Dashboard ──────────────────────────────╮\n"+
|
||||||
|
"│ Remotes: %-24d│\n"+
|
||||||
|
"│ Cached Objects: %-24d│\n"+
|
||||||
|
"│ Storage Used: %-24s│\n"+
|
||||||
|
"│ Dedup Savings: %-20d blobs │\n"+
|
||||||
|
"│ Virtuals: %-24d│\n"+
|
||||||
|
"╰──────────────────────────────────────────╯",
|
||||||
|
stats.TotalRemotes,
|
||||||
|
stats.TotalObjects,
|
||||||
|
FormatBytes(stats.TotalBytes),
|
||||||
|
stats.TotalBlobsDeduped,
|
||||||
|
virtualCount,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
package virtual
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Engine struct {
|
||||||
|
db *database.DB
|
||||||
|
proxyEngine *proxy.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEngine(db *database.DB, proxyEngine *proxy.Engine) *Engine {
|
||||||
|
return &Engine{db: db, proxyEngine: proxyEngine}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) Fetch(ctx context.Context, virt models.Virtual, path string, proxyBaseURL string) ([]byte, string, error) {
|
||||||
|
merger, err := GetMerger(virt.PackageType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("unsupported virtual type %q: %w", virt.PackageType, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
members, err := e.fetchMemberIndexes(ctx, virt, path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(members) == 0 {
|
||||||
|
return nil, "", fmt.Errorf("no members reachable for virtual %q", virt.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
merged, err := merger.MergeIndexes(members, proxyBaseURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("merge indexes: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := "application/octet-stream"
|
||||||
|
switch virt.PackageType {
|
||||||
|
case models.PackageHelm:
|
||||||
|
contentType = "text/yaml"
|
||||||
|
case models.PackagePyPI:
|
||||||
|
contentType = "text/html"
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged, contentType, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) fetchMemberIndexes(ctx context.Context, virt models.Virtual, path string) ([]MemberIndex, error) {
|
||||||
|
type result struct {
|
||||||
|
index MemberIndex
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]result, len(virt.Members))
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
for i, memberName := range virt.Members {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(idx int, name string) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
remote, err := e.db.GetRemote(ctx, name)
|
||||||
|
if err != nil {
|
||||||
|
results[idx] = result{err: fmt.Errorf("remote %q: %w", name, err)}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prov, err := provider.Get(remote.PackageType)
|
||||||
|
if err != nil {
|
||||||
|
results[idx] = result{err: fmt.Errorf("provider %q: %w", remote.PackageType, err)}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchResult, err := e.proxyEngine.Fetch(ctx, *remote, path, prov)
|
||||||
|
if err != nil {
|
||||||
|
results[idx] = result{err: fmt.Errorf("fetch %q/%s: %w", name, path, err)}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer fetchResult.Reader.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(fetchResult.Reader)
|
||||||
|
if err != nil {
|
||||||
|
results[idx] = result{err: fmt.Errorf("read %q: %w", name, err)}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
results[idx] = result{index: MemberIndex{RemoteName: name, Body: body}}
|
||||||
|
}(i, memberName)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
var members []MemberIndex
|
||||||
|
for _, r := range results {
|
||||||
|
if r.err != nil {
|
||||||
|
slog.Warn("virtual member fetch failed", "error", r.err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
members = append(members, r.index)
|
||||||
|
}
|
||||||
|
|
||||||
|
return members, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package virtual
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterMerger(models.PackageHelm, &HelmMerger{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type HelmMerger struct{}
|
||||||
|
|
||||||
|
type helmIndex struct {
|
||||||
|
APIVersion string `yaml:"apiVersion"`
|
||||||
|
Entries map[string][]helmChartVersion `yaml:"entries"`
|
||||||
|
Generated string `yaml:"generated,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type helmChartVersion struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Version string `yaml:"version"`
|
||||||
|
URLs []string `yaml:"urls"`
|
||||||
|
rest map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *HelmMerger) MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error) {
|
||||||
|
merged := &helmIndex{
|
||||||
|
APIVersion: "v1",
|
||||||
|
Entries: make(map[string][]helmChartVersion),
|
||||||
|
}
|
||||||
|
|
||||||
|
seen := map[string]map[string]bool{}
|
||||||
|
|
||||||
|
for _, member := range members {
|
||||||
|
var idx helmIndex
|
||||||
|
if err := yaml.Unmarshal(member.Body, &idx); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for chart, versions := range idx.Entries {
|
||||||
|
if seen[chart] == nil {
|
||||||
|
seen[chart] = map[string]bool{}
|
||||||
|
}
|
||||||
|
for _, ver := range versions {
|
||||||
|
key := chart + ":" + ver.Version
|
||||||
|
if seen[chart][ver.Version] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[chart][ver.Version] = true
|
||||||
|
|
||||||
|
if proxyBaseURL != "" {
|
||||||
|
for i, u := range ver.URLs {
|
||||||
|
if strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") {
|
||||||
|
ver.URLs[i] = fmt.Sprintf("%s/api/v1/remote/%s/%s",
|
||||||
|
strings.TrimRight(proxyBaseURL, "/"),
|
||||||
|
member.RemoteName,
|
||||||
|
extractPath(u))
|
||||||
|
} else {
|
||||||
|
ver.URLs[i] = fmt.Sprintf("%s/api/v1/remote/%s/%s",
|
||||||
|
strings.TrimRight(proxyBaseURL, "/"),
|
||||||
|
member.RemoteName,
|
||||||
|
u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
merged.Entries[chart] = append(merged.Entries[chart], ver)
|
||||||
|
_ = key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return yaml.Marshal(merged)
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractPath(rawURL string) string {
|
||||||
|
idx := strings.Index(rawURL, "://")
|
||||||
|
if idx == -1 {
|
||||||
|
return rawURL
|
||||||
|
}
|
||||||
|
rest := rawURL[idx+3:]
|
||||||
|
slashIdx := strings.Index(rest, "/")
|
||||||
|
if slashIdx == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return rest[slashIdx+1:]
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
package virtual_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/virtual"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHelmMerger_BasicMerge(t *testing.T) {
|
||||||
|
m := &virtual.HelmMerger{}
|
||||||
|
|
||||||
|
member1 := virtual.MemberIndex{
|
||||||
|
RemoteName: "repo-a",
|
||||||
|
Body: []byte(`apiVersion: v1
|
||||||
|
entries:
|
||||||
|
nginx:
|
||||||
|
- name: nginx
|
||||||
|
version: "1.0.0"
|
||||||
|
urls:
|
||||||
|
- https://charts-a.example.com/nginx-1.0.0.tgz
|
||||||
|
`),
|
||||||
|
}
|
||||||
|
|
||||||
|
member2 := virtual.MemberIndex{
|
||||||
|
RemoteName: "repo-b",
|
||||||
|
Body: []byte(`apiVersion: v1
|
||||||
|
entries:
|
||||||
|
redis:
|
||||||
|
- name: redis
|
||||||
|
version: "2.0.0"
|
||||||
|
urls:
|
||||||
|
- https://charts-b.example.com/redis-2.0.0.tgz
|
||||||
|
`),
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := m.MergeIndexes([]virtual.MemberIndex{member1, member2}, "https://proxy.example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := string(result)
|
||||||
|
if !strings.Contains(body, "nginx") {
|
||||||
|
t.Error("expected nginx in merged index")
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "redis") {
|
||||||
|
t.Error("expected redis in merged index")
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "proxy.example.com/api/v1/remote/repo-a") {
|
||||||
|
t.Error("expected proxy URL for repo-a")
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "proxy.example.com/api/v1/remote/repo-b") {
|
||||||
|
t.Error("expected proxy URL for repo-b")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelmMerger_Dedup(t *testing.T) {
|
||||||
|
m := &virtual.HelmMerger{}
|
||||||
|
|
||||||
|
idx := []byte(`apiVersion: v1
|
||||||
|
entries:
|
||||||
|
nginx:
|
||||||
|
- name: nginx
|
||||||
|
version: "1.0.0"
|
||||||
|
urls:
|
||||||
|
- nginx-1.0.0.tgz
|
||||||
|
`)
|
||||||
|
|
||||||
|
members := []virtual.MemberIndex{
|
||||||
|
{RemoteName: "repo-a", Body: idx},
|
||||||
|
{RemoteName: "repo-b", Body: idx},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := m.MergeIndexes(members, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
count := strings.Count(string(result), "name: nginx")
|
||||||
|
if count != 1 {
|
||||||
|
t.Errorf("expected 1 entry for nginx, got %d\n%s", count, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelmMerger_PriorityOrder(t *testing.T) {
|
||||||
|
m := &virtual.HelmMerger{}
|
||||||
|
|
||||||
|
member1 := virtual.MemberIndex{
|
||||||
|
RemoteName: "priority-repo",
|
||||||
|
Body: []byte(`apiVersion: v1
|
||||||
|
entries:
|
||||||
|
chart:
|
||||||
|
- name: chart
|
||||||
|
version: "1.0.0"
|
||||||
|
urls:
|
||||||
|
- chart-from-priority.tgz
|
||||||
|
`),
|
||||||
|
}
|
||||||
|
|
||||||
|
member2 := virtual.MemberIndex{
|
||||||
|
RemoteName: "fallback-repo",
|
||||||
|
Body: []byte(`apiVersion: v1
|
||||||
|
entries:
|
||||||
|
chart:
|
||||||
|
- name: chart
|
||||||
|
version: "1.0.0"
|
||||||
|
urls:
|
||||||
|
- chart-from-fallback.tgz
|
||||||
|
`),
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := m.MergeIndexes([]virtual.MemberIndex{member1, member2}, "https://proxy")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := string(result)
|
||||||
|
if !strings.Contains(body, "priority-repo") {
|
||||||
|
t.Error("expected priority repo URL to win")
|
||||||
|
}
|
||||||
|
if strings.Contains(body, "fallback-repo") {
|
||||||
|
t.Error("expected fallback repo to be excluded for duplicate")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package virtual
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MemberIndex struct {
|
||||||
|
RemoteName string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndexMerger interface {
|
||||||
|
MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var mergers = map[models.PackageType]IndexMerger{}
|
||||||
|
|
||||||
|
func RegisterMerger(pt models.PackageType, m IndexMerger) {
|
||||||
|
mergers[pt] = m
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMerger(pt models.PackageType) (IndexMerger, error) {
|
||||||
|
m, ok := mergers[pt]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("no merger registered for package type %q", pt)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package virtual
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterMerger(models.PackagePyPI, &PyPIMerger{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type PyPIMerger struct{}
|
||||||
|
|
||||||
|
func (m *PyPIMerger) MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error) {
|
||||||
|
links := map[string]string{}
|
||||||
|
|
||||||
|
for _, member := range members {
|
||||||
|
body := string(member.Body)
|
||||||
|
for _, line := range strings.Split(body, "\n") {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if !strings.HasPrefix(line, "<a ") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
href := extractHref(line)
|
||||||
|
text := extractLinkText(line)
|
||||||
|
if text == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, exists := links[text]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if proxyBaseURL != "" && href != "" {
|
||||||
|
href = fmt.Sprintf("%s/api/v1/remote/%s/%s",
|
||||||
|
strings.TrimRight(proxyBaseURL, "/"),
|
||||||
|
member.RemoteName,
|
||||||
|
strings.TrimLeft(href, "/"))
|
||||||
|
}
|
||||||
|
|
||||||
|
links[text] = href
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := make([]string, 0, len(links))
|
||||||
|
for k := range links {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString("<!DOCTYPE html>\n<html><body>\n")
|
||||||
|
for _, name := range keys {
|
||||||
|
sb.WriteString(fmt.Sprintf(" <a href=\"%s\">%s</a>\n", links[name], name))
|
||||||
|
}
|
||||||
|
sb.WriteString("</body></html>\n")
|
||||||
|
|
||||||
|
return []byte(sb.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractHref(tag string) string {
|
||||||
|
idx := strings.Index(tag, `href="`)
|
||||||
|
if idx == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
rest := tag[idx+6:]
|
||||||
|
end := strings.Index(rest, `"`)
|
||||||
|
if end == -1 {
|
||||||
|
return rest
|
||||||
|
}
|
||||||
|
return rest[:end]
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractLinkText(tag string) string {
|
||||||
|
start := strings.Index(tag, ">")
|
||||||
|
if start == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
rest := tag[start+1:]
|
||||||
|
end := strings.Index(rest, "</a>")
|
||||||
|
if end == -1 {
|
||||||
|
return strings.TrimSpace(rest)
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(rest[:end])
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package virtual_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/virtual"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPyPIMerger_BasicMerge(t *testing.T) {
|
||||||
|
m := &virtual.PyPIMerger{}
|
||||||
|
|
||||||
|
member1 := virtual.MemberIndex{
|
||||||
|
RemoteName: "pypi-a",
|
||||||
|
Body: []byte(`<!DOCTYPE html>
|
||||||
|
<html><body>
|
||||||
|
<a href="/simple/requests/">requests</a>
|
||||||
|
<a href="/simple/flask/">flask</a>
|
||||||
|
</body></html>`),
|
||||||
|
}
|
||||||
|
|
||||||
|
member2 := virtual.MemberIndex{
|
||||||
|
RemoteName: "pypi-b",
|
||||||
|
Body: []byte(`<!DOCTYPE html>
|
||||||
|
<html><body>
|
||||||
|
<a href="/simple/django/">django</a>
|
||||||
|
</body></html>`),
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := m.MergeIndexes([]virtual.MemberIndex{member1, member2}, "https://proxy.example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := string(result)
|
||||||
|
if !strings.Contains(body, "requests") {
|
||||||
|
t.Error("expected requests")
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "flask") {
|
||||||
|
t.Error("expected flask")
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "django") {
|
||||||
|
t.Error("expected django")
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "proxy.example.com/api/v1/remote/pypi-a") {
|
||||||
|
t.Error("expected proxy URL for pypi-a")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPyPIMerger_Dedup(t *testing.T) {
|
||||||
|
m := &virtual.PyPIMerger{}
|
||||||
|
|
||||||
|
idx := []byte(`<html><body>
|
||||||
|
<a href="/simple/requests/">requests</a>
|
||||||
|
</body></html>`)
|
||||||
|
|
||||||
|
members := []virtual.MemberIndex{
|
||||||
|
{RemoteName: "a", Body: idx},
|
||||||
|
{RemoteName: "b", Body: idx},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := m.MergeIndexes(members, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
count := strings.Count(string(result), "<a ")
|
||||||
|
if count != 1 {
|
||||||
|
t.Errorf("expected 1 <a> tag for deduplicated requests, got %d\n%s", count, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPyPIMerger_Sorted(t *testing.T) {
|
||||||
|
m := &virtual.PyPIMerger{}
|
||||||
|
|
||||||
|
member := virtual.MemberIndex{
|
||||||
|
RemoteName: "pypi",
|
||||||
|
Body: []byte(`<html><body>
|
||||||
|
<a href="/z/">zebra</a>
|
||||||
|
<a href="/a/">alpha</a>
|
||||||
|
<a href="/m/">middle</a>
|
||||||
|
</body></html>`),
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := m.MergeIndexes([]virtual.MemberIndex{member}, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := string(result)
|
||||||
|
alphaIdx := strings.Index(body, "alpha")
|
||||||
|
middleIdx := strings.Index(body, "middle")
|
||||||
|
zebraIdx := strings.Index(body, "zebra")
|
||||||
|
|
||||||
|
if alphaIdx > middleIdx || middleIdx > zebraIdx {
|
||||||
|
t.Error("expected sorted output")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
baseURL string
|
||||||
|
httpClient *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(baseURL string) *Client {
|
||||||
|
return &Client{
|
||||||
|
baseURL: baseURL,
|
||||||
|
httpClient: http.DefaultClient,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) get(ctx context.Context, path string, out any) error {
|
||||||
|
return c.do(ctx, http.MethodGet, path, nil, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) post(ctx context.Context, path string, body any, out any) error {
|
||||||
|
return c.do(ctx, http.MethodPost, path, body, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) put(ctx context.Context, path string, body any, out any) error {
|
||||||
|
return c.do(ctx, http.MethodPut, path, body, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) delete(ctx context.Context, path string) error {
|
||||||
|
return c.do(ctx, http.MethodDelete, path, nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) do(ctx context.Context, method, path string, body any, out any) error {
|
||||||
|
var bodyReader io.Reader
|
||||||
|
if body != nil {
|
||||||
|
b, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("marshal: %w", err)
|
||||||
|
}
|
||||||
|
bodyReader = bytes.NewReader(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("request: %w", err)
|
||||||
|
}
|
||||||
|
if body != nil {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := c.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("do: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
b, _ := io.ReadAll(resp.Body)
|
||||||
|
return fmt.Errorf("api error %d: %s", resp.StatusCode, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
if out != nil && resp.StatusCode != http.StatusNoContent {
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(out); err != nil {
|
||||||
|
return fmt.Errorf("decode: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) ListRemotes(ctx context.Context) ([]models.Remote, error) {
|
||||||
|
var remotes []models.Remote
|
||||||
|
err := c.get(ctx, "/api/v2/remotes", &remotes)
|
||||||
|
return remotes, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetRemote(ctx context.Context, name string) (*models.Remote, error) {
|
||||||
|
var remote models.Remote
|
||||||
|
err := c.get(ctx, fmt.Sprintf("/api/v2/remotes/%s", name), &remote)
|
||||||
|
return &remote, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) CreateRemote(ctx context.Context, r *models.Remote) error {
|
||||||
|
return c.post(ctx, "/api/v2/remotes", r, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) UpdateRemote(ctx context.Context, r *models.Remote) error {
|
||||||
|
return c.put(ctx, fmt.Sprintf("/api/v2/remotes/%s", r.Name), r, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) DeleteRemote(ctx context.Context, name string) error {
|
||||||
|
return c.delete(ctx, fmt.Sprintf("/api/v2/remotes/%s", name))
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) Stats(ctx context.Context) (*models.OverviewStats, error) {
|
||||||
|
var stats models.OverviewStats
|
||||||
|
err := c.get(ctx, "/api/v2/stats", &stats)
|
||||||
|
return &stats, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Health(ctx context.Context) (*models.RemoteHealth, error) {
|
||||||
|
var health models.RemoteHealth
|
||||||
|
err := c.get(ctx, "/api/v2/health", &health)
|
||||||
|
return &health, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ListObjects(ctx context.Context, remote string, page, perPage int) ([]models.Artifact, error) {
|
||||||
|
var artifacts []models.Artifact
|
||||||
|
err := c.get(ctx, fmt.Sprintf("/api/v2/remotes/%s/objects?page=%d&per_page=%d", remote, page, perPage), &artifacts)
|
||||||
|
return artifacts, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) EvictObject(ctx context.Context, remote, path string) error {
|
||||||
|
return c.delete(ctx, fmt.Sprintf("/api/v2/remotes/%s/objects/%s", remote, path))
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) ListVirtuals(ctx context.Context) ([]models.Virtual, error) {
|
||||||
|
var virtuals []models.Virtual
|
||||||
|
err := c.get(ctx, "/api/v2/virtuals", &virtuals)
|
||||||
|
return virtuals, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetVirtual(ctx context.Context, name string) (*models.Virtual, error) {
|
||||||
|
var virt models.Virtual
|
||||||
|
err := c.get(ctx, fmt.Sprintf("/api/v2/virtuals/%s", name), &virt)
|
||||||
|
return &virt, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) CreateVirtual(ctx context.Context, v *models.Virtual) error {
|
||||||
|
return c.post(ctx, "/api/v2/virtuals", v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) UpdateVirtual(ctx context.Context, v *models.Virtual) error {
|
||||||
|
return c.put(ctx, fmt.Sprintf("/api/v2/virtuals/%s", v.Name), v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) DeleteVirtual(ctx context.Context, name string) error {
|
||||||
|
return c.delete(ctx, fmt.Sprintf("/api/v2/virtuals/%s", name))
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Blob struct {
|
||||||
|
ContentHash string `json:"content_hash"`
|
||||||
|
S3Key string `json:"s3_key"`
|
||||||
|
SizeBytes int64 `json:"size_bytes"`
|
||||||
|
ContentType string `json:"content_type"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Artifact struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
RemoteName string `json:"remote_name"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
ContentHash string `json:"content_hash"`
|
||||||
|
UpstreamETag string `json:"upstream_etag,omitempty"`
|
||||||
|
UpstreamLastModified *time.Time `json:"upstream_last_modified,omitempty"`
|
||||||
|
FirstSeenAt time.Time `json:"first_seen_at"`
|
||||||
|
LastFetchedAt time.Time `json:"last_fetched_at"`
|
||||||
|
LastAccessedAt time.Time `json:"last_accessed_at"`
|
||||||
|
FetchCount int64 `json:"fetch_count"`
|
||||||
|
AccessCount int64 `json:"access_count"`
|
||||||
|
SizeBytes int64 `json:"size_bytes"`
|
||||||
|
ContentType string `json:"content_type,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AccessLogEntry struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
RemoteName string `json:"remote_name"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
CacheHit bool `json:"cache_hit"`
|
||||||
|
SizeBytes int64 `json:"size_bytes"`
|
||||||
|
UpstreamMS int `json:"upstream_ms"`
|
||||||
|
ClientIP string `json:"client_ip"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type LocalFile struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
RepoName string `json:"repo_name"`
|
||||||
|
FilePath string `json:"file_path"`
|
||||||
|
ContentHash string `json:"content_hash"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type PackageType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
PackageGeneric PackageType = "generic"
|
||||||
|
PackageDocker PackageType = "docker"
|
||||||
|
PackageHelm PackageType = "helm"
|
||||||
|
PackagePyPI PackageType = "pypi"
|
||||||
|
PackageNPM PackageType = "npm"
|
||||||
|
PackageRPM PackageType = "rpm"
|
||||||
|
PackageAlpine PackageType = "alpine"
|
||||||
|
PackagePuppet PackageType = "puppet"
|
||||||
|
PackageTerraform PackageType = "terraform"
|
||||||
|
PackageGoProxy PackageType = "goproxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
var validPackageTypes = map[PackageType]bool{
|
||||||
|
PackageGeneric: true,
|
||||||
|
PackageDocker: true,
|
||||||
|
PackageHelm: true,
|
||||||
|
PackagePyPI: true,
|
||||||
|
PackageNPM: true,
|
||||||
|
PackageRPM: true,
|
||||||
|
PackageAlpine: true,
|
||||||
|
PackagePuppet: true,
|
||||||
|
PackageTerraform: true,
|
||||||
|
PackageGoProxy: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PackageType) Valid() bool {
|
||||||
|
return validPackageTypes[p]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PackageType) String() string {
|
||||||
|
return string(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParsePackageType(s string) (PackageType, error) {
|
||||||
|
pt := PackageType(s)
|
||||||
|
if !pt.Valid() {
|
||||||
|
return "", fmt.Errorf("unknown package type: %q", s)
|
||||||
|
}
|
||||||
|
return pt, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package models_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPackageTypeValid(t *testing.T) {
|
||||||
|
valid := []models.PackageType{
|
||||||
|
models.PackageGeneric,
|
||||||
|
models.PackageDocker,
|
||||||
|
models.PackageHelm,
|
||||||
|
models.PackagePyPI,
|
||||||
|
models.PackageNPM,
|
||||||
|
models.PackageRPM,
|
||||||
|
models.PackageAlpine,
|
||||||
|
models.PackagePuppet,
|
||||||
|
models.PackageTerraform,
|
||||||
|
models.PackageGoProxy,
|
||||||
|
}
|
||||||
|
for _, pt := range valid {
|
||||||
|
if !pt.Valid() {
|
||||||
|
t.Errorf("expected %q to be valid", pt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPackageTypeInvalid(t *testing.T) {
|
||||||
|
invalid := []string{"", "bogus", "Docker", "HELM"}
|
||||||
|
for _, s := range invalid {
|
||||||
|
pt := models.PackageType(s)
|
||||||
|
if pt.Valid() {
|
||||||
|
t.Errorf("expected %q to be invalid", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParsePackageType(t *testing.T) {
|
||||||
|
pt, err := models.ParsePackageType("docker")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if pt != models.PackageDocker {
|
||||||
|
t.Errorf("expected docker, got %q", pt)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = models.ParsePackageType("nope")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for unknown type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPackageTypeString(t *testing.T) {
|
||||||
|
if models.PackageGoProxy.String() != "goproxy" {
|
||||||
|
t.Errorf("expected 'goproxy', got %q", models.PackageGoProxy.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Remote struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
PackageType PackageType `json:"package_type"`
|
||||||
|
BaseURL string `json:"base_url"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
Username string `json:"-"`
|
||||||
|
Password string `json:"-"`
|
||||||
|
|
||||||
|
ImmutableTTL int `json:"immutable_ttl"`
|
||||||
|
MutableTTL int `json:"mutable_ttl"`
|
||||||
|
CheckMutable bool `json:"check_mutable"`
|
||||||
|
|
||||||
|
Patterns []string `json:"patterns,omitempty"`
|
||||||
|
Blocklist []string `json:"blocklist,omitempty"`
|
||||||
|
MutablePatterns []string `json:"mutable_patterns,omitempty"`
|
||||||
|
ImmutablePatterns []string `json:"immutable_patterns,omitempty"`
|
||||||
|
|
||||||
|
BanTagsEnabled bool `json:"ban_tags_enabled,omitempty"`
|
||||||
|
BanTags []string `json:"ban_tags,omitempty"`
|
||||||
|
|
||||||
|
QuarantineEnabled bool `json:"quarantine_enabled,omitempty"`
|
||||||
|
QuarantineDays int `json:"quarantine_days,omitempty"`
|
||||||
|
|
||||||
|
StaleOnError bool `json:"stale_on_error"`
|
||||||
|
|
||||||
|
ReleasesRemote string `json:"releases_remote,omitempty"`
|
||||||
|
ManagedBy string `json:"managed_by,omitempty"`
|
||||||
|
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RemoteWithStats struct {
|
||||||
|
Remote
|
||||||
|
Stats RemoteStats `json:"stats"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type RemoteStats struct {
|
||||||
|
ObjectCount int64 `json:"object_count"`
|
||||||
|
TotalBytes int64 `json:"total_bytes"`
|
||||||
|
HitRate30d float64 `json:"hit_rate_30d"`
|
||||||
|
Requests30d int64 `json:"requests_30d"`
|
||||||
|
BandwidthSaved int64 `json:"bandwidth_saved_30d"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OverviewStats struct {
|
||||||
|
TotalRemotes int `json:"total_remotes"`
|
||||||
|
TotalObjects int64 `json:"total_objects"`
|
||||||
|
TotalBytes int64 `json:"total_bytes"`
|
||||||
|
TotalBlobsDeduped int64 `json:"total_blobs_deduped"`
|
||||||
|
BandwidthSaved30d int64 `json:"bandwidth_saved_30d"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RemoteHealth struct {
|
||||||
|
Status string `json:"status"` // healthy, degraded, down
|
||||||
|
LastError string `json:"last_error,omitempty"`
|
||||||
|
ConsecutiveFailures int `json:"consecutive_failures"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Virtual struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
PackageType PackageType `json:"package_type"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
Members []string `json:"members"`
|
||||||
|
ManagedBy string `json:"managed_by,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
FROM node:22-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package.json package-lock.json* ./
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
FROM nginx:alpine
|
||||||
|
|
||||||
|
COPY --from=builder /app/dist /usr/share/nginx/html
|
||||||
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
|
|
||||||
|
EXPOSE 80
|
||||||
|
|
||||||
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>ArtifactAPI</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
location /api/ {
|
||||||
|
proxy_pass http://artifactapi:8000;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_buffering off;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /v2/ {
|
||||||
|
proxy_pass http://artifactapi:8000;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_buffering off;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /health {
|
||||||
|
proxy_pass http://artifactapi:8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /metrics {
|
||||||
|
proxy_pass http://artifactapi:8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ /index.html;
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+2758
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "artifactapi-ui",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "tsc -b && vite build",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"react": "^19.1.0",
|
||||||
|
"react-dom": "^19.1.0",
|
||||||
|
"react-router-dom": "^7.6.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "^19.1.0",
|
||||||
|
"@types/react-dom": "^19.1.0",
|
||||||
|
"@vitejs/plugin-react": "^4.5.0",
|
||||||
|
"typescript": "~5.8.0",
|
||||||
|
"vite": "^6.3.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
.app {
|
||||||
|
display: flex;
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar {
|
||||||
|
width: 220px;
|
||||||
|
background: var(--bg-surface);
|
||||||
|
border-right: 1px solid var(--border);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
padding: 20px 0;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-brand {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
padding: 0 20px 24px;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.brand-icon {
|
||||||
|
font-size: 1.5em;
|
||||||
|
color: var(--accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.brand-text {
|
||||||
|
font-weight: 700;
|
||||||
|
font-size: 1.05em;
|
||||||
|
color: var(--text-bright);
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-nav {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
padding: 0 8px;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-nav a {
|
||||||
|
display: block;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
color: var(--text-muted);
|
||||||
|
font-size: 0.9em;
|
||||||
|
font-weight: 500;
|
||||||
|
transition: all 0.15s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-nav a:hover {
|
||||||
|
background: var(--bg-elevated);
|
||||||
|
color: var(--text);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-nav a.active {
|
||||||
|
background: var(--accent);
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-footer {
|
||||||
|
padding: 16px 20px 0;
|
||||||
|
border-top: 1px solid var(--border);
|
||||||
|
margin-top: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version {
|
||||||
|
font-size: 0.75em;
|
||||||
|
color: var(--text-muted);
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
}
|
||||||
|
|
||||||
|
.content {
|
||||||
|
flex: 1;
|
||||||
|
padding: 32px 40px;
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-title {
|
||||||
|
font-size: 1.5em;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--text-bright);
|
||||||
|
margin-bottom: 24px;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { Routes, Route, NavLink } from 'react-router-dom';
|
||||||
|
import { Dashboard } from './pages/Dashboard';
|
||||||
|
import { Remotes } from './pages/Remotes';
|
||||||
|
import { RemoteDetail } from './pages/RemoteDetail';
|
||||||
|
import { Virtuals } from './pages/Virtuals';
|
||||||
|
import { Objects } from './pages/Objects';
|
||||||
|
import { Probe } from './pages/Probe';
|
||||||
|
import './App.css';
|
||||||
|
|
||||||
|
export function App() {
|
||||||
|
return (
|
||||||
|
<div className="app">
|
||||||
|
<nav className="sidebar">
|
||||||
|
<div className="sidebar-brand">
|
||||||
|
<span className="brand-icon">⬢</span>
|
||||||
|
<span className="brand-text">ArtifactAPI</span>
|
||||||
|
</div>
|
||||||
|
<div className="sidebar-nav">
|
||||||
|
<NavLink to="/" end>Dashboard</NavLink>
|
||||||
|
<NavLink to="/remotes">Remotes</NavLink>
|
||||||
|
<NavLink to="/virtuals">Virtuals</NavLink>
|
||||||
|
<NavLink to="/probe">Test Remote</NavLink>
|
||||||
|
</div>
|
||||||
|
<div className="sidebar-footer">
|
||||||
|
<span className="version">v3.0-dev</span>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<main className="content">
|
||||||
|
<Routes>
|
||||||
|
<Route path="/" element={<Dashboard />} />
|
||||||
|
<Route path="/remotes" element={<Remotes />} />
|
||||||
|
<Route path="/remotes/:name" element={<RemoteDetail />} />
|
||||||
|
<Route path="/remotes/:name/objects" element={<Objects />} />
|
||||||
|
<Route path="/virtuals" element={<Virtuals />} />
|
||||||
|
<Route path="/probe" element={<Probe />} />
|
||||||
|
</Routes>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import type { Remote, Virtual, Artifact, OverviewStats, RemoteStatRow, HealthStatus, ProbeResult } from './types';
|
||||||
|
|
||||||
|
const BASE = '';
|
||||||
|
|
||||||
|
async function fetchJSON<T>(path: string, init?: RequestInit): Promise<T> {
|
||||||
|
const resp = await fetch(`${BASE}${path}`, {
|
||||||
|
...init,
|
||||||
|
headers: { 'Content-Type': 'application/json', ...init?.headers },
|
||||||
|
});
|
||||||
|
if (!resp.ok) {
|
||||||
|
const text = await resp.text();
|
||||||
|
throw new Error(`${resp.status}: ${text}`);
|
||||||
|
}
|
||||||
|
if (resp.status === 204) return undefined as T;
|
||||||
|
return resp.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const api = {
|
||||||
|
health: () => fetchJSON<HealthStatus>('/api/v2/health'),
|
||||||
|
stats: () => fetchJSON<OverviewStats>('/api/v2/stats'),
|
||||||
|
topRemotes: () => fetchJSON<RemoteStatRow[]>('/api/v2/stats/top-remotes'),
|
||||||
|
|
||||||
|
listRemotes: () => fetchJSON<Remote[]>('/api/v2/remotes'),
|
||||||
|
getRemote: (name: string) => fetchJSON<Remote>(`/api/v2/remotes/${name}`),
|
||||||
|
|
||||||
|
listVirtuals: () => fetchJSON<Virtual[]>('/api/v2/virtuals'),
|
||||||
|
getVirtual: (name: string) => fetchJSON<Virtual>(`/api/v2/virtuals/${name}`),
|
||||||
|
|
||||||
|
listObjects: (remote: string, page = 1, perPage = 50) =>
|
||||||
|
fetchJSON<Artifact[]>(`/api/v2/remotes/${remote}/objects?page=${page}&per_page=${perPage}`),
|
||||||
|
|
||||||
|
evictObject: (remote: string, path: string) =>
|
||||||
|
fetchJSON<void>(`/api/v2/remotes/${remote}/objects/${path}`, { method: 'DELETE' }),
|
||||||
|
|
||||||
|
flushRemoteCache: (remote: string) =>
|
||||||
|
fetchJSON<void>(`/api/v2/remotes/${remote}/cache`, { method: 'DELETE' }),
|
||||||
|
|
||||||
|
probe: (remote: string, path: string) =>
|
||||||
|
fetchJSON<ProbeResult>('/api/v2/probe', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ remote, path }),
|
||||||
|
}),
|
||||||
|
};
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
export interface Remote {
|
||||||
|
name: string;
|
||||||
|
package_type: string;
|
||||||
|
base_url: string;
|
||||||
|
description: string;
|
||||||
|
username?: string;
|
||||||
|
immutable_ttl: number;
|
||||||
|
mutable_ttl: number;
|
||||||
|
check_mutable: boolean;
|
||||||
|
patterns: string[];
|
||||||
|
blocklist: string[];
|
||||||
|
mutable_patterns: string[];
|
||||||
|
immutable_patterns: string[];
|
||||||
|
ban_tags_enabled: boolean;
|
||||||
|
ban_tags: string[];
|
||||||
|
quarantine_enabled: boolean;
|
||||||
|
quarantine_days: number;
|
||||||
|
stale_on_error: boolean;
|
||||||
|
releases_remote: string;
|
||||||
|
managed_by: string;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Virtual {
|
||||||
|
name: string;
|
||||||
|
package_type: string;
|
||||||
|
description: string;
|
||||||
|
members: string[];
|
||||||
|
managed_by: string;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Artifact {
|
||||||
|
id: number;
|
||||||
|
remote_name: string;
|
||||||
|
path: string;
|
||||||
|
content_hash: string;
|
||||||
|
upstream_etag: string;
|
||||||
|
first_seen_at: string;
|
||||||
|
last_fetched_at: string;
|
||||||
|
last_accessed_at: string;
|
||||||
|
fetch_count: number;
|
||||||
|
access_count: number;
|
||||||
|
size_bytes: number;
|
||||||
|
content_type: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OverviewStats {
|
||||||
|
total_remotes: number;
|
||||||
|
total_objects: number;
|
||||||
|
total_bytes: number;
|
||||||
|
total_blobs_deduped: number;
|
||||||
|
bandwidth_saved_30d: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RemoteStatRow {
|
||||||
|
name: string;
|
||||||
|
object_count: number;
|
||||||
|
total_bytes: number;
|
||||||
|
requests_30d: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HealthStatus {
|
||||||
|
status: string;
|
||||||
|
postgres: string;
|
||||||
|
redis: string;
|
||||||
|
s3: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProbeResult {
|
||||||
|
status: number;
|
||||||
|
source: string;
|
||||||
|
content_type: string;
|
||||||
|
size_bytes: number;
|
||||||
|
headers: Record<string, string>;
|
||||||
|
duration_ms: number;
|
||||||
|
error: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
.badge {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 0.75em;
|
||||||
|
font-weight: 600;
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.03em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-default {
|
||||||
|
background: var(--bg-elevated);
|
||||||
|
color: var(--text-muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-green {
|
||||||
|
background: rgba(34, 197, 94, 0.15);
|
||||||
|
color: var(--green);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-yellow {
|
||||||
|
background: rgba(234, 179, 8, 0.15);
|
||||||
|
color: var(--yellow);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-red {
|
||||||
|
background: rgba(239, 68, 68, 0.15);
|
||||||
|
color: var(--red);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-blue {
|
||||||
|
background: rgba(59, 130, 246, 0.15);
|
||||||
|
color: var(--accent);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import './Badge.css';
|
||||||
|
|
||||||
|
interface BadgeProps {
|
||||||
|
children: React.ReactNode;
|
||||||
|
variant?: 'default' | 'green' | 'yellow' | 'red' | 'blue';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Badge({ children, variant = 'default' }: BadgeProps) {
|
||||||
|
return <span className={`badge badge-${variant}`}>{children}</span>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
.data-table-wrap {
|
||||||
|
overflow-x: auto;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: var(--bg-surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table th {
|
||||||
|
text-align: left;
|
||||||
|
padding: 10px 14px;
|
||||||
|
font-size: 0.8em;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-muted);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.04em;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
background: var(--bg-elevated);
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table td {
|
||||||
|
padding: 10px 14px;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
color: var(--text);
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table tbody tr:last-child td {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table tbody tr:hover {
|
||||||
|
background: var(--bg-elevated);
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table tbody tr.clickable {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table-empty {
|
||||||
|
text-align: center;
|
||||||
|
color: var(--text-muted);
|
||||||
|
padding: 32px 14px !important;
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import './DataTable.css';
|
||||||
|
|
||||||
|
interface Column<T> {
|
||||||
|
key: string;
|
||||||
|
header: string;
|
||||||
|
render: (item: T) => React.ReactNode;
|
||||||
|
width?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DataTableProps<T> {
|
||||||
|
columns: Column<T>[];
|
||||||
|
data: T[];
|
||||||
|
emptyMessage?: string;
|
||||||
|
onRowClick?: (item: T) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DataTable<T>({ columns, data, emptyMessage = 'No data', onRowClick }: DataTableProps<T>) {
|
||||||
|
return (
|
||||||
|
<div className="data-table-wrap">
|
||||||
|
<table className="data-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{columns.map(col => (
|
||||||
|
<th key={col.key} style={col.width ? { width: col.width } : undefined}>
|
||||||
|
{col.header}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{data.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={columns.length} className="data-table-empty">
|
||||||
|
{emptyMessage}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
data.map((item, i) => (
|
||||||
|
<tr
|
||||||
|
key={i}
|
||||||
|
onClick={onRowClick ? () => onRowClick(item) : undefined}
|
||||||
|
className={onRowClick ? 'clickable' : ''}
|
||||||
|
>
|
||||||
|
{columns.map(col => (
|
||||||
|
<td key={col.key}>{col.render(item)}</td>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
.stats-card {
|
||||||
|
background: var(--bg-surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
padding: 20px;
|
||||||
|
min-width: 160px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stats-label {
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: var(--text-muted);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stats-value {
|
||||||
|
font-size: 1.8em;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--text-bright);
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
line-height: 1.2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stats-sub {
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: var(--text-muted);
|
||||||
|
margin-top: 4px;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import './StatsCard.css';
|
||||||
|
|
||||||
|
interface StatsCardProps {
|
||||||
|
label: string;
|
||||||
|
value: string | number;
|
||||||
|
sub?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function StatsCard({ label, value, sub }: StatsCardProps) {
|
||||||
|
return (
|
||||||
|
<div className="stats-card">
|
||||||
|
<div className="stats-label">{label}</div>
|
||||||
|
<div className="stats-value">{value}</div>
|
||||||
|
{sub && <div className="stats-sub">{sub}</div>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
export function formatBytes(bytes: number): string {
|
||||||
|
if (bytes === 0) return '0 B';
|
||||||
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||||
|
const val = bytes / Math.pow(1024, i);
|
||||||
|
return `${val.toFixed(i === 0 ? 0 : 1)} ${units[i]}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatNumber(n: number): string {
|
||||||
|
return n.toLocaleString();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function timeAgo(dateStr: string): string {
|
||||||
|
const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
|
||||||
|
if (seconds < 60) return `${seconds}s ago`;
|
||||||
|
const minutes = Math.floor(seconds / 60);
|
||||||
|
if (minutes < 60) return `${minutes}m ago`;
|
||||||
|
const hours = Math.floor(minutes / 60);
|
||||||
|
if (hours < 24) return `${hours}h ago`;
|
||||||
|
const days = Math.floor(hours / 24);
|
||||||
|
return `${days}d ago`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function truncateHash(hash: string, len = 12): string {
|
||||||
|
if (hash.startsWith('sha256:')) {
|
||||||
|
return hash.slice(0, 7 + len);
|
||||||
|
}
|
||||||
|
return hash.slice(0, len);
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
*,
|
||||||
|
*::before,
|
||||||
|
*::after {
|
||||||
|
box-sizing: border-box;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
:root {
|
||||||
|
--bg: #0b0e14;
|
||||||
|
--bg-surface: #111620;
|
||||||
|
--bg-elevated: #1a2030;
|
||||||
|
--border: #252d3d;
|
||||||
|
--text: #c5cdd9;
|
||||||
|
--text-muted: #6b7a90;
|
||||||
|
--text-bright: #e8ecf2;
|
||||||
|
--accent: #3b82f6;
|
||||||
|
--accent-hover: #2563eb;
|
||||||
|
--green: #22c55e;
|
||||||
|
--yellow: #eab308;
|
||||||
|
--red: #ef4444;
|
||||||
|
--orange: #f97316;
|
||||||
|
--radius: 8px;
|
||||||
|
--font-mono: 'JetBrains Mono', 'Fira Code', 'SF Mono', Menlo, monospace;
|
||||||
|
--font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: var(--font-sans);
|
||||||
|
background: var(--bg);
|
||||||
|
color: var(--text);
|
||||||
|
line-height: 1.5;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: var(--accent);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
color: var(--accent-hover);
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
code, .mono {
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
font-size: 0.85em;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { StrictMode } from 'react';
|
||||||
|
import { createRoot } from 'react-dom/client';
|
||||||
|
import { BrowserRouter } from 'react-router-dom';
|
||||||
|
import { App } from './App';
|
||||||
|
import './index.css';
|
||||||
|
|
||||||
|
createRoot(document.getElementById('root')!).render(
|
||||||
|
<StrictMode>
|
||||||
|
<BrowserRouter>
|
||||||
|
<App />
|
||||||
|
</BrowserRouter>
|
||||||
|
</StrictMode>,
|
||||||
|
);
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
.stats-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 16px;
|
||||||
|
margin-bottom: 24px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.health-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
margin-bottom: 28px;
|
||||||
|
padding: 12px 16px;
|
||||||
|
background: var(--bg-surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
.health-label {
|
||||||
|
font-size: 0.85em;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-muted);
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-title {
|
||||||
|
font-size: 1.1em;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-bright);
|
||||||
|
margin-bottom: 12px;
|
||||||
|
margin-top: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error-banner {
|
||||||
|
background: rgba(239, 68, 68, 0.1);
|
||||||
|
border: 1px solid var(--red);
|
||||||
|
color: var(--red);
|
||||||
|
padding: 12px 16px;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loading {
|
||||||
|
color: var(--text-muted);
|
||||||
|
font-size: 0.9em;
|
||||||
|
padding: 32px 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { Link } from 'react-router-dom';
|
||||||
|
import { api } from '../api/client';
|
||||||
|
import type { OverviewStats, RemoteStatRow, HealthStatus } from '../api/types';
|
||||||
|
import { StatsCard } from '../components/StatsCard';
|
||||||
|
import { Badge } from '../components/Badge';
|
||||||
|
import { DataTable } from '../components/DataTable';
|
||||||
|
import { formatBytes, formatNumber } from '../components/format';
|
||||||
|
import './Dashboard.css';
|
||||||
|
|
||||||
|
export function Dashboard() {
|
||||||
|
const [stats, setStats] = useState<OverviewStats | null>(null);
|
||||||
|
const [topRemotes, setTopRemotes] = useState<RemoteStatRow[]>([]);
|
||||||
|
const [health, setHealth] = useState<HealthStatus | null>(null);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
Promise.all([api.stats(), api.topRemotes(), api.health()])
|
||||||
|
.then(([s, tr, h]) => {
|
||||||
|
setStats(s);
|
||||||
|
setTopRemotes(tr || []);
|
||||||
|
setHealth(h);
|
||||||
|
})
|
||||||
|
.catch(e => setError(e.message));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (error) return <div className="error-banner">{error}</div>;
|
||||||
|
if (!stats) return <div className="loading">Loading...</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1 className="page-title">Dashboard</h1>
|
||||||
|
|
||||||
|
<div className="stats-row">
|
||||||
|
<StatsCard label="Remotes" value={formatNumber(stats.total_remotes)} />
|
||||||
|
<StatsCard label="Cached Objects" value={formatNumber(stats.total_objects)} />
|
||||||
|
<StatsCard label="Storage Used" value={formatBytes(stats.total_bytes)} />
|
||||||
|
<StatsCard
|
||||||
|
label="Dedup Savings"
|
||||||
|
value={formatNumber(stats.total_blobs_deduped)}
|
||||||
|
sub="shared blobs"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{health && (
|
||||||
|
<div className="health-row">
|
||||||
|
<span className="health-label">Services</span>
|
||||||
|
<Badge variant={health.postgres === 'ok' ? 'green' : 'red'}>
|
||||||
|
postgres: {health.postgres}
|
||||||
|
</Badge>
|
||||||
|
<Badge variant={health.redis === 'ok' ? 'green' : 'red'}>
|
||||||
|
redis: {health.redis}
|
||||||
|
</Badge>
|
||||||
|
<Badge variant={health.s3 === 'ok' ? 'green' : 'red'}>
|
||||||
|
s3: {health.s3}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<h2 className="section-title">Top Remotes by Size</h2>
|
||||||
|
<DataTable
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
|
key: 'name',
|
||||||
|
header: 'Name',
|
||||||
|
render: (r: RemoteStatRow) => <Link to={`/remotes/${r.name}`}>{r.name}</Link>,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'objects',
|
||||||
|
header: 'Objects',
|
||||||
|
render: (r: RemoteStatRow) => formatNumber(r.object_count),
|
||||||
|
width: '120px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'size',
|
||||||
|
header: 'Size',
|
||||||
|
render: (r: RemoteStatRow) => formatBytes(r.total_bytes),
|
||||||
|
width: '120px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'requests',
|
||||||
|
header: 'Requests (30d)',
|
||||||
|
render: (r: RemoteStatRow) => formatNumber(r.requests_30d),
|
||||||
|
width: '140px',
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
data={topRemotes}
|
||||||
|
emptyMessage="No remotes configured yet"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
.objects-toolbar {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.obj-path {
|
||||||
|
font-size: 0.85em;
|
||||||
|
word-break: break-all;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hash-cell {
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: var(--text-muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-evict {
|
||||||
|
background: transparent;
|
||||||
|
border: 1px solid var(--red);
|
||||||
|
color: var(--red);
|
||||||
|
padding: 3px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 0.8em;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.15s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-evict:hover {
|
||||||
|
background: rgba(239, 68, 68, 0.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 16px;
|
||||||
|
margin-top: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-info {
|
||||||
|
font-size: 0.85em;
|
||||||
|
color: var(--text-muted);
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-sm {
|
||||||
|
padding: 5px 12px;
|
||||||
|
font-size: 0.85em;
|
||||||
|
background: var(--bg-surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
color: var(--text);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-sm:hover:not(:disabled) {
|
||||||
|
background: var(--bg-elevated);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-sm:disabled {
|
||||||
|
opacity: 0.4;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
import { useEffect, useState, useCallback } from 'react';
|
||||||
|
import { useParams, Link } from 'react-router-dom';
|
||||||
|
import { api } from '../api/client';
|
||||||
|
import type { Artifact } from '../api/types';
|
||||||
|
import { DataTable } from '../components/DataTable';
|
||||||
|
import { formatBytes, timeAgo, truncateHash } from '../components/format';
|
||||||
|
import './Objects.css';
|
||||||
|
|
||||||
|
export function Objects() {
|
||||||
|
const { name } = useParams<{ name: string }>();
|
||||||
|
const [artifacts, setArtifacts] = useState<Artifact[]>([]);
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [filter, setFilter] = useState('');
|
||||||
|
|
||||||
|
const load = useCallback(() => {
|
||||||
|
if (!name) return;
|
||||||
|
setLoading(true);
|
||||||
|
api.listObjects(name, page, 50)
|
||||||
|
.then(a => setArtifacts(a || []))
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}, [name, page]);
|
||||||
|
|
||||||
|
useEffect(() => { load(); }, [load]);
|
||||||
|
|
||||||
|
const handleEvict = async (path: string) => {
|
||||||
|
if (!name || !confirm(`Evict ${path}?`)) return;
|
||||||
|
await api.evictObject(name, path);
|
||||||
|
load();
|
||||||
|
};
|
||||||
|
|
||||||
|
const filtered = filter
|
||||||
|
? artifacts.filter(a => a.path.toLowerCase().includes(filter.toLowerCase()))
|
||||||
|
: artifacts;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="detail-header">
|
||||||
|
<Link to={`/remotes/${name}`} className="back-link">← {name}</Link>
|
||||||
|
<h1 className="page-title">Cached Objects</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="objects-toolbar">
|
||||||
|
<input
|
||||||
|
className="search-input"
|
||||||
|
placeholder="Filter by path..."
|
||||||
|
value={filter}
|
||||||
|
onChange={e => setFilter(e.target.value)}
|
||||||
|
/>
|
||||||
|
<span className="result-count">{filtered.length} objects</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{loading ? (
|
||||||
|
<div className="loading">Loading...</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<DataTable
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
|
key: 'path',
|
||||||
|
header: 'Path',
|
||||||
|
render: (a: Artifact) => <span className="mono obj-path">{a.path}</span>,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'size',
|
||||||
|
header: 'Size',
|
||||||
|
render: (a: Artifact) => formatBytes(a.size_bytes),
|
||||||
|
width: '100px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'hash',
|
||||||
|
header: 'Hash',
|
||||||
|
render: (a: Artifact) => (
|
||||||
|
<span className="mono hash-cell" title={a.content_hash}>
|
||||||
|
{truncateHash(a.content_hash)}
|
||||||
|
</span>
|
||||||
|
),
|
||||||
|
width: '160px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'accessed',
|
||||||
|
header: 'Last Accessed',
|
||||||
|
render: (a: Artifact) => timeAgo(a.last_accessed_at),
|
||||||
|
width: '120px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'hits',
|
||||||
|
header: 'Hits',
|
||||||
|
render: (a: Artifact) => a.access_count,
|
||||||
|
width: '70px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'actions',
|
||||||
|
header: '',
|
||||||
|
render: (a: Artifact) => (
|
||||||
|
<button
|
||||||
|
className="btn-evict"
|
||||||
|
onClick={(e) => { e.stopPropagation(); handleEvict(a.path); }}
|
||||||
|
>
|
||||||
|
Evict
|
||||||
|
</button>
|
||||||
|
),
|
||||||
|
width: '80px',
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
data={filtered}
|
||||||
|
emptyMessage="No cached objects"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div className="pagination">
|
||||||
|
<button
|
||||||
|
className="btn btn-sm"
|
||||||
|
disabled={page === 1}
|
||||||
|
onClick={() => setPage(p => Math.max(1, p - 1))}
|
||||||
|
>
|
||||||
|
Previous
|
||||||
|
</button>
|
||||||
|
<span className="page-info">Page {page}</span>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm"
|
||||||
|
disabled={artifacts.length < 50}
|
||||||
|
onClick={() => setPage(p => p + 1)}
|
||||||
|
>
|
||||||
|
Next
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user