Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,99 @@ jobs:
--set serviceMonitor.interval=30s \
--set 'serviceMonitor.labels.release=prometheus' \
>/dev/null
helm template proxy deploy/charts/proxy \
--set replicaCount=3 \
--set config.data.database.driver=postgres \
--set deploymentStrategy.type=RollingUpdate \
--set persistence.enabled=false \
--set persistence.emptyDirSizeLimit=1Gi \
--set podDisruptionBudget.enabled=true \
--set serviceAccount.create=true \
--set-string 'serviceAccount.annotations.eks\.amazonaws\.com/role-arn=arn:aws:iam::123456789012:role/proxy' \
--set httpRoute.enabled=true \
--set 'httpRoute.parentRefs[0].name=public' \
--set 'httpRoute.hostnames[0]=proxy.example.com' \
--set httpRoute.timeouts.request=600s \
--set service.type=LoadBalancer \
--set 'service.loadBalancerSourceRanges[0]=10.0.0.0/8' \
--set priorityClassName=high \
--set revisionHistoryLimit=3 \
--set 'extraContainers[0].name=sidecar' \
--set 'extraContainers[0].image=busybox:1.37' \
--set networkPolicy.enabled=true \
--set 'networkPolicy.from[0].namespaceSelector.matchLabels.kubernetes\.io/metadata\.name=ingress-nginx' \
--set 'networkPolicy.egress[0].ports[0].protocol=UDP' \
--set 'networkPolicy.egress[0].ports[0].port=53' \
--set-string 'config.data.upstream.auth.npm\.pkg\.github\.com.token=${GITHUB_TOKEN}' \
>build-chart-full.yaml
grep -F 'kind: PodDisruptionBudget' build-chart-full.yaml >/dev/null
grep -F 'kind: HTTPRoute' build-chart-full.yaml >/dev/null
grep -F 'serviceAccountName: "proxy"' build-chart-full.yaml >/dev/null
grep -F 'kind: NetworkPolicy' build-chart-full.yaml >/dev/null

- name: Check the values the chart derives and refuses
run: |
set -euo pipefail
fails() {
if "$@" >/dev/null 2>&1; then
echo "expected to fail: $*" >&2
return 1
fi
}
# The container port follows the listen address.
helm template proxy deploy/charts/proxy --set-string config.data.listen=:9090 |
grep -F 'containerPort: 9090' >/dev/null
# Replicas can't share SQLite, unless something else sets the database.
fails helm template proxy deploy/charts/proxy --set replicaCount=2
helm template proxy deploy/charts/proxy --set replicaCount=2 \
--set 'extraEnv[0].name=PROXY_DATABASE_DRIVER' \
--set 'extraEnv[0].value=postgres' >/dev/null
# A budget for one replica would block node drains.
if helm template proxy deploy/charts/proxy --set podDisruptionBudget.enabled=true |
grep -F 'kind: PodDisruptionBudget' >/dev/null; then
echo "a PodDisruptionBudget was rendered for one replica" >&2
exit 1
fi
# Secrets stay out of the ConfigMap unless allowed.
fails helm template proxy deploy/charts/proxy \
--set 'config.data.upstream.auth.npm.token=plain'
fails helm template proxy deploy/charts/proxy \
--set-string 'config.data.database.url=postgres://proxy:secret@db/proxy'
fails helm template proxy deploy/charts/proxy \
--set-string 'config.data.database.url=postgres://proxy@db/proxy?password=secret'
fails helm template proxy deploy/charts/proxy \
--set-string 'config.data.database.url=host=db user=proxy password=secret'
helm template proxy deploy/charts/proxy \
--set-string 'config.data.database.url=host=db user=proxy dbname=proxy' >/dev/null
helm template proxy deploy/charts/proxy \
--set-string 'config.data.database.url=postgres://proxy@db/proxy?sslmode=require' >/dev/null
helm template proxy deploy/charts/proxy \
--set 'config.data.upstream.auth.npm.token=plain' \
--set config.allowSecretsInConfigMap=true >/dev/null
# A NetworkPolicy without peers would shut everyone out.
fails helm template proxy deploy/charts/proxy --set networkPolicy.enabled=true
# The schema rejects a misspelled key.
fails helm template proxy deploy/charts/proxy --set persistance.enabled=false
fails helm template proxy deploy/charts/proxy --set httpRoute.enabled=true
fails helm template proxy deploy/charts/proxy \
--set httpRoute.enabled=true \
--set 'httpRoute.parentRefs[0].name=public' \
--set ingress.enabled=true \
--set 'ingress.hosts[0].host=proxy.example.com' \
--set 'ingress.hosts[0].paths[0].path=/' \
--set 'ingress.hosts[0].paths[0].pathType=Prefix'

- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version-file: go.mod

# HTTPRoute and ServiceMonitor are CRDs without a schema here, so they
# are skipped.
- name: Validate manifests against the Kubernetes schemas
run: |
set -euo pipefail
go install github.com/yannh/kubeconform/cmd/kubeconform@v0.7.0
kubeconform="$(go env GOPATH)/bin/kubeconform"
helm template proxy deploy/charts/proxy |
"$kubeconform" -strict -summary -ignore-missing-schemas
"$kubeconform" -strict -summary -ignore-missing-schemas build-chart-full.yaml
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ The default chart deploys one replica backed by a 10 GiB persistent volume,
using SQLite and filesystem artifact storage under `/data`. See
[`deploy/charts/proxy/values.yaml`](deploy/charts/proxy/values.yaml) for ingress,
external database and object-storage configuration options.
The [chart's README](deploy/charts/proxy/README.md) covers running more than one
replica, cloud credentials through a ServiceAccount, and exposing the proxy.

Set `deploymentAnnotations` to add annotations to the Deployment itself,
separately from `podAnnotations`.
Expand Down
142 changes: 142 additions & 0 deletions deploy/charts/proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# git-pkgs proxy

A caching proxy for package registries. This chart runs it as a Deployment
with a Service, and optionally an Ingress or HTTPRoute, a ServiceMonitor, a
ServiceAccount, a PodDisruptionBudget and a NetworkPolicy.

```bash
helm install proxy oci://ghcr.io/git-pkgs/charts/proxy \
--set config.data.base_url=https://proxy.example.com
```

The defaults give one replica with a 10 GiB volume at `/data`, holding a
SQLite database and the cached artifacts. [`values.yaml`](values.yaml)
describes every value; `values.schema.json` rejects a value the chart doesn't
know, so a misspelled key fails the install instead of being ignored.

## Configuration

`config.data` is the proxy's configuration file, passed through as it is; the
[configuration reference](https://github.com/git-pkgs/proxy/blob/main/docs/configuration.md)
lists the keys. Environment variables override it, and are the place for
secrets:

```yaml
config:
data:
base_url: https://proxy.example.com
database:
driver: postgres

extraEnv:
- name: PROXY_DATABASE_URL
valueFrom:
secretKeyRef:
name: proxy-database
key: url
```

Secrets in `config.data` would be stored in the ConfigMap in plain text, so
the chart refuses to render a `token`, `password`, `header_value` or
`signing_key` that isn't a `${VAR}` reference, or a `database.url` with a
password, whether as `user:password@`, as `?password=` or as a `password=`
keyword (set such a URL through `PROXY_DATABASE_URL` instead). The proxy
expands `${VAR}` in the other keys, so the value can come from a Secret:

```yaml
config:
data:
upstream:
auth:
"https://npm.pkg.github.com":
type: bearer
token: "${GITHUB_TOKEN}"

extraEnv:
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: proxy-upstream
key: github-token
```

`config.allowSecretsInConfigMap: true` turns the check off. Other keys that
can hold credentials, such as scanner `headers`, aren't checked.

Set `config.existingConfigMap` to use a ConfigMap of your own instead.

The container port follows `config.data.listen`. Set `service.containerPort`
when the listen address comes from an existing ConfigMap or `PROXY_LISTEN`.

## More than one replica

Replicas can't share a SQLite file, so they need:

- `config.data.database.driver: postgres`, with the URL in `PROXY_DATABASE_URL`
- `deploymentStrategy.type: RollingUpdate`
- artifact storage all replicas reach: object storage with
`persistence.enabled: false`, or a `file://` URL on a `ReadWriteMany` volume

The chart refuses to render more than one replica when the generated
configuration uses SQLite and nothing else can set the database.
`podDisruptionBudget.enabled` adds a budget once there is more than one.

## Cloud credentials

The `ecr` upstream auth type and S3 storage take AWS credentials from the
SDK's default chain. To bind them to the pod, give it a ServiceAccount:

```yaml
serviceAccount:
create: true
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/proxy
```

## Exposing the proxy

Use `ingress` or `httpRoute` (Gateway API), not both. Artifacts and hosted
uploads can be large, and most controllers cap the request body and time out
long responses by default; `values.yaml` shows the annotations for
ingress-nginx, and `httpRoute.timeouts` sets the route's.

## Limiting who reaches the proxy

`/ui`, `/api/cache`, `/api/mirror` and `/_internal/scan-fetch` have no
authentication of their own. `networkPolicy.enabled` adds a NetworkPolicy that
admits only the peers in `networkPolicy.from` to the proxy's port, and the pod
of `helm test`:
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

```yaml
networkPolicy:
enabled: true
from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ingress-nginx
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ci
```

List the ingress controller or gateway, the clients that pull packages, and
Prometheus if it scrapes `/metrics`. `networkPolicy.egress` restricts outgoing
traffic as well; once set, everything it doesn't list is denied, DNS included.

The chart's policy doesn't replace other NetworkPolicies that select the proxy
pods: Kubernetes adds their allow rules together, so a policy that admits all
traffic keeps `/ui`, `/api/cache`, `/api/mirror` and `/_internal/scan-fetch`
open to everyone it admits. Look for such policies in the namespace and
restrict them, for ingress and egress.

A NetworkPolicy sees addresses and ports, not paths, and the cluster's network
plugin has to enforce it. To keep `/ui` or the APIs from clients that may pull
packages, restrict those paths at the ingress or gateway too.

## Checking an install

```bash
helm test proxy
```

runs a pod that asks the proxy for `/health` through its Service.
9 changes: 9 additions & 0 deletions deploy/charts/proxy/templates/NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ To access the proxy locally:

Then visit http://localhost:{{ .Values.service.port }}/.

To check that it answers:

helm -n {{ .Release.Namespace }} test {{ .Release.Name }}
{{- if not .Values.persistence.enabled }}

WARNING: persistence is disabled. Cached artifacts and the default SQLite
database will be lost when the pod is replaced.
{{- end }}
{{- if and (gt (int .Values.replicaCount) 1) (eq (toString .Values.deploymentStrategy.type) "Recreate") }}

WARNING: deploymentStrategy is Recreate with {{ .Values.replicaCount }} replicas, so every
upgrade stops all of them first. Set deploymentStrategy.type to RollingUpdate.
{{- end }}
97 changes: 97 additions & 0 deletions deploy/charts/proxy/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,100 @@ app.kubernetes.io/instance: {{ .Release.Name }}
{{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}}
{{- end -}}
{{- end }}

{{/*
The name is always rendered, "default" included. Leaving the field out doesn't
take a pod back to the default account: an upgrade that drops
serviceAccountName keeps the old name in the deprecated serviceAccount field.
*/}}
{{- define "proxy.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
{{- default (include "proxy.fullname" .) .Values.serviceAccount.name -}}
{{- else -}}
{{- default "default" .Values.serviceAccount.name -}}
{{- end -}}
{{- end }}

{{/*
The port the container listens on: service.containerPort when set, else the
port of config.data.listen in the generated configuration, else 8080.
*/}}
{{- define "proxy.containerPort" -}}
{{- $data := .Values.config.data | default dict -}}
{{- $listen := get $data "listen" | default "" | toString -}}
{{- if .Values.service.containerPort -}}
{{- int .Values.service.containerPort -}}
{{- else if and (not .Values.config.existingConfigMap) (regexMatch ":[0-9]+$" $listen) -}}
{{- regexFind "[1-9][0-9]*$" $listen | int -}}
{{- else -}}
8080
{{- end -}}
{{- end }}

{{/*
proxy.sqliteForCertain is "true" when the generated configuration uses SQLite
and nothing outside it can set the database.
*/}}
{{- define "proxy.sqliteForCertain" -}}
{{- $data := .Values.config.data | default dict -}}
{{- $database := get $data "database" | default dict -}}
{{- $driver := get $database "driver" | default "sqlite" | toString | lower -}}
{{- $fromEnv := false -}}
{{- range .Values.extraEnv -}}
{{- if hasPrefix "PROXY_DATABASE_" (toString .name) -}}
{{- $fromEnv = true -}}
{{- end -}}
{{- end -}}
{{- if and (eq $driver "sqlite") (not .Values.config.existingConfigMap) (not $fromEnv) (not .Values.extraEnvFrom) -}}
true
{{- end -}}
{{- end }}

{{/*
proxy.secretPaths lists, space-separated, the paths in config.data whose key
names a secret (token, password, header_value, signing_key) and whose value
is set without a ${VAR} reference. Called with a dict of path and value.
*/}}
{{- define "proxy.secretPaths" -}}
{{- $path := .path -}}
{{- if kindIs "map" .value -}}
{{- range $key, $value := .value -}}
{{- $at := ternary (toString $key) (printf "%s.%s" $path (toString $key)) (eq $path "") -}}
{{- if or (kindIs "map" $value) (kindIs "slice" $value) -}}
{{- include "proxy.secretPaths" (dict "path" $at "value" $value) -}}
{{- else if and (has (lower (toString $key)) (list "token" "password" "header_value" "signing_key")) (not (kindIs "invalid" $value)) (ne (toString $value) "") (not (contains "${" (toString $value))) -}}
{{- printf "%s " $at -}}
{{- end -}}
{{- end -}}
{{- else if kindIs "slice" .value -}}
{{- range $index, $value := .value -}}
{{- include "proxy.secretPaths" (dict "path" (printf "%s[%d]" $path $index) "value" $value) -}}
{{- end -}}
{{- end -}}
{{- end }}

{{/* Fail on values that can't work, before anything is installed. */}}
{{- define "proxy.validate" -}}
{{- if and (gt (int .Values.replicaCount) 1) (include "proxy.sqliteForCertain" .) -}}
{{- fail "replicaCount > 1 needs Postgres: set config.data.database.driver to postgres and supply PROXY_DATABASE_URL through extraEnv. Replicas can't share a SQLite file." -}}
{{- end -}}
{{- if and .Values.ingress.enabled .Values.httpRoute.enabled -}}
{{- fail "ingress.enabled and httpRoute.enabled are both set; pick one" -}}
{{- end -}}
{{- if and (not .Values.config.existingConfigMap) (not .Values.config.allowSecretsInConfigMap) -}}
{{- $data := .Values.config.data | default dict -}}
{{- $secrets := include "proxy.secretPaths" (dict "path" "" "value" $data) | trim | splitList " " | compact -}}
{{- $database := get $data "database" | default dict -}}
{{- $url := "" -}}
{{- if kindIs "map" $database -}}
{{- $url = get $database "url" | default "" | toString -}}
{{- end -}}
{{- /* A password as userinfo, as a query parameter or as a keyword DSN pair. */ -}}
{{- if or (regexMatch `://[^/@]*:[^/@]+@` $url) (regexMatch `(?i)[?&]password=[^&]` $url) (regexMatch `(?i)(^|\s)password\s*=\s*[^\s]` $url) -}}
{{- $secrets = append $secrets "database.url" -}}
{{- end -}}
{{- if $secrets -}}
{{- fail (printf "config.data would put secrets in the ConfigMap in plain text: %s. Write them as ${VAR} references and set the variables through extraEnv from a Secret (the database URL as PROXY_DATABASE_URL), or set config.allowSecretsInConfigMap=true." (join ", " $secrets)) -}}
{{- end -}}
{{- end -}}
{{- end }}
Loading
Loading