Compare commits
4 Commits
7f569cdcdc
...
v3.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a6721c2a7 | |||
| 7b13644421 | |||
| de96637122 | |||
| 1e91a5fb72 |
@@ -9,9 +9,11 @@ import (
|
|||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
v2 "git.unkin.net/unkin/artifactapi/internal/api/v2"
|
||||||
"git.unkin.net/unkin/artifactapi/internal/database"
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
||||||
"git.unkin.net/unkin/artifactapi/internal/provider"
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
"git.unkin.net/unkin/artifactapi/internal/proxy"
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/storage"
|
||||||
"git.unkin.net/unkin/artifactapi/internal/virtual"
|
"git.unkin.net/unkin/artifactapi/internal/virtual"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,15 +21,18 @@ type ProxyHandler struct {
|
|||||||
engine *proxy.Engine
|
engine *proxy.Engine
|
||||||
virtualEngine *virtual.Engine
|
virtualEngine *virtual.Engine
|
||||||
db *database.DB
|
db *database.DB
|
||||||
|
store *storage.S3
|
||||||
|
local *v2.LocalHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxyHandler(engine *proxy.Engine, virtualEngine *virtual.Engine, db *database.DB) *ProxyHandler {
|
func NewProxyHandler(engine *proxy.Engine, virtualEngine *virtual.Engine, db *database.DB, store *storage.S3, local *v2.LocalHandler) *ProxyHandler {
|
||||||
return &ProxyHandler{engine: engine, virtualEngine: virtualEngine, db: db}
|
return &ProxyHandler{engine: engine, virtualEngine: virtualEngine, db: db, store: store, local: local}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ProxyHandler) Routes() chi.Router {
|
func (h *ProxyHandler) Routes() chi.Router {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/remote/{remoteName}/*", h.handleProxy)
|
r.Get("/remote/{remoteName}/*", h.handleProxy)
|
||||||
|
r.Get("/local/{localName}/*", h.handleLocal)
|
||||||
r.Get("/virtual/{virtualName}/*", h.handleVirtual)
|
r.Get("/virtual/{virtualName}/*", h.handleVirtual)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@@ -95,6 +100,54 @@ func (h *ProxyHandler) handleVirtual(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(body)
|
w.Write(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *ProxyHandler) handleLocal(w http.ResponseWriter, r *http.Request) {
|
||||||
|
localName := chi.URLParam(r, "localName")
|
||||||
|
path := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
remote, err := h.db.GetRemote(r.Context(), localName)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("local %q not found", localName), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prov, _ := provider.Get(remote.PackageType)
|
||||||
|
if indexer, ok := prov.(provider.LocalIndexer); ok {
|
||||||
|
if indexer.ServeLocalIndex(w, r, h.db, remote.Name, path) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
h.serveLocalFile(w, r, localName, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ProxyHandler) serveLocalFile(w http.ResponseWriter, r *http.Request, repoName, path string) {
|
||||||
|
file, err := h.db.GetLocalFile(r.Context(), repoName, path)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("local file lookup failed", "repo", repoName, "path", path, "error", err)
|
||||||
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if file == nil {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s3Key := storage.BlobKey(file.ContentHash[len("sha256:"):])
|
||||||
|
reader, info, err := h.store.Download(r.Context(), s3Key)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("local file download failed", "repo", repoName, "path", path, "error", err)
|
||||||
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", info.ContentType)
|
||||||
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", info.Size))
|
||||||
|
w.Header().Set("X-Artifact-Source", "local")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
io.Copy(w, reader)
|
||||||
|
}
|
||||||
|
|
||||||
func scheme(r *http.Request) string {
|
func scheme(r *http.Request) string {
|
||||||
if r.TLS != nil {
|
if r.TLS != nil {
|
||||||
return "https"
|
return "https"
|
||||||
|
|||||||
@@ -0,0 +1,192 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"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/storage"
|
||||||
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LocalHandler struct {
|
||||||
|
db *database.DB
|
||||||
|
store *storage.S3
|
||||||
|
cas *storage.CAS
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLocalHandler(db *database.DB, store *storage.S3) *LocalHandler {
|
||||||
|
return &LocalHandler{
|
||||||
|
db: db,
|
||||||
|
store: store,
|
||||||
|
cas: storage.NewCAS(store),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Put("/*", h.upload)
|
||||||
|
r.Get("/*", h.download)
|
||||||
|
r.Delete("/*", h.remove)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) upload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
repoName := chi.URLParam(r, "name")
|
||||||
|
filePath := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
if filePath == "" {
|
||||||
|
http.Error(w, "file path required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remote, err := h.db.GetRemote(r.Context(), repoName)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("remote %q not found", repoName), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if remote.RepoType != models.RepoTypeLocal {
|
||||||
|
http.Error(w, "upload only allowed for local repository types", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prov, _ := provider.Get(remote.PackageType)
|
||||||
|
|
||||||
|
if uploader, ok := prov.(provider.LocalUploader); ok {
|
||||||
|
h.uploadValidated(w, r, remote, filePath, uploader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.uploadGeneric(w, r, remote, filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) uploadValidated(w http.ResponseWriter, r *http.Request, remote *models.Remote, filePath string, uploader provider.LocalUploader) {
|
||||||
|
storagePath, contentType, err := uploader.ValidateUpload(filePath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
existing, err := h.db.GetLocalFile(r.Context(), remote.Name, storagePath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existing != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("file %q already exists; overwrites are not allowed", storagePath), http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := h.cas.Store(r.Context(), r.Body, contentType)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("store failed: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.db.UpsertBlob(r.Context(), result.ContentHash, result.S3Key, result.SizeBytes, contentType); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("record blob: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.db.CreateLocalFile(r.Context(), remote.Name, storagePath, result.ContentHash); err != nil {
|
||||||
|
if errors.Is(err, database.ErrAlreadyExists) {
|
||||||
|
http.Error(w, fmt.Sprintf("file %q already exists; overwrites are not allowed", storagePath), http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Error(w, fmt.Sprintf("record file: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusCreated, uploader.UploadResponse(storagePath, result.ContentHash, result.SizeBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) uploadGeneric(w http.ResponseWriter, r *http.Request, remote *models.Remote, filePath string) {
|
||||||
|
existing, err := h.db.GetLocalFile(r.Context(), remote.Name, filePath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existing != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("file %q already exists; overwrites are not allowed", filePath), http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := "application/octet-stream"
|
||||||
|
if ct := r.Header.Get("Content-Type"); ct != "" && ct != "application/octet-stream" {
|
||||||
|
contentType = ct
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := h.cas.Store(r.Context(), r.Body, contentType)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("store failed: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.db.UpsertBlob(r.Context(), result.ContentHash, result.S3Key, result.SizeBytes, contentType); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("record blob: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.db.CreateLocalFile(r.Context(), remote.Name, filePath, result.ContentHash); err != nil {
|
||||||
|
if errors.Is(err, database.ErrAlreadyExists) {
|
||||||
|
http.Error(w, fmt.Sprintf("file %q already exists; overwrites are not allowed", filePath), http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Error(w, fmt.Sprintf("record file: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusCreated, map[string]any{
|
||||||
|
"path": filePath,
|
||||||
|
"content_hash": result.ContentHash,
|
||||||
|
"size_bytes": result.SizeBytes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) download(w http.ResponseWriter, r *http.Request) {
|
||||||
|
repoName := chi.URLParam(r, "name")
|
||||||
|
filePath := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
file, err := h.db.GetLocalFile(r.Context(), repoName, filePath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if file == nil {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s3Key := storage.BlobKey(file.ContentHash[len("sha256:"):])
|
||||||
|
reader, info, err := h.store.Download(r.Context(), s3Key)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("download failed: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", info.ContentType)
|
||||||
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", info.Size))
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
io.Copy(w, reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) remove(w http.ResponseWriter, r *http.Request) {
|
||||||
|
repoName := chi.URLParam(r, "name")
|
||||||
|
filePath := chi.URLParam(r, "*")
|
||||||
|
|
||||||
|
if err := h.db.DeleteLocalFile(r.Context(), repoName, filePath); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *LocalHandler) DB() *database.DB {
|
||||||
|
return h.db
|
||||||
|
}
|
||||||
@@ -58,6 +58,17 @@ func (h *RemotesHandler) create(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, fmt.Sprintf("invalid package type: %q", remote.PackageType), http.StatusBadRequest)
|
http.Error(w, fmt.Sprintf("invalid package type: %q", remote.PackageType), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if remote.RepoType == "" {
|
||||||
|
remote.RepoType = models.RepoTypeRemote
|
||||||
|
}
|
||||||
|
if !remote.RepoType.Valid() {
|
||||||
|
http.Error(w, fmt.Sprintf("invalid repo type: %q", remote.RepoType), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if remote.RepoType == models.RepoTypeRemote && remote.BaseURL == "" {
|
||||||
|
http.Error(w, "base_url is required for remote repositories", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
if err := h.db.CreateRemote(r.Context(), &remote); err != nil {
|
if err := h.db.CreateRemote(r.Context(), &remote); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
|
||||||
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrAlreadyExists = fmt.Errorf("file already exists")
|
||||||
|
|
||||||
|
func (db *DB) CreateLocalFile(ctx context.Context, repoName, filePath, contentHash string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `
|
||||||
|
INSERT INTO local_files (repo_name, file_path, content_hash)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
`, repoName, filePath, contentHash)
|
||||||
|
if err != nil {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||||
|
return ErrAlreadyExists
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetLocalFile(ctx context.Context, repoName, filePath string) (*LocalFile, error) {
|
||||||
|
row := db.Pool.QueryRow(ctx, `
|
||||||
|
SELECT id, repo_name, file_path, content_hash, created_at
|
||||||
|
FROM local_files
|
||||||
|
WHERE repo_name = $1 AND file_path = $2
|
||||||
|
`, repoName, filePath)
|
||||||
|
|
||||||
|
var f LocalFile
|
||||||
|
if err := row.Scan(&f.ID, &f.RepoName, &f.FilePath, &f.ContentHash, &f.CreatedAt); err != nil {
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &f, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListLocalFiles(ctx context.Context, repoName string, limit, offset int) ([]LocalFile, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT id, repo_name, file_path, content_hash, created_at
|
||||||
|
FROM local_files
|
||||||
|
WHERE repo_name = $1
|
||||||
|
ORDER BY file_path
|
||||||
|
LIMIT $2 OFFSET $3
|
||||||
|
`, repoName, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var files []LocalFile
|
||||||
|
for rows.Next() {
|
||||||
|
var f LocalFile
|
||||||
|
if err := rows.Scan(&f.ID, &f.RepoName, &f.FilePath, &f.ContentHash, &f.CreatedAt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
files = append(files, f)
|
||||||
|
}
|
||||||
|
return files, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListLocalFilesByPrefix(ctx context.Context, repoName, prefix string) ([]LocalFile, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT id, repo_name, file_path, content_hash, created_at
|
||||||
|
FROM local_files
|
||||||
|
WHERE repo_name = $1 AND file_path LIKE $2
|
||||||
|
ORDER BY file_path
|
||||||
|
`, repoName, prefix+"%")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var files []LocalFile
|
||||||
|
for rows.Next() {
|
||||||
|
var f LocalFile
|
||||||
|
if err := rows.Scan(&f.ID, &f.RepoName, &f.FilePath, &f.ContentHash, &f.CreatedAt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
files = append(files, f)
|
||||||
|
}
|
||||||
|
return files, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListLocalFilePackages(ctx context.Context, repoName string) ([]string, error) {
|
||||||
|
rows, err := db.Pool.Query(ctx, `
|
||||||
|
SELECT DISTINCT split_part(file_path, '/', 1)
|
||||||
|
FROM local_files
|
||||||
|
WHERE repo_name = $1
|
||||||
|
ORDER BY 1
|
||||||
|
`, repoName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var packages []string
|
||||||
|
for rows.Next() {
|
||||||
|
var pkg string
|
||||||
|
if err := rows.Scan(&pkg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
packages = append(packages, pkg)
|
||||||
|
}
|
||||||
|
return packages, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListFilesByPrefix(ctx context.Context, repoName, prefix string) ([]provider.FileEntry, error) {
|
||||||
|
files, err := db.ListLocalFilesByPrefix(ctx, repoName, prefix)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := make([]provider.FileEntry, len(files))
|
||||||
|
for i, f := range files {
|
||||||
|
result[i] = provider.FileEntry{FilePath: f.FilePath, ContentHash: f.ContentHash}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ListPackages(ctx context.Context, repoName string) ([]string, error) {
|
||||||
|
return db.ListLocalFilePackages(ctx, repoName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) DeleteLocalFile(ctx context.Context, repoName, filePath string) error {
|
||||||
|
_, err := db.Pool.Exec(ctx, `DELETE FROM local_files WHERE repo_name = $1 AND file_path = $2`, repoName, filePath)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -42,7 +42,8 @@ func (db *DB) migrate() error {
|
|||||||
CREATE TABLE IF NOT EXISTS remotes (
|
CREATE TABLE IF NOT EXISTS remotes (
|
||||||
name TEXT PRIMARY KEY,
|
name TEXT PRIMARY KEY,
|
||||||
package_type TEXT NOT NULL,
|
package_type TEXT NOT NULL,
|
||||||
base_url TEXT NOT NULL,
|
repo_type TEXT DEFAULT 'remote',
|
||||||
|
base_url TEXT NOT NULL DEFAULT '',
|
||||||
description TEXT DEFAULT '',
|
description TEXT DEFAULT '',
|
||||||
username TEXT DEFAULT '',
|
username TEXT DEFAULT '',
|
||||||
password TEXT DEFAULT '',
|
password TEXT DEFAULT '',
|
||||||
@@ -121,6 +122,8 @@ func (db *DB) migrate() error {
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_access_log_remote_time ON access_log(remote_name, created_at);
|
CREATE INDEX IF NOT EXISTS idx_access_log_remote_time ON access_log(remote_name, created_at);
|
||||||
|
|
||||||
|
ALTER TABLE remotes ADD COLUMN IF NOT EXISTS repo_type TEXT DEFAULT 'remote';
|
||||||
`)
|
`)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"git.unkin.net/unkin/artifactapi/pkg/models"
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const remoteCols = `name, package_type, base_url, description, username, password,
|
const remoteCols = `name, package_type, repo_type, base_url, description, username, password,
|
||||||
immutable_ttl, mutable_ttl, check_mutable,
|
immutable_ttl, mutable_ttl, check_mutable,
|
||||||
patterns, blocklist, mutable_patterns, immutable_patterns,
|
patterns, blocklist, mutable_patterns, immutable_patterns,
|
||||||
ban_tags_enabled, ban_tags,
|
ban_tags_enabled, ban_tags,
|
||||||
@@ -15,7 +15,7 @@ const remoteCols = `name, package_type, base_url, description, username, passwor
|
|||||||
|
|
||||||
func scanRemote(scanner interface{ Scan(...any) error }, r *models.Remote) error {
|
func scanRemote(scanner interface{ Scan(...any) error }, r *models.Remote) error {
|
||||||
return scanner.Scan(
|
return scanner.Scan(
|
||||||
&r.Name, &r.PackageType, &r.BaseURL, &r.Description, &r.Username, &r.Password,
|
&r.Name, &r.PackageType, &r.RepoType, &r.BaseURL, &r.Description, &r.Username, &r.Password,
|
||||||
&r.ImmutableTTL, &r.MutableTTL, &r.CheckMutable,
|
&r.ImmutableTTL, &r.MutableTTL, &r.CheckMutable,
|
||||||
&r.Patterns, &r.Blocklist, &r.MutablePatterns, &r.ImmutablePatterns,
|
&r.Patterns, &r.Blocklist, &r.MutablePatterns, &r.ImmutablePatterns,
|
||||||
&r.BanTagsEnabled, &r.BanTags,
|
&r.BanTagsEnabled, &r.BanTags,
|
||||||
@@ -54,15 +54,15 @@ func (db *DB) ListRemotes(ctx context.Context) ([]models.Remote, error) {
|
|||||||
func (db *DB) CreateRemote(ctx context.Context, r *models.Remote) error {
|
func (db *DB) CreateRemote(ctx context.Context, r *models.Remote) error {
|
||||||
_, err := db.Pool.Exec(ctx, `
|
_, err := db.Pool.Exec(ctx, `
|
||||||
INSERT INTO remotes (
|
INSERT INTO remotes (
|
||||||
name, package_type, base_url, description, username, password,
|
name, package_type, repo_type, base_url, description, username, password,
|
||||||
immutable_ttl, mutable_ttl, check_mutable,
|
immutable_ttl, mutable_ttl, check_mutable,
|
||||||
patterns, blocklist, mutable_patterns, immutable_patterns,
|
patterns, blocklist, mutable_patterns, immutable_patterns,
|
||||||
ban_tags_enabled, ban_tags,
|
ban_tags_enabled, ban_tags,
|
||||||
quarantine_enabled, quarantine_days, stale_on_error,
|
quarantine_enabled, quarantine_days, stale_on_error,
|
||||||
releases_remote, managed_by
|
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)
|
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21)
|
||||||
`,
|
`,
|
||||||
r.Name, r.PackageType, r.BaseURL, r.Description, r.Username, r.Password,
|
r.Name, r.PackageType, r.RepoType, r.BaseURL, r.Description, r.Username, r.Password,
|
||||||
r.ImmutableTTL, r.MutableTTL, r.CheckMutable,
|
r.ImmutableTTL, r.MutableTTL, r.CheckMutable,
|
||||||
r.Patterns, r.Blocklist, r.MutablePatterns, r.ImmutablePatterns,
|
r.Patterns, r.Blocklist, r.MutablePatterns, r.ImmutablePatterns,
|
||||||
r.BanTagsEnabled, r.BanTags,
|
r.BanTagsEnabled, r.BanTags,
|
||||||
@@ -75,15 +75,15 @@ func (db *DB) CreateRemote(ctx context.Context, r *models.Remote) error {
|
|||||||
func (db *DB) UpdateRemote(ctx context.Context, r *models.Remote) error {
|
func (db *DB) UpdateRemote(ctx context.Context, r *models.Remote) error {
|
||||||
_, err := db.Pool.Exec(ctx, `
|
_, err := db.Pool.Exec(ctx, `
|
||||||
UPDATE remotes SET
|
UPDATE remotes SET
|
||||||
package_type=$2, base_url=$3, description=$4, username=$5, password=$6,
|
package_type=$2, repo_type=$3, base_url=$4, description=$5, username=$6, password=$7,
|
||||||
immutable_ttl=$7, mutable_ttl=$8, check_mutable=$9,
|
immutable_ttl=$8, mutable_ttl=$9, check_mutable=$10,
|
||||||
patterns=$10, blocklist=$11, mutable_patterns=$12, immutable_patterns=$13,
|
patterns=$11, blocklist=$12, mutable_patterns=$13, immutable_patterns=$14,
|
||||||
ban_tags_enabled=$14, ban_tags=$15,
|
ban_tags_enabled=$15, ban_tags=$16,
|
||||||
quarantine_enabled=$16, quarantine_days=$17, stale_on_error=$18,
|
quarantine_enabled=$17, quarantine_days=$18, stale_on_error=$19,
|
||||||
releases_remote=$19, managed_by=$20, updated_at=NOW()
|
releases_remote=$20, managed_by=$21, updated_at=NOW()
|
||||||
WHERE name=$1
|
WHERE name=$1
|
||||||
`,
|
`,
|
||||||
r.Name, r.PackageType, r.BaseURL, r.Description, r.Username, r.Password,
|
r.Name, r.PackageType, r.RepoType, r.BaseURL, r.Description, r.Username, r.Password,
|
||||||
r.ImmutableTTL, r.MutableTTL, r.CheckMutable,
|
r.ImmutableTTL, r.MutableTTL, r.CheckMutable,
|
||||||
r.Patterns, r.Blocklist, r.MutablePatterns, r.ImmutablePatterns,
|
r.Patterns, r.Blocklist, r.MutablePatterns, r.ImmutablePatterns,
|
||||||
r.BanTagsEnabled, r.BanTags,
|
r.BanTagsEnabled, r.BanTags,
|
||||||
|
|||||||
@@ -24,6 +24,26 @@ type Provider interface {
|
|||||||
AuthHeaders(ctx context.Context, remote models.Remote) (http.Header, error)
|
AuthHeaders(ctx context.Context, remote models.Remote) (http.Header, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FileEntry struct {
|
||||||
|
FilePath string
|
||||||
|
ContentHash string
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileStore interface {
|
||||||
|
ListFilesByPrefix(ctx context.Context, repoName, prefix string) ([]FileEntry, error)
|
||||||
|
ListPackages(ctx context.Context, repoName string) ([]string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type LocalUploader interface {
|
||||||
|
ValidateUpload(filePath string) (storagePath, contentType string, err error)
|
||||||
|
UploadResponse(storagePath, contentHash string, sizeBytes int64) map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type LocalIndexer interface {
|
||||||
|
ServeLocalIndex(w http.ResponseWriter, r *http.Request, files FileStore, repoName, path string) bool
|
||||||
|
GenerateLocalIndex(ctx context.Context, files FileStore, repoName, path string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
type IndexMerger interface {
|
type IndexMerger interface {
|
||||||
MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error)
|
MergeIndexes(members []MemberIndex, proxyBaseURL string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package pypi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.unkin.net/unkin/artifactapi/internal/auth"
|
"git.unkin.net/unkin/artifactapi/internal/auth"
|
||||||
@@ -14,6 +17,9 @@ func init() {
|
|||||||
provider.Register(&Provider{})
|
provider.Register(&Provider{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fileRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*\.(whl|tar\.gz|zip)$`)
|
||||||
|
var normalizeRe = regexp.MustCompile(`[-_.]+`)
|
||||||
|
|
||||||
type Provider struct{}
|
type Provider struct{}
|
||||||
|
|
||||||
func (p *Provider) Type() models.PackageType { return models.PackagePyPI }
|
func (p *Provider) Type() models.PackageType { return models.PackagePyPI }
|
||||||
@@ -60,3 +66,177 @@ func (p *Provider) RewriteResponse(body []byte, remote models.Remote, proxyBaseU
|
|||||||
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
return auth.BasicHeaders(remote), nil
|
return auth.BasicHeaders(remote), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalize(name string) string {
|
||||||
|
return strings.ToLower(normalizeRe.ReplaceAllString(name, "-"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func packageFromWheel(filename string) string {
|
||||||
|
parts := strings.SplitN(filename, "-", 3)
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return normalize(parts[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
func packageFromSdist(filename string) string {
|
||||||
|
name := filename
|
||||||
|
for _, suffix := range []string{".tar.gz", ".zip"} {
|
||||||
|
if strings.HasSuffix(name, suffix) {
|
||||||
|
name = strings.TrimSuffix(name, suffix)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
idx := strings.LastIndex(name, "-")
|
||||||
|
if idx <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return normalize(name[:idx])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ValidateUpload(filePath string) (storagePath, contentType string, err error) {
|
||||||
|
filename := filePath
|
||||||
|
if idx := strings.LastIndex(filePath, "/"); idx >= 0 {
|
||||||
|
filename = filePath[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fileRe.MatchString(filename) {
|
||||||
|
return "", "", fmt.Errorf("filename %q must be a .whl, .tar.gz, or .zip file", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
var pkgName string
|
||||||
|
if strings.HasSuffix(filename, ".whl") {
|
||||||
|
pkgName = packageFromWheel(filename)
|
||||||
|
} else {
|
||||||
|
pkgName = packageFromSdist(filename)
|
||||||
|
}
|
||||||
|
if pkgName == "" {
|
||||||
|
return "", "", fmt.Errorf("cannot parse package name from %q", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
ct := "application/zip"
|
||||||
|
if strings.HasSuffix(filename, ".tar.gz") {
|
||||||
|
ct = "application/gzip"
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkgName + "/" + filename, ct, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UploadResponse(storagePath, contentHash string, sizeBytes int64) map[string]any {
|
||||||
|
parts := strings.SplitN(storagePath, "/", 2)
|
||||||
|
filename := storagePath
|
||||||
|
if len(parts) == 2 {
|
||||||
|
filename = parts[1]
|
||||||
|
}
|
||||||
|
return map[string]any{
|
||||||
|
"package": parts[0],
|
||||||
|
"filename": filename,
|
||||||
|
"content_hash": contentHash,
|
||||||
|
"size_bytes": sizeBytes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ServeLocalIndex(w http.ResponseWriter, r *http.Request, files provider.FileStore, repoName, path string) bool {
|
||||||
|
if path == "simple" || path == "simple/" {
|
||||||
|
p.servePackageList(w, r, files, repoName)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(path, "simple/") {
|
||||||
|
pkg := strings.TrimPrefix(path, "simple/")
|
||||||
|
pkg = strings.TrimSuffix(pkg, "/")
|
||||||
|
if pkg != "" && !strings.Contains(pkg, "/") {
|
||||||
|
p.servePackageFiles(w, r, files, repoName, pkg)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) GenerateLocalIndex(ctx context.Context, files provider.FileStore, repoName, path string) ([]byte, error) {
|
||||||
|
if !strings.HasPrefix(path, "simple/") {
|
||||||
|
return nil, fmt.Errorf("unsupported index path: %q", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
pkg := strings.TrimPrefix(path, "simple/")
|
||||||
|
pkg = strings.TrimSuffix(pkg, "/")
|
||||||
|
if pkg == "" {
|
||||||
|
return p.generatePackageListHTML(ctx, files, repoName)
|
||||||
|
}
|
||||||
|
return p.generatePackageFilesHTML(ctx, files, repoName, pkg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) servePackageList(w http.ResponseWriter, r *http.Request, files provider.FileStore, repoName string) {
|
||||||
|
body, err := p.generatePackageListHTML(r.Context(), files, repoName)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) servePackageFiles(w http.ResponseWriter, r *http.Request, files provider.FileStore, repoName, packageName string) {
|
||||||
|
normalized := normalize(packageName)
|
||||||
|
prefix := normalized + "/"
|
||||||
|
entries, err := files.ListFilesByPrefix(r.Context(), repoName, prefix)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(entries) == 0 {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("<!DOCTYPE html>\n<html><body>\n")
|
||||||
|
for _, f := range entries {
|
||||||
|
filename := strings.TrimPrefix(f.FilePath, normalized+"/")
|
||||||
|
hash := strings.TrimPrefix(f.ContentHash, "sha256:")
|
||||||
|
fmt.Fprintf(&b, "<a href=\"../../%s/%s#sha256=%s\">%s</a>\n",
|
||||||
|
normalized, filename, hash, filename)
|
||||||
|
}
|
||||||
|
b.WriteString("</body></html>\n")
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
io.WriteString(w, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) generatePackageListHTML(ctx context.Context, files provider.FileStore, repoName string) ([]byte, error) {
|
||||||
|
packages, err := files.ListPackages(ctx, repoName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("<!DOCTYPE html>\n<html><body>\n")
|
||||||
|
for _, pkg := range packages {
|
||||||
|
fmt.Fprintf(&b, "<a href=\"%s/\">%s</a>\n", pkg, pkg)
|
||||||
|
}
|
||||||
|
b.WriteString("</body></html>\n")
|
||||||
|
return []byte(b.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) generatePackageFilesHTML(ctx context.Context, files provider.FileStore, repoName, packageName string) ([]byte, error) {
|
||||||
|
normalized := normalize(packageName)
|
||||||
|
prefix := normalized + "/"
|
||||||
|
entries, err := files.ListFilesByPrefix(ctx, repoName, prefix)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("<!DOCTYPE html>\n<html><body>\n")
|
||||||
|
for _, f := range entries {
|
||||||
|
filename := strings.TrimPrefix(f.FilePath, normalized+"/")
|
||||||
|
hash := strings.TrimPrefix(f.ContentHash, "sha256:")
|
||||||
|
fmt.Fprintf(&b, "<a href=\"%s/%s#sha256=%s\">%s</a>\n",
|
||||||
|
normalized, filename, hash, filename)
|
||||||
|
}
|
||||||
|
b.WriteString("</body></html>\n")
|
||||||
|
return []byte(b.String()), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package terraform
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -19,6 +20,12 @@ func init() {
|
|||||||
|
|
||||||
var versionsRe = regexp.MustCompile(`[^/]+/[^/]+/versions$`)
|
var versionsRe = regexp.MustCompile(`[^/]+/[^/]+/versions$`)
|
||||||
|
|
||||||
|
var providerZipRe = regexp.MustCompile(
|
||||||
|
`^terraform-provider-([a-zA-Z0-9_-]+)_([0-9]+\.[0-9]+\.[0-9]+(?:-[a-zA-Z0-9.]+)?)_([a-z0-9]+)_([a-z0-9]+)\.zip$`,
|
||||||
|
)
|
||||||
|
|
||||||
|
var semverRe = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+(?:-[a-zA-Z0-9.]+)?$`)
|
||||||
|
|
||||||
type Provider struct{}
|
type Provider struct{}
|
||||||
|
|
||||||
func (p *Provider) Type() models.PackageType { return models.PackageTerraform }
|
func (p *Provider) Type() models.PackageType { return models.PackageTerraform }
|
||||||
@@ -86,3 +93,145 @@ func rewriteDownloadURL(originalURL, releasesRemote, proxyBaseURL string) string
|
|||||||
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
func (p *Provider) AuthHeaders(_ context.Context, remote models.Remote) (http.Header, error) {
|
||||||
return auth.BasicHeaders(remote), nil
|
return auth.BasicHeaders(remote), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ValidateUpload(filePath string) (storagePath, contentType string, err error) {
|
||||||
|
parts := strings.Split(filePath, "/")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return "", "", fmt.Errorf("path must be {namespace}/{type}/{filename}.zip")
|
||||||
|
}
|
||||||
|
namespace, typeName, filename := parts[0], parts[1], parts[2]
|
||||||
|
|
||||||
|
m := providerZipRe.FindStringSubmatch(filename)
|
||||||
|
if m == nil {
|
||||||
|
return "", "", fmt.Errorf("filename %q does not match terraform-provider-{type}_{version}_{os}_{arch}.zip", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m[1] != typeName {
|
||||||
|
return "", "", fmt.Errorf("provider type in filename %q does not match path type %q", m[1], typeName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s/%s/%s", namespace, typeName, filename), "application/zip", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) UploadResponse(storagePath, contentHash string, sizeBytes int64) map[string]any {
|
||||||
|
parts := strings.Split(storagePath, "/")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return map[string]any{"path": storagePath, "content_hash": contentHash, "size_bytes": sizeBytes}
|
||||||
|
}
|
||||||
|
|
||||||
|
m := providerZipRe.FindStringSubmatch(parts[2])
|
||||||
|
if m == nil {
|
||||||
|
return map[string]any{"path": storagePath, "content_hash": contentHash, "size_bytes": sizeBytes}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map[string]any{
|
||||||
|
"namespace": parts[0],
|
||||||
|
"type": parts[1],
|
||||||
|
"version": m[2],
|
||||||
|
"os": m[3],
|
||||||
|
"arch": m[4],
|
||||||
|
"content_hash": contentHash,
|
||||||
|
"size_bytes": sizeBytes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type terraformIndex struct {
|
||||||
|
Versions map[string]json.RawMessage `json:"versions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type terraformVersionDoc struct {
|
||||||
|
Archives map[string]terraformArchive `json:"archives"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type terraformArchive struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
Hashes []string `json:"hashes,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) ServeLocalIndex(w http.ResponseWriter, r *http.Request, files provider.FileStore, repoName, path string) bool {
|
||||||
|
parts := strings.Split(path, "/")
|
||||||
|
if len(parts) < 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace, typeName := parts[0], parts[1]
|
||||||
|
tail := parts[2]
|
||||||
|
|
||||||
|
if tail == "index.json" {
|
||||||
|
p.serveIndex(w, r, files, repoName, namespace, typeName)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(tail, ".json") {
|
||||||
|
version := strings.TrimSuffix(tail, ".json")
|
||||||
|
if semverRe.MatchString(version) {
|
||||||
|
p.serveVersionDoc(w, r, files, repoName, namespace, typeName, version)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) GenerateLocalIndex(ctx context.Context, files provider.FileStore, repoName, path string) ([]byte, error) {
|
||||||
|
return nil, fmt.Errorf("terraform local index generation for virtual repos not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) serveIndex(w http.ResponseWriter, r *http.Request, files provider.FileStore, repoName, namespace, typeName string) {
|
||||||
|
prefix := fmt.Sprintf("%s/%s/", namespace, typeName)
|
||||||
|
entries, err := files.ListFilesByPrefix(r.Context(), repoName, prefix)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
versions := map[string]json.RawMessage{}
|
||||||
|
for _, f := range entries {
|
||||||
|
filename := strings.TrimPrefix(f.FilePath, prefix)
|
||||||
|
m := providerZipRe.FindStringSubmatch(filename)
|
||||||
|
if m == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
versions[m[2]] = json.RawMessage(`{}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(versions) == 0 {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(terraformIndex{Versions: versions})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) serveVersionDoc(w http.ResponseWriter, r *http.Request, files provider.FileStore, repoName, namespace, typeName, version string) {
|
||||||
|
prefix := fmt.Sprintf("%s/%s/terraform-provider-%s_%s_", namespace, typeName, typeName, version)
|
||||||
|
entries, err := files.ListFilesByPrefix(r.Context(), repoName, prefix)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
archives := map[string]terraformArchive{}
|
||||||
|
for _, f := range entries {
|
||||||
|
filename := strings.TrimPrefix(f.FilePath, fmt.Sprintf("%s/%s/", namespace, typeName))
|
||||||
|
m := providerZipRe.FindStringSubmatch(filename)
|
||||||
|
if m == nil || m[2] != version {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
platform := m[3] + "_" + m[4]
|
||||||
|
archive := terraformArchive{URL: filename}
|
||||||
|
if f.ContentHash != "" {
|
||||||
|
archive.Hashes = []string{"zh:" + strings.TrimPrefix(f.ContentHash, "sha256:")}
|
||||||
|
}
|
||||||
|
archives[platform] = archive
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(archives) == 0 {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(terraformVersionDoc{Archives: archives})
|
||||||
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ type Server struct {
|
|||||||
store *storage.S3
|
store *storage.S3
|
||||||
engine *proxy.Engine
|
engine *proxy.Engine
|
||||||
virtEngine *virtual.Engine
|
virtEngine *virtual.Engine
|
||||||
|
localHandler *v2.LocalHandler
|
||||||
gc *gc.Collector
|
gc *gc.Collector
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +62,7 @@ func New(cfg *config.Config) (*Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine := proxy.NewEngine(db, redis, s3)
|
engine := proxy.NewEngine(db, redis, s3)
|
||||||
|
localHandler := v2.NewLocalHandler(db, s3)
|
||||||
virtEngine := virtual.NewEngine(db, engine)
|
virtEngine := virtual.NewEngine(db, engine)
|
||||||
collector := gc.New(db, s3, 1*time.Hour)
|
collector := gc.New(db, s3, 1*time.Hour)
|
||||||
|
|
||||||
@@ -71,6 +73,7 @@ func New(cfg *config.Config) (*Server, error) {
|
|||||||
store: s3,
|
store: s3,
|
||||||
engine: engine,
|
engine: engine,
|
||||||
virtEngine: virtEngine,
|
virtEngine: virtEngine,
|
||||||
|
localHandler: localHandler,
|
||||||
gc: collector,
|
gc: collector,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +94,7 @@ func (s *Server) routes() chi.Router {
|
|||||||
r.Get("/health", s.handleHealth)
|
r.Get("/health", s.handleHealth)
|
||||||
r.Get("/", s.handleRoot)
|
r.Get("/", s.handleRoot)
|
||||||
|
|
||||||
proxyHandler := v1.NewProxyHandler(s.engine, s.virtEngine, s.db)
|
proxyHandler := v1.NewProxyHandler(s.engine, s.virtEngine, s.db, s.store, s.localHandler)
|
||||||
r.Mount("/api/v1", proxyHandler.Routes())
|
r.Mount("/api/v1", proxyHandler.Routes())
|
||||||
|
|
||||||
remotesHandler := v2.NewRemotesHandler(s.db)
|
remotesHandler := v2.NewRemotesHandler(s.db)
|
||||||
@@ -114,6 +117,12 @@ func (s *Server) routes() chi.Router {
|
|||||||
r.Get("/", objHandler.Routes().ServeHTTP)
|
r.Get("/", objHandler.Routes().ServeHTTP)
|
||||||
r.Delete("/*", objHandler.Routes().ServeHTTP)
|
r.Delete("/*", objHandler.Routes().ServeHTTP)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
r.Route("/remotes/{name}/files", func(r chi.Router) {
|
||||||
|
r.Put("/*", s.localHandler.Routes().ServeHTTP)
|
||||||
|
r.Get("/*", s.localHandler.Routes().ServeHTTP)
|
||||||
|
r.Delete("/*", s.localHandler.Routes().ServeHTTP)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|||||||
@@ -73,6 +73,16 @@ func (e *Engine) fetchMemberIndexes(ctx context.Context, virt models.Virtual, pa
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if remote.RepoType == models.RepoTypeLocal {
|
||||||
|
body, err := e.fetchLocalIndex(ctx, *remote, path)
|
||||||
|
if err != nil {
|
||||||
|
results[idx] = result{err: fmt.Errorf("local index %q: %w", name, err)}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
results[idx] = result{index: MemberIndex{RemoteName: name, RepoType: remote.RepoType, Body: body}}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
prov, err := provider.Get(remote.PackageType)
|
prov, err := provider.Get(remote.PackageType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
results[idx] = result{err: fmt.Errorf("provider %q: %w", remote.PackageType, err)}
|
results[idx] = result{err: fmt.Errorf("provider %q: %w", remote.PackageType, err)}
|
||||||
@@ -92,7 +102,7 @@ func (e *Engine) fetchMemberIndexes(ctx context.Context, virt models.Virtual, pa
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
results[idx] = result{index: MemberIndex{RemoteName: name, Body: body}}
|
results[idx] = result{index: MemberIndex{RemoteName: name, RepoType: remote.RepoType, Body: body}}
|
||||||
}(i, memberName)
|
}(i, memberName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,3 +119,17 @@ func (e *Engine) fetchMemberIndexes(ctx context.Context, virt models.Virtual, pa
|
|||||||
|
|
||||||
return members, nil
|
return members, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Engine) fetchLocalIndex(ctx context.Context, remote models.Remote, path string) ([]byte, error) {
|
||||||
|
prov, err := provider.Get(remote.PackageType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("no provider for %q: %w", remote.PackageType, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
indexer, ok := prov.(provider.LocalIndexer)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("provider %q does not support local index generation", remote.PackageType)
|
||||||
|
}
|
||||||
|
|
||||||
|
return indexer.GenerateLocalIndex(ctx, e.db, remote.Name, path)
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
type MemberIndex struct {
|
type MemberIndex struct {
|
||||||
RemoteName string
|
RemoteName string
|
||||||
|
RepoType models.RepoType
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,8 +36,13 @@ func (m *PyPIMerger) MergeIndexes(members []MemberIndex, proxyBaseURL string) ([
|
|||||||
}
|
}
|
||||||
|
|
||||||
if proxyBaseURL != "" && href != "" {
|
if proxyBaseURL != "" && href != "" {
|
||||||
href = fmt.Sprintf("%s/api/v1/remote/%s/%s",
|
routePrefix := "remote"
|
||||||
|
if member.RepoType == "local" {
|
||||||
|
routePrefix = "local"
|
||||||
|
}
|
||||||
|
href = fmt.Sprintf("%s/api/v1/%s/%s/%s",
|
||||||
strings.TrimRight(proxyBaseURL, "/"),
|
strings.TrimRight(proxyBaseURL, "/"),
|
||||||
|
routePrefix,
|
||||||
member.RemoteName,
|
member.RemoteName,
|
||||||
strings.TrimLeft(href, "/"))
|
strings.TrimLeft(href, "/"))
|
||||||
}
|
}
|
||||||
|
|||||||
+33
-1
@@ -1,10 +1,42 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RepoType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
RepoTypeRemote RepoType = "remote"
|
||||||
|
RepoTypeLocal RepoType = "local"
|
||||||
|
)
|
||||||
|
|
||||||
|
var validRepoTypes = map[RepoType]bool{
|
||||||
|
RepoTypeRemote: true,
|
||||||
|
RepoTypeLocal: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r RepoType) Valid() bool {
|
||||||
|
return validRepoTypes[r]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r RepoType) String() string {
|
||||||
|
return string(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseRepoType(s string) (RepoType, error) {
|
||||||
|
rt := RepoType(s)
|
||||||
|
if !rt.Valid() {
|
||||||
|
return "", fmt.Errorf("unknown repo type: %q", s)
|
||||||
|
}
|
||||||
|
return rt, nil
|
||||||
|
}
|
||||||
|
|
||||||
type Remote struct {
|
type Remote struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
PackageType PackageType `json:"package_type"`
|
PackageType PackageType `json:"package_type"`
|
||||||
|
RepoType RepoType `json:"repo_type"`
|
||||||
BaseURL string `json:"base_url"`
|
BaseURL string `json:"base_url"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
Username string `json:"-"`
|
Username string `json:"-"`
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export interface Remote {
|
export interface Remote {
|
||||||
name: string;
|
name: string;
|
||||||
package_type: string;
|
package_type: string;
|
||||||
|
repo_type: string;
|
||||||
base_url: string;
|
base_url: string;
|
||||||
description: string;
|
description: string;
|
||||||
username?: string;
|
username?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user