Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4789635e87 | |||
| ba52fedd27 | |||
| 76633403b2 | |||
| cae3503ac4 | |||
| 3f098df428 | |||
| 64266f40e9 | |||
| be25fc19f7 | |||
| 3bd3ca8b74 | |||
| 373366e695 | |||
| e6d9b175ce | |||
| 0daca40156 | |||
| 0df726467a | |||
| b8bc7f8714 | |||
| 0c780c1bd1 | |||
| 173b5d8b10 | |||
| 3352a3e886 | |||
| 8adcbac405 | |||
| 4ca89b9159 | |||
| 25b85ddc92 | |||
| d585ab425c | |||
| 6b1a6c9eb4 | |||
| 5de912db75 | |||
| 8e9d313892 | |||
| 70cd439961 | |||
| fe837dabf7 | |||
| 78296dae8f | |||
| 8fe4bac2b9 | |||
| 8bc9285117 | |||
| ce01a94141 | |||
| 4619ae18d8 | |||
| ac51d3a51d | |||
| 2887ce4476 | |||
| 9e52929d73 |
@@ -0,0 +1,15 @@
|
|||||||
|
.git/
|
||||||
|
.venv/
|
||||||
|
dist/
|
||||||
|
tests/
|
||||||
|
remotes.yaml
|
||||||
|
ca-bundle.pem
|
||||||
|
.env
|
||||||
|
*.log
|
||||||
|
docker-compose.yml
|
||||||
|
.woodpecker/
|
||||||
|
.tox/
|
||||||
|
.ruff_cache/
|
||||||
|
.pytest_cache/
|
||||||
|
.pre-commit-cache/
|
||||||
|
minio_data/
|
||||||
@@ -35,7 +35,6 @@ env/
|
|||||||
|
|
||||||
# Environment variables
|
# Environment variables
|
||||||
.env
|
.env
|
||||||
remotes.yaml
|
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
*.log
|
*.log
|
||||||
@@ -59,5 +58,4 @@ uv.lock
|
|||||||
minio_data/
|
minio_data/
|
||||||
|
|
||||||
# Local configuration overrides
|
# Local configuration overrides
|
||||||
docker-compose.yml
|
|
||||||
ca-bundle.pem
|
ca-bundle.pem
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
when:
|
||||||
|
- event: pull_request
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: docker-build
|
||||||
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
|
settings:
|
||||||
|
repo: git.unkin.net/unkin/artifactapi
|
||||||
|
dry_run: true
|
||||||
@@ -6,3 +6,4 @@ steps:
|
|||||||
image: git.unkin.net/unkin/almalinux9-base:20260308
|
image: git.unkin.net/unkin/almalinux9-base:20260308
|
||||||
commands:
|
commands:
|
||||||
- uvx pre-commit run --all-files
|
- uvx pre-commit run --all-files
|
||||||
|
|
||||||
|
|||||||
+15
-45
@@ -1,53 +1,23 @@
|
|||||||
# Use Alpine Linux as base image
|
FROM git.unkin.net/unkin/almalinux9-base:latest
|
||||||
FROM python:3.11-alpine
|
|
||||||
|
|
||||||
# Set working directory
|
ARG VERSION=0.0.0.dev0
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Install system dependencies
|
COPY . /build
|
||||||
RUN apk add --no-cache \
|
|
||||||
gcc \
|
|
||||||
musl-dev \
|
|
||||||
libffi-dev \
|
|
||||||
postgresql-dev \
|
|
||||||
curl \
|
|
||||||
wget \
|
|
||||||
tar
|
|
||||||
|
|
||||||
# Install uv
|
RUN HATCH_VCS_PRETEND_VERSION=${VERSION} \
|
||||||
ARG PACKAGE_VERSION=0.9.21
|
SETUPTOOLS_SCM_PRETEND_VERSION=${VERSION} \
|
||||||
RUN wget -O /app/uv-x86_64-unknown-linux-musl.tar.gz https://github.com/astral-sh/uv/releases/download/${PACKAGE_VERSION}/uv-x86_64-unknown-linux-musl.tar.gz && \
|
uv build --wheel --directory /build && \
|
||||||
tar xf /app/uv-x86_64-unknown-linux-musl.tar.gz -C /app && \
|
useradd -m -r -s /bin/sh appuser
|
||||||
mv /app/uv-x86_64-unknown-linux-musl/uv /usr/local/bin/uv && \
|
|
||||||
rm -rf /app/uv-x86_64-unknown-linux-musl* && \
|
|
||||||
chmod +x /usr/local/bin/uv && \
|
|
||||||
uv --version
|
|
||||||
|
|
||||||
# Create non-root user first
|
|
||||||
RUN adduser -D -s /bin/sh appuser && \
|
|
||||||
chown -R appuser:appuser /app
|
|
||||||
|
|
||||||
# Copy dependency files and change ownership
|
|
||||||
COPY --chown=appuser:appuser pyproject.toml uv.lock README.md ./
|
|
||||||
|
|
||||||
# Switch to appuser and install Python dependencies
|
|
||||||
USER appuser
|
USER appuser
|
||||||
ARG VERSION=dev
|
RUN uv tool install --from /build/dist/*.whl artifactapi
|
||||||
ENV HATCH_VCS_PRETEND_VERSION=${VERSION} \
|
|
||||||
SETUPTOOLS_SCM_PRETEND_VERSION=${VERSION}
|
|
||||||
RUN uv sync --frozen
|
|
||||||
|
|
||||||
# Copy application source
|
USER root
|
||||||
COPY --chown=appuser:appuser src/ ./src/
|
RUN rm -rf /build
|
||||||
COPY --chown=appuser:appuser remotes.yaml ./
|
|
||||||
COPY --chown=appuser:appuser ca-bundle.pem ./
|
|
||||||
|
|
||||||
# Expose port
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD curl -f http://localhost:8000/health || exit 1
|
||||||
# Health check
|
USER appuser
|
||||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
ENV PATH="/home/appuser/.local/bin:$PATH"
|
||||||
CMD curl -f http://localhost:8000/health || exit 1
|
WORKDIR /app
|
||||||
|
CMD ["artifactapi"]
|
||||||
# Run the application
|
|
||||||
CMD ["uv", "run", "python", "-m", "src.artifactapi.main"]
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
.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 install dev clean test lint format pre-commit tox docker-build docker-up docker-down docker-logs docker-rebuild docker-clean docker-restart
|
||||||
|
|
||||||
build:
|
build:
|
||||||
docker build --no-cache -t artifactapi:latest .
|
docker build -t artifactapi:dev .
|
||||||
|
|
||||||
install: build
|
install: build
|
||||||
|
|
||||||
@@ -74,4 +74,3 @@ major:
|
|||||||
|
|
||||||
_tag:
|
_tag:
|
||||||
git push origin $(TAG)
|
git push origin $(TAG)
|
||||||
docker-compose build --no-cache --build-arg VERSION=$(TAG:v%=%)
|
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
remotes:
|
||||||
|
alpine:
|
||||||
|
base_url: "https://dl-cdn.alpinelinux.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "alpine"
|
||||||
|
description: "Alpine Linux APK package repository"
|
||||||
|
immutable_patterns:
|
||||||
|
- ".*/x86_64/.*\\.apk$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 7200
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
remotes:
|
||||||
|
github:
|
||||||
|
base_url: "https://github.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "generic"
|
||||||
|
description: "GitHub releases and files"
|
||||||
|
immutable_patterns:
|
||||||
|
- "gruntwork-io/terragrunt/.*terragrunt_linux_amd64.*"
|
||||||
|
- "prometheus/node_exporter/.*/node_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 0
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
remotes:
|
||||||
|
pypi:
|
||||||
|
base_url: "https://files.pythonhosted.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "pypi"
|
||||||
|
description: "Python Package Index"
|
||||||
|
check_mutable_updates: true
|
||||||
|
quarantine_new: true
|
||||||
|
quarantine_days: 3
|
||||||
|
immutable_patterns:
|
||||||
|
- "packages/.*\\.whl$"
|
||||||
|
- "packages/.*\\.whl\\.metadata$"
|
||||||
|
- "packages/.*\\.tar\\.gz$"
|
||||||
|
- "packages/.*\\.zip$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 600
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
artifactapi:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
args:
|
||||||
|
- VERSION=2.2.2.dev0
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
volumes:
|
||||||
|
- ./examples/single-file/remotes.yaml:/app/remotes.yaml:ro,z
|
||||||
|
- ./ca-bundle.pem:/app/ca-bundle.pem:ro,z
|
||||||
|
environment:
|
||||||
|
- CONFIG_PATH=/app/remotes.yaml
|
||||||
|
- DBHOST=postgres
|
||||||
|
- DBPORT=5432
|
||||||
|
- DBUSER=artifacts
|
||||||
|
- DBPASS=artifacts123
|
||||||
|
- DBNAME=artifacts
|
||||||
|
- REDIS_URL=redis://redis:6379
|
||||||
|
- MINIO_ENDPOINT=minio:9000
|
||||||
|
- MINIO_ACCESS_KEY=minioadmin
|
||||||
|
- MINIO_SECRET_KEY=minioadmin
|
||||||
|
- MINIO_BUCKET=artifacts
|
||||||
|
- MINIO_SECURE=false
|
||||||
|
- REQUESTS_CA_BUNDLE=/app/ca-bundle.pem
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
minio:
|
||||||
|
image: minio/minio:latest
|
||||||
|
ports:
|
||||||
|
- "9000:9000"
|
||||||
|
- "9001:9001"
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: minioadmin
|
||||||
|
MINIO_ROOT_PASSWORD: minioadmin
|
||||||
|
command: server /data --console-address ":9001"
|
||||||
|
volumes:
|
||||||
|
- minio_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 20s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
command: redis-server --save 20 1
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:15-alpine
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: artifacts
|
||||||
|
POSTGRES_USER: artifacts
|
||||||
|
POSTGRES_PASSWORD: artifacts123
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U artifacts -d artifacts"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
minio_data:
|
||||||
|
redis_data:
|
||||||
|
postgres_data:
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
remotes:
|
||||||
|
alpine:
|
||||||
|
base_url: "https://dl-cdn.alpinelinux.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "alpine"
|
||||||
|
description: "Alpine Linux APK package repository"
|
||||||
|
immutable_patterns:
|
||||||
|
- ".*/x86_64/.*\\.apk$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 7200
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
remotes:
|
||||||
|
github:
|
||||||
|
base_url: "https://github.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "generic"
|
||||||
|
description: "GitHub releases and files"
|
||||||
|
immutable_patterns:
|
||||||
|
- "gruntwork-io/terragrunt/.*terragrunt_linux_amd64.*"
|
||||||
|
- "prometheus/node_exporter/.*/node_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 0
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
remotes:
|
||||||
|
pypi:
|
||||||
|
base_url: "https://files.pythonhosted.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "pypi"
|
||||||
|
description: "Python Package Index"
|
||||||
|
check_mutable_updates: true
|
||||||
|
quarantine_new: true
|
||||||
|
quarantine_days: 3
|
||||||
|
immutable_patterns:
|
||||||
|
- "packages/.*\\.whl$"
|
||||||
|
- "packages/.*\\.whl\\.metadata$"
|
||||||
|
- "packages/.*\\.tar\\.gz$"
|
||||||
|
- "packages/.*\\.zip$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 600
|
||||||
@@ -0,0 +1,277 @@
|
|||||||
|
# Example remotes configuration — copy and adapt for your environment.
|
||||||
|
#
|
||||||
|
# immutable_patterns: artifacts cached forever (e.g. release binaries, versioned tags).
|
||||||
|
# mutable_patterns: artifacts that expire after cache.mutable_ttl seconds and are
|
||||||
|
# re-fetched from upstream on next request (e.g. index files,
|
||||||
|
# branch archives). Defaults to the package-type built-ins when
|
||||||
|
# not set (APKINDEX, repomd.xml, Docker manifests, etc.).
|
||||||
|
# cache:
|
||||||
|
# immutable_ttl: TTL for immutable files (0 = forever, rarely needed to change).
|
||||||
|
# mutable_ttl: TTL in seconds for mutable files. Omit to use the default (3600).
|
||||||
|
#
|
||||||
|
# quarantine_new: Set to true to block immutable artifacts published within the last
|
||||||
|
# quarantine_days days. Requests return 404 until the quarantine period
|
||||||
|
# expires. Fails open when the publish date cannot be determined.
|
||||||
|
# quarantine_days: Number of days to quarantine newly published artifacts (requires
|
||||||
|
# quarantine_new: true). The upstream Last-Modified header is used as
|
||||||
|
# the publish date.
|
||||||
|
#
|
||||||
|
# WARNING: this file may contain credentials — do not commit real values.
|
||||||
|
#
|
||||||
|
# Global configuration
|
||||||
|
#s3:
|
||||||
|
# endpoint: "localhost:9000"
|
||||||
|
# access_key: "minioadmin"
|
||||||
|
# secret_key: "minioadmin"
|
||||||
|
# bucket: "artifacts"
|
||||||
|
# secure: false
|
||||||
|
#
|
||||||
|
#redis:
|
||||||
|
# url: "redis://localhost:6379/0"
|
||||||
|
#
|
||||||
|
#database:
|
||||||
|
# url: "postgresql://artifacts:artifacts123@localhost:5432/artifacts"
|
||||||
|
#
|
||||||
|
remotes:
|
||||||
|
github:
|
||||||
|
base_url: "https://github.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "generic"
|
||||||
|
description: "GitHub releases and files"
|
||||||
|
immutable_patterns:
|
||||||
|
- "gruntwork-io/terragrunt/.*terragrunt_linux_amd64.*"
|
||||||
|
- "lxc/incus/.*\\.tar\\.gz$"
|
||||||
|
- "prometheus/node_exporter/.*/node_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
- "VictoriaMetrics/VictoriaMetrics/.*/vmutils-linux-amd64-.*\\.tar\\.gz$"
|
||||||
|
- "VictoriaMetrics/VictoriaMetrics/.*/victoria-metrics-linux-amd64-.*-cluster\\.tar\\.gz$"
|
||||||
|
- "VictoriaMetrics/VictoriaMetrics/.*/victoria-logs-linux-amd64-.*\\.tar\\.gz$"
|
||||||
|
- "VictoriaMetrics/VictoriaMetrics/.*/vlutils-linux-amd64-.*\\.tar\\.gz$"
|
||||||
|
- "prometheus-community/bind_exporter/.*/bind_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
- "prometheus-community/pgbouncer_exporter/.*/pgbouncer_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
- "prometheus-community/postgres_exporter/.*/postgres_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
- "onedr0p/exportarr/.*/exportarr_.*_linux_amd64\\.tar\\.gz$"
|
||||||
|
- "tynany/frr_exporter/.*/frr_exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
- "camptocamp/prometheus-puppetdb-exporter/.*/prometheus-puppetdb-exporter-.*\\.linux-amd64\\.tar\\.gz$"
|
||||||
|
- "grafana/jsonnet-language-server/.*/jsonnet-language-server_.*_linux_amd64$"
|
||||||
|
- "helmfile/helmfile/.*/helmfile_.*_linux_amd64\\.tar\\.gz$"
|
||||||
|
- "helmfile/vals/.*/vals_.*_linux_amd64\\.tar\\.gz$"
|
||||||
|
- "openbao/openbao-plugins/.*/openbao-plugin-secrets-consul_linux_amd64_.*\\.tar\\.gz$"
|
||||||
|
- "openbao/openbao-plugins/.*/openbao-plugin-secrets-nomad_linux_amd64_.*\\.tar\\.gz$"
|
||||||
|
- "apple/foundationdb/.*/libfdb_c\\.x86_64\\.so$"
|
||||||
|
- "stalwartlabs/stalwart/.*/stalwart-cli-x86_64-unknown-linux-gnu\\.tar\\.gz$"
|
||||||
|
- "stalwartlabs/stalwart/.*/stalwart-foundationdb-x86_64-unknown-linux-gnu\\.tar\\.gz$"
|
||||||
|
- "stalwartlabs/stalwart/.*/stalwart-x86_64-unknown-linux-gnu\\.tar\\.gz$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 0
|
||||||
|
|
||||||
|
github-archive:
|
||||||
|
base_url: "https://github.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "generic"
|
||||||
|
description: "GitHub repository archive tarballs"
|
||||||
|
immutable_patterns:
|
||||||
|
# Tag archives are immutable — a tag never changes
|
||||||
|
- ".*/archive/refs/tags/.*\\.tar\\.gz$"
|
||||||
|
mutable_patterns:
|
||||||
|
# Branch archives can change on every push
|
||||||
|
- ".*/archive/refs/heads/main\\.tar\\.gz$"
|
||||||
|
- ".*/archive/refs/heads/master\\.tar\\.gz$"
|
||||||
|
# Before re-downloading an expired branch archive, check whether it has
|
||||||
|
# actually changed (304 Not Modified → just refresh the TTL, no transfer).
|
||||||
|
# Only applies to user-defined mutable_patterns, not package-type defaults.
|
||||||
|
check_mutable_updates: true
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Tag archives cached indefinitely
|
||||||
|
mutable_ttl: 86400 # Branch archives refreshed after 1 day
|
||||||
|
|
||||||
|
gitea-dl:
|
||||||
|
base_url: "https://dl.gitea.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "generic"
|
||||||
|
description: "Gitea download site"
|
||||||
|
immutable_patterns:
|
||||||
|
- "act_runner/.*/act_runner-.*-linux-amd64$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 0
|
||||||
|
|
||||||
|
hashicorp-releases:
|
||||||
|
base_url: "https://releases.hashicorp.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "generic"
|
||||||
|
description: "HashiCorp product releases"
|
||||||
|
immutable_patterns:
|
||||||
|
- "terraform/.*terraform_.*_linux_amd64\\.zip$"
|
||||||
|
- "terraform/.*terraform_.*_windows_amd64\\.zip$"
|
||||||
|
- "terraform/.*terraform_.*_darwin_amd64\\.zip$"
|
||||||
|
- "vault/.*vault_.*_linux_amd64\\.zip$"
|
||||||
|
- "vault/.*vault_.*_windows_amd64\\.zip$"
|
||||||
|
- "vault/.*vault_.*_darwin_amd64\\.zip$"
|
||||||
|
- "consul-cni/.*/consul-cni_.*_linux_amd64\\.zip$"
|
||||||
|
- "consul/.*/consul_.*_linux_amd64\\.zip$"
|
||||||
|
- "nomad-autoscaler/.*/nomad-autoscaler_.*_linux_amd64\\.zip$"
|
||||||
|
- "nomad/.*/nomad_.*_linux_amd64\\.zip$"
|
||||||
|
- "packer/.*/packer_.*_linux_amd64\\.zip$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 0
|
||||||
|
|
||||||
|
alpine:
|
||||||
|
base_url: "https://dl-cdn.alpinelinux.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "alpine"
|
||||||
|
description: "Alpine Linux APK package repository"
|
||||||
|
immutable_patterns:
|
||||||
|
- ".*/x86_64/.*\\.apk$"
|
||||||
|
# check_mutable_updates not set: APKINDEX.tar.gz is a package-type default
|
||||||
|
# and is always re-fetched on expiry — conditional checks are skipped for
|
||||||
|
# built-in mutable patterns regardless of this flag.
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 7200 # Index files (APKINDEX.tar.gz) cached for 2 hours
|
||||||
|
|
||||||
|
almalinux:
|
||||||
|
base_url: "https://gsl-syd.mm.fcix.net/almalinux"
|
||||||
|
type: "remote"
|
||||||
|
package: "rpm"
|
||||||
|
description: "AlmaLinux RPM package repository"
|
||||||
|
immutable_patterns:
|
||||||
|
- ".*/x86_64/.*\\.rpm$"
|
||||||
|
- ".*/noarch/.*\\.rpm$"
|
||||||
|
- ".*/repodata/.*$"
|
||||||
|
- ".*\\.rpm$" # Allow all RPM files
|
||||||
|
# repomd.xml / repodata are package-type defaults — always re-fetched on
|
||||||
|
# expiry. check_mutable_updates would only apply to any custom
|
||||||
|
# mutable_patterns added here.
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 7200 # Metadata files cached for 2 hours
|
||||||
|
|
||||||
|
epel:
|
||||||
|
base_url: "http://mirror.aarnet.edu.au/pub/epel"
|
||||||
|
type: "remote"
|
||||||
|
package: "rpm"
|
||||||
|
description: "EPEL (Extra Packages for Enterprise Linux)"
|
||||||
|
immutable_patterns:
|
||||||
|
- "8/Everything/x86_64/.*\\.rpm$"
|
||||||
|
- "9/Everything/x86_64/.*\\.rpm$"
|
||||||
|
- "10/Everything/x86_64/.*\\.rpm$"
|
||||||
|
- ".*/noarch/.*\\.rpm$"
|
||||||
|
- ".*/repodata/.*$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 7200 # Metadata files cached for 2 hours
|
||||||
|
|
||||||
|
fedora:
|
||||||
|
base_url: "https://gsl-syd.mm.fcix.net/fedora/linux"
|
||||||
|
type: "remote"
|
||||||
|
package: "rpm"
|
||||||
|
description: "Fedora Linux RPM package repository"
|
||||||
|
immutable_patterns:
|
||||||
|
- "releases/.*/Everything/x86_64/.*\\.rpm$"
|
||||||
|
- "updates/.*/Everything/x86_64/.*\\.rpm$"
|
||||||
|
- "development/.*/Everything/x86_64/.*\\.rpm$"
|
||||||
|
- ".*/noarch/.*\\.rpm$"
|
||||||
|
- "updates/.*/Everything/x86_64/repodata/.*$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 300 # Metadata files cached for 5 minutes
|
||||||
|
|
||||||
|
ghcr:
|
||||||
|
base_url: "https://ghcr.io"
|
||||||
|
type: "remote"
|
||||||
|
package: "docker"
|
||||||
|
description: "GitHub Container Registry"
|
||||||
|
# username: "your-github-username"
|
||||||
|
# password: "your-github-pat" # needs read:packages scope
|
||||||
|
# Docker manifest/tag-list patterns are package-type defaults — always
|
||||||
|
# re-fetched on expiry. check_mutable_updates only applies to any custom
|
||||||
|
# mutable_patterns you add (e.g. a metadata endpoint).
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 300
|
||||||
|
|
||||||
|
dockerhub:
|
||||||
|
base_url: "https://registry-1.docker.io"
|
||||||
|
type: "remote"
|
||||||
|
package: "docker"
|
||||||
|
description: "Docker Hub registry"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 300
|
||||||
|
|
||||||
|
pypi:
|
||||||
|
base_url: "https://files.pythonhosted.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "pypi"
|
||||||
|
description: "Python Package Index — simple index and package files via a single remote"
|
||||||
|
# simple/ requests are transparently fetched from pypi.org; package files come from
|
||||||
|
# files.pythonhosted.org (base_url). URLs in the simple index are rewritten to this remote.
|
||||||
|
check_mutable_updates: true
|
||||||
|
# Block packages published within the last 3 days (supply-chain attack mitigation).
|
||||||
|
# Immutable artifacts (wheel/sdist) newer than quarantine_days return 404 until
|
||||||
|
# the window passes. Disable by setting quarantine_new: false or removing both keys.
|
||||||
|
quarantine_new: true
|
||||||
|
quarantine_days: 3
|
||||||
|
immutable_patterns:
|
||||||
|
- "packages/.*\\.whl$"
|
||||||
|
- "packages/.*\\.whl\\.metadata$"
|
||||||
|
- "packages/.*\\.tar\\.gz$"
|
||||||
|
- "packages/.*\\.zip$"
|
||||||
|
- "packages/.*\\.egg$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 600 # Simple index pages refreshed after 10 minutes
|
||||||
|
|
||||||
|
pypi-gitea:
|
||||||
|
base_url: "https://gitea.example.com/api/packages/myorg/pypi"
|
||||||
|
type: "remote"
|
||||||
|
package: "pypi"
|
||||||
|
description: "Private Gitea PyPI registry — simple index and files at the same host"
|
||||||
|
# username: "your-gitea-username"
|
||||||
|
# password: "your-personal-access-token" # needs package:read scope
|
||||||
|
check_mutable_updates: true
|
||||||
|
immutable_patterns:
|
||||||
|
- "files/.*\\.whl$"
|
||||||
|
- "files/.*\\.whl\\.metadata$"
|
||||||
|
- "files/.*\\.tar\\.gz$"
|
||||||
|
- "files/.*\\.zip$"
|
||||||
|
- "files/.*\\.egg$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 600
|
||||||
|
|
||||||
|
npm:
|
||||||
|
base_url: "https://registry.npmjs.org"
|
||||||
|
type: "remote"
|
||||||
|
package: "npm"
|
||||||
|
description: "npm registry — package metadata with tarball URL rewriting"
|
||||||
|
check_mutable_updates: true
|
||||||
|
immutable_patterns:
|
||||||
|
- \.tgz$
|
||||||
|
mutable_patterns:
|
||||||
|
- ^(?!.*\.tgz$).*
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0
|
||||||
|
mutable_ttl: 600 # Package metadata refreshed after 10 minutes
|
||||||
|
|
||||||
|
hashicorp-helm:
|
||||||
|
base_url: "https://helm.releases.hashicorp.com"
|
||||||
|
type: "remote"
|
||||||
|
package: "helm"
|
||||||
|
description: "HashiCorp Helm chart repository (Vault, Consul, Nomad, etc.)"
|
||||||
|
check_mutable_updates: true
|
||||||
|
immutable_patterns:
|
||||||
|
- "\\.tgz$"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Chart tarballs are versioned — cache forever
|
||||||
|
mutable_ttl: 3600 # index.yaml refreshed after 1 hour
|
||||||
|
|
||||||
|
local-generic:
|
||||||
|
type: "local"
|
||||||
|
package: "generic"
|
||||||
|
description: "Local generic file repository"
|
||||||
|
cache:
|
||||||
|
immutable_ttl: 0 # Files cached indefinitely
|
||||||
|
mutable_ttl: 0
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from . import discovery, docker, flush, local, proxy
|
||||||
|
|
||||||
|
__all__ = ["discovery", "docker", "flush", "local", "proxy"]
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import logging
|
||||||
|
import re
|
||||||
|
from typing import Any
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
from .proxy import cache_single_artifact
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def _discover_github_releases(remote: str, include_pattern: str) -> list[str]:
|
||||||
|
match = re.match(r"github\.com/([^/]+)/([^/]+)", remote)
|
||||||
|
if not match:
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid GitHub remote format")
|
||||||
|
|
||||||
|
owner, repo = match.groups()
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
|
response = await client.get(f"https://api.github.com/repos/{owner}/{repo}/releases")
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise HTTPException(status_code=response.status_code, detail=f"Failed to fetch releases: {response.text}")
|
||||||
|
|
||||||
|
releases = response.json()
|
||||||
|
regex = re.compile(include_pattern.replace("*", ".*"))
|
||||||
|
return [
|
||||||
|
asset["browser_download_url"]
|
||||||
|
for release in releases
|
||||||
|
for asset in release.get("assets", [])
|
||||||
|
if regex.search(asset["browser_download_url"])
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def _discover(remote: str, include_pattern: str) -> list[str]:
|
||||||
|
if "github.com" in remote:
|
||||||
|
return await _discover_github_releases(remote, include_pattern)
|
||||||
|
raise HTTPException(status_code=400, detail=f"Unsupported remote: {remote}")
|
||||||
|
|
||||||
|
|
||||||
|
async def cache_artifacts(remote: str, include_pattern: str, storage) -> dict[str, Any]:
|
||||||
|
try:
|
||||||
|
matching_urls = await _discover(remote, include_pattern)
|
||||||
|
|
||||||
|
if not matching_urls:
|
||||||
|
return {"message": "No matching artifacts found", "cached_count": 0, "artifacts": []}
|
||||||
|
|
||||||
|
cached_artifacts = []
|
||||||
|
for url in matching_urls:
|
||||||
|
result = await cache_single_artifact(url, "", "", storage, {})
|
||||||
|
cached_artifacts.append(result)
|
||||||
|
|
||||||
|
cached_count = sum(1 for a in cached_artifacts if a["status"] in ["cached", "already_cached"])
|
||||||
|
return {
|
||||||
|
"message": f"Processed {len(matching_urls)} artifacts, {cached_count} successfully cached",
|
||||||
|
"cached_count": cached_count,
|
||||||
|
"artifacts": cached_artifacts,
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
|
async def list_artifacts(remote: str, include_pattern: str, storage) -> dict[str, Any]:
|
||||||
|
try:
|
||||||
|
matching_urls = await _discover(remote, include_pattern)
|
||||||
|
cached_artifacts = []
|
||||||
|
for url in matching_urls:
|
||||||
|
parsed = urlparse(url)
|
||||||
|
key = storage.get_object_key(remote, parsed.path)
|
||||||
|
if storage.exists(key):
|
||||||
|
cached_artifacts.append({"url": url, "cached_url": storage.get_url(key), "key": key})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"remote": remote,
|
||||||
|
"pattern": include_pattern,
|
||||||
|
"total_found": len(matching_urls),
|
||||||
|
"cached_count": len(cached_artifacts),
|
||||||
|
"artifacts": cached_artifacts,
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
|
||||||
|
from fastapi import HTTPException, Request, Response
|
||||||
|
|
||||||
|
from . import proxy as _proxy
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def ping() -> Response:
|
||||||
|
return Response(
|
||||||
|
content="{}",
|
||||||
|
media_type="application/json",
|
||||||
|
headers={"Docker-Distribution-Api-Version": "registry/2.0"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def proxy(request: Request, remote_name: str, path: str, storage, cache, config, metrics) -> Response:
|
||||||
|
remote_config = config.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
||||||
|
if remote_config.get("package") != "docker":
|
||||||
|
raise HTTPException(status_code=400, detail=f"Remote '{remote_name}' is not a docker remote")
|
||||||
|
|
||||||
|
patterns = config.get_immutable_patterns(remote_name, "")
|
||||||
|
if patterns:
|
||||||
|
path_parts = path.split("/")
|
||||||
|
image_name = "/".join(path_parts[:2]) if len(path_parts) >= 2 else path
|
||||||
|
if not any(re.search(p, path) or re.search(p, image_name) for p in patterns):
|
||||||
|
logger.info(f"PATTERN BLOCKED: {remote_name}/{path}")
|
||||||
|
raise HTTPException(status_code=403, detail="Image not allowed by configuration patterns")
|
||||||
|
|
||||||
|
base_url = remote_config.get("base_url", "").rstrip("/")
|
||||||
|
remote_url = f"{base_url}/v2/{path}"
|
||||||
|
|
||||||
|
cached_key = storage.get_object_key(remote_name, path)
|
||||||
|
if not storage.exists(cached_key):
|
||||||
|
cached_key = None
|
||||||
|
|
||||||
|
is_mutable = cache.is_mutable_file(path, config.get_mutable_patterns(remote_name))
|
||||||
|
|
||||||
|
if cached_key and is_mutable:
|
||||||
|
if not cache.is_index_valid(remote_name, path):
|
||||||
|
if not await _proxy.handle_expired_mutable(remote_name, path, remote_url, config, cache, storage):
|
||||||
|
cached_key = None
|
||||||
|
|
||||||
|
if not cached_key:
|
||||||
|
logger.info(f"Cache MISS: {remote_name}/{path} - fetching from remote: {remote_url}")
|
||||||
|
result = await _proxy.cache_single_artifact(remote_url, remote_name, path, storage, remote_config)
|
||||||
|
if result["status"] == "error":
|
||||||
|
raise HTTPException(status_code=502, detail=f"Failed to fetch: {result['error']}")
|
||||||
|
if result["status"] == "cached" and is_mutable:
|
||||||
|
cache_config = config.get_cache_config(remote_name)
|
||||||
|
mutable_ttl = cache_config.get("mutable_ttl", 3600)
|
||||||
|
cache.mark_index_cached(remote_name, path, mutable_ttl)
|
||||||
|
logger.info(f"Mutable file cached with TTL: {remote_name}/{path} (ttl: {mutable_ttl}s)")
|
||||||
|
if result.get("etag") or result.get("last_modified"):
|
||||||
|
cache.store_mutable_meta(remote_name, path, result.get("etag"), result.get("last_modified"))
|
||||||
|
if not is_mutable:
|
||||||
|
published = result.get("last_modified")
|
||||||
|
if published:
|
||||||
|
cache.store_artifact_published(remote_name, path, published)
|
||||||
|
_proxy._check_quarantine(remote_name, published, config)
|
||||||
|
elif not is_mutable:
|
||||||
|
published = cache.get_artifact_published(remote_name, path)
|
||||||
|
if not published:
|
||||||
|
published = await _proxy._fetch_last_modified(remote_url, remote_config)
|
||||||
|
if published:
|
||||||
|
cache.store_artifact_published(remote_name, path, published)
|
||||||
|
_proxy._check_quarantine(remote_name, published, config)
|
||||||
|
|
||||||
|
artifact_data = storage.download_object(storage.get_object_key(remote_name, path))
|
||||||
|
|
||||||
|
is_blob = "/blobs/" in path
|
||||||
|
if is_blob:
|
||||||
|
content_type = "application/octet-stream"
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
manifest_json = json.loads(artifact_data)
|
||||||
|
content_type = manifest_json.get("mediaType")
|
||||||
|
if not content_type:
|
||||||
|
if "manifests" in manifest_json:
|
||||||
|
content_type = "application/vnd.oci.image.index.v1+json"
|
||||||
|
else:
|
||||||
|
content_type = "application/vnd.oci.image.manifest.v1+json"
|
||||||
|
except Exception:
|
||||||
|
content_type = "application/vnd.oci.image.manifest.v1+json"
|
||||||
|
|
||||||
|
digest = f"sha256:{hashlib.sha256(artifact_data).hexdigest()}"
|
||||||
|
headers = {
|
||||||
|
"Docker-Distribution-Api-Version": "registry/2.0",
|
||||||
|
"Docker-Content-Digest": digest,
|
||||||
|
"Content-Length": str(len(artifact_data)),
|
||||||
|
}
|
||||||
|
|
||||||
|
if request.method == "HEAD":
|
||||||
|
return Response(status_code=200, headers=headers, media_type=content_type)
|
||||||
|
|
||||||
|
metrics.record_cache_hit(remote_name, len(artifact_data))
|
||||||
|
return Response(content=artifact_data, media_type=content_type, headers=headers)
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def handle(remote: str | None, cache_type: str, cache, storage) -> dict:
|
||||||
|
try:
|
||||||
|
result = {"remote": remote, "cache_type": cache_type, "flushed": {"redis_keys": 0, "s3_objects": 0, "operations": []}}
|
||||||
|
|
||||||
|
if cache_type in ["all", "index", "metrics"] and cache.available and cache.client:
|
||||||
|
patterns = []
|
||||||
|
|
||||||
|
if cache_type in ["all", "index"]:
|
||||||
|
if remote:
|
||||||
|
patterns += [f"index:{remote}:*", f"mutable:meta:{remote}:*"]
|
||||||
|
else:
|
||||||
|
patterns += ["index:*", "mutable:meta:*"]
|
||||||
|
|
||||||
|
if cache_type in ["all", "metrics"]:
|
||||||
|
patterns.append(f"metrics:*:{remote}" if remote else "metrics:*")
|
||||||
|
|
||||||
|
for pattern in patterns:
|
||||||
|
keys = cache.client.keys(pattern)
|
||||||
|
if keys:
|
||||||
|
cache.client.delete(*keys)
|
||||||
|
result["flushed"]["redis_keys"] += len(keys)
|
||||||
|
logger.info(f"Cache flush: deleted {len(keys)} Redis keys matching '{pattern}'")
|
||||||
|
|
||||||
|
if result["flushed"]["redis_keys"] > 0:
|
||||||
|
result["flushed"]["operations"].append(f"Deleted {result['flushed']['redis_keys']} Redis keys")
|
||||||
|
|
||||||
|
if cache_type in ["all", "files"]:
|
||||||
|
try:
|
||||||
|
list_params = {"Bucket": storage.bucket}
|
||||||
|
if remote:
|
||||||
|
list_params["Prefix"] = f"{remote}/"
|
||||||
|
|
||||||
|
response = storage.client.list_objects_v2(**list_params)
|
||||||
|
if "Contents" in response:
|
||||||
|
objects_to_delete = [obj["Key"] for obj in response["Contents"]]
|
||||||
|
for key in objects_to_delete:
|
||||||
|
try:
|
||||||
|
storage.client.delete_object(Bucket=storage.bucket, Key=key)
|
||||||
|
result["flushed"]["s3_objects"] += 1
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to delete S3 object {key}: {e}")
|
||||||
|
|
||||||
|
if objects_to_delete:
|
||||||
|
scope = f" for remote '{remote}'" if remote else ""
|
||||||
|
result["flushed"]["operations"].append(f"Deleted {len(objects_to_delete)} S3 objects{scope}")
|
||||||
|
logger.info(f"Cache flush: deleted {len(objects_to_delete)} S3 objects{scope}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
result["flushed"]["operations"].append(f"S3 flush failed: {str(e)}")
|
||||||
|
logger.error(f"Cache flush S3 error: {e}")
|
||||||
|
|
||||||
|
if not result["flushed"]["operations"]:
|
||||||
|
result["flushed"]["operations"].append("No cache entries found to flush")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Cache flush error: {e}")
|
||||||
|
raise HTTPException(status_code=500, detail=f"Cache flush failed: {str(e)}")
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from fastapi import HTTPException, Response, UploadFile
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def upload(remote_name: str, path: str, file: UploadFile, storage, database, config) -> JSONResponse:
|
||||||
|
remote_config = config.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
||||||
|
if remote_config.get("type") != "local":
|
||||||
|
raise HTTPException(status_code=400, detail="Upload only supported for local repositories")
|
||||||
|
|
||||||
|
try:
|
||||||
|
content = await file.read()
|
||||||
|
sha256_sum = hashlib.sha256(content).hexdigest()
|
||||||
|
|
||||||
|
if database.file_exists(remote_name, path):
|
||||||
|
raise HTTPException(status_code=409, detail="File already exists")
|
||||||
|
|
||||||
|
s3_key = f"local/{remote_name}/{path}"
|
||||||
|
content_type = file.content_type or "application/octet-stream"
|
||||||
|
|
||||||
|
try:
|
||||||
|
storage.upload(s3_key, content)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Upload failed: {e}")
|
||||||
|
|
||||||
|
success = database.add_local_file(
|
||||||
|
repository_name=remote_name,
|
||||||
|
file_path=path,
|
||||||
|
s3_key=s3_key,
|
||||||
|
size_bytes=len(content),
|
||||||
|
sha256_sum=sha256_sum,
|
||||||
|
content_type=content_type,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not success:
|
||||||
|
storage.delete_object(s3_key)
|
||||||
|
raise HTTPException(status_code=500, detail="Failed to save file metadata")
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
{
|
||||||
|
"message": "File uploaded successfully",
|
||||||
|
"file_path": path,
|
||||||
|
"size_bytes": len(content),
|
||||||
|
"sha256_sum": sha256_sum,
|
||||||
|
"content_type": content_type,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
def check_exists(remote_name: str, path: str, database, config) -> Response:
|
||||||
|
remote_config = config.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
||||||
|
|
||||||
|
if remote_config.get("type") != "local":
|
||||||
|
raise HTTPException(status_code=405, detail="HEAD method only supported for local repositories")
|
||||||
|
|
||||||
|
try:
|
||||||
|
metadata = database.get_local_file_metadata(remote_name, path)
|
||||||
|
if not metadata:
|
||||||
|
raise HTTPException(status_code=404, detail="File not found")
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
headers={
|
||||||
|
"Content-Length": str(metadata["size_bytes"]),
|
||||||
|
"Content-Type": metadata.get("content_type", "application/octet-stream"),
|
||||||
|
"X-SHA256": metadata["sha256_sum"],
|
||||||
|
"X-Created-At": metadata["created_at"].isoformat() if metadata["created_at"] else "",
|
||||||
|
"X-Uploaded-At": metadata["uploaded_at"].isoformat() if metadata["uploaded_at"] else "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Check failed: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
def delete(remote_name: str, path: str, storage, database, config) -> JSONResponse:
|
||||||
|
remote_config = config.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
||||||
|
if remote_config.get("type") != "local":
|
||||||
|
raise HTTPException(status_code=400, detail="Delete only supported for local repositories")
|
||||||
|
|
||||||
|
try:
|
||||||
|
s3_key = database.delete_local_file(remote_name, path)
|
||||||
|
if not s3_key:
|
||||||
|
raise HTTPException(status_code=404, detail="File not found")
|
||||||
|
|
||||||
|
if not storage.delete_object(s3_key):
|
||||||
|
logger.warning(f"Failed to delete S3 object {s3_key} after database removal")
|
||||||
|
|
||||||
|
return JSONResponse({"message": "File deleted successfully"})
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Delete failed: {str(e)}")
|
||||||
@@ -0,0 +1,331 @@
|
|||||||
|
import base64
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from datetime import UTC, datetime, timedelta
|
||||||
|
from email.utils import parsedate_to_datetime
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from fastapi import HTTPException, Request, Response
|
||||||
|
|
||||||
|
from ..auth import get_docker_token_for_response
|
||||||
|
from ..remote import helm as _helm
|
||||||
|
from ..remote import npm as _npm
|
||||||
|
from ..remote import python as _pypi
|
||||||
|
from ..remote.base import get_content_type
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class UpstreamUnreachable(Exception):
|
||||||
|
"""Raised when the upstream backend cannot be contacted (network or timeout error)."""
|
||||||
|
|
||||||
|
|
||||||
|
def _check_quarantine(remote_name: str, last_modified_str: str | None, config) -> None:
|
||||||
|
"""Raise HTTP 404 if the artifact is within the per-remote quarantine window.
|
||||||
|
|
||||||
|
Fails open (allows the request) when the publish date cannot be determined.
|
||||||
|
"""
|
||||||
|
enabled, days = config.get_quarantine_config(remote_name)
|
||||||
|
if not enabled or not days:
|
||||||
|
return
|
||||||
|
if not last_modified_str:
|
||||||
|
return # cannot determine age → allow
|
||||||
|
try:
|
||||||
|
publish_date = parsedate_to_datetime(last_modified_str)
|
||||||
|
except Exception:
|
||||||
|
return # unparseable → allow
|
||||||
|
cutoff = datetime.now(UTC) - timedelta(days=days)
|
||||||
|
if publish_date > cutoff:
|
||||||
|
available_on = (publish_date + timedelta(days=days)).date()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=(
|
||||||
|
f"Package quarantined: published {publish_date.date()}, available after {available_on} ({days}-day new-release quarantine)"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _fetch_last_modified(remote_url: str, remote_cfg: dict) -> str | None:
|
||||||
|
"""HEAD the upstream URL and return the Last-Modified header, or None on any failure."""
|
||||||
|
auth = _basic_auth_header(remote_cfg)
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
|
response = await client.head(remote_url, headers=auth, timeout=10.0)
|
||||||
|
return response.headers.get("Last-Modified")
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _basic_auth_header(remote_cfg: dict) -> dict[str, str]:
|
||||||
|
username = remote_cfg.get("username")
|
||||||
|
password = remote_cfg.get("password")
|
||||||
|
if username and password:
|
||||||
|
token = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||||
|
return {"Authorization": f"Basic {token}"}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_content(
|
||||||
|
data: bytes,
|
||||||
|
path: str,
|
||||||
|
filename: str,
|
||||||
|
remote_config: dict,
|
||||||
|
request: Request,
|
||||||
|
remote_name: str = "",
|
||||||
|
) -> tuple[bytes, str]:
|
||||||
|
package = remote_config.get("package")
|
||||||
|
proxy_base = str(request.base_url).rstrip("/")
|
||||||
|
base_url = remote_config.get("base_url", "").rstrip("/")
|
||||||
|
|
||||||
|
if package == "pypi":
|
||||||
|
return _pypi.resolve_content(data, path, filename, remote_config.get("immutable_patterns", []), base_url, proxy_base, remote_name)
|
||||||
|
if package == "npm":
|
||||||
|
return _npm.resolve_content(data, path, filename, remote_config.get("immutable_patterns", []), base_url, proxy_base, remote_name)
|
||||||
|
if package == "helm":
|
||||||
|
return _helm.resolve_content(data, path, filename, base_url, proxy_base, remote_name)
|
||||||
|
return data, get_content_type(filename)
|
||||||
|
|
||||||
|
|
||||||
|
def construct_url(remote_config: dict, path: str) -> str:
|
||||||
|
base_url = remote_config.get("base_url", "").rstrip("/")
|
||||||
|
if remote_config.get("package") == "docker":
|
||||||
|
return f"{base_url}/v2/{path}"
|
||||||
|
if remote_config.get("package") == "pypi":
|
||||||
|
return _pypi.construct_url(base_url, path)
|
||||||
|
return f"{base_url}/{path}"
|
||||||
|
|
||||||
|
|
||||||
|
async def cache_single_artifact(url: str, remote_name: str, path: str, storage, remote_config: dict) -> dict:
|
||||||
|
key = storage.get_object_key(remote_name, path)
|
||||||
|
|
||||||
|
if storage.exists(key):
|
||||||
|
logger.info(f"Cache ALREADY EXISTS: {url} (key: {key})")
|
||||||
|
return {"url": url, "cached_url": storage.get_url(key), "status": "already_cached"}
|
||||||
|
|
||||||
|
try:
|
||||||
|
is_docker = remote_config.get("package") == "docker" or "/v2/" in url
|
||||||
|
headers = {}
|
||||||
|
username = remote_config.get("username")
|
||||||
|
password = remote_config.get("password")
|
||||||
|
|
||||||
|
if is_docker:
|
||||||
|
if "/manifests/" in url:
|
||||||
|
headers["Accept"] = (
|
||||||
|
"application/vnd.docker.distribution.manifest.v2+json,"
|
||||||
|
"application/vnd.oci.image.manifest.v1+json,"
|
||||||
|
"application/vnd.oci.image.index.v1+json,"
|
||||||
|
"application/vnd.docker.distribution.manifest.list.v2+json"
|
||||||
|
)
|
||||||
|
elif "/blobs/" in url:
|
||||||
|
headers["Accept"] = "application/octet-stream"
|
||||||
|
elif username and password:
|
||||||
|
headers["Authorization"] = "Basic " + base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
|
response = await client.get(url, headers=headers)
|
||||||
|
|
||||||
|
if response.status_code == 401 and is_docker:
|
||||||
|
www_auth = response.headers.get("WWW-Authenticate", "")
|
||||||
|
token = await get_docker_token_for_response(www_auth, username, password)
|
||||||
|
if token:
|
||||||
|
headers["Authorization"] = f"Bearer {token}"
|
||||||
|
response = await client.get(url, headers=headers)
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
storage.upload(key, response.content)
|
||||||
|
logger.info(f"Cache ADD SUCCESS: {url} (size: {len(response.content)} bytes, key: {key})")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"url": url,
|
||||||
|
"cached_url": storage.get_url(key),
|
||||||
|
"storage_path": f"s3://{storage.bucket}/{key}",
|
||||||
|
"size": len(response.content),
|
||||||
|
"status": "cached",
|
||||||
|
"etag": response.headers.get("ETag"),
|
||||||
|
"last_modified": response.headers.get("Last-Modified"),
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return {"url": url, "status": "error", "error": str(e)}
|
||||||
|
|
||||||
|
|
||||||
|
async def _upstream_reachable(url: str, auth_headers: dict | None = None) -> bool:
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
|
await client.head(url, headers=auth_headers or {}, timeout=10.0)
|
||||||
|
return True
|
||||||
|
except (httpx.NetworkError, httpx.TimeoutException):
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def check_upstream_changed(remote_url: str, remote_name: str, path: str, cache, auth_headers: dict | None = None) -> bool:
|
||||||
|
meta = cache.get_mutable_meta(remote_name, path)
|
||||||
|
if not meta:
|
||||||
|
return True
|
||||||
|
|
||||||
|
headers = dict(auth_headers or {})
|
||||||
|
if meta.get("etag"):
|
||||||
|
headers["If-None-Match"] = meta["etag"]
|
||||||
|
if meta.get("last_modified"):
|
||||||
|
headers["If-Modified-Since"] = meta["last_modified"]
|
||||||
|
if not (meta.get("etag") or meta.get("last_modified")):
|
||||||
|
return True
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
|
response = await client.head(remote_url, headers=headers)
|
||||||
|
return response.status_code != 304
|
||||||
|
except (httpx.NetworkError, httpx.TimeoutException) as exc:
|
||||||
|
raise UpstreamUnreachable(str(exc)) from exc
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_expired_mutable(remote_name: str, path: str, remote_url: str, config, cache, storage) -> bool:
|
||||||
|
"""Handle an expired mutable file. Returns True if the cached copy is still valid."""
|
||||||
|
mutable_ttl = config.get_cache_config(remote_name).get("mutable_ttl", 3600)
|
||||||
|
remote_cfg = config.get_remote_config(remote_name) or {}
|
||||||
|
auth = _basic_auth_header(remote_cfg)
|
||||||
|
check_updates = remote_cfg.get("check_mutable_updates", False)
|
||||||
|
user_mutable = check_updates and cache.is_mutable_file(path, config.get_user_mutable_patterns(remote_name))
|
||||||
|
|
||||||
|
if user_mutable:
|
||||||
|
try:
|
||||||
|
changed = await check_upstream_changed(remote_url, remote_name, path, cache, auth)
|
||||||
|
except UpstreamUnreachable:
|
||||||
|
cache.mark_index_cached(remote_name, path, mutable_ttl)
|
||||||
|
logger.warning(f"Mutable STALE (backend unreachable): {remote_name}/{path} - TTL extended ({mutable_ttl}s)")
|
||||||
|
return True
|
||||||
|
if not changed:
|
||||||
|
cache.mark_index_cached(remote_name, path, mutable_ttl)
|
||||||
|
logger.info(f"Mutable file UNCHANGED: {remote_name}/{path} - TTL refreshed ({mutable_ttl}s)")
|
||||||
|
return True
|
||||||
|
logger.info(f"Mutable file CHANGED: {remote_name}/{path} - re-downloading")
|
||||||
|
else:
|
||||||
|
if not await _upstream_reachable(remote_url, auth):
|
||||||
|
cache.mark_index_cached(remote_name, path, mutable_ttl)
|
||||||
|
logger.warning(f"Mutable STALE (backend unreachable): {remote_name}/{path} - TTL extended ({mutable_ttl}s)")
|
||||||
|
return True
|
||||||
|
logger.info(f"Mutable file EXPIRED: {remote_name}/{path} - removing from cache")
|
||||||
|
|
||||||
|
cache.cleanup_expired_index(storage, remote_name, path)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def handle(request: Request, remote_name: str, path: str, storage, cache, config, database, metrics) -> Response:
|
||||||
|
remote_config = config.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
||||||
|
|
||||||
|
if remote_config.get("type") == "local":
|
||||||
|
metadata = database.get_local_file_metadata(remote_name, path)
|
||||||
|
if not metadata:
|
||||||
|
raise HTTPException(status_code=404, detail="File not found")
|
||||||
|
content = storage.download_object(metadata["s3_key"])
|
||||||
|
if content is None:
|
||||||
|
raise HTTPException(status_code=500, detail="File not accessible")
|
||||||
|
return Response(
|
||||||
|
content=content,
|
||||||
|
media_type=metadata.get("content_type", "application/octet-stream"),
|
||||||
|
headers={"Content-Disposition": f"attachment; filename={os.path.basename(path)}"},
|
||||||
|
)
|
||||||
|
|
||||||
|
path_parts = path.split("/")
|
||||||
|
if len(path_parts) >= 2:
|
||||||
|
repo_path = f"{path_parts[0]}/{path_parts[1]}"
|
||||||
|
file_path = "/".join(path_parts[2:])
|
||||||
|
else:
|
||||||
|
repo_path = path
|
||||||
|
file_path = path
|
||||||
|
|
||||||
|
mutable_patterns = config.get_mutable_patterns(remote_name)
|
||||||
|
if not cache.is_mutable_file(file_path, mutable_patterns) and not cache.is_mutable_file(path, mutable_patterns):
|
||||||
|
patterns = config.get_immutable_patterns(remote_name, repo_path)
|
||||||
|
if patterns and not any(re.search(p, file_path) or re.search(p, path) for p in patterns):
|
||||||
|
logger.info(f"PATTERN BLOCKED: {remote_name}/{path} - not matching include patterns")
|
||||||
|
raise HTTPException(status_code=403, detail="Artifact not allowed by configuration patterns")
|
||||||
|
|
||||||
|
remote_url = construct_url(remote_config, path)
|
||||||
|
if not remote_config.get("base_url"):
|
||||||
|
raise HTTPException(status_code=500, detail=f"No base_url configured for remote '{remote_name}'")
|
||||||
|
|
||||||
|
cached_key = storage.get_object_key(remote_name, path)
|
||||||
|
if not storage.exists(cached_key):
|
||||||
|
cached_key = None
|
||||||
|
|
||||||
|
filename = os.path.basename(path)
|
||||||
|
is_mutable = cache.is_mutable_file(path, mutable_patterns)
|
||||||
|
|
||||||
|
if cached_key and is_mutable:
|
||||||
|
if not cache.is_index_valid(remote_name, path):
|
||||||
|
if not await handle_expired_mutable(remote_name, path, remote_url, config, cache, storage):
|
||||||
|
cached_key = None
|
||||||
|
|
||||||
|
if cached_key:
|
||||||
|
if not is_mutable:
|
||||||
|
published = cache.get_artifact_published(remote_name, path)
|
||||||
|
if not published:
|
||||||
|
published = await _fetch_last_modified(remote_url, remote_config)
|
||||||
|
if published:
|
||||||
|
cache.store_artifact_published(remote_name, path, published)
|
||||||
|
_check_quarantine(remote_name, published, config)
|
||||||
|
|
||||||
|
try:
|
||||||
|
artifact_data = storage.download_object(cached_key)
|
||||||
|
artifact_data, content_type = _resolve_content(artifact_data, path, filename, remote_config, request, remote_name)
|
||||||
|
logger.info(f"Cache HIT: {remote_name}/{path} (size: {len(artifact_data)} bytes, key: {cached_key})")
|
||||||
|
metrics.record_cache_hit(remote_name, len(artifact_data))
|
||||||
|
database.record_artifact_mapping(cached_key, remote_name, path, len(artifact_data))
|
||||||
|
return Response(
|
||||||
|
content=artifact_data,
|
||||||
|
media_type=content_type,
|
||||||
|
headers={
|
||||||
|
"Content-Disposition": f"attachment; filename={filename}",
|
||||||
|
"X-Artifact-Source": "cache",
|
||||||
|
"X-Artifact-Size": str(len(artifact_data)),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Error retrieving cached artifact: {str(e)}")
|
||||||
|
|
||||||
|
logger.info(f"Cache MISS: {remote_name}/{path} - fetching from remote: {remote_url}")
|
||||||
|
result = await cache_single_artifact(remote_url, remote_name, path, storage, remote_config)
|
||||||
|
|
||||||
|
if result["status"] == "error":
|
||||||
|
logger.error(f"Cache ADD FAILED: {remote_name}/{path} - {result['error']}")
|
||||||
|
raise HTTPException(status_code=502, detail=f"Failed to fetch artifact: {result['error']}")
|
||||||
|
|
||||||
|
if result["status"] == "cached" and is_mutable:
|
||||||
|
cache_config = config.get_cache_config(remote_name)
|
||||||
|
mutable_ttl = cache_config.get("mutable_ttl", 3600)
|
||||||
|
cache.mark_index_cached(remote_name, path, mutable_ttl)
|
||||||
|
logger.info(f"Mutable file cached with TTL: {remote_name}/{path} (ttl: {mutable_ttl}s)")
|
||||||
|
if result.get("etag") or result.get("last_modified"):
|
||||||
|
cache.store_mutable_meta(remote_name, path, result.get("etag"), result.get("last_modified"))
|
||||||
|
|
||||||
|
if not is_mutable:
|
||||||
|
published = result.get("last_modified")
|
||||||
|
if published:
|
||||||
|
cache.store_artifact_published(remote_name, path, published)
|
||||||
|
_check_quarantine(remote_name, published, config)
|
||||||
|
|
||||||
|
try:
|
||||||
|
cache_key = storage.get_object_key(remote_name, path)
|
||||||
|
artifact_data = storage.download_object(cache_key)
|
||||||
|
artifact_data, content_type = _resolve_content(artifact_data, path, filename, remote_config, request, remote_name)
|
||||||
|
metrics.record_cache_miss(remote_name, len(artifact_data))
|
||||||
|
database.record_artifact_mapping(cache_key, remote_name, path, len(artifact_data))
|
||||||
|
return Response(
|
||||||
|
content=artifact_data,
|
||||||
|
media_type=content_type,
|
||||||
|
headers={
|
||||||
|
"Content-Disposition": f"attachment; filename={filename}",
|
||||||
|
"X-Artifact-Source": "remote",
|
||||||
|
"X-Artifact-Size": str(len(artifact_data)),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Error serving artifact: {str(e)}")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .docker import fetch_token, get_docker_token_for_response, parse_www_authenticate
|
||||||
|
|
||||||
|
__all__ = ["fetch_token", "get_docker_token_for_response", "parse_www_authenticate"]
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# In-memory token cache: key -> (token, expires_at)
|
||||||
|
_token_cache: dict[str, tuple[str, float]] = {}
|
||||||
|
|
||||||
|
_WWW_AUTH_RE = re.compile(
|
||||||
|
r'Bearer\s+realm="(?P<realm>[^"]+)"'
|
||||||
|
r'(?:,service="(?P<service>[^"]*)")?'
|
||||||
|
r'(?:,scope="(?P<scope>[^"]*)")?',
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _cache_key(realm: str, service: str, scope: str, username: str | None) -> str:
|
||||||
|
return f"{realm}|{service}|{scope}|{username or ''}"
|
||||||
|
|
||||||
|
|
||||||
|
def _get_cached_token(key: str) -> str | None:
|
||||||
|
entry = _token_cache.get(key)
|
||||||
|
if entry and entry[1] > time.time():
|
||||||
|
return entry[0]
|
||||||
|
_token_cache.pop(key, None)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _store_token(key: str, token: str, expires_in: int) -> None:
|
||||||
|
# Expire 30s early to avoid using a token right as it expires
|
||||||
|
_token_cache[key] = (token, time.time() + max(expires_in - 30, 10))
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_token(
|
||||||
|
realm: str,
|
||||||
|
service: str,
|
||||||
|
scope: str,
|
||||||
|
username: str | None = None,
|
||||||
|
password: str | None = None,
|
||||||
|
) -> str | None:
|
||||||
|
"""Fetch a Bearer token from a Docker registry auth server."""
|
||||||
|
key = _cache_key(realm, service, scope, username)
|
||||||
|
cached = _get_cached_token(key)
|
||||||
|
if cached:
|
||||||
|
return cached
|
||||||
|
|
||||||
|
params: dict[str, str] = {}
|
||||||
|
if service:
|
||||||
|
params["service"] = service
|
||||||
|
if scope:
|
||||||
|
params["scope"] = scope
|
||||||
|
|
||||||
|
auth = (username, password) if username and password else None
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
|
response = await client.get(realm, params=params, auth=auth)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Docker token fetch failed ({realm}): {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
token = data.get("token") or data.get("access_token")
|
||||||
|
if not token:
|
||||||
|
logger.warning(f"Docker token response missing token field: {data}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
expires_in = int(data.get("expires_in", 300))
|
||||||
|
_store_token(key, token, expires_in)
|
||||||
|
logger.debug(f"Docker token obtained (realm={realm}, service={service}, scope={scope}, expires_in={expires_in}s)")
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
def parse_www_authenticate(header: str) -> tuple[str, str, str] | None:
|
||||||
|
"""Parse WWW-Authenticate: Bearer header. Returns (realm, service, scope) or None."""
|
||||||
|
m = _WWW_AUTH_RE.search(header)
|
||||||
|
if not m:
|
||||||
|
return None
|
||||||
|
return m.group("realm"), m.group("service") or "", m.group("scope") or ""
|
||||||
|
|
||||||
|
|
||||||
|
async def get_docker_token_for_response(
|
||||||
|
www_authenticate: str,
|
||||||
|
username: str | None = None,
|
||||||
|
password: str | None = None,
|
||||||
|
) -> str | None:
|
||||||
|
"""Given a WWW-Authenticate header value, fetch and return a Bearer token."""
|
||||||
|
parsed = parse_www_authenticate(www_authenticate)
|
||||||
|
if not parsed:
|
||||||
|
return None
|
||||||
|
realm, service, scope = parsed
|
||||||
|
return await fetch_token(realm, service, scope, username, password)
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
import hashlib
|
|
||||||
import re
|
|
||||||
import time
|
|
||||||
|
|
||||||
import redis
|
|
||||||
|
|
||||||
|
|
||||||
class RedisCache:
|
|
||||||
def __init__(self, redis_url: str):
|
|
||||||
self.redis_url = redis_url
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.client = redis.from_url(self.redis_url, decode_responses=True)
|
|
||||||
# Test connection
|
|
||||||
self.client.ping()
|
|
||||||
self.available = True
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Redis not available: {e}")
|
|
||||||
self.client = None
|
|
||||||
self.available = False
|
|
||||||
|
|
||||||
def is_index_file(self, file_path: str, patterns: list[str] | None = None) -> bool:
|
|
||||||
"""Return True if file_path matches any of the index patterns."""
|
|
||||||
if patterns is None:
|
|
||||||
patterns = []
|
|
||||||
return any(re.search(p, file_path) for p in patterns)
|
|
||||||
|
|
||||||
def get_index_cache_key(self, remote_name: str, path: str) -> str:
|
|
||||||
"""Generate cache key for index files"""
|
|
||||||
return f"index:{remote_name}:{hashlib.sha256(path.encode()).hexdigest()[:16]}"
|
|
||||||
|
|
||||||
def is_index_valid(self, remote_name: str, path: str, ttl_override: int = None) -> bool:
|
|
||||||
"""Check if index file is still valid (not expired)"""
|
|
||||||
if not self.available:
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
|
||||||
key = self.get_index_cache_key(remote_name, path)
|
|
||||||
return self.client.exists(key) > 0
|
|
||||||
except Exception:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def mark_index_cached(self, remote_name: str, path: str, ttl: int = 300) -> None:
|
|
||||||
"""Mark index file as cached with TTL"""
|
|
||||||
if not self.available:
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
key = self.get_index_cache_key(remote_name, path)
|
|
||||||
self.client.setex(key, ttl, str(int(time.time())))
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def cleanup_expired_index(self, storage, remote_name: str, path: str) -> None:
|
|
||||||
"""Remove expired index from S3 storage"""
|
|
||||||
if not self.available:
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Construct the URL the same way as in the main flow
|
|
||||||
import os
|
|
||||||
|
|
||||||
from .config import ConfigManager
|
|
||||||
|
|
||||||
config_path = os.environ.get("CONFIG_PATH")
|
|
||||||
if config_path:
|
|
||||||
config = ConfigManager(config_path)
|
|
||||||
remote_config = config.get_remote_config(remote_name)
|
|
||||||
if remote_config:
|
|
||||||
base_url = remote_config.get("base_url")
|
|
||||||
if base_url:
|
|
||||||
# Use hierarchical path-based key (same as cache_single_artifact)
|
|
||||||
s3_key = storage.get_object_key(remote_name, path)
|
|
||||||
if storage.exists(s3_key):
|
|
||||||
storage.client.delete_object(Bucket=storage.bucket, Key=s3_key)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
Vendored
+3
@@ -0,0 +1,3 @@
|
|||||||
|
from .redis import RedisCache
|
||||||
|
|
||||||
|
__all__ = ["RedisCache"]
|
||||||
Vendored
+124
@@ -0,0 +1,124 @@
|
|||||||
|
import hashlib
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
|
||||||
|
import redis
|
||||||
|
|
||||||
|
|
||||||
|
class RedisCache:
|
||||||
|
def __init__(self, redis_url: str):
|
||||||
|
self.redis_url = redis_url
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.client = redis.from_url(self.redis_url, decode_responses=True)
|
||||||
|
self.client.ping()
|
||||||
|
self.available = True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Redis not available: {e}")
|
||||||
|
self.client = None
|
||||||
|
self.available = False
|
||||||
|
|
||||||
|
def is_mutable_file(self, file_path: str, patterns: list[str] | None = None) -> bool:
|
||||||
|
if patterns is None:
|
||||||
|
patterns = []
|
||||||
|
return any(re.search(p, file_path) for p in patterns)
|
||||||
|
|
||||||
|
def get_index_cache_key(self, remote_name: str, path: str) -> str:
|
||||||
|
return f"index:{remote_name}:{hashlib.sha256(path.encode()).hexdigest()[:16]}"
|
||||||
|
|
||||||
|
def get_mutable_meta_key(self, remote_name: str, path: str) -> str:
|
||||||
|
return f"mutable:meta:{remote_name}:{hashlib.sha256(path.encode()).hexdigest()[:16]}"
|
||||||
|
|
||||||
|
def is_index_valid(self, remote_name: str, path: str) -> bool:
|
||||||
|
if not self.available:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
key = self.get_index_cache_key(remote_name, path)
|
||||||
|
return self.client.exists(key) > 0
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def mark_index_cached(self, remote_name: str, path: str, ttl: int = 300) -> None:
|
||||||
|
if not self.available:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
key = self.get_index_cache_key(remote_name, path)
|
||||||
|
self.client.setex(key, ttl, str(int(time.time())))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def store_mutable_meta(self, remote_name: str, path: str, etag: str | None, last_modified: str | None) -> None:
|
||||||
|
if not self.available:
|
||||||
|
return
|
||||||
|
data = {}
|
||||||
|
if etag:
|
||||||
|
data["etag"] = etag
|
||||||
|
if last_modified:
|
||||||
|
data["last_modified"] = last_modified
|
||||||
|
if not data:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
self.client.hset(self.get_mutable_meta_key(remote_name, path), mapping=data)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_mutable_meta(self, remote_name: str, path: str) -> dict:
|
||||||
|
if not self.available:
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
return self.client.hgetall(self.get_mutable_meta_key(remote_name, path)) or {}
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def delete_mutable_meta(self, remote_name: str, path: str) -> None:
|
||||||
|
if not self.available:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
self.client.delete(self.get_mutable_meta_key(remote_name, path))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_artifact_published_key(self, remote_name: str, path: str) -> str:
|
||||||
|
return f"pkg:published:{remote_name}:{hashlib.sha256(path.encode()).hexdigest()[:16]}"
|
||||||
|
|
||||||
|
def store_artifact_published(self, remote_name: str, path: str, last_modified: str) -> None:
|
||||||
|
"""Persist the upstream Last-Modified header for a (typically immutable) artifact."""
|
||||||
|
if not self.available:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
self.client.set(self.get_artifact_published_key(remote_name, path), last_modified)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_artifact_published(self, remote_name: str, path: str) -> str | None:
|
||||||
|
"""Return the stored Last-Modified string for an artifact, or None."""
|
||||||
|
if not self.available:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return self.client.get(self.get_artifact_published_key(remote_name, path))
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def cleanup_expired_index(self, storage, remote_name: str, path: str) -> None:
|
||||||
|
if not self.available:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
import os
|
||||||
|
|
||||||
|
from ..config import ConfigManager
|
||||||
|
|
||||||
|
config_path = os.environ.get("CONFIG_PATH")
|
||||||
|
if config_path:
|
||||||
|
config = ConfigManager(config_path)
|
||||||
|
remote_config = config.get_remote_config(remote_name)
|
||||||
|
if remote_config:
|
||||||
|
base_url = remote_config.get("base_url")
|
||||||
|
if base_url:
|
||||||
|
s3_key = storage.get_object_key(remote_name, path)
|
||||||
|
if storage.exists(s3_key):
|
||||||
|
storage.client.delete_object(Bucket=storage.bucket, Key=s3_key)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.delete_mutable_meta(remote_name, path)
|
||||||
+108
-29
@@ -1,9 +1,10 @@
|
|||||||
|
import glob
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
_PACKAGE_INDEX_PATTERNS: dict[str, list[str]] = {
|
_PACKAGE_MUTABLE_PATTERNS: dict[str, list[str]] = {
|
||||||
"alpine": [
|
"alpine": [
|
||||||
r"APKINDEX\.tar\.gz$",
|
r"APKINDEX\.tar\.gz$",
|
||||||
],
|
],
|
||||||
@@ -18,36 +19,99 @@ _PACKAGE_INDEX_PATTERNS: dict[str, list[str]] = {
|
|||||||
r"/manifests/(?!sha256:)[^/]+$",
|
r"/manifests/(?!sha256:)[^/]+$",
|
||||||
r"/tags/list$",
|
r"/tags/list$",
|
||||||
],
|
],
|
||||||
|
"pypi": [
|
||||||
|
r"simple/", # Per-package and top-level simple index pages
|
||||||
|
],
|
||||||
|
"npm": [],
|
||||||
|
"helm": [
|
||||||
|
r"index\.yaml$",
|
||||||
|
],
|
||||||
"generic": [],
|
"generic": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class ConfigManager:
|
class ConfigManager:
|
||||||
def __init__(self, config_file: str = "remotes.yaml"):
|
def __init__(self, config_path: str = "remotes.yaml"):
|
||||||
self.config_file = config_file
|
self.config_path = config_path
|
||||||
self._last_modified = 0
|
self._config_dir: str | None = None
|
||||||
|
self._last_modified: float = 0.0
|
||||||
self.config = self._load_config()
|
self.config = self._load_config()
|
||||||
|
|
||||||
def _load_config(self) -> dict:
|
def _load_single_file(self, path: str) -> dict:
|
||||||
try:
|
try:
|
||||||
with open(self.config_file) as f:
|
with open(path) as f:
|
||||||
if self.config_file.endswith(".yaml") or self.config_file.endswith(".yml"):
|
if path.endswith((".yaml", ".yml")):
|
||||||
return yaml.safe_load(f)
|
return yaml.safe_load(f) or {}
|
||||||
else:
|
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _merge(base: dict, overlay: dict) -> dict:
|
||||||
|
result = {**base}
|
||||||
|
for key, value in overlay.items():
|
||||||
|
if key == "remotes" and isinstance(base.get("remotes"), dict) and isinstance(value, dict):
|
||||||
|
result["remotes"] = {**base.get("remotes", {}), **value}
|
||||||
|
else:
|
||||||
|
result[key] = value
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _load_from_dir(self, dir_path: str) -> dict:
|
||||||
|
merged: dict = {}
|
||||||
|
files = sorted(glob.glob(os.path.join(dir_path, "*.yaml")) + glob.glob(os.path.join(dir_path, "*.yml")))
|
||||||
|
for path in files:
|
||||||
|
merged = self._merge(merged, self._load_single_file(path))
|
||||||
|
return merged
|
||||||
|
|
||||||
|
def _load_config(self) -> dict:
|
||||||
|
self._config_dir = None
|
||||||
|
|
||||||
|
if os.path.isdir(self.config_path):
|
||||||
|
return self._load_from_dir(self.config_path) or {"remotes": {}}
|
||||||
|
|
||||||
|
config = self._load_single_file(self.config_path)
|
||||||
|
if not config:
|
||||||
return {"remotes": {}}
|
return {"remotes": {}}
|
||||||
|
|
||||||
def _check_reload(self) -> None:
|
config_dir = config.pop("config_dir", None)
|
||||||
"""Check if config file has been modified and reload if needed"""
|
if config_dir:
|
||||||
try:
|
if not os.path.isabs(config_dir):
|
||||||
import os
|
config_dir = os.path.join(os.path.dirname(os.path.abspath(self.config_path)), config_dir)
|
||||||
|
self._config_dir = config_dir
|
||||||
|
config = self._merge(config, self._load_from_dir(config_dir))
|
||||||
|
|
||||||
current_modified = os.path.getmtime(self.config_file)
|
return config
|
||||||
|
|
||||||
|
def _file_mtimes(self) -> list[float]:
|
||||||
|
mtimes: list[float] = []
|
||||||
|
if os.path.isdir(self.config_path):
|
||||||
|
for f in glob.glob(os.path.join(self.config_path, "*.yaml")) + glob.glob(os.path.join(self.config_path, "*.yml")):
|
||||||
|
try:
|
||||||
|
mtimes.append(os.path.getmtime(f))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
mtimes.append(os.path.getmtime(self.config_path))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if self._config_dir and os.path.isdir(self._config_dir):
|
||||||
|
for f in glob.glob(os.path.join(self._config_dir, "*.yaml")) + glob.glob(os.path.join(self._config_dir, "*.yml")):
|
||||||
|
try:
|
||||||
|
mtimes.append(os.path.getmtime(f))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return mtimes
|
||||||
|
|
||||||
|
def _check_reload(self) -> None:
|
||||||
|
try:
|
||||||
|
current_modified = max(self._file_mtimes(), default=0.0)
|
||||||
if current_modified > self._last_modified:
|
if current_modified > self._last_modified:
|
||||||
self._last_modified = current_modified
|
self._last_modified = current_modified
|
||||||
self.config = self._load_config()
|
self.config = self._load_config()
|
||||||
print(f"Config reloaded from {self.config_file}")
|
print(f"Config reloaded from {self.config_path}")
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -55,26 +119,21 @@ class ConfigManager:
|
|||||||
self._check_reload()
|
self._check_reload()
|
||||||
return self.config.get("remotes", {}).get(remote_name)
|
return self.config.get("remotes", {}).get(remote_name)
|
||||||
|
|
||||||
def get_repository_patterns(self, remote_name: str, repo_path: str) -> list:
|
def get_immutable_patterns(self, remote_name: str, repo_path: str = "") -> list[str]:
|
||||||
remote_config = self.get_remote_config(remote_name)
|
remote_config = self.get_remote_config(remote_name)
|
||||||
if not remote_config:
|
if not remote_config:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
repositories = remote_config.get("repositories", {})
|
repositories = remote_config.get("repositories", {})
|
||||||
|
|
||||||
# Handle both dict (GitHub style) and list (Alpine style) repositories
|
|
||||||
if isinstance(repositories, dict):
|
if isinstance(repositories, dict):
|
||||||
repo_config = repositories.get(repo_path)
|
repo_config = repositories.get(repo_path)
|
||||||
if repo_config:
|
if repo_config:
|
||||||
patterns = repo_config.get("include_patterns", [])
|
patterns = repo_config.get("immutable_patterns", [])
|
||||||
else:
|
else:
|
||||||
patterns = remote_config.get("include_patterns", [])
|
patterns = remote_config.get("immutable_patterns", [])
|
||||||
elif isinstance(repositories, list):
|
|
||||||
# For Alpine, repositories is just a list of allowed repo names
|
|
||||||
# Pattern matching is handled by the main include_patterns
|
|
||||||
patterns = remote_config.get("include_patterns", [])
|
|
||||||
else:
|
else:
|
||||||
patterns = remote_config.get("include_patterns", [])
|
patterns = remote_config.get("immutable_patterns", [])
|
||||||
|
|
||||||
return patterns
|
return patterns
|
||||||
|
|
||||||
@@ -129,18 +188,25 @@ class ConfigManager:
|
|||||||
db_url = f"postgresql://{db_user}:{db_pass}@{db_host}:{db_port}/{db_name}"
|
db_url = f"postgresql://{db_user}:{db_pass}@{db_host}:{db_port}/{db_name}"
|
||||||
return {"url": db_url}
|
return {"url": db_url}
|
||||||
|
|
||||||
def get_index_patterns(self, remote_name: str) -> list[str]:
|
def get_user_mutable_patterns(self, remote_name: str) -> list[str]:
|
||||||
"""Return index-file patterns for a remote.
|
"""Return only user-configured mutable_patterns, excluding package-type defaults."""
|
||||||
|
remote_config = self.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
return []
|
||||||
|
return remote_config.get("mutable_patterns", [])
|
||||||
|
|
||||||
|
def get_mutable_patterns(self, remote_name: str) -> list[str]:
|
||||||
|
"""Return mutable-file patterns for a remote (TTL is configured per-remote in cache.index_ttl).
|
||||||
|
|
||||||
Merges the package-level defaults with any extra patterns listed under
|
Merges the package-level defaults with any extra patterns listed under
|
||||||
``index_patterns`` in the remote's config.
|
``mutable_patterns`` in the remote's config.
|
||||||
"""
|
"""
|
||||||
remote_config = self.get_remote_config(remote_name)
|
remote_config = self.get_remote_config(remote_name)
|
||||||
if not remote_config:
|
if not remote_config:
|
||||||
return []
|
return []
|
||||||
package = remote_config.get("package", "generic")
|
package = remote_config.get("package", "generic")
|
||||||
defaults = _PACKAGE_INDEX_PATTERNS.get(package, [])
|
defaults = _PACKAGE_MUTABLE_PATTERNS.get(package, [])
|
||||||
extra = remote_config.get("index_patterns", [])
|
extra = remote_config.get("mutable_patterns", [])
|
||||||
return defaults + [p for p in extra if p not in defaults]
|
return defaults + [p for p in extra if p not in defaults]
|
||||||
|
|
||||||
def get_cache_config(self, remote_name: str) -> dict:
|
def get_cache_config(self, remote_name: str) -> dict:
|
||||||
@@ -150,3 +216,16 @@ class ConfigManager:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
return remote_config.get("cache", {})
|
return remote_config.get("cache", {})
|
||||||
|
|
||||||
|
def get_quarantine_config(self, remote_name: str) -> tuple[bool, int]:
|
||||||
|
"""Return (enabled, quarantine_days) for a remote.
|
||||||
|
|
||||||
|
When enabled=True and quarantine_days>0, immutable artifacts published
|
||||||
|
within the last quarantine_days days are blocked with a 404.
|
||||||
|
"""
|
||||||
|
remote_config = self.get_remote_config(remote_name)
|
||||||
|
if not remote_config:
|
||||||
|
return False, 0
|
||||||
|
enabled = bool(remote_config.get("quarantine_new", False))
|
||||||
|
days = int(remote_config.get("quarantine_days", 0))
|
||||||
|
return enabled, days
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .postgres import DatabaseManager
|
||||||
|
|
||||||
|
__all__ = ["DatabaseManager"]
|
||||||
@@ -9,7 +9,6 @@ class DatabaseManager:
|
|||||||
self._init_database()
|
self._init_database()
|
||||||
|
|
||||||
def _init_database(self):
|
def _init_database(self):
|
||||||
"""Initialize database connection and create schema if needed"""
|
|
||||||
try:
|
try:
|
||||||
self.connection = psycopg2.connect(self.db_url)
|
self.connection = psycopg2.connect(self.db_url)
|
||||||
self.connection.autocommit = True
|
self.connection.autocommit = True
|
||||||
@@ -21,10 +20,8 @@ class DatabaseManager:
|
|||||||
self.available = False
|
self.available = False
|
||||||
|
|
||||||
def _create_schema(self):
|
def _create_schema(self):
|
||||||
"""Create tables if they don't exist"""
|
|
||||||
try:
|
try:
|
||||||
with self.connection.cursor() as cursor:
|
with self.connection.cursor() as cursor:
|
||||||
# Create table to map S3 keys to remote names
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS artifact_mappings (
|
CREATE TABLE IF NOT EXISTS artifact_mappings (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
@@ -51,7 +48,6 @@ class DatabaseManager:
|
|||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
# Create indexes separately
|
|
||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_s3_key ON artifact_mappings (s3_key)")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_s3_key ON artifact_mappings (s3_key)")
|
||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_remote_name ON artifact_mappings (remote_name)")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_remote_name ON artifact_mappings (remote_name)")
|
||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_local_repo_path ON local_files (repository_name, file_path)")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_local_repo_path ON local_files (repository_name, file_path)")
|
||||||
@@ -61,7 +57,6 @@ class DatabaseManager:
|
|||||||
print(f"Error creating schema: {e}")
|
print(f"Error creating schema: {e}")
|
||||||
|
|
||||||
def record_artifact_mapping(self, s3_key: str, remote_name: str, file_path: str, size_bytes: int):
|
def record_artifact_mapping(self, s3_key: str, remote_name: str, file_path: str, size_bytes: int):
|
||||||
"""Record mapping between S3 key and remote"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -83,7 +78,6 @@ class DatabaseManager:
|
|||||||
print(f"Error recording artifact mapping: {e}")
|
print(f"Error recording artifact mapping: {e}")
|
||||||
|
|
||||||
def get_storage_by_remote(self) -> dict[str, int]:
|
def get_storage_by_remote(self) -> dict[str, int]:
|
||||||
"""Get storage size breakdown by remote from database"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -101,7 +95,6 @@ class DatabaseManager:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
def get_remote_for_s3_key(self, s3_key: str) -> str | None:
|
def get_remote_for_s3_key(self, s3_key: str) -> str | None:
|
||||||
"""Get remote name for given S3 key"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -126,7 +119,6 @@ class DatabaseManager:
|
|||||||
sha256_sum: str,
|
sha256_sum: str,
|
||||||
content_type: str = None,
|
content_type: str = None,
|
||||||
):
|
):
|
||||||
"""Add a file to local repository"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -153,7 +145,6 @@ class DatabaseManager:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def get_local_file_metadata(self, repository_name: str, file_path: str):
|
def get_local_file_metadata(self, repository_name: str, file_path: str):
|
||||||
"""Get metadata for a local file"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -185,7 +176,6 @@ class DatabaseManager:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def list_local_files(self, repository_name: str, prefix: str = ""):
|
def list_local_files(self, repository_name: str, prefix: str = ""):
|
||||||
"""List files in local repository with optional path prefix"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -229,7 +219,6 @@ class DatabaseManager:
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def delete_local_file(self, repository_name: str, file_path: str):
|
def delete_local_file(self, repository_name: str, file_path: str):
|
||||||
"""Delete a file from local repository"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -251,7 +240,6 @@ class DatabaseManager:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def file_exists(self, repository_name: str, file_path: str):
|
def file_exists(self, repository_name: str, file_path: str):
|
||||||
"""Check if file exists in local repository"""
|
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -1,96 +1,19 @@
|
|||||||
import logging
|
from .auth.docker import (
|
||||||
import re
|
_cache_key,
|
||||||
import time
|
_get_cached_token,
|
||||||
|
_store_token,
|
||||||
import httpx
|
_token_cache,
|
||||||
|
fetch_token,
|
||||||
logger = logging.getLogger(__name__)
|
get_docker_token_for_response,
|
||||||
|
parse_www_authenticate,
|
||||||
# In-memory token cache: key -> (token, expires_at)
|
|
||||||
_token_cache: dict[str, tuple[str, float]] = {}
|
|
||||||
|
|
||||||
_WWW_AUTH_RE = re.compile(
|
|
||||||
r'Bearer\s+realm="(?P<realm>[^"]+)"'
|
|
||||||
r'(?:,service="(?P<service>[^"]*)")?'
|
|
||||||
r'(?:,scope="(?P<scope>[^"]*)")?',
|
|
||||||
re.IGNORECASE,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
def _cache_key(realm: str, service: str, scope: str, username: str | None) -> str:
|
"_cache_key",
|
||||||
return f"{realm}|{service}|{scope}|{username or ''}"
|
"_get_cached_token",
|
||||||
|
"_store_token",
|
||||||
|
"_token_cache",
|
||||||
def _get_cached_token(key: str) -> str | None:
|
"fetch_token",
|
||||||
entry = _token_cache.get(key)
|
"get_docker_token_for_response",
|
||||||
if entry and entry[1] > time.time():
|
"parse_www_authenticate",
|
||||||
return entry[0]
|
]
|
||||||
_token_cache.pop(key, None)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _store_token(key: str, token: str, expires_in: int) -> None:
|
|
||||||
# Expire 30s early to avoid using a token right as it expires
|
|
||||||
_token_cache[key] = (token, time.time() + max(expires_in - 30, 10))
|
|
||||||
|
|
||||||
|
|
||||||
async def fetch_token(
|
|
||||||
realm: str,
|
|
||||||
service: str,
|
|
||||||
scope: str,
|
|
||||||
username: str | None = None,
|
|
||||||
password: str | None = None,
|
|
||||||
) -> str | None:
|
|
||||||
"""Fetch a Bearer token from a Docker registry auth server."""
|
|
||||||
key = _cache_key(realm, service, scope, username)
|
|
||||||
cached = _get_cached_token(key)
|
|
||||||
if cached:
|
|
||||||
return cached
|
|
||||||
|
|
||||||
params: dict[str, str] = {}
|
|
||||||
if service:
|
|
||||||
params["service"] = service
|
|
||||||
if scope:
|
|
||||||
params["scope"] = scope
|
|
||||||
|
|
||||||
auth = (username, password) if username and password else None
|
|
||||||
|
|
||||||
try:
|
|
||||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
|
||||||
response = await client.get(realm, params=params, auth=auth)
|
|
||||||
response.raise_for_status()
|
|
||||||
data = response.json()
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Docker token fetch failed ({realm}): {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
token = data.get("token") or data.get("access_token")
|
|
||||||
if not token:
|
|
||||||
logger.warning(f"Docker token response missing token field: {data}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
expires_in = int(data.get("expires_in", 300))
|
|
||||||
_store_token(key, token, expires_in)
|
|
||||||
logger.debug(f"Docker token obtained (realm={realm}, service={service}, scope={scope}, expires_in={expires_in}s)")
|
|
||||||
return token
|
|
||||||
|
|
||||||
|
|
||||||
def parse_www_authenticate(header: str) -> tuple[str, str, str] | None:
|
|
||||||
"""Parse WWW-Authenticate: Bearer header. Returns (realm, service, scope) or None."""
|
|
||||||
m = _WWW_AUTH_RE.search(header)
|
|
||||||
if not m:
|
|
||||||
return None
|
|
||||||
return m.group("realm"), m.group("service") or "", m.group("scope") or ""
|
|
||||||
|
|
||||||
|
|
||||||
async def get_docker_token_for_response(
|
|
||||||
www_authenticate: str,
|
|
||||||
username: str | None = None,
|
|
||||||
password: str | None = None,
|
|
||||||
) -> str | None:
|
|
||||||
"""Given a WWW-Authenticate header value, fetch and return a Bearer token."""
|
|
||||||
parsed = parse_www_authenticate(www_authenticate)
|
|
||||||
if not parsed:
|
|
||||||
return None
|
|
||||||
realm, service, scope = parsed
|
|
||||||
return await fetch_token(realm, service, scope, username, password)
|
|
||||||
|
|||||||
+51
-666
@@ -1,13 +1,8 @@
|
|||||||
import hashlib
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import httpx
|
from fastapi import FastAPI, File, Query, Request, UploadFile
|
||||||
from fastapi import FastAPI, File, HTTPException, Query, Request, Response, UploadFile
|
from fastapi.responses import PlainTextResponse
|
||||||
from fastapi.responses import JSONResponse, PlainTextResponse
|
|
||||||
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
|
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -16,54 +11,45 @@ try:
|
|||||||
|
|
||||||
__version__ = version("artifactapi")
|
__version__ = version("artifactapi")
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Fallback for development when package isn't installed
|
|
||||||
__version__ = "dev"
|
__version__ = "dev"
|
||||||
|
|
||||||
|
from .artifact import discovery, flush, local, proxy
|
||||||
|
from .artifact import docker as docker_handler
|
||||||
from .cache import RedisCache
|
from .cache import RedisCache
|
||||||
from .config import ConfigManager
|
from .config import ConfigManager
|
||||||
from .database import DatabaseManager
|
from .database import DatabaseManager
|
||||||
from .docker_auth import get_docker_token_for_response
|
|
||||||
from .metrics import MetricsManager
|
from .metrics import MetricsManager
|
||||||
from .storage import S3Storage
|
from .storage import S3Storage
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
app = FastAPI(title="Artifact Storage API", version=__version__)
|
||||||
|
|
||||||
|
config_path = os.environ.get("CONFIG_PATH")
|
||||||
|
if not config_path:
|
||||||
|
raise ValueError("CONFIG_PATH environment variable is required")
|
||||||
|
config = ConfigManager(config_path)
|
||||||
|
|
||||||
|
s3_config = config.get_s3_config()
|
||||||
|
redis_config = config.get_redis_config()
|
||||||
|
db_config = config.get_database_config()
|
||||||
|
|
||||||
|
storage = S3Storage(**s3_config)
|
||||||
|
cache = RedisCache(redis_config["url"])
|
||||||
|
database = DatabaseManager(db_config["url"])
|
||||||
|
metrics = MetricsManager(cache, database)
|
||||||
|
|
||||||
|
|
||||||
class ArtifactRequest(BaseModel):
|
class ArtifactRequest(BaseModel):
|
||||||
remote: str
|
remote: str
|
||||||
include_pattern: str
|
include_pattern: str
|
||||||
|
|
||||||
|
|
||||||
# Configure logging
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
app = FastAPI(title="Artifact Storage API", version=__version__)
|
|
||||||
|
|
||||||
# Initialize components using config
|
|
||||||
config_path = os.environ.get("CONFIG_PATH")
|
|
||||||
if not config_path:
|
|
||||||
raise ValueError("CONFIG_PATH environment variable is required")
|
|
||||||
config = ConfigManager(config_path)
|
|
||||||
|
|
||||||
# Get configurations
|
|
||||||
s3_config = config.get_s3_config()
|
|
||||||
redis_config = config.get_redis_config()
|
|
||||||
db_config = config.get_database_config()
|
|
||||||
|
|
||||||
# Initialize services
|
|
||||||
storage = S3Storage(**s3_config)
|
|
||||||
cache = RedisCache(redis_config["url"])
|
|
||||||
database = DatabaseManager(db_config["url"])
|
|
||||||
metrics = MetricsManager(cache, database)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def read_root():
|
def read_root():
|
||||||
config._check_reload()
|
config._check_reload()
|
||||||
return {
|
return {"message": "Artifact Storage API", "version": app.version, "remotes": list(config.config.get("remotes", {}).keys())}
|
||||||
"message": "Artifact Storage API",
|
|
||||||
"version": app.version,
|
|
||||||
"remotes": list(config.config.get("remotes", {}).keys()),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
@@ -71,667 +57,66 @@ def health_check():
|
|||||||
return {"status": "healthy"}
|
return {"status": "healthy"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/config")
|
||||||
|
def get_config():
|
||||||
|
return config.config
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/metrics")
|
||||||
|
def get_metrics(json: bool | None = Query(False, description="Return JSON format instead of Prometheus")):
|
||||||
|
config._check_reload()
|
||||||
|
if json:
|
||||||
|
return metrics.get_metrics(storage, config)
|
||||||
|
metrics.get_metrics(storage, config)
|
||||||
|
return PlainTextResponse(generate_latest().decode("utf-8"), media_type=CONTENT_TYPE_LATEST)
|
||||||
|
|
||||||
|
|
||||||
@app.put("/cache/flush")
|
@app.put("/cache/flush")
|
||||||
def flush_cache(
|
def flush_cache(
|
||||||
remote: str = Query(default=None, description="Specific remote to flush (optional)"),
|
remote: str = Query(default=None, description="Specific remote to flush (optional)"),
|
||||||
cache_type: str = Query(default="all", description="Type to flush: 'all', 'index', 'files', 'metrics'"),
|
cache_type: str = Query(default="all", description="Type to flush: 'all', 'index', 'files', 'metrics'"),
|
||||||
):
|
):
|
||||||
"""Flush cache entries for specified remote or all remotes"""
|
return flush.handle(remote, cache_type, cache, storage)
|
||||||
try:
|
|
||||||
result = {"remote": remote, "cache_type": cache_type, "flushed": {"redis_keys": 0, "s3_objects": 0, "operations": []}}
|
|
||||||
|
|
||||||
# Flush Redis entries based on cache_type
|
|
||||||
if cache_type in ["all", "index", "metrics"] and cache.available and cache.client:
|
|
||||||
patterns = []
|
|
||||||
|
|
||||||
if cache_type in ["all", "index"]:
|
|
||||||
if remote:
|
|
||||||
patterns.append(f"index:{remote}:*")
|
|
||||||
else:
|
|
||||||
patterns.append("index:*")
|
|
||||||
|
|
||||||
if cache_type in ["all", "metrics"]:
|
|
||||||
if remote:
|
|
||||||
patterns.append(f"metrics:*:{remote}")
|
|
||||||
else:
|
|
||||||
patterns.append("metrics:*")
|
|
||||||
|
|
||||||
for pattern in patterns:
|
|
||||||
keys = cache.client.keys(pattern)
|
|
||||||
if keys:
|
|
||||||
cache.client.delete(*keys)
|
|
||||||
result["flushed"]["redis_keys"] += len(keys)
|
|
||||||
logger.info(f"Cache flush: Deleted {len(keys)} Redis keys matching '{pattern}'")
|
|
||||||
|
|
||||||
if result["flushed"]["redis_keys"] > 0:
|
|
||||||
result["flushed"]["operations"].append(f"Deleted {result['flushed']['redis_keys']} Redis keys")
|
|
||||||
|
|
||||||
# Flush S3 objects if requested
|
|
||||||
if cache_type in ["all", "files"]:
|
|
||||||
try:
|
|
||||||
# Use prefix filtering for remote-specific deletion
|
|
||||||
list_params = {"Bucket": storage.bucket}
|
|
||||||
if remote:
|
|
||||||
list_params["Prefix"] = f"{remote}/"
|
|
||||||
|
|
||||||
response = storage.client.list_objects_v2(**list_params)
|
|
||||||
if "Contents" in response:
|
|
||||||
objects_to_delete = [obj["Key"] for obj in response["Contents"]]
|
|
||||||
|
|
||||||
for key in objects_to_delete:
|
|
||||||
try:
|
|
||||||
storage.client.delete_object(Bucket=storage.bucket, Key=key)
|
|
||||||
result["flushed"]["s3_objects"] += 1
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Failed to delete S3 object {key}: {e}")
|
|
||||||
|
|
||||||
if objects_to_delete:
|
|
||||||
scope = f" for remote '{remote}'" if remote else ""
|
|
||||||
result["flushed"]["operations"].append(f"Deleted {len(objects_to_delete)} S3 objects{scope}")
|
|
||||||
logger.info(f"Cache flush: Deleted {len(objects_to_delete)} S3 objects{scope}")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
result["flushed"]["operations"].append(f"S3 flush failed: {str(e)}")
|
|
||||||
logger.error(f"Cache flush S3 error: {e}")
|
|
||||||
|
|
||||||
if not result["flushed"]["operations"]:
|
|
||||||
result["flushed"]["operations"].append("No cache entries found to flush")
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Cache flush error: {e}")
|
|
||||||
raise HTTPException(status_code=500, detail=f"Cache flush failed: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
async def construct_remote_url(remote_name: str, path: str) -> str:
|
|
||||||
remote_config = config.get_remote_config(remote_name)
|
|
||||||
if not remote_config:
|
|
||||||
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
|
||||||
|
|
||||||
base_url = remote_config.get("base_url")
|
|
||||||
if not base_url:
|
|
||||||
raise HTTPException(status_code=500, detail=f"No base_url configured for remote '{remote_name}'")
|
|
||||||
|
|
||||||
# Handle Docker registry URLs
|
|
||||||
if remote_config.get("package") == "docker":
|
|
||||||
# Convert Docker paths to v2 API format
|
|
||||||
# e.g., library/nginx/manifests/latest -> v2/library/nginx/manifests/latest
|
|
||||||
return f"{base_url}/v2/{path}"
|
|
||||||
|
|
||||||
return f"{base_url}/{path}"
|
|
||||||
|
|
||||||
|
|
||||||
async def check_artifact_patterns(remote_name: str, repo_path: str, file_path: str, full_path: str) -> bool:
|
|
||||||
# First check if this is an index file - always allow index files
|
|
||||||
index_patterns = config.get_index_patterns(remote_name)
|
|
||||||
if cache.is_index_file(file_path, index_patterns) or cache.is_index_file(full_path, index_patterns):
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Then check basic include patterns
|
|
||||||
patterns = config.get_repository_patterns(remote_name, repo_path)
|
|
||||||
if not patterns:
|
|
||||||
return True # Allow all if no patterns configured
|
|
||||||
|
|
||||||
pattern_matched = False
|
|
||||||
for pattern in patterns:
|
|
||||||
# Check both file_path and full_path to handle different pattern types
|
|
||||||
if re.search(pattern, file_path) or re.search(pattern, full_path):
|
|
||||||
pattern_matched = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not pattern_matched:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# All remotes now use pattern-based filtering only - no additional checks needed
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
async def cache_single_artifact(url: str, remote_name: str, path: str) -> dict:
|
|
||||||
# Use hierarchical path-based key
|
|
||||||
key = storage.get_object_key(remote_name, path)
|
|
||||||
|
|
||||||
if storage.exists(key):
|
|
||||||
logger.info(f"Cache ALREADY EXISTS: {url} (key: {key})")
|
|
||||||
return {
|
|
||||||
"url": url,
|
|
||||||
"cached_url": storage.get_url(key),
|
|
||||||
"status": "already_cached",
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
|
||||||
remote_config = config.get_remote_config(remote_name) or {}
|
|
||||||
is_docker = remote_config.get("package") == "docker" or "/v2/" in url
|
|
||||||
|
|
||||||
# Prepare headers for Docker registry requests
|
|
||||||
headers = {}
|
|
||||||
if is_docker:
|
|
||||||
if "/manifests/" in url:
|
|
||||||
headers["Accept"] = (
|
|
||||||
"application/vnd.docker.distribution.manifest.v2+json,"
|
|
||||||
"application/vnd.oci.image.manifest.v1+json,"
|
|
||||||
"application/vnd.oci.image.index.v1+json,"
|
|
||||||
"application/vnd.docker.distribution.manifest.list.v2+json"
|
|
||||||
)
|
|
||||||
elif "/blobs/" in url:
|
|
||||||
headers["Accept"] = "application/octet-stream"
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
|
||||||
response = await client.get(url, headers=headers)
|
|
||||||
|
|
||||||
# Handle Docker Bearer token challenge
|
|
||||||
if response.status_code == 401 and is_docker:
|
|
||||||
www_auth = response.headers.get("WWW-Authenticate", "")
|
|
||||||
username = remote_config.get("username")
|
|
||||||
password = remote_config.get("password")
|
|
||||||
token = await get_docker_token_for_response(www_auth, username, password)
|
|
||||||
if token:
|
|
||||||
headers["Authorization"] = f"Bearer {token}"
|
|
||||||
response = await client.get(url, headers=headers)
|
|
||||||
|
|
||||||
response.raise_for_status()
|
|
||||||
|
|
||||||
storage_path = storage.upload(key, response.content)
|
|
||||||
|
|
||||||
logger.info(f"Cache ADD SUCCESS: {url} (size: {len(response.content)} bytes, key: {key})")
|
|
||||||
|
|
||||||
return {
|
|
||||||
"url": url,
|
|
||||||
"cached_url": storage.get_url(key),
|
|
||||||
"storage_path": storage_path,
|
|
||||||
"size": len(response.content),
|
|
||||||
"status": "cached",
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
return {"url": url, "status": "error", "error": str(e)}
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/remote/{remote_name}/{path:path}")
|
|
||||||
async def get_artifact(remote_name: str, path: str):
|
|
||||||
# Check if remote is configured
|
|
||||||
remote_config = config.get_remote_config(remote_name)
|
|
||||||
if not remote_config:
|
|
||||||
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
|
||||||
|
|
||||||
# Check if this is a local repository
|
|
||||||
if remote_config.get("type") == "local":
|
|
||||||
# Handle local repository download
|
|
||||||
metadata = database.get_local_file_metadata(remote_name, path)
|
|
||||||
if not metadata:
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
# Get file from S3
|
|
||||||
content = storage.download_object(metadata["s3_key"])
|
|
||||||
if content is None:
|
|
||||||
raise HTTPException(status_code=500, detail="File not accessible")
|
|
||||||
|
|
||||||
# Determine content type
|
|
||||||
content_type = metadata.get("content_type", "application/octet-stream")
|
|
||||||
|
|
||||||
return Response(
|
|
||||||
content=content,
|
|
||||||
media_type=content_type,
|
|
||||||
headers={"Content-Disposition": f"attachment; filename={os.path.basename(path)}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Extract repository path for pattern checking
|
|
||||||
path_parts = path.split("/")
|
|
||||||
if len(path_parts) >= 2:
|
|
||||||
repo_path = f"{path_parts[0]}/{path_parts[1]}"
|
|
||||||
file_path = "/".join(path_parts[2:])
|
|
||||||
else:
|
|
||||||
repo_path = path
|
|
||||||
file_path = path
|
|
||||||
|
|
||||||
# Check if artifact matches configured patterns
|
|
||||||
if not await check_artifact_patterns(remote_name, repo_path, file_path, path):
|
|
||||||
logger.info(f"PATTERN BLOCKED: {remote_name}/{path} - not matching include patterns")
|
|
||||||
raise HTTPException(status_code=403, detail="Artifact not allowed by configuration patterns")
|
|
||||||
|
|
||||||
# Construct the remote URL
|
|
||||||
remote_url = await construct_remote_url(remote_name, path)
|
|
||||||
|
|
||||||
# Check if artifact is already cached
|
|
||||||
cached_key = storage.get_object_key(remote_name, path)
|
|
||||||
if not storage.exists(cached_key):
|
|
||||||
cached_key = None
|
|
||||||
|
|
||||||
# For index files, check Redis TTL validity
|
|
||||||
filename = os.path.basename(path)
|
|
||||||
is_index = cache.is_index_file(path, config.get_index_patterns(remote_name))
|
|
||||||
|
|
||||||
if cached_key and is_index:
|
|
||||||
# Index file exists, but check if it's still valid
|
|
||||||
if not cache.is_index_valid(remote_name, path):
|
|
||||||
# Index has expired, remove it from S3
|
|
||||||
logger.info(f"Index EXPIRED: {remote_name}/{path} - removing from cache")
|
|
||||||
cache.cleanup_expired_index(storage, remote_name, path)
|
|
||||||
cached_key = None # Force re-download
|
|
||||||
|
|
||||||
if cached_key:
|
|
||||||
# Return cached artifact
|
|
||||||
try:
|
|
||||||
artifact_data = storage.download_object(cached_key)
|
|
||||||
filename = os.path.basename(path)
|
|
||||||
|
|
||||||
# Log cache hit
|
|
||||||
logger.info(f"Cache HIT: {remote_name}/{path} (size: {len(artifact_data)} bytes, key: {cached_key})")
|
|
||||||
|
|
||||||
# Determine content type based on file extension
|
|
||||||
content_type = "application/octet-stream"
|
|
||||||
if filename.endswith(".tar.gz"):
|
|
||||||
content_type = "application/gzip"
|
|
||||||
elif filename.endswith(".zip"):
|
|
||||||
content_type = "application/zip"
|
|
||||||
elif filename.endswith(".exe"):
|
|
||||||
content_type = "application/x-msdownload"
|
|
||||||
elif filename.endswith(".rpm"):
|
|
||||||
content_type = "application/x-rpm"
|
|
||||||
elif filename.endswith(".xml"):
|
|
||||||
content_type = "application/xml"
|
|
||||||
elif filename.endswith((".xml.gz", ".xml.bz2", ".xml.xz")):
|
|
||||||
content_type = "application/gzip"
|
|
||||||
|
|
||||||
# Record cache hit metrics
|
|
||||||
metrics.record_cache_hit(remote_name, len(artifact_data))
|
|
||||||
|
|
||||||
# Record artifact mapping in database if not already recorded
|
|
||||||
database.record_artifact_mapping(cached_key, remote_name, path, len(artifact_data))
|
|
||||||
|
|
||||||
return Response(
|
|
||||||
content=artifact_data,
|
|
||||||
media_type=content_type,
|
|
||||||
headers={
|
|
||||||
"Content-Disposition": f"attachment; filename={filename}",
|
|
||||||
"X-Artifact-Source": "cache",
|
|
||||||
"X-Artifact-Size": str(len(artifact_data)),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error retrieving cached artifact: {str(e)}")
|
|
||||||
|
|
||||||
# Artifact not cached, cache it first
|
|
||||||
logger.info(f"Cache MISS: {remote_name}/{path} - fetching from remote: {remote_url}")
|
|
||||||
result = await cache_single_artifact(remote_url, remote_name, path)
|
|
||||||
|
|
||||||
if result["status"] == "error":
|
|
||||||
logger.error(f"Cache ADD FAILED: {remote_name}/{path} - {result['error']}")
|
|
||||||
raise HTTPException(status_code=502, detail=f"Failed to fetch artifact: {result['error']}")
|
|
||||||
|
|
||||||
# Mark index files as cached in Redis if this was a new download
|
|
||||||
if result["status"] == "cached" and is_index:
|
|
||||||
# Get TTL from remote config
|
|
||||||
cache_config = config.get_cache_config(remote_name)
|
|
||||||
index_ttl = cache_config.get("index_ttl", 300) # Default 5 minutes
|
|
||||||
cache.mark_index_cached(remote_name, path, index_ttl)
|
|
||||||
logger.info(f"Index file cached with TTL: {remote_name}/{path} (ttl: {index_ttl}s)")
|
|
||||||
|
|
||||||
# Now return the cached artifact
|
|
||||||
try:
|
|
||||||
cache_key = storage.get_object_key(remote_name, path)
|
|
||||||
artifact_data = storage.download_object(cache_key)
|
|
||||||
filename = os.path.basename(path)
|
|
||||||
|
|
||||||
content_type = "application/octet-stream"
|
|
||||||
if filename.endswith(".tar.gz"):
|
|
||||||
content_type = "application/gzip"
|
|
||||||
elif filename.endswith(".zip"):
|
|
||||||
content_type = "application/zip"
|
|
||||||
elif filename.endswith(".exe"):
|
|
||||||
content_type = "application/x-msdownload"
|
|
||||||
elif filename.endswith(".rpm"):
|
|
||||||
content_type = "application/x-rpm"
|
|
||||||
elif filename.endswith(".xml"):
|
|
||||||
content_type = "application/xml"
|
|
||||||
elif filename.endswith((".xml.gz", ".xml.bz2", ".xml.xz")):
|
|
||||||
content_type = "application/gzip"
|
|
||||||
|
|
||||||
# Record cache miss metrics
|
|
||||||
metrics.record_cache_miss(remote_name, len(artifact_data))
|
|
||||||
|
|
||||||
# Record artifact mapping in database
|
|
||||||
cache_key = storage.get_object_key(remote_name, path)
|
|
||||||
database.record_artifact_mapping(cache_key, remote_name, path, len(artifact_data))
|
|
||||||
|
|
||||||
return Response(
|
|
||||||
content=artifact_data,
|
|
||||||
media_type=content_type,
|
|
||||||
headers={
|
|
||||||
"Content-Disposition": f"attachment; filename={filename}",
|
|
||||||
"X-Artifact-Source": "remote",
|
|
||||||
"X-Artifact-Size": str(len(artifact_data)),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error serving artifact: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/v2/")
|
@app.get("/v2/")
|
||||||
async def docker_v2_ping():
|
async def docker_v2_ping():
|
||||||
return Response(
|
return docker_handler.ping()
|
||||||
content="{}",
|
|
||||||
media_type="application/json",
|
|
||||||
headers={"Docker-Distribution-Api-Version": "registry/2.0"},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.api_route("/v2/{remote_name}/{path:path}", methods=["GET", "HEAD"])
|
@app.api_route("/v2/{remote_name}/{path:path}", methods=["GET", "HEAD"])
|
||||||
async def docker_v2_proxy(request: Request, remote_name: str, path: str):
|
async def docker_v2_proxy(request: Request, remote_name: str, path: str):
|
||||||
remote_config = config.get_remote_config(remote_name)
|
return await docker_handler.proxy(request, remote_name, path, storage, cache, config, metrics)
|
||||||
if not remote_config:
|
|
||||||
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
|
||||||
if remote_config.get("package") != "docker":
|
|
||||||
raise HTTPException(status_code=400, detail=f"Remote '{remote_name}' is not a docker remote")
|
|
||||||
|
|
||||||
# Check include_patterns against the image name (e.g. "library/nginx")
|
|
||||||
patterns = config.get_repository_patterns(remote_name, "")
|
|
||||||
if patterns:
|
|
||||||
path_parts = path.split("/")
|
|
||||||
image_name = "/".join(path_parts[:2]) if len(path_parts) >= 2 else path
|
|
||||||
if not any(re.search(p, path) or re.search(p, image_name) for p in patterns):
|
|
||||||
logger.info(f"PATTERN BLOCKED: {remote_name}/{path}")
|
|
||||||
raise HTTPException(status_code=403, detail="Image not allowed by configuration patterns")
|
|
||||||
|
|
||||||
remote_url = await construct_remote_url(remote_name, path)
|
|
||||||
|
|
||||||
cached_key = storage.get_object_key(remote_name, path)
|
|
||||||
if not storage.exists(cached_key):
|
|
||||||
cached_key = None
|
|
||||||
|
|
||||||
is_index = cache.is_index_file(path, config.get_index_patterns(remote_name))
|
|
||||||
|
|
||||||
if cached_key and is_index:
|
|
||||||
if not cache.is_index_valid(remote_name, path):
|
|
||||||
logger.info(f"Index EXPIRED: {remote_name}/{path} - removing from cache")
|
|
||||||
cache.cleanup_expired_index(storage, remote_name, path)
|
|
||||||
cached_key = None
|
|
||||||
|
|
||||||
if not cached_key:
|
|
||||||
logger.info(f"Cache MISS: {remote_name}/{path} - fetching from remote: {remote_url}")
|
|
||||||
result = await cache_single_artifact(remote_url, remote_name, path)
|
|
||||||
if result["status"] == "error":
|
|
||||||
raise HTTPException(status_code=502, detail=f"Failed to fetch: {result['error']}")
|
|
||||||
if result["status"] == "cached" and is_index:
|
|
||||||
cache_config = config.get_cache_config(remote_name)
|
|
||||||
index_ttl = cache_config.get("index_ttl", 300)
|
|
||||||
cache.mark_index_cached(remote_name, path, index_ttl)
|
|
||||||
logger.info(f"Index file cached with TTL: {remote_name}/{path} (ttl: {index_ttl}s)")
|
|
||||||
|
|
||||||
artifact_data = storage.download_object(storage.get_object_key(remote_name, path))
|
|
||||||
|
|
||||||
is_blob = "/blobs/" in path
|
|
||||||
if is_blob:
|
|
||||||
content_type = "application/octet-stream"
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
manifest_json = json.loads(artifact_data)
|
|
||||||
content_type = manifest_json.get("mediaType")
|
|
||||||
if not content_type:
|
|
||||||
if "manifests" in manifest_json:
|
|
||||||
content_type = "application/vnd.oci.image.index.v1+json"
|
|
||||||
else:
|
|
||||||
content_type = "application/vnd.oci.image.manifest.v1+json"
|
|
||||||
except Exception:
|
|
||||||
content_type = "application/vnd.oci.image.manifest.v1+json"
|
|
||||||
|
|
||||||
digest = f"sha256:{hashlib.sha256(artifact_data).hexdigest()}"
|
|
||||||
headers = {
|
|
||||||
"Docker-Distribution-Api-Version": "registry/2.0",
|
|
||||||
"Docker-Content-Digest": digest,
|
|
||||||
"Content-Length": str(len(artifact_data)),
|
|
||||||
}
|
|
||||||
|
|
||||||
if request.method == "HEAD":
|
|
||||||
return Response(status_code=200, headers=headers, media_type=content_type)
|
|
||||||
|
|
||||||
metrics.record_cache_hit(remote_name, len(artifact_data))
|
|
||||||
return Response(content=artifact_data, media_type=content_type, headers=headers)
|
|
||||||
|
|
||||||
|
|
||||||
async def discover_artifacts(remote: str, include_pattern: str) -> list[str]:
|
@app.get("/api/v1/remote/{remote_name}/{path:path}")
|
||||||
if "github.com" in remote:
|
async def get_artifact(request: Request, remote_name: str, path: str):
|
||||||
return await discover_github_releases(remote, include_pattern)
|
return await proxy.handle(request, remote_name, path, storage, cache, config, database, metrics)
|
||||||
else:
|
|
||||||
raise HTTPException(status_code=400, detail=f"Unsupported remote: {remote}")
|
|
||||||
|
|
||||||
|
|
||||||
async def discover_github_releases(remote: str, include_pattern: str) -> list[str]:
|
|
||||||
match = re.match(r"github\.com/([^/]+)/([^/]+)", remote)
|
|
||||||
if not match:
|
|
||||||
raise HTTPException(status_code=400, detail="Invalid GitHub remote format")
|
|
||||||
|
|
||||||
owner, repo = match.groups()
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
|
||||||
response = await client.get(f"https://api.github.com/repos/{owner}/{repo}/releases")
|
|
||||||
|
|
||||||
if response.status_code != 200:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=response.status_code,
|
|
||||||
detail=f"Failed to fetch releases: {response.text}",
|
|
||||||
)
|
|
||||||
|
|
||||||
releases = response.json()
|
|
||||||
|
|
||||||
matching_urls = []
|
|
||||||
pattern = include_pattern.replace("*", ".*")
|
|
||||||
regex = re.compile(pattern)
|
|
||||||
|
|
||||||
for release in releases:
|
|
||||||
for asset in release.get("assets", []):
|
|
||||||
download_url = asset["browser_download_url"]
|
|
||||||
if regex.search(download_url):
|
|
||||||
matching_urls.append(download_url)
|
|
||||||
|
|
||||||
return matching_urls
|
|
||||||
|
|
||||||
|
|
||||||
@app.put("/api/v1/remote/{remote_name}/{path:path}")
|
@app.put("/api/v1/remote/{remote_name}/{path:path}")
|
||||||
async def upload_file(remote_name: str, path: str, file: UploadFile = File(...)):
|
async def upload_file(remote_name: str, path: str, file: UploadFile = File(...)):
|
||||||
"""Upload a file to local repository"""
|
return await local.upload(remote_name, path, file, storage, database, config)
|
||||||
# Check if remote is configured and is local
|
|
||||||
remote_config = config.get_remote_config(remote_name)
|
|
||||||
if not remote_config:
|
|
||||||
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
|
||||||
|
|
||||||
if remote_config.get("type") != "local":
|
|
||||||
raise HTTPException(status_code=400, detail="Upload only supported for local repositories")
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Read file content
|
|
||||||
content = await file.read()
|
|
||||||
|
|
||||||
# Calculate SHA256
|
|
||||||
sha256_sum = hashlib.sha256(content).hexdigest()
|
|
||||||
|
|
||||||
# Check if file already exists (prevent overwrite)
|
|
||||||
if database.file_exists(remote_name, path):
|
|
||||||
raise HTTPException(status_code=409, detail="File already exists")
|
|
||||||
|
|
||||||
# Generate S3 key
|
|
||||||
s3_key = f"local/{remote_name}/{path}"
|
|
||||||
|
|
||||||
# Determine content type
|
|
||||||
content_type = file.content_type or "application/octet-stream"
|
|
||||||
|
|
||||||
# Upload to S3
|
|
||||||
try:
|
|
||||||
storage.upload(s3_key, content)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Upload failed: {e}")
|
|
||||||
|
|
||||||
# Add to database
|
|
||||||
success = database.add_local_file(
|
|
||||||
repository_name=remote_name,
|
|
||||||
file_path=path,
|
|
||||||
s3_key=s3_key,
|
|
||||||
size_bytes=len(content),
|
|
||||||
sha256_sum=sha256_sum,
|
|
||||||
content_type=content_type,
|
|
||||||
)
|
|
||||||
|
|
||||||
if not success:
|
|
||||||
# Clean up S3 if database insert failed
|
|
||||||
storage.delete_object(s3_key)
|
|
||||||
raise HTTPException(status_code=500, detail="Failed to save file metadata")
|
|
||||||
|
|
||||||
return JSONResponse(
|
|
||||||
{
|
|
||||||
"message": "File uploaded successfully",
|
|
||||||
"file_path": path,
|
|
||||||
"size_bytes": len(content),
|
|
||||||
"sha256_sum": sha256_sum,
|
|
||||||
"content_type": content_type,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
except HTTPException:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
@app.head("/api/v1/remote/{remote_name}/{path:path}")
|
@app.head("/api/v1/remote/{remote_name}/{path:path}")
|
||||||
def check_file_exists(remote_name: str, path: str):
|
def check_file_exists(remote_name: str, path: str):
|
||||||
"""Check if file exists (for CI jobs) - supports local repositories only"""
|
return local.check_exists(remote_name, path, database, config)
|
||||||
# Check if remote is configured
|
|
||||||
remote_config = config.get_remote_config(remote_name)
|
|
||||||
if not remote_config:
|
|
||||||
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
|
||||||
|
|
||||||
# Handle local repository
|
|
||||||
if remote_config.get("type") == "local":
|
|
||||||
try:
|
|
||||||
metadata = database.get_local_file_metadata(remote_name, path)
|
|
||||||
if not metadata:
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
return Response(
|
|
||||||
headers={
|
|
||||||
"Content-Length": str(metadata["size_bytes"]),
|
|
||||||
"Content-Type": metadata.get("content_type", "application/octet-stream"),
|
|
||||||
"X-SHA256": metadata["sha256_sum"],
|
|
||||||
"X-Created-At": metadata["created_at"].isoformat() if metadata["created_at"] else "",
|
|
||||||
"X-Uploaded-At": metadata["uploaded_at"].isoformat() if metadata["uploaded_at"] else "",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
except HTTPException:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Check failed: {str(e)}")
|
|
||||||
else:
|
|
||||||
# For remote repositories, just return 405 Method Not Allowed
|
|
||||||
raise HTTPException(status_code=405, detail="HEAD method only supported for local repositories")
|
|
||||||
|
|
||||||
|
|
||||||
@app.delete("/api/v1/remote/{remote_name}/{path:path}")
|
@app.delete("/api/v1/remote/{remote_name}/{path:path}")
|
||||||
def delete_file(remote_name: str, path: str):
|
def delete_file(remote_name: str, path: str):
|
||||||
"""Delete a file from local repository"""
|
return local.delete(remote_name, path, storage, database, config)
|
||||||
# Check if remote is configured and is local
|
|
||||||
remote_config = config.get_remote_config(remote_name)
|
|
||||||
if not remote_config:
|
|
||||||
raise HTTPException(status_code=404, detail=f"Remote '{remote_name}' not configured")
|
|
||||||
|
|
||||||
if remote_config.get("type") != "local":
|
|
||||||
raise HTTPException(status_code=400, detail="Delete only supported for local repositories")
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Get S3 key before deleting from database
|
|
||||||
s3_key = database.delete_local_file(remote_name, path)
|
|
||||||
if not s3_key:
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
# Delete from S3
|
|
||||||
if not storage.delete_object(s3_key):
|
|
||||||
# File was deleted from database but not from S3 - log warning but continue
|
|
||||||
print(f"Warning: Failed to delete S3 object {s3_key}")
|
|
||||||
|
|
||||||
return JSONResponse({"message": "File deleted successfully"})
|
|
||||||
except HTTPException:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Delete failed: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/v1/artifacts/cache")
|
@app.post("/api/v1/artifacts/cache")
|
||||||
async def cache_artifact(request: ArtifactRequest) -> dict[str, Any]:
|
async def cache_artifact(request: ArtifactRequest):
|
||||||
try:
|
return await discovery.cache_artifacts(request.remote, request.include_pattern, storage)
|
||||||
matching_urls = await discover_artifacts(request.remote, request.include_pattern)
|
|
||||||
|
|
||||||
if not matching_urls:
|
|
||||||
return {
|
|
||||||
"message": "No matching artifacts found",
|
|
||||||
"cached_count": 0,
|
|
||||||
"artifacts": [],
|
|
||||||
}
|
|
||||||
|
|
||||||
cached_artifacts = []
|
|
||||||
|
|
||||||
for url in matching_urls:
|
|
||||||
result = await cache_single_artifact(url, "", "")
|
|
||||||
cached_artifacts.append(result)
|
|
||||||
|
|
||||||
cached_count = sum(1 for artifact in cached_artifacts if artifact["status"] in ["cached", "already_cached"])
|
|
||||||
|
|
||||||
return {
|
|
||||||
"message": f"Processed {len(matching_urls)} artifacts, {cached_count} successfully cached",
|
|
||||||
"cached_count": cached_count,
|
|
||||||
"artifacts": cached_artifacts,
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/artifacts/{remote:path}")
|
@app.get("/api/v1/artifacts/{remote:path}")
|
||||||
async def list_cached_artifacts(remote: str, include_pattern: str = ".*") -> dict[str, Any]:
|
async def list_cached_artifacts(remote: str, include_pattern: str = ".*"):
|
||||||
try:
|
return await discovery.list_artifacts(remote, include_pattern, storage)
|
||||||
matching_urls = await discover_artifacts(remote, include_pattern)
|
|
||||||
|
|
||||||
cached_artifacts = []
|
|
||||||
for url in matching_urls:
|
|
||||||
# Extract path from URL for hierarchical key generation
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
parsed = urlparse(url)
|
|
||||||
path = parsed.path
|
|
||||||
key = storage.get_object_key(remote, path)
|
|
||||||
if storage.exists(key):
|
|
||||||
cached_artifacts.append({"url": url, "cached_url": storage.get_url(key), "key": key})
|
|
||||||
|
|
||||||
return {
|
|
||||||
"remote": remote,
|
|
||||||
"pattern": include_pattern,
|
|
||||||
"total_found": len(matching_urls),
|
|
||||||
"cached_count": len(cached_artifacts),
|
|
||||||
"artifacts": cached_artifacts,
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/metrics")
|
|
||||||
def get_metrics(
|
|
||||||
json: bool | None = Query(False, description="Return JSON format instead of Prometheus"),
|
|
||||||
):
|
|
||||||
"""Get comprehensive metrics about the artifact storage system"""
|
|
||||||
config._check_reload()
|
|
||||||
|
|
||||||
if json:
|
|
||||||
# Return JSON format
|
|
||||||
return metrics.get_metrics(storage, config)
|
|
||||||
else:
|
|
||||||
# Return Prometheus format
|
|
||||||
metrics.get_metrics(storage, config) # Update gauges
|
|
||||||
prometheus_data = generate_latest().decode("utf-8")
|
|
||||||
return PlainTextResponse(prometheus_data, media_type=CONTENT_TYPE_LATEST)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/config")
|
|
||||||
def get_config():
|
|
||||||
return config.config
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
from . import generic, helm, npm, python, rpm
|
||||||
|
from .base import get_content_type
|
||||||
|
|
||||||
|
__all__ = ["generic", "helm", "npm", "python", "rpm", "get_content_type"]
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
def get_content_type(filename: str) -> str:
|
||||||
|
if filename.endswith((".tar.gz", ".tgz")):
|
||||||
|
return "application/gzip"
|
||||||
|
if filename.endswith(".zip") or filename.endswith(".whl"):
|
||||||
|
return "application/zip"
|
||||||
|
if filename.endswith(".exe"):
|
||||||
|
return "application/x-msdownload"
|
||||||
|
if filename.endswith(".rpm"):
|
||||||
|
return "application/x-rpm"
|
||||||
|
if filename.endswith(".xml"):
|
||||||
|
return "application/xml"
|
||||||
|
if filename.endswith((".xml.gz", ".xml.bz2", ".xml.xz")):
|
||||||
|
return "application/gzip"
|
||||||
|
if filename.endswith((".yaml", ".yml")):
|
||||||
|
return "text/yaml"
|
||||||
|
return "application/octet-stream"
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .base import get_content_type
|
||||||
|
|
||||||
|
__all__ = ["get_content_type"]
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
from .base import get_content_type
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_content(
|
||||||
|
data: bytes,
|
||||||
|
path: str,
|
||||||
|
filename: str,
|
||||||
|
base_url: str,
|
||||||
|
proxy_url: str,
|
||||||
|
remote_name: str,
|
||||||
|
) -> tuple[bytes, str]:
|
||||||
|
if filename == "index.yaml":
|
||||||
|
data = data.replace(
|
||||||
|
base_url.encode(),
|
||||||
|
f"{proxy_url}/api/v1/remote/{remote_name}".encode(),
|
||||||
|
)
|
||||||
|
return data, "text/yaml"
|
||||||
|
return data, get_content_type(filename)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from .base import get_content_type
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_content(
|
||||||
|
data: bytes,
|
||||||
|
path: str,
|
||||||
|
filename: str,
|
||||||
|
immutable_patterns: list[str],
|
||||||
|
base_url: str,
|
||||||
|
proxy_url: str,
|
||||||
|
remote_name: str,
|
||||||
|
) -> tuple[bytes, str]:
|
||||||
|
if not any(re.search(p, path) for p in immutable_patterns):
|
||||||
|
data = data.replace(
|
||||||
|
base_url.encode(),
|
||||||
|
f"{proxy_url}/api/v1/remote/{remote_name}".encode(),
|
||||||
|
)
|
||||||
|
return data, "application/json"
|
||||||
|
return data, get_content_type(filename)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from .base import get_content_type
|
||||||
|
|
||||||
|
|
||||||
|
def construct_url(base_url: str, path: str) -> str:
|
||||||
|
"""Build the upstream URL for a PyPI request.
|
||||||
|
|
||||||
|
PyPI splits simple/ index pages (pypi.org) from file downloads
|
||||||
|
(files.pythonhosted.org), so simple/ requests are redirected to pypi.org.
|
||||||
|
"""
|
||||||
|
if base_url.rstrip("/") == "https://files.pythonhosted.org" and "simple/" in path:
|
||||||
|
return f"https://pypi.org/{path}"
|
||||||
|
return f"{base_url}/{path}"
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_content(
|
||||||
|
data: bytes,
|
||||||
|
path: str,
|
||||||
|
filename: str,
|
||||||
|
immutable_patterns: list[str],
|
||||||
|
base_url: str,
|
||||||
|
proxy_url: str,
|
||||||
|
remote_name: str,
|
||||||
|
) -> tuple[bytes, str]:
|
||||||
|
if not any(re.search(p, path) for p in immutable_patterns):
|
||||||
|
data = data.replace(
|
||||||
|
base_url.encode(),
|
||||||
|
f"{proxy_url}/api/v1/remote/{remote_name}".encode(),
|
||||||
|
)
|
||||||
|
return data, "text/html; charset=utf-8"
|
||||||
|
return data, get_content_type(filename)
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .base import get_content_type
|
||||||
|
|
||||||
|
__all__ = ["get_content_type"]
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .s3 import S3Storage
|
||||||
|
|
||||||
|
__all__ = ["S3Storage"]
|
||||||
@@ -41,7 +41,6 @@ class S3Storage:
|
|||||||
|
|
||||||
self.client = boto3.client("s3", **client_kwargs)
|
self.client = boto3.client("s3", **client_kwargs)
|
||||||
|
|
||||||
# Try to ensure bucket exists, but don't fail if MinIO isn't ready yet
|
|
||||||
try:
|
try:
|
||||||
self._ensure_bucket_exists()
|
self._ensure_bucket_exists()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -55,25 +54,21 @@ class S3Storage:
|
|||||||
self.client.create_bucket(Bucket=self.bucket)
|
self.client.create_bucket(Bucket=self.bucket)
|
||||||
|
|
||||||
def get_object_key(self, remote_name: str, path: str) -> str:
|
def get_object_key(self, remote_name: str, path: str) -> str:
|
||||||
# Extract directory path and filename
|
|
||||||
clean_path = path.lstrip("/")
|
clean_path = path.lstrip("/")
|
||||||
filename = os.path.basename(clean_path)
|
filename = os.path.basename(clean_path)
|
||||||
directory_path = os.path.dirname(clean_path)
|
directory_path = os.path.dirname(clean_path)
|
||||||
|
|
||||||
# Special handling for Docker registry blobs (use digest as key for deduplication)
|
# Docker blobs are keyed by digest for deduplication across images
|
||||||
if "/blobs/sha256:" in clean_path:
|
if "/blobs/sha256:" in clean_path:
|
||||||
# Extract the SHA256 digest for Docker blobs
|
|
||||||
parts = clean_path.split("/blobs/sha256:")
|
parts = clean_path.split("/blobs/sha256:")
|
||||||
if len(parts) == 2:
|
if len(parts) == 2:
|
||||||
digest = parts[1]
|
digest = parts[1]
|
||||||
return f"{remote_name}/blobs/sha256/{digest}"
|
return f"{remote_name}/blobs/sha256/{digest}"
|
||||||
|
|
||||||
# Hash the directory path to keep keys manageable while preserving remote structure
|
|
||||||
if directory_path:
|
if directory_path:
|
||||||
path_hash = hashlib.sha256(directory_path.encode()).hexdigest()[:16]
|
path_hash = hashlib.sha256(directory_path.encode()).hexdigest()[:16]
|
||||||
return f"{remote_name}/{path_hash}/{filename}"
|
return f"{remote_name}/{path_hash}/{filename}"
|
||||||
else:
|
else:
|
||||||
# If no directory, just use remote and filename
|
|
||||||
return f"{remote_name}/{filename}"
|
return f"{remote_name}/{filename}"
|
||||||
|
|
||||||
def exists(self, key: str) -> bool:
|
def exists(self, key: str) -> bool:
|
||||||
+64
-12
@@ -22,47 +22,99 @@ TEST_REMOTES = {
|
|||||||
"base_url": "https://dl-cdn.alpinelinux.org",
|
"base_url": "https://dl-cdn.alpinelinux.org",
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "alpine",
|
"package": "alpine",
|
||||||
"include_patterns": [".*/x86_64/.*\\.apk$"],
|
"immutable_patterns": [".*/x86_64/.*\\.apk$"],
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 3600},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 3600},
|
||||||
},
|
},
|
||||||
"rpm-test": {
|
"rpm-test": {
|
||||||
"base_url": "https://example.com/rpm",
|
"base_url": "https://example.com/rpm",
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "rpm",
|
"package": "rpm",
|
||||||
"include_patterns": [".*/x86_64/.*\\.rpm$", ".*/repodata/.*$"],
|
"immutable_patterns": [".*/x86_64/.*\\.rpm$", ".*/repodata/.*$"],
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 3600},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 3600},
|
||||||
},
|
},
|
||||||
"docker-test": {
|
"docker-test": {
|
||||||
"base_url": "https://registry.example.com",
|
"base_url": "https://registry.example.com",
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "docker",
|
"package": "docker",
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 300},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 300},
|
||||||
},
|
},
|
||||||
"docker-restricted": {
|
"docker-restricted": {
|
||||||
"base_url": "https://registry.example.com",
|
"base_url": "https://registry.example.com",
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "docker",
|
"package": "docker",
|
||||||
"include_patterns": ["^library/nginx"],
|
"immutable_patterns": ["^library/nginx"],
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 300},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 300},
|
||||||
},
|
},
|
||||||
"generic-test": {
|
"generic-test": {
|
||||||
"base_url": "https://releases.example.com",
|
"base_url": "https://releases.example.com",
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"include_patterns": [".*\\.tar\\.gz$"],
|
"immutable_patterns": [".*\\.tar\\.gz$"],
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 0},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 0},
|
||||||
},
|
},
|
||||||
"custom-index-test": {
|
"custom-index-test": {
|
||||||
"base_url": "https://example.com",
|
"base_url": "https://example.com",
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"index_patterns": ["metadata\\.json$"],
|
"mutable_patterns": ["metadata\\.json$"],
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 600},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 600},
|
||||||
|
},
|
||||||
|
"check-mutable-test": {
|
||||||
|
"base_url": "https://example.com",
|
||||||
|
"type": "remote",
|
||||||
|
"package": "generic",
|
||||||
|
"mutable_patterns": ["metadata\\.json$"],
|
||||||
|
"check_mutable_updates": True,
|
||||||
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 600},
|
||||||
},
|
},
|
||||||
"local-test": {
|
"local-test": {
|
||||||
"type": "local",
|
"type": "local",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 0},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 0},
|
||||||
|
},
|
||||||
|
"pypi-test": {
|
||||||
|
"base_url": "https://files.pythonhosted.org",
|
||||||
|
"type": "remote",
|
||||||
|
"package": "pypi",
|
||||||
|
"immutable_patterns": [
|
||||||
|
r"packages/.*\.whl$",
|
||||||
|
r"packages/.*\.whl\.metadata$",
|
||||||
|
r"packages/.*\.tar\.gz$",
|
||||||
|
],
|
||||||
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 600},
|
||||||
|
},
|
||||||
|
"npm-test": {
|
||||||
|
"base_url": "https://registry.npmjs.org",
|
||||||
|
"type": "remote",
|
||||||
|
"package": "npm",
|
||||||
|
"immutable_patterns": [r"\.tgz$"],
|
||||||
|
"mutable_patterns": [r"^(?!.*\.tgz$).*"],
|
||||||
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 600},
|
||||||
|
},
|
||||||
|
"helm-test": {
|
||||||
|
"base_url": "https://helm.releases.hashicorp.com",
|
||||||
|
"type": "remote",
|
||||||
|
"package": "helm",
|
||||||
|
"immutable_patterns": [r"\.tgz$"],
|
||||||
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 3600},
|
||||||
|
},
|
||||||
|
"quarantine-test": {
|
||||||
|
"base_url": "https://releases.example.com",
|
||||||
|
"type": "remote",
|
||||||
|
"package": "generic",
|
||||||
|
"immutable_patterns": [r".*\.tar\.gz$"],
|
||||||
|
"quarantine_new": True,
|
||||||
|
"quarantine_days": 3,
|
||||||
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 0},
|
||||||
|
},
|
||||||
|
"quarantine-disabled": {
|
||||||
|
"base_url": "https://releases.example.com",
|
||||||
|
"type": "remote",
|
||||||
|
"package": "generic",
|
||||||
|
"immutable_patterns": [r".*\.tar\.gz$"],
|
||||||
|
"quarantine_new": False,
|
||||||
|
"quarantine_days": 3,
|
||||||
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 0},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+153
-61
@@ -1,4 +1,4 @@
|
|||||||
"""Tests for RedisCache, focusing on is_index_file with configurable patterns."""
|
"""Tests for RedisCache, focusing on is_mutable_file with configurable patterns."""
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
from unittest.mock import ANY, MagicMock, patch
|
from unittest.mock import ANY, MagicMock, patch
|
||||||
@@ -6,7 +6,7 @@ from unittest.mock import ANY, MagicMock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from artifactapi.cache import RedisCache
|
from artifactapi.cache import RedisCache
|
||||||
from artifactapi.config import _PACKAGE_INDEX_PATTERNS
|
from artifactapi.config import _PACKAGE_MUTABLE_PATTERNS
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -38,139 +38,139 @@ def cache_with_redis(mock_redis_client):
|
|||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# is_index_file — alpine patterns
|
# is_mutable_file — alpine patterns
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class TestIsIndexFileAlpine:
|
class TestIsMutableFileAlpine:
|
||||||
def test_apkindex_tarball_is_index(self, bare_cache):
|
def test_apkindex_tarball_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["alpine"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["alpine"]
|
||||||
assert bare_cache.is_index_file("alpine/v3.18/x86_64/APKINDEX.tar.gz", patterns)
|
assert bare_cache.is_mutable_file("alpine/v3.18/x86_64/APKINDEX.tar.gz", patterns)
|
||||||
|
|
||||||
def test_nested_apkindex_is_index(self, bare_cache):
|
def test_nested_apkindex_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["alpine"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["alpine"]
|
||||||
assert bare_cache.is_index_file("mirrors/dl-cdn/alpine/v3.19/community/x86_64/APKINDEX.tar.gz", patterns)
|
assert bare_cache.is_mutable_file("mirrors/dl-cdn/alpine/v3.19/community/x86_64/APKINDEX.tar.gz", patterns)
|
||||||
|
|
||||||
def test_apk_package_is_not_index(self, bare_cache):
|
def test_apk_package_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["alpine"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["alpine"]
|
||||||
assert not bare_cache.is_index_file("alpine/v3.18/x86_64/musl-1.2.4-r2.apk", patterns)
|
assert not bare_cache.is_mutable_file("alpine/v3.18/x86_64/musl-1.2.4-r2.apk", patterns)
|
||||||
|
|
||||||
def test_random_tarball_is_not_index(self, bare_cache):
|
def test_random_tarball_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["alpine"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["alpine"]
|
||||||
assert not bare_cache.is_index_file("some/path/archive.tar.gz", patterns)
|
assert not bare_cache.is_mutable_file("some/path/archive.tar.gz", patterns)
|
||||||
|
|
||||||
def test_apkindex_signature_file_is_not_index(self, bare_cache):
|
def test_apkindex_signature_file_is_not_index(self, bare_cache):
|
||||||
# Signature file adjacent to the index should not be treated as an index
|
# Signature file adjacent to the index should not be treated as an index
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["alpine"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["alpine"]
|
||||||
assert not bare_cache.is_index_file("alpine/v3.18/x86_64/APKINDEX.tar.gz.sig", patterns)
|
assert not bare_cache.is_mutable_file("alpine/v3.18/x86_64/APKINDEX.tar.gz.sig", patterns)
|
||||||
|
|
||||||
def test_apkindex_tmp_file_is_not_index(self, bare_cache):
|
def test_apkindex_tmp_file_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["alpine"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["alpine"]
|
||||||
assert not bare_cache.is_index_file("alpine/v3.18/x86_64/APKINDEX.tar.gz.tmp", patterns)
|
assert not bare_cache.is_mutable_file("alpine/v3.18/x86_64/APKINDEX.tar.gz.tmp", patterns)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# is_index_file — rpm patterns
|
# is_mutable_file — rpm patterns
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class TestIsIndexFileRpm:
|
class TestIsMutableFileRpm:
|
||||||
def test_repomd_xml_is_index(self, bare_cache):
|
def test_repomd_xml_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert bare_cache.is_index_file("almalinux/9/x86_64/repomd.xml", patterns)
|
assert bare_cache.is_mutable_file("almalinux/9/x86_64/repomd.xml", patterns)
|
||||||
|
|
||||||
def test_repodata_primary_xml_gz_is_index(self, bare_cache):
|
def test_repodata_primary_xml_gz_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert bare_cache.is_index_file("repo/repodata/primary.xml.gz", patterns)
|
assert bare_cache.is_mutable_file("repo/repodata/primary.xml.gz", patterns)
|
||||||
|
|
||||||
def test_repodata_sqlite_is_index(self, bare_cache):
|
def test_repodata_sqlite_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert bare_cache.is_index_file("repo/repodata/primary.sqlite", patterns)
|
assert bare_cache.is_mutable_file("repo/repodata/primary.sqlite", patterns)
|
||||||
|
|
||||||
def test_repodata_sqlite_bz2_is_index(self, bare_cache):
|
def test_repodata_sqlite_bz2_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert bare_cache.is_index_file("repo/repodata/other.sqlite.bz2", patterns)
|
assert bare_cache.is_mutable_file("repo/repodata/other.sqlite.bz2", patterns)
|
||||||
|
|
||||||
def test_repodata_yaml_xz_is_index(self, bare_cache):
|
def test_repodata_yaml_xz_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert bare_cache.is_index_file("repo/repodata/comps.yaml.xz", patterns)
|
assert bare_cache.is_mutable_file("repo/repodata/comps.yaml.xz", patterns)
|
||||||
|
|
||||||
def test_packages_gz_pattern_matches_any_path(self, bare_cache):
|
def test_packages_gz_pattern_matches_any_path(self, bare_cache):
|
||||||
# The Packages.gz$ regex is a carryover from the original hardcoded logic and
|
# The Packages.gz$ regex is a carryover from the original hardcoded logic and
|
||||||
# deliberately matches any path ending in Packages.gz — including Debian-style paths.
|
# deliberately matches any path ending in Packages.gz — including Debian-style paths.
|
||||||
# This test documents that intentional behaviour.
|
# This test documents that intentional behaviour.
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert bare_cache.is_index_file("debian/dists/stable/main/binary-amd64/Packages.gz", patterns)
|
assert bare_cache.is_mutable_file("debian/dists/stable/main/binary-amd64/Packages.gz", patterns)
|
||||||
|
|
||||||
def test_rpm_package_is_not_index(self, bare_cache):
|
def test_rpm_package_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert not bare_cache.is_index_file("almalinux/9/x86_64/Packages/bash-5.1.8.x86_64.rpm", patterns)
|
assert not bare_cache.is_mutable_file("almalinux/9/x86_64/Packages/bash-5.1.8.x86_64.rpm", patterns)
|
||||||
|
|
||||||
def test_arbitrary_xml_outside_repodata_is_not_index(self, bare_cache):
|
def test_arbitrary_xml_outside_repodata_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["rpm"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["rpm"]
|
||||||
assert not bare_cache.is_index_file("some/path/config.xml", patterns)
|
assert not bare_cache.is_mutable_file("some/path/config.xml", patterns)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# is_index_file — docker patterns
|
# is_mutable_file — docker patterns
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class TestIsIndexFileDocker:
|
class TestIsMutableFileDocker:
|
||||||
def test_tag_manifest_is_index(self, bare_cache):
|
def test_tag_manifest_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
assert bare_cache.is_index_file("library/nginx/manifests/latest", patterns)
|
assert bare_cache.is_mutable_file("library/nginx/manifests/latest", patterns)
|
||||||
|
|
||||||
def test_version_tag_manifest_is_index(self, bare_cache):
|
def test_version_tag_manifest_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
assert bare_cache.is_index_file("library/nginx/manifests/1.25.3", patterns)
|
assert bare_cache.is_mutable_file("library/nginx/manifests/1.25.3", patterns)
|
||||||
|
|
||||||
def test_hyphenated_tag_manifest_is_index(self, bare_cache):
|
def test_hyphenated_tag_manifest_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
assert bare_cache.is_index_file("library/nginx/manifests/latest-rc", patterns)
|
assert bare_cache.is_mutable_file("library/nginx/manifests/latest-rc", patterns)
|
||||||
|
|
||||||
def test_numeric_date_tag_manifest_is_index(self, bare_cache):
|
def test_numeric_date_tag_manifest_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
assert bare_cache.is_index_file("library/nginx/manifests/20240101", patterns)
|
assert bare_cache.is_mutable_file("library/nginx/manifests/20240101", patterns)
|
||||||
|
|
||||||
def test_digest_manifest_is_not_index(self, bare_cache):
|
def test_digest_manifest_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
digest = "sha256:" + "a" * 64
|
digest = "sha256:" + "a" * 64
|
||||||
assert not bare_cache.is_index_file(f"library/nginx/manifests/{digest}", patterns)
|
assert not bare_cache.is_mutable_file(f"library/nginx/manifests/{digest}", patterns)
|
||||||
|
|
||||||
def test_tags_list_is_index(self, bare_cache):
|
def test_tags_list_is_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
assert bare_cache.is_index_file("library/nginx/tags/list", patterns)
|
assert bare_cache.is_mutable_file("library/nginx/tags/list", patterns)
|
||||||
|
|
||||||
def test_blob_is_not_index(self, bare_cache):
|
def test_blob_is_not_index(self, bare_cache):
|
||||||
patterns = _PACKAGE_INDEX_PATTERNS["docker"]
|
patterns = _PACKAGE_MUTABLE_PATTERNS["docker"]
|
||||||
assert not bare_cache.is_index_file("library/nginx/blobs/sha256:abc123", patterns)
|
assert not bare_cache.is_mutable_file("library/nginx/blobs/sha256:abc123", patterns)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# is_index_file — edge cases
|
# is_mutable_file — edge cases
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class TestIsIndexFileEdgeCases:
|
class TestIsMutableFileEdgeCases:
|
||||||
def test_empty_patterns_nothing_is_index(self, bare_cache):
|
def test_empty_patterns_nothing_is_index(self, bare_cache):
|
||||||
assert not bare_cache.is_index_file("APKINDEX.tar.gz", [])
|
assert not bare_cache.is_mutable_file("APKINDEX.tar.gz", [])
|
||||||
assert not bare_cache.is_index_file("repomd.xml", [])
|
assert not bare_cache.is_mutable_file("repomd.xml", [])
|
||||||
assert not bare_cache.is_index_file("library/nginx/manifests/latest", [])
|
assert not bare_cache.is_mutable_file("library/nginx/manifests/latest", [])
|
||||||
|
|
||||||
def test_none_patterns_nothing_is_index(self, bare_cache):
|
def test_none_patterns_nothing_is_index(self, bare_cache):
|
||||||
assert not bare_cache.is_index_file("APKINDEX.tar.gz", None)
|
assert not bare_cache.is_mutable_file("APKINDEX.tar.gz", None)
|
||||||
assert not bare_cache.is_index_file("repomd.xml", None)
|
assert not bare_cache.is_mutable_file("repomd.xml", None)
|
||||||
|
|
||||||
def test_custom_patterns_match(self, bare_cache):
|
def test_custom_patterns_match(self, bare_cache):
|
||||||
patterns = [r"metadata\.json$", r"index\.yaml$"]
|
patterns = [r"metadata\.json$", r"index\.yaml$"]
|
||||||
assert bare_cache.is_index_file("repo/metadata.json", patterns)
|
assert bare_cache.is_mutable_file("repo/metadata.json", patterns)
|
||||||
assert bare_cache.is_index_file("repo/subdir/index.yaml", patterns)
|
assert bare_cache.is_mutable_file("repo/subdir/index.yaml", patterns)
|
||||||
assert not bare_cache.is_index_file("repo/data.tar.gz", patterns)
|
assert not bare_cache.is_mutable_file("repo/data.tar.gz", patterns)
|
||||||
|
|
||||||
def test_custom_pattern_does_not_match_standard_index(self, bare_cache):
|
def test_custom_pattern_does_not_match_standard_index(self, bare_cache):
|
||||||
patterns = [r"metadata\.json$"]
|
patterns = [r"metadata\.json$"]
|
||||||
assert not bare_cache.is_index_file("APKINDEX.tar.gz", patterns)
|
assert not bare_cache.is_mutable_file("APKINDEX.tar.gz", patterns)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -235,3 +235,95 @@ class TestIndexValidity:
|
|||||||
# client is None when Redis is unavailable — setex cannot be called
|
# client is None when Redis is unavailable — setex cannot be called
|
||||||
assert unavailable_cache.client is None
|
assert unavailable_cache.client is None
|
||||||
unavailable_cache.mark_index_cached("remote", "some/path", 300) # must not raise
|
unavailable_cache.mark_index_cached("remote", "some/path", 300) # must not raise
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# mutable meta (ETag / Last-Modified storage)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestMutableMeta:
|
||||||
|
def test_meta_key_format(self, bare_cache):
|
||||||
|
path = "repo/metadata.json"
|
||||||
|
expected_hash = hashlib.sha256(path.encode()).hexdigest()[:16]
|
||||||
|
assert bare_cache.get_mutable_meta_key("myremote", path) == f"mutable:meta:myremote:{expected_hash}"
|
||||||
|
|
||||||
|
def test_meta_key_hash_is_16_chars(self, bare_cache):
|
||||||
|
key = bare_cache.get_mutable_meta_key("remote", "some/path/file.json")
|
||||||
|
assert len(key.split(":")[-1]) == 16
|
||||||
|
|
||||||
|
def test_store_and_retrieve_etag(self, cache_with_redis, mock_redis_client):
|
||||||
|
mock_redis_client.hgetall.return_value = {"etag": '"abc123"'}
|
||||||
|
cache_with_redis.store_mutable_meta("remote", "path/meta.json", '"abc123"', None)
|
||||||
|
mock_redis_client.hset.assert_called_once()
|
||||||
|
meta = cache_with_redis.get_mutable_meta("remote", "path/meta.json")
|
||||||
|
assert meta["etag"] == '"abc123"'
|
||||||
|
|
||||||
|
def test_store_and_retrieve_last_modified(self, cache_with_redis, mock_redis_client):
|
||||||
|
lm = "Mon, 01 Jan 2024 00:00:00 GMT"
|
||||||
|
mock_redis_client.hgetall.return_value = {"last_modified": lm}
|
||||||
|
cache_with_redis.store_mutable_meta("remote", "path/meta.json", None, lm)
|
||||||
|
meta = cache_with_redis.get_mutable_meta("remote", "path/meta.json")
|
||||||
|
assert meta["last_modified"] == lm
|
||||||
|
|
||||||
|
def test_store_no_op_when_both_none(self, cache_with_redis, mock_redis_client):
|
||||||
|
cache_with_redis.store_mutable_meta("remote", "path/meta.json", None, None)
|
||||||
|
mock_redis_client.hset.assert_not_called()
|
||||||
|
|
||||||
|
def test_store_no_op_when_unavailable(self, unavailable_cache):
|
||||||
|
unavailable_cache.store_mutable_meta("remote", "path", "etag", None) # must not raise
|
||||||
|
|
||||||
|
def test_get_returns_empty_when_unavailable(self, unavailable_cache):
|
||||||
|
assert unavailable_cache.get_mutable_meta("remote", "path") == {}
|
||||||
|
|
||||||
|
def test_delete_removes_meta_key(self, cache_with_redis, mock_redis_client):
|
||||||
|
expected_key = cache_with_redis.get_mutable_meta_key("remote", "path/meta.json")
|
||||||
|
cache_with_redis.delete_mutable_meta("remote", "path/meta.json")
|
||||||
|
mock_redis_client.delete.assert_called_once_with(expected_key)
|
||||||
|
|
||||||
|
def test_delete_no_op_when_unavailable(self, unavailable_cache):
|
||||||
|
unavailable_cache.delete_mutable_meta("remote", "path") # must not raise
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# artifact published date (quarantine support)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestArtifactPublished:
|
||||||
|
def test_key_format_is_deterministic(self, bare_cache):
|
||||||
|
path = "some/path/package-1.0.tar.gz"
|
||||||
|
expected_hash = hashlib.sha256(path.encode()).hexdigest()[:16]
|
||||||
|
assert bare_cache.get_artifact_published_key("myremote", path) == f"pkg:published:myremote:{expected_hash}"
|
||||||
|
|
||||||
|
def test_key_hash_is_16_chars(self, bare_cache):
|
||||||
|
key = bare_cache.get_artifact_published_key("remote", "path/to/file.whl")
|
||||||
|
assert len(key.split(":")[-1]) == 16
|
||||||
|
|
||||||
|
def test_different_paths_produce_different_keys(self, bare_cache):
|
||||||
|
k1 = bare_cache.get_artifact_published_key("remote", "pkg-1.0.tar.gz")
|
||||||
|
k2 = bare_cache.get_artifact_published_key("remote", "pkg-2.0.tar.gz")
|
||||||
|
assert k1 != k2
|
||||||
|
|
||||||
|
def test_store_calls_set_with_correct_value(self, cache_with_redis, mock_redis_client):
|
||||||
|
lm = "Mon, 01 Jan 2024 00:00:00 GMT"
|
||||||
|
cache_with_redis.store_artifact_published("remote", "path/pkg.tar.gz", lm)
|
||||||
|
expected_key = cache_with_redis.get_artifact_published_key("remote", "path/pkg.tar.gz")
|
||||||
|
mock_redis_client.set.assert_called_once_with(expected_key, lm)
|
||||||
|
|
||||||
|
def test_get_returns_stored_value(self, cache_with_redis, mock_redis_client):
|
||||||
|
lm = "Tue, 15 Mar 2022 12:00:00 GMT"
|
||||||
|
mock_redis_client.get.return_value = lm
|
||||||
|
result = cache_with_redis.get_artifact_published("remote", "path/pkg.tar.gz")
|
||||||
|
assert result == lm
|
||||||
|
|
||||||
|
def test_get_returns_none_when_not_stored(self, cache_with_redis, mock_redis_client):
|
||||||
|
mock_redis_client.get.return_value = None
|
||||||
|
result = cache_with_redis.get_artifact_published("remote", "path/pkg.tar.gz")
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
def test_store_no_op_when_unavailable(self, unavailable_cache):
|
||||||
|
unavailable_cache.store_artifact_published("remote", "path", "Mon, 01 Jan 2024 00:00:00 GMT")
|
||||||
|
|
||||||
|
def test_get_returns_none_when_unavailable(self, unavailable_cache):
|
||||||
|
assert unavailable_cache.get_artifact_published("remote", "path") is None
|
||||||
|
|||||||
+309
-38
@@ -1,4 +1,4 @@
|
|||||||
"""Tests for ConfigManager, focusing on get_index_patterns (new logic)."""
|
"""Tests for ConfigManager, focusing on get_mutable_patterns and get_immutable_patterns."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -21,45 +21,44 @@ def make_config(tmp_path):
|
|||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# get_index_patterns
|
# get_mutable_patterns
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class TestGetIndexPatterns:
|
class TestGetMutablePatterns:
|
||||||
def test_alpine_returns_package_defaults(self, make_config):
|
def test_alpine_returns_package_defaults(self, make_config):
|
||||||
cfg = make_config({"r": {"type": "remote", "package": "alpine", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "package": "alpine", "base_url": "https://x.com"}})
|
||||||
patterns = cfg.get_index_patterns("r")
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
# Assert against literal strings, not the live constant, so a rename doesn't mask a regression
|
|
||||||
assert r"APKINDEX\.tar\.gz$" in patterns
|
assert r"APKINDEX\.tar\.gz$" in patterns
|
||||||
|
|
||||||
def test_rpm_returns_package_defaults(self, make_config):
|
def test_rpm_returns_package_defaults(self, make_config):
|
||||||
cfg = make_config({"r": {"type": "remote", "package": "rpm", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "package": "rpm", "base_url": "https://x.com"}})
|
||||||
patterns = cfg.get_index_patterns("r")
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
assert r"repomd\.xml$" in patterns
|
assert r"repomd\.xml$" in patterns
|
||||||
assert any("repodata" in p for p in patterns)
|
assert any("repodata" in p for p in patterns)
|
||||||
|
|
||||||
def test_docker_returns_package_defaults(self, make_config):
|
def test_docker_returns_package_defaults(self, make_config):
|
||||||
cfg = make_config({"r": {"type": "remote", "package": "docker", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "package": "docker", "base_url": "https://x.com"}})
|
||||||
patterns = cfg.get_index_patterns("r")
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
assert any("manifests" in p for p in patterns)
|
assert any("manifests" in p for p in patterns)
|
||||||
assert any("tags/list" in p for p in patterns)
|
assert any("tags/list" in p for p in patterns)
|
||||||
|
|
||||||
def test_generic_returns_empty_list(self, make_config):
|
def test_generic_returns_empty_list(self, make_config):
|
||||||
cfg = make_config({"r": {"type": "remote", "package": "generic", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "package": "generic", "base_url": "https://x.com"}})
|
||||||
assert cfg.get_index_patterns("r") == []
|
assert cfg.get_mutable_patterns("r") == []
|
||||||
|
|
||||||
def test_unknown_remote_returns_empty_list(self, make_config):
|
def test_unknown_remote_returns_empty_list(self, make_config):
|
||||||
cfg = make_config({})
|
cfg = make_config({})
|
||||||
assert cfg.get_index_patterns("nonexistent") == []
|
assert cfg.get_mutable_patterns("nonexistent") == []
|
||||||
|
|
||||||
def test_missing_package_field_defaults_to_generic(self, make_config):
|
def test_missing_package_field_defaults_to_generic(self, make_config):
|
||||||
cfg = make_config({"r": {"type": "remote", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "base_url": "https://x.com"}})
|
||||||
assert cfg.get_index_patterns("r") == []
|
assert cfg.get_mutable_patterns("r") == []
|
||||||
|
|
||||||
def test_unknown_package_type_returns_empty_list(self, make_config):
|
def test_unknown_package_type_returns_empty_list(self, make_config):
|
||||||
# A mis-spelled package type silently returns [] — this is a known footgun
|
# A mis-spelled package type silently returns [] — this is a known footgun
|
||||||
cfg = make_config({"r": {"type": "remote", "package": "deb", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "package": "deb", "base_url": "https://x.com"}})
|
||||||
assert cfg.get_index_patterns("r") == []
|
assert cfg.get_mutable_patterns("r") == []
|
||||||
|
|
||||||
def test_extra_patterns_appended_after_defaults(self, make_config):
|
def test_extra_patterns_appended_after_defaults(self, make_config):
|
||||||
cfg = make_config(
|
cfg = make_config(
|
||||||
@@ -68,11 +67,11 @@ class TestGetIndexPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "alpine",
|
"package": "alpine",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"index_patterns": [r"custom\.json$"],
|
"mutable_patterns": [r"custom\.json$"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
patterns = cfg.get_index_patterns("r")
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
assert r"APKINDEX\.tar\.gz$" in patterns
|
assert r"APKINDEX\.tar\.gz$" in patterns
|
||||||
assert r"custom\.json$" in patterns
|
assert r"custom\.json$" in patterns
|
||||||
# Defaults come first
|
# Defaults come first
|
||||||
@@ -85,11 +84,11 @@ class TestGetIndexPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "alpine",
|
"package": "alpine",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"index_patterns": [],
|
"mutable_patterns": [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert r"APKINDEX\.tar\.gz$" in cfg.get_index_patterns("r")
|
assert r"APKINDEX\.tar\.gz$" in cfg.get_mutable_patterns("r")
|
||||||
|
|
||||||
def test_duplicate_extra_pattern_not_added_twice(self, make_config):
|
def test_duplicate_extra_pattern_not_added_twice(self, make_config):
|
||||||
existing = r"APKINDEX\.tar\.gz$"
|
existing = r"APKINDEX\.tar\.gz$"
|
||||||
@@ -99,11 +98,11 @@ class TestGetIndexPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "alpine",
|
"package": "alpine",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"index_patterns": [existing],
|
"mutable_patterns": [existing],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
patterns = cfg.get_index_patterns("r")
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
assert patterns.count(existing) == 1
|
assert patterns.count(existing) == 1
|
||||||
|
|
||||||
def test_generic_with_only_extra_patterns(self, make_config):
|
def test_generic_with_only_extra_patterns(self, make_config):
|
||||||
@@ -113,11 +112,11 @@ class TestGetIndexPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"index_patterns": [r"meta\.json$", r"index\.yaml$"],
|
"mutable_patterns": [r"meta\.json$", r"index\.yaml$"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert cfg.get_index_patterns("r") == [r"meta\.json$", r"index\.yaml$"]
|
assert cfg.get_mutable_patterns("r") == [r"meta\.json$", r"index\.yaml$"]
|
||||||
|
|
||||||
def test_rpm_extra_patterns_merged(self, make_config):
|
def test_rpm_extra_patterns_merged(self, make_config):
|
||||||
cfg = make_config(
|
cfg = make_config(
|
||||||
@@ -126,41 +125,93 @@ class TestGetIndexPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "rpm",
|
"package": "rpm",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"index_patterns": [r"custom-meta\.xml$"],
|
"mutable_patterns": [r"custom-meta\.xml$"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
patterns = cfg.get_index_patterns("r")
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
assert r"repomd\.xml$" in patterns
|
assert r"repomd\.xml$" in patterns
|
||||||
assert r"custom-meta\.xml$" in patterns
|
assert r"custom-meta\.xml$" in patterns
|
||||||
|
|
||||||
|
def test_npm_has_no_package_defaults(self, make_config):
|
||||||
|
cfg = make_config({"r": {"type": "remote", "package": "npm", "base_url": "https://x.com"}})
|
||||||
|
assert cfg.get_mutable_patterns("r") == []
|
||||||
|
|
||||||
|
def test_npm_explicit_mutable_pattern_matches_metadata(self, make_config):
|
||||||
|
import re
|
||||||
|
|
||||||
|
cfg = make_config(
|
||||||
|
{
|
||||||
|
"r": {
|
||||||
|
"type": "remote",
|
||||||
|
"package": "npm",
|
||||||
|
"base_url": "https://x.com",
|
||||||
|
"mutable_patterns": [r"^(?!.*\.tgz$).*"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
|
assert any(re.search(p, "express") for p in patterns)
|
||||||
|
assert any(re.search(p, "@babel/core") for p in patterns)
|
||||||
|
|
||||||
|
def test_helm_returns_index_yaml_as_mutable(self, make_config):
|
||||||
|
cfg = make_config({"r": {"type": "remote", "package": "helm", "base_url": "https://helm.example.com"}})
|
||||||
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
|
assert r"index\.yaml$" in patterns
|
||||||
|
|
||||||
|
def test_helm_chart_tarballs_not_mutable_by_default(self, make_config):
|
||||||
|
import re
|
||||||
|
|
||||||
|
cfg = make_config({"r": {"type": "remote", "package": "helm", "base_url": "https://helm.example.com"}})
|
||||||
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
|
# Only index.yaml is mutable; .tgz chart tarballs are not
|
||||||
|
assert not any(re.search(p, "vault-0.29.1.tgz") for p in patterns)
|
||||||
|
assert not any(re.search(p, "consul-1.5.0.tgz") for p in patterns)
|
||||||
|
|
||||||
|
def test_npm_explicit_mutable_pattern_excludes_tarballs(self, make_config):
|
||||||
|
import re
|
||||||
|
|
||||||
|
cfg = make_config(
|
||||||
|
{
|
||||||
|
"r": {
|
||||||
|
"type": "remote",
|
||||||
|
"package": "npm",
|
||||||
|
"base_url": "https://x.com",
|
||||||
|
"mutable_patterns": [r"^(?!.*\.tgz$).*"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
patterns = cfg.get_mutable_patterns("r")
|
||||||
|
assert not any(re.search(p, "express-4.18.2.tgz") for p in patterns)
|
||||||
|
assert not any(re.search(p, "express/-/express-4.18.2.tgz") for p in patterns)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# get_repository_patterns
|
# get_immutable_patterns
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class TestGetRepositoryPatterns:
|
class TestGetImmutablePatterns:
|
||||||
def test_returns_include_patterns(self, make_config):
|
def test_returns_immutable_patterns(self, make_config):
|
||||||
cfg = make_config(
|
cfg = make_config(
|
||||||
{
|
{
|
||||||
"r": {
|
"r": {
|
||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"include_patterns": [r".*\.tar\.gz$"],
|
"immutable_patterns": [r".*\.tar\.gz$"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert cfg.get_repository_patterns("r", "") == [r".*\.tar\.gz$"]
|
assert cfg.get_immutable_patterns("r") == [r".*\.tar\.gz$"]
|
||||||
|
|
||||||
def test_returns_empty_for_missing_remote(self, make_config):
|
def test_returns_empty_for_missing_remote(self, make_config):
|
||||||
cfg = make_config({})
|
cfg = make_config({})
|
||||||
assert cfg.get_repository_patterns("nonexistent", "") == []
|
assert cfg.get_immutable_patterns("nonexistent") == []
|
||||||
|
|
||||||
def test_returns_empty_when_no_patterns_configured(self, make_config):
|
def test_returns_empty_when_no_patterns_configured(self, make_config):
|
||||||
cfg = make_config({"r": {"type": "remote", "package": "generic", "base_url": "https://x.com"}})
|
cfg = make_config({"r": {"type": "remote", "package": "generic", "base_url": "https://x.com"}})
|
||||||
assert cfg.get_repository_patterns("r", "") == []
|
assert cfg.get_immutable_patterns("r") == []
|
||||||
|
|
||||||
def test_multiple_patterns_returned(self, make_config):
|
def test_multiple_patterns_returned(self, make_config):
|
||||||
patterns = [r".*\.rpm$", r".*/repodata/.*$"]
|
patterns = [r".*\.rpm$", r".*/repodata/.*$"]
|
||||||
@@ -170,11 +221,11 @@ class TestGetRepositoryPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "rpm",
|
"package": "rpm",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"include_patterns": patterns,
|
"immutable_patterns": patterns,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert cfg.get_repository_patterns("r", "") == patterns
|
assert cfg.get_immutable_patterns("r") == patterns
|
||||||
|
|
||||||
def test_dict_keyed_repositories_returns_per_repo_patterns(self, make_config):
|
def test_dict_keyed_repositories_returns_per_repo_patterns(self, make_config):
|
||||||
cfg = make_config(
|
cfg = make_config(
|
||||||
@@ -183,14 +234,14 @@ class TestGetRepositoryPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"include_patterns": [r".*\.tar\.gz$"],
|
"immutable_patterns": [r".*\.tar\.gz$"],
|
||||||
"repositories": {
|
"repositories": {
|
||||||
"/path/to/repo": {"include_patterns": [r".*\.rpm$"]},
|
"/path/to/repo": {"immutable_patterns": [r".*\.rpm$"]},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert cfg.get_repository_patterns("r", "/path/to/repo") == [r".*\.rpm$"]
|
assert cfg.get_immutable_patterns("r", "/path/to/repo") == [r".*\.rpm$"]
|
||||||
|
|
||||||
def test_dict_keyed_repositories_falls_back_to_remote_patterns(self, make_config):
|
def test_dict_keyed_repositories_falls_back_to_remote_patterns(self, make_config):
|
||||||
cfg = make_config(
|
cfg = make_config(
|
||||||
@@ -199,14 +250,47 @@ class TestGetRepositoryPatterns:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"include_patterns": [r".*\.tar\.gz$"],
|
"immutable_patterns": [r".*\.tar\.gz$"],
|
||||||
"repositories": {
|
"repositories": {
|
||||||
"/path/to/repo": {"include_patterns": [r".*\.rpm$"]},
|
"/path/to/repo": {"immutable_patterns": [r".*\.rpm$"]},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert cfg.get_repository_patterns("r", "/unknown/path") == [r".*\.tar\.gz$"]
|
assert cfg.get_immutable_patterns("r", "/unknown/path") == [r".*\.tar\.gz$"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# get_user_mutable_patterns
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetUserMutablePatterns:
|
||||||
|
def test_returns_only_user_patterns(self, make_config):
|
||||||
|
cfg = make_config(
|
||||||
|
{
|
||||||
|
"r": {
|
||||||
|
"type": "remote",
|
||||||
|
"package": "alpine",
|
||||||
|
"base_url": "https://x.com",
|
||||||
|
"mutable_patterns": [r"custom\.json$"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assert cfg.get_user_mutable_patterns("r") == [r"custom\.json$"]
|
||||||
|
|
||||||
|
def test_excludes_package_defaults(self, make_config):
|
||||||
|
# Package defaults (APKINDEX etc.) must NOT appear here
|
||||||
|
cfg = make_config({"r": {"type": "remote", "package": "alpine", "base_url": "https://x.com"}})
|
||||||
|
assert cfg.get_user_mutable_patterns("r") == []
|
||||||
|
|
||||||
|
def test_returns_empty_for_missing_remote(self, make_config):
|
||||||
|
cfg = make_config({})
|
||||||
|
assert cfg.get_user_mutable_patterns("nonexistent") == []
|
||||||
|
|
||||||
|
def test_returns_empty_when_key_absent(self, make_config):
|
||||||
|
cfg = make_config({"r": {"type": "remote", "package": "generic", "base_url": "https://x.com"}})
|
||||||
|
assert cfg.get_user_mutable_patterns("r") == []
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -222,11 +306,11 @@ class TestGetCacheConfig:
|
|||||||
"type": "remote",
|
"type": "remote",
|
||||||
"package": "generic",
|
"package": "generic",
|
||||||
"base_url": "https://x.com",
|
"base_url": "https://x.com",
|
||||||
"cache": {"file_ttl": 0, "index_ttl": 7200},
|
"cache": {"immutable_ttl": 0, "mutable_ttl": 7200},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert cfg.get_cache_config("r") == {"file_ttl": 0, "index_ttl": 7200}
|
assert cfg.get_cache_config("r") == {"immutable_ttl": 0, "mutable_ttl": 7200}
|
||||||
|
|
||||||
def test_returns_empty_dict_for_missing_remote(self, make_config):
|
def test_returns_empty_dict_for_missing_remote(self, make_config):
|
||||||
cfg = make_config({})
|
cfg = make_config({})
|
||||||
@@ -267,3 +351,190 @@ class TestConfigReload:
|
|||||||
cfg._check_reload()
|
cfg._check_reload()
|
||||||
|
|
||||||
assert "repo-a" in cfg.config["remotes"]
|
assert "repo-a" in cfg.config["remotes"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# get_quarantine_config
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetQuarantineConfig:
|
||||||
|
def test_returns_false_zero_when_not_configured(self, make_config):
|
||||||
|
cfg = make_config({"r": {"type": "remote", "package": "generic", "base_url": "https://x.com"}})
|
||||||
|
enabled, days = cfg.get_quarantine_config("r")
|
||||||
|
assert enabled is False
|
||||||
|
assert days == 0
|
||||||
|
|
||||||
|
def test_returns_false_zero_for_missing_remote(self, make_config):
|
||||||
|
cfg = make_config({})
|
||||||
|
enabled, days = cfg.get_quarantine_config("nonexistent")
|
||||||
|
assert enabled is False
|
||||||
|
assert days == 0
|
||||||
|
|
||||||
|
def test_enabled_true_and_days_returned(self, make_config):
|
||||||
|
cfg = make_config(
|
||||||
|
{
|
||||||
|
"r": {
|
||||||
|
"type": "remote",
|
||||||
|
"package": "generic",
|
||||||
|
"base_url": "https://x.com",
|
||||||
|
"quarantine_new": True,
|
||||||
|
"quarantine_days": 7,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
enabled, days = cfg.get_quarantine_config("r")
|
||||||
|
assert enabled is True
|
||||||
|
assert days == 7
|
||||||
|
|
||||||
|
def test_quarantine_new_false_returns_disabled(self, make_config):
|
||||||
|
cfg = make_config(
|
||||||
|
{
|
||||||
|
"r": {
|
||||||
|
"type": "remote",
|
||||||
|
"package": "generic",
|
||||||
|
"base_url": "https://x.com",
|
||||||
|
"quarantine_new": False,
|
||||||
|
"quarantine_days": 7,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
enabled, days = cfg.get_quarantine_config("r")
|
||||||
|
assert enabled is False
|
||||||
|
assert days == 7
|
||||||
|
|
||||||
|
def test_enabled_with_zero_days_returns_zero(self, make_config):
|
||||||
|
cfg = make_config(
|
||||||
|
{
|
||||||
|
"r": {
|
||||||
|
"type": "remote",
|
||||||
|
"package": "generic",
|
||||||
|
"base_url": "https://x.com",
|
||||||
|
"quarantine_new": True,
|
||||||
|
"quarantine_days": 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
enabled, days = cfg.get_quarantine_config("r")
|
||||||
|
assert enabled is True
|
||||||
|
assert days == 0
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Directory mode (CONFIG_PATH points to a directory)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def _remote(base_url: str = "https://x.com") -> dict:
|
||||||
|
return {"type": "remote", "package": "generic", "base_url": base_url}
|
||||||
|
|
||||||
|
|
||||||
|
class TestConfigDirMode:
|
||||||
|
def test_loads_all_yaml_files(self, tmp_path):
|
||||||
|
(tmp_path / "a.yaml").write_text(yaml.dump({"remotes": {"repo-a": _remote()}}))
|
||||||
|
(tmp_path / "b.yaml").write_text(yaml.dump({"remotes": {"repo-b": _remote("https://y.com")}}))
|
||||||
|
cfg = ConfigManager(str(tmp_path))
|
||||||
|
assert "repo-a" in cfg.config["remotes"]
|
||||||
|
assert "repo-b" in cfg.config["remotes"]
|
||||||
|
|
||||||
|
def test_later_file_overrides_earlier_on_same_key(self, tmp_path):
|
||||||
|
(tmp_path / "a.yaml").write_text(yaml.dump({"remotes": {"r": _remote("https://first.com")}}))
|
||||||
|
(tmp_path / "b.yaml").write_text(yaml.dump({"remotes": {"r": _remote("https://second.com")}}))
|
||||||
|
cfg = ConfigManager(str(tmp_path))
|
||||||
|
assert cfg.config["remotes"]["r"]["base_url"] == "https://second.com"
|
||||||
|
|
||||||
|
def test_empty_directory_returns_empty_remotes(self, tmp_path):
|
||||||
|
cfg = ConfigManager(str(tmp_path))
|
||||||
|
assert cfg.config == {"remotes": {}}
|
||||||
|
|
||||||
|
def test_ignores_non_yaml_files(self, tmp_path):
|
||||||
|
(tmp_path / "notes.txt").write_text("not yaml")
|
||||||
|
(tmp_path / "a.yaml").write_text(yaml.dump({"remotes": {"repo-a": _remote()}}))
|
||||||
|
cfg = ConfigManager(str(tmp_path))
|
||||||
|
assert list(cfg.config["remotes"].keys()) == ["repo-a"]
|
||||||
|
|
||||||
|
def test_reload_picks_up_new_file(self, tmp_path):
|
||||||
|
(tmp_path / "a.yaml").write_text(yaml.dump({"remotes": {"repo-a": _remote()}}))
|
||||||
|
cfg = ConfigManager(str(tmp_path))
|
||||||
|
assert "repo-a" in cfg.config["remotes"]
|
||||||
|
assert "repo-b" not in cfg.config["remotes"]
|
||||||
|
|
||||||
|
new_file = tmp_path / "b.yaml"
|
||||||
|
new_file.write_text(yaml.dump({"remotes": {"repo-b": _remote("https://y.com")}}))
|
||||||
|
future_mtime = cfg._last_modified + 1
|
||||||
|
os.utime(str(new_file), (future_mtime, future_mtime))
|
||||||
|
|
||||||
|
cfg._check_reload()
|
||||||
|
|
||||||
|
assert "repo-a" in cfg.config["remotes"]
|
||||||
|
assert "repo-b" in cfg.config["remotes"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# config_dir key (main file contains a config_dir pointer)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestConfigDirKey:
|
||||||
|
def test_merges_remotes_from_config_dir(self, tmp_path):
|
||||||
|
conf_d = tmp_path / "conf.d"
|
||||||
|
conf_d.mkdir()
|
||||||
|
(conf_d / "remotes.yaml").write_text(yaml.dump({"remotes": {"repo-extra": _remote("https://extra.com")}}))
|
||||||
|
main = tmp_path / "config.yaml"
|
||||||
|
main.write_text(yaml.dump({"config_dir": str(conf_d), "remotes": {"repo-main": _remote()}}))
|
||||||
|
cfg = ConfigManager(str(main))
|
||||||
|
assert "repo-main" in cfg.config["remotes"]
|
||||||
|
assert "repo-extra" in cfg.config["remotes"]
|
||||||
|
|
||||||
|
def test_relative_config_dir_resolved_from_main_file(self, tmp_path):
|
||||||
|
conf_d = tmp_path / "conf.d"
|
||||||
|
conf_d.mkdir()
|
||||||
|
(conf_d / "r.yaml").write_text(yaml.dump({"remotes": {"repo-a": _remote()}}))
|
||||||
|
main = tmp_path / "config.yaml"
|
||||||
|
main.write_text(yaml.dump({"config_dir": "conf.d", "remotes": {}}))
|
||||||
|
cfg = ConfigManager(str(main))
|
||||||
|
assert "repo-a" in cfg.config["remotes"]
|
||||||
|
|
||||||
|
def test_config_dir_key_not_present_in_loaded_config(self, tmp_path):
|
||||||
|
conf_d = tmp_path / "conf.d"
|
||||||
|
conf_d.mkdir()
|
||||||
|
main = tmp_path / "config.yaml"
|
||||||
|
main.write_text(yaml.dump({"config_dir": str(conf_d), "remotes": {}}))
|
||||||
|
cfg = ConfigManager(str(main))
|
||||||
|
assert "config_dir" not in cfg.config
|
||||||
|
|
||||||
|
def test_dir_remote_overrides_main_file_remote(self, tmp_path):
|
||||||
|
conf_d = tmp_path / "conf.d"
|
||||||
|
conf_d.mkdir()
|
||||||
|
(conf_d / "override.yaml").write_text(yaml.dump({"remotes": {"r": _remote("https://new.com")}}))
|
||||||
|
main = tmp_path / "config.yaml"
|
||||||
|
main.write_text(yaml.dump({"config_dir": str(conf_d), "remotes": {"r": _remote("https://old.com")}}))
|
||||||
|
cfg = ConfigManager(str(main))
|
||||||
|
assert cfg.config["remotes"]["r"]["base_url"] == "https://new.com"
|
||||||
|
|
||||||
|
def test_empty_config_dir_uses_main_file_only(self, tmp_path):
|
||||||
|
conf_d = tmp_path / "conf.d"
|
||||||
|
conf_d.mkdir()
|
||||||
|
main = tmp_path / "config.yaml"
|
||||||
|
main.write_text(yaml.dump({"config_dir": str(conf_d), "remotes": {"repo-main": _remote()}}))
|
||||||
|
cfg = ConfigManager(str(main))
|
||||||
|
assert list(cfg.config["remotes"].keys()) == ["repo-main"]
|
||||||
|
|
||||||
|
def test_reload_picks_up_changed_dir_file(self, tmp_path):
|
||||||
|
conf_d = tmp_path / "conf.d"
|
||||||
|
conf_d.mkdir()
|
||||||
|
dir_file = conf_d / "r.yaml"
|
||||||
|
dir_file.write_text(yaml.dump({"remotes": {"repo-v1": _remote()}}))
|
||||||
|
main = tmp_path / "config.yaml"
|
||||||
|
main.write_text(yaml.dump({"config_dir": str(conf_d), "remotes": {}}))
|
||||||
|
cfg = ConfigManager(str(main))
|
||||||
|
assert "repo-v1" in cfg.config["remotes"]
|
||||||
|
|
||||||
|
dir_file.write_text(yaml.dump({"remotes": {"repo-v2": _remote("https://v2.com")}}))
|
||||||
|
future_mtime = cfg._last_modified + 1
|
||||||
|
os.utime(str(dir_file), (future_mtime, future_mtime))
|
||||||
|
|
||||||
|
cfg._check_reload()
|
||||||
|
|
||||||
|
assert "repo-v2" in cfg.config["remotes"]
|
||||||
|
assert "repo-v1" not in cfg.config["remotes"]
|
||||||
|
|||||||
+555
-34
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
from datetime import UTC
|
||||||
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -25,7 +26,7 @@ def mock_storage():
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_cache():
|
def mock_cache():
|
||||||
m = MagicMock()
|
m = MagicMock()
|
||||||
m.is_index_file.return_value = False
|
m.is_mutable_file.return_value = False
|
||||||
m.is_index_valid.return_value = True
|
m.is_index_valid.return_value = True
|
||||||
m.available = False
|
m.available = False
|
||||||
m.client = None
|
m.client = None
|
||||||
@@ -123,7 +124,7 @@ class TestDockerProxy:
|
|||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
deps["cache"].is_index_valid.return_value = True
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
response = client.get("/v2/docker-restricted/library/nginx/manifests/latest")
|
response = client.get("/v2/docker-restricted/library/nginx/manifests/latest")
|
||||||
@@ -140,7 +141,7 @@ class TestDockerProxy:
|
|||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
deps["cache"].is_index_valid.return_value = True
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
response = client.get("/v2/docker-test/library/nginx/manifests/latest")
|
response = client.get("/v2/docker-test/library/nginx/manifests/latest")
|
||||||
@@ -158,7 +159,7 @@ class TestDockerProxy:
|
|||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
deps["cache"].is_index_valid.return_value = True
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
response = client.get("/v2/docker-test/library/nginx/manifests/latest")
|
response = client.get("/v2/docker-test/library/nginx/manifests/latest")
|
||||||
@@ -170,7 +171,7 @@ class TestDockerProxy:
|
|||||||
manifest = json.dumps({"mediaType": "application/vnd.oci.image.manifest.v1+json", "layers": []}).encode()
|
manifest = json.dumps({"mediaType": "application/vnd.oci.image.manifest.v1+json", "layers": []}).encode()
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
client.get("/v2/docker-test/library/nginx/manifests/latest")
|
client.get("/v2/docker-test/library/nginx/manifests/latest")
|
||||||
deps["metrics"].record_cache_hit.assert_called_once_with("docker-test", ANY)
|
deps["metrics"].record_cache_hit.assert_called_once_with("docker-test", ANY)
|
||||||
@@ -185,7 +186,7 @@ class TestDockerProxy:
|
|||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
response = client.head("/v2/docker-test/library/nginx/manifests/latest")
|
response = client.head("/v2/docker-test/library/nginx/manifests/latest")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -201,10 +202,10 @@ class TestDockerProxy:
|
|||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = False
|
deps["storage"].exists.return_value = False
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "cached"},
|
return_value={"status": "cached"},
|
||||||
) as mock_fetch:
|
) as mock_fetch:
|
||||||
@@ -223,10 +224,10 @@ class TestDockerProxy:
|
|||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = False
|
deps["storage"].exists.return_value = False
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "cached"},
|
return_value={"status": "cached"},
|
||||||
):
|
):
|
||||||
@@ -244,12 +245,13 @@ class TestDockerProxy:
|
|||||||
}
|
}
|
||||||
).encode()
|
).encode()
|
||||||
deps["storage"].exists.return_value = True # cached in S3
|
deps["storage"].exists.return_value = True # cached in S3
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
deps["cache"].is_index_valid.return_value = False # but TTL expired
|
deps["cache"].is_index_valid.return_value = False # but TTL expired
|
||||||
deps["storage"].download_object.return_value = manifest
|
deps["storage"].download_object.return_value = manifest
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy._upstream_reachable", new_callable=AsyncMock, return_value=True):
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "cached"},
|
return_value={"status": "cached"},
|
||||||
) as mock_fetch:
|
) as mock_fetch:
|
||||||
@@ -278,7 +280,7 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"tar content"
|
deps["storage"].download_object.return_value = b"tar content"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
response = client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
response = client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -289,7 +291,7 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"content"
|
deps["storage"].download_object.return_value = b"content"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
response = client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
response = client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
||||||
disposition = response.headers["content-disposition"]
|
disposition = response.headers["content-disposition"]
|
||||||
@@ -301,7 +303,7 @@ class TestGenericArtifactRoute:
|
|||||||
content = b"some artifact content bytes"
|
content = b"some artifact content bytes"
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = content
|
deps["storage"].download_object.return_value = content
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
response = client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
response = client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
||||||
assert response.headers["X-Artifact-Size"] == str(len(content))
|
assert response.headers["X-Artifact-Size"] == str(len(content))
|
||||||
@@ -310,7 +312,7 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"content"
|
deps["storage"].download_object.return_value = b"content"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
||||||
deps["metrics"].record_cache_hit.assert_called_once_with("generic-test", ANY)
|
deps["metrics"].record_cache_hit.assert_called_once_with("generic-test", ANY)
|
||||||
@@ -319,7 +321,7 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"content"
|
deps["storage"].download_object.return_value = b"content"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
client.get("/api/v1/remote/generic-test/some/path/archive.tar.gz")
|
||||||
deps["database"].record_artifact_mapping.assert_called_once()
|
deps["database"].record_artifact_mapping.assert_called_once()
|
||||||
@@ -328,7 +330,7 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"rpm bytes"
|
deps["storage"].download_object.return_value = b"rpm bytes"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
response = client.get("/api/v1/remote/rpm-test/almalinux/9/x86_64/bash-5.1.8.x86_64.rpm")
|
response = client.get("/api/v1/remote/rpm-test/almalinux/9/x86_64/bash-5.1.8.x86_64.rpm")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -338,7 +340,7 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"<?xml version='1.0'?>"
|
deps["storage"].download_object.return_value = b"<?xml version='1.0'?>"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
response = client.get("/api/v1/remote/rpm-test/repo/repodata/primary.xml")
|
response = client.get("/api/v1/remote/rpm-test/repo/repodata/primary.xml")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -348,10 +350,10 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = False
|
deps["storage"].exists.return_value = False
|
||||||
deps["storage"].download_object.return_value = b"fresh content"
|
deps["storage"].download_object.return_value = b"fresh content"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "cached"},
|
return_value={"status": "cached"},
|
||||||
) as mock_fetch:
|
) as mock_fetch:
|
||||||
@@ -365,10 +367,10 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = False
|
deps["storage"].exists.return_value = False
|
||||||
deps["storage"].download_object.return_value = b"fresh content"
|
deps["storage"].download_object.return_value = b"fresh content"
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "cached"},
|
return_value={"status": "cached"},
|
||||||
):
|
):
|
||||||
@@ -380,10 +382,10 @@ class TestGenericArtifactRoute:
|
|||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = False
|
deps["storage"].exists.return_value = False
|
||||||
deps["storage"].download_object.return_value = b"APKINDEX content"
|
deps["storage"].download_object.return_value = b"APKINDEX content"
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "cached"},
|
return_value={"status": "cached"},
|
||||||
):
|
):
|
||||||
@@ -395,10 +397,10 @@ class TestGenericArtifactRoute:
|
|||||||
def test_upstream_error_returns_502(self, client, patched_deps):
|
def test_upstream_error_returns_502(self, client, patched_deps):
|
||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = False
|
deps["storage"].exists.return_value = False
|
||||||
deps["cache"].is_index_file.return_value = False
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"artifactapi.main.cache_single_artifact",
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
new_callable=AsyncMock,
|
new_callable=AsyncMock,
|
||||||
return_value={"status": "error", "error": "upstream unreachable"},
|
return_value={"status": "error", "error": "upstream unreachable"},
|
||||||
):
|
):
|
||||||
@@ -406,19 +408,116 @@ class TestGenericArtifactRoute:
|
|||||||
|
|
||||||
assert response.status_code == 502
|
assert response.status_code == 502
|
||||||
|
|
||||||
def test_index_file_bypasses_include_patterns(self, client, patched_deps):
|
def test_mutable_file_bypasses_immutable_patterns(self, client, patched_deps):
|
||||||
"""Index files must be served even when they don't match include_patterns."""
|
"""Mutable files must be served even when they don't match immutable_patterns."""
|
||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["storage"].exists.return_value = True
|
deps["storage"].exists.return_value = True
|
||||||
deps["storage"].download_object.return_value = b"APKINDEX content"
|
deps["storage"].download_object.return_value = b"APKINDEX content"
|
||||||
deps["cache"].is_index_file.return_value = True
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
deps["cache"].is_index_valid.return_value = True
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
# APKINDEX.tar.gz does not match alpine-test's include_patterns (.*.apk$),
|
# APKINDEX.tar.gz does not match alpine-test's immutable_patterns (.*.apk$),
|
||||||
# but since is_index_file returns True it must be allowed through.
|
# but since is_mutable_file returns True it must be allowed through.
|
||||||
response = client.get("/api/v1/remote/alpine-test/alpine/v3.18/x86_64/APKINDEX.tar.gz")
|
response = client.get("/api/v1/remote/alpine-test/alpine/v3.18/x86_64/APKINDEX.tar.gz")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_mutable_unchanged_refreshes_ttl_without_redownload(self, client, patched_deps):
|
||||||
|
"""When check_mutable_updates=True and upstream says 304, TTL is refreshed in place."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"metadata content"
|
||||||
|
# File is mutable and its TTL has expired
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = False
|
||||||
|
deps["cache"].get_mutable_meta.return_value = {"etag": '"abc"'}
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy.check_upstream_changed", new_callable=AsyncMock, return_value=False):
|
||||||
|
response = client.get("/api/v1/remote/check-mutable-test/metadata.json")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
deps["cache"].mark_index_cached.assert_called()
|
||||||
|
# S3 object must NOT have been deleted (no re-download)
|
||||||
|
deps["storage"].client.delete_object.assert_not_called()
|
||||||
|
|
||||||
|
def test_mutable_changed_triggers_redownload(self, client, patched_deps):
|
||||||
|
"""When check_mutable_updates=True and upstream says 200, cache is invalidated."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = False
|
||||||
|
deps["cache"].get_mutable_meta.return_value = {"etag": '"abc"'}
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy.check_upstream_changed", new_callable=AsyncMock, return_value=True):
|
||||||
|
with patch("artifactapi.artifact.proxy.cache_single_artifact", new_callable=AsyncMock) as mock_cache:
|
||||||
|
mock_cache.return_value = {"status": "error", "error": "upstream gone"}
|
||||||
|
response = client.get("/api/v1/remote/check-mutable-test/metadata.json")
|
||||||
|
|
||||||
|
assert response.status_code == 502
|
||||||
|
|
||||||
|
def test_mutable_changed_redownloads_successfully(self, client, patched_deps):
|
||||||
|
"""When check_mutable_updates=True and upstream says 200, fresh copy is fetched and served."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"fresh metadata"
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = False
|
||||||
|
deps["cache"].get_mutable_meta.return_value = {"etag": '"abc"'}
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy.check_upstream_changed", new_callable=AsyncMock, return_value=True):
|
||||||
|
with patch("artifactapi.artifact.proxy.cache_single_artifact", new_callable=AsyncMock) as mock_cache:
|
||||||
|
mock_cache.return_value = {"status": "cached", "etag": '"def"', "last_modified": None}
|
||||||
|
response = client.get("/api/v1/remote/check-mutable-test/metadata.json")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
mock_cache.assert_called_once()
|
||||||
|
|
||||||
|
def test_mutable_backend_unreachable_on_check_updates_keeps_stale(self, client, patched_deps):
|
||||||
|
"""When check_mutable_updates=True and backend is unreachable, stale copy is kept and TTL refreshed."""
|
||||||
|
from artifactapi.artifact.proxy import UpstreamUnreachable
|
||||||
|
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"stale metadata"
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = False
|
||||||
|
deps["cache"].get_mutable_meta.return_value = {"etag": '"abc"'}
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy.check_upstream_changed", side_effect=UpstreamUnreachable("connection refused")):
|
||||||
|
response = client.get("/api/v1/remote/check-mutable-test/metadata.json")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
deps["cache"].mark_index_cached.assert_called()
|
||||||
|
deps["storage"].client.delete_object.assert_not_called()
|
||||||
|
|
||||||
|
def test_mutable_backend_unreachable_on_expiry_keeps_stale(self, client, patched_deps):
|
||||||
|
"""When a regular mutable file expires and backend is unreachable, stale copy is kept and TTL refreshed."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"stale APKINDEX"
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = False
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy._upstream_reachable", new_callable=AsyncMock, return_value=False):
|
||||||
|
response = client.get("/api/v1/remote/alpine-test/alpine/v3.18/x86_64/APKINDEX.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
deps["cache"].mark_index_cached.assert_called()
|
||||||
|
deps["storage"].client.delete_object.assert_not_called()
|
||||||
|
|
||||||
|
def test_mutable_flag_off_skips_conditional_check(self, client, patched_deps):
|
||||||
|
"""When check_mutable_updates is not set, expired mutable files are always re-fetched."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = False
|
||||||
|
|
||||||
|
with patch("artifactapi.artifact.proxy.check_upstream_changed", new_callable=AsyncMock) as mock_check:
|
||||||
|
with patch("artifactapi.artifact.proxy.cache_single_artifact", new_callable=AsyncMock) as mock_cache:
|
||||||
|
mock_cache.return_value = {"status": "error", "error": "upstream gone"}
|
||||||
|
client.get("/api/v1/remote/custom-index-test/metadata.json")
|
||||||
|
|
||||||
|
mock_check.assert_not_called()
|
||||||
|
|
||||||
def test_local_repo_file_not_found_returns_404(self, client, patched_deps):
|
def test_local_repo_file_not_found_returns_404(self, client, patched_deps):
|
||||||
deps = patched_deps
|
deps = patched_deps
|
||||||
deps["database"].get_local_file_metadata.return_value = None
|
deps["database"].get_local_file_metadata.return_value = None
|
||||||
@@ -519,8 +618,8 @@ class TestCacheFlushEndpoint:
|
|||||||
deps["cache"].available = True
|
deps["cache"].available = True
|
||||||
redis_mock = MagicMock()
|
redis_mock = MagicMock()
|
||||||
deps["cache"].client = redis_mock
|
deps["cache"].client = redis_mock
|
||||||
# First pattern (index:*) returns keys; subsequent pattern returns nothing
|
# index:* returns keys; mutable:meta:* and metrics:* return nothing
|
||||||
redis_mock.keys.side_effect = [["index:test:abc", "index:test:def"], []]
|
redis_mock.keys.side_effect = [["index:test:abc", "index:test:def"], [], []]
|
||||||
deps["storage"].client.list_objects_v2.return_value = {}
|
deps["storage"].client.list_objects_v2.return_value = {}
|
||||||
|
|
||||||
response = client.put("/cache/flush")
|
response = client.put("/cache/flush")
|
||||||
@@ -554,3 +653,425 @@ class TestConfigEndpoint:
|
|||||||
data = response.json()
|
data = response.json()
|
||||||
assert "remotes" in data
|
assert "remotes" in data
|
||||||
assert "alpine-test" in data["remotes"]
|
assert "alpine-test" in data["remotes"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# PyPI remote /api/v1/remote/pypi-test/...
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestPyPIRemote:
|
||||||
|
def test_simple_index_is_mutable(self, client, patched_deps):
|
||||||
|
"""simple/ paths are detected as mutable (package-type default)."""
|
||||||
|
deps = patched_deps
|
||||||
|
html = b"<html><body><a href='https://files.pythonhosted.org/packages/requests-2.31.0.tar.gz'>...</a></body></html>"
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = html
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/simple/requests/")
|
||||||
|
assert response.status_code == 200
|
||||||
|
deps["cache"].mark_index_cached.assert_not_called()
|
||||||
|
|
||||||
|
def test_simple_index_urls_rewritten_to_proxy(self, client, patched_deps):
|
||||||
|
"""files.pythonhosted.org URLs in a cached simple index are rewritten to our proxy."""
|
||||||
|
deps = patched_deps
|
||||||
|
html = b"<html><body><a href='https://files.pythonhosted.org/packages/requests-2.31.0.tar.gz'>...</a></body></html>"
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = html
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/simple/requests/")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"files.pythonhosted.org" not in response.content
|
||||||
|
assert b"/api/v1/remote/pypi-test/packages/requests-2.31.0.tar.gz" in response.content
|
||||||
|
|
||||||
|
def test_simple_index_content_type_is_html(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"<html></html>"
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/simple/requests/")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "text/html" in response.headers["content-type"]
|
||||||
|
|
||||||
|
def test_simple_index_cache_miss_fetches_upstream(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
html = b"<html><body><a href='https://files.pythonhosted.org/packages/p-1.0.whl'>...</a></body></html>"
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = html
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached"},
|
||||||
|
) as mock_fetch:
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/simple/requests/")
|
||||||
|
|
||||||
|
mock_fetch.assert_called_once()
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"files.pythonhosted.org" not in response.content
|
||||||
|
|
||||||
|
def test_wheel_file_immutable_returns_correct_content_type(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"PK wheel bytes"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/packages/requests-2.31.0-py3-none-any.whl")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "application/zip" in response.headers["content-type"]
|
||||||
|
assert response.headers["X-Artifact-Source"] == "cache"
|
||||||
|
|
||||||
|
def test_sdist_immutable_returns_correct_content_type(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"tar bytes"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/packages/requests-2.31.0.tar.gz")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "application/gzip" in response.headers["content-type"]
|
||||||
|
|
||||||
|
def test_unknown_extension_on_pypi_remote_returns_403(self, client, patched_deps):
|
||||||
|
"""Paths that don't match immutable_patterns and aren't mutable are blocked."""
|
||||||
|
response = client.get("/api/v1/remote/pypi-test/packages/requests.unknown")
|
||||||
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# npm remote /api/v1/remote/npm-test/...
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestNpmRemote:
|
||||||
|
def test_package_metadata_is_mutable(self, client, patched_deps):
|
||||||
|
"""Top-level package metadata paths are detected as mutable."""
|
||||||
|
deps = patched_deps
|
||||||
|
meta = b'{"name":"express","versions":{}}'
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = meta
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/npm-test/express")
|
||||||
|
assert response.status_code == 200
|
||||||
|
deps["cache"].mark_index_cached.assert_not_called()
|
||||||
|
|
||||||
|
def test_metadata_tarball_urls_rewritten_to_proxy(self, client, patched_deps):
|
||||||
|
"""registry.npmjs.org tarball URLs in metadata JSON are rewritten to our proxy."""
|
||||||
|
deps = patched_deps
|
||||||
|
meta = b'{"dist":{"tarball":"https://registry.npmjs.org/express/-/express-4.18.2.tgz"}}'
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = meta
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/npm-test/express")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"registry.npmjs.org" not in response.content
|
||||||
|
assert b"/api/v1/remote/npm-test/express/-/express-4.18.2.tgz" in response.content
|
||||||
|
|
||||||
|
def test_metadata_content_type_is_json(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b'{"name":"express"}'
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/npm-test/express")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "application/json" in response.headers["content-type"]
|
||||||
|
|
||||||
|
def test_scoped_package_metadata_rewritten(self, client, patched_deps):
|
||||||
|
"""@scope/package metadata URLs are also rewritten back to the same npm-test remote."""
|
||||||
|
deps = patched_deps
|
||||||
|
meta = b'{"dist":{"tarball":"https://registry.npmjs.org/@babel/core/-/core-7.21.0.tgz"}}'
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = meta
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/npm-test/@babel/core")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"registry.npmjs.org" not in response.content
|
||||||
|
assert b"/api/v1/remote/npm-test/@babel/core/-/core-7.21.0.tgz" in response.content
|
||||||
|
|
||||||
|
def test_tarball_not_rewritten(self, client, patched_deps):
|
||||||
|
"""Tarball requests (.tgz) bypass URL rewriting and return binary."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"\x1f\x8b tgz bytes"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/npm-test/express/-/express-4.18.2.tgz")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "application/gzip" in response.headers["content-type"]
|
||||||
|
assert response.headers["X-Artifact-Source"] == "cache"
|
||||||
|
|
||||||
|
def test_metadata_cache_miss_fetches_upstream(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
meta = b'{"dist":{"tarball":"https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"}}'
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = meta
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached"},
|
||||||
|
) as mock_fetch:
|
||||||
|
response = client.get("/api/v1/remote/npm-test/lodash")
|
||||||
|
|
||||||
|
mock_fetch.assert_called_once()
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"registry.npmjs.org" not in response.content
|
||||||
|
|
||||||
|
def test_tarball_immutable_allowed_on_npm_remote(self, client, patched_deps):
|
||||||
|
"""Tarballs (.tgz) match immutable_patterns and are served without rewriting."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"tgz bytes"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/npm-test/express/-/express-4.18.2.tgz")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "application/gzip" in response.headers["content-type"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Helm remote /api/v1/remote/helm-test/...
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestHelmRemote:
|
||||||
|
def test_index_yaml_is_mutable(self, client, patched_deps):
|
||||||
|
"""index.yaml is detected as mutable (package-type default)."""
|
||||||
|
deps = patched_deps
|
||||||
|
index = b"apiVersion: v1\nentries:\n vault:\n - urls:\n - https://helm.releases.hashicorp.com/vault-0.29.1.tgz\n"
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = index
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/helm-test/index.yaml")
|
||||||
|
assert response.status_code == 200
|
||||||
|
deps["cache"].mark_index_cached.assert_not_called()
|
||||||
|
|
||||||
|
def test_index_yaml_urls_rewritten_to_proxy(self, client, patched_deps):
|
||||||
|
"""base_url chart URLs in a cached index.yaml are rewritten to our proxy."""
|
||||||
|
deps = patched_deps
|
||||||
|
index = b"apiVersion: v1\nentries:\n vault:\n - urls:\n - https://helm.releases.hashicorp.com/vault-0.29.1.tgz\n"
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = index
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/helm-test/index.yaml")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"helm.releases.hashicorp.com" not in response.content
|
||||||
|
assert b"/api/v1/remote/helm-test/vault-0.29.1.tgz" in response.content
|
||||||
|
|
||||||
|
def test_index_yaml_content_type_is_yaml(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"apiVersion: v1\nentries: {}\n"
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
deps["cache"].is_index_valid.return_value = True
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/helm-test/index.yaml")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "text/yaml" in response.headers["content-type"]
|
||||||
|
|
||||||
|
def test_chart_tarball_immutable_returns_gzip_content_type(self, client, patched_deps):
|
||||||
|
"""Versioned chart tarballs match immutable_patterns and are served as binary."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"\x1f\x8b chart bytes"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/helm-test/vault-0.29.1.tgz")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "application/gzip" in response.headers["content-type"]
|
||||||
|
assert response.headers["X-Artifact-Source"] == "cache"
|
||||||
|
|
||||||
|
def test_index_yaml_cache_miss_fetches_upstream(self, client, patched_deps):
|
||||||
|
deps = patched_deps
|
||||||
|
index = b"apiVersion: v1\nentries:\n vault:\n - urls:\n - https://helm.releases.hashicorp.com/vault-0.29.1.tgz\n"
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = index
|
||||||
|
deps["cache"].is_mutable_file.return_value = True
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached"},
|
||||||
|
) as mock_fetch:
|
||||||
|
response = client.get("/api/v1/remote/helm-test/index.yaml")
|
||||||
|
|
||||||
|
mock_fetch.assert_called_once()
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b"helm.releases.hashicorp.com" not in response.content
|
||||||
|
|
||||||
|
def test_non_tgz_non_yaml_path_blocked_by_pattern(self, client, patched_deps):
|
||||||
|
"""Paths that don't match immutable_patterns and aren't mutable are blocked."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/helm-test/vault.zip")
|
||||||
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Quarantine (quarantine-test remote: quarantine_new=True, quarantine_days=3)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestQuarantine:
|
||||||
|
def _recent_date(self, days_ago=1):
|
||||||
|
"""Return an HTTP-format date string N days in the past (within quarantine window)."""
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from email.utils import format_datetime
|
||||||
|
|
||||||
|
dt = datetime.now(UTC) - timedelta(days=days_ago)
|
||||||
|
return format_datetime(dt, usegmt=True)
|
||||||
|
|
||||||
|
def _old_date(self, days_ago=10):
|
||||||
|
"""Return an HTTP-format date string N days in the past (outside quarantine window)."""
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from email.utils import format_datetime
|
||||||
|
|
||||||
|
dt = datetime.now(UTC) - timedelta(days=days_ago)
|
||||||
|
return format_datetime(dt, usegmt=True)
|
||||||
|
|
||||||
|
def test_cache_miss_recent_artifact_quarantined(self, client, patched_deps):
|
||||||
|
"""Cache miss: artifact published within quarantine window → 404."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached", "last_modified": self._recent_date()},
|
||||||
|
):
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 404
|
||||||
|
assert "quarantined" in response.json()["detail"].lower()
|
||||||
|
|
||||||
|
def test_cache_miss_old_artifact_allowed(self, client, patched_deps):
|
||||||
|
"""Cache miss: artifact published outside quarantine window → 200."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached", "last_modified": self._old_date()},
|
||||||
|
):
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_cache_miss_no_last_modified_fails_open(self, client, patched_deps):
|
||||||
|
"""Cache miss: no Last-Modified header → fail open (200, not quarantined)."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached", "last_modified": None},
|
||||||
|
):
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_cache_hit_recent_artifact_quarantined(self, client, patched_deps):
|
||||||
|
"""Cache hit: stored publish date within quarantine window → 404."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
deps["cache"].get_artifact_published.return_value = self._recent_date()
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 404
|
||||||
|
assert "quarantined" in response.json()["detail"].lower()
|
||||||
|
|
||||||
|
def test_cache_hit_old_artifact_allowed(self, client, patched_deps):
|
||||||
|
"""Cache hit: stored publish date outside quarantine window → 200."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
deps["cache"].get_artifact_published.return_value = self._old_date()
|
||||||
|
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_cache_hit_no_stored_date_fetches_upstream(self, client, patched_deps):
|
||||||
|
"""Cache hit: no stored date → HEAD upstream to get Last-Modified."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = True
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
deps["cache"].get_artifact_published.return_value = None
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy._fetch_last_modified",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value=self._old_date(),
|
||||||
|
) as mock_fetch:
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
mock_fetch.assert_called_once()
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_quarantine_disabled_allows_recent_artifact(self, client, patched_deps):
|
||||||
|
"""quarantine_new=False: recent artifacts are not blocked."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached", "last_modified": self._recent_date()},
|
||||||
|
):
|
||||||
|
response = client.get("/api/v1/remote/quarantine-disabled/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_quarantine_detail_includes_available_date(self, client, patched_deps):
|
||||||
|
"""The 404 detail should include the date when the artifact becomes available."""
|
||||||
|
deps = patched_deps
|
||||||
|
deps["storage"].exists.return_value = False
|
||||||
|
deps["storage"].download_object.return_value = b"content"
|
||||||
|
deps["cache"].is_mutable_file.return_value = False
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"artifactapi.artifact.proxy.cache_single_artifact",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value={"status": "cached", "last_modified": self._recent_date()},
|
||||||
|
):
|
||||||
|
response = client.get("/api/v1/remote/quarantine-test/some/path/package-1.0.tar.gz")
|
||||||
|
|
||||||
|
assert response.status_code == 404
|
||||||
|
detail = response.json()["detail"]
|
||||||
|
assert "available after" in detail
|
||||||
|
assert "3-day" in detail
|
||||||
|
|||||||
Reference in New Issue
Block a user