mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
security(harbor): give the inner registry its own credential and port policy
The Harbor application never set a credential for the registry that
Harbor core and jobservice talk to, so the vendored chart's published
default applied, and the registry Service accepted it from any pod the
tenant network policies let through. That bypasses Harbor's own
authorisation, quotas and audit log.
Each release now gets a random registry password with a matching
htpasswd line, kept in a Secret that stays out of the tenant-visible
allowlist and is reused across upgrades so the bcrypt salt does not
restart the registry on every reconcile. An existing installation
rotates on upgrade: core, jobservice and registry roll together because
their Secret checksums change; registry storage is untouched.
The inner chart also ships a CiliumNetworkPolicy that denies the
registry and registryctl ports to everything but core, jobservice and
trivy of the same release. A deny is needed because the tenant base
policy already allows the traffic and Cilium allow rules are additive.
Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
(cherry picked from commit 40525fc756)
This commit is contained in:
5 files changed
+329
-2
No files matched your search
@@ -11,6 +11,37 @@
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- /*
|
||||
The inner registry authenticates Harbor core and jobservice with htpasswd
|
||||
basic auth. Left unset, the vendored chart falls back to the credential
|
||||
published in its values.yaml, which anyone who can reach the registry
|
||||
Service can use to bypass Harbor's authorisation. Each release gets its own
|
||||
random password instead.
|
||||
|
||||
The htpasswd line is stored next to the password rather than computed on
|
||||
every render: bcrypt salts each call, and a hash that changed on every
|
||||
reconcile would change the registry Secret checksum and restart the registry
|
||||
each time. Both keys are regenerated together when either is missing, so the
|
||||
pair always matches. The Secret is kept out of the tenant allowlist on
|
||||
purpose: nothing but core and jobservice needs this credential.
|
||||
*/}}
|
||||
{{- $registrySecretName := printf "%s-registry-credentials" .Release.Name }}
|
||||
{{- $registryUser := "harbor_registry_user" }}
|
||||
{{- $registryPassword := "" }}
|
||||
{{- $registryHtpasswd := "" }}
|
||||
{{- $existingRegistrySecret := lookup "v1" "Secret" .Release.Namespace $registrySecretName }}
|
||||
{{- if $existingRegistrySecret }}
|
||||
{{- $registryData := $existingRegistrySecret.data | default dict }}
|
||||
{{- if and (hasKey $registryData "password") (hasKey $registryData "htpasswd") }}
|
||||
{{- $registryPassword = index $registryData "password" | b64dec }}
|
||||
{{- $registryHtpasswd = index $registryData "htpasswd" | b64dec }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if not (and $registryPassword $registryHtpasswd) }}
|
||||
{{- $registryPassword = randAlphaNum 32 }}
|
||||
{{- $registryHtpasswd = htpasswd $registryUser $registryPassword }}
|
||||
{{- end }}
|
||||
|
||||
{{- $existingCoreSecret := lookup "v1" "Secret" .Release.Namespace (printf "%s-core" .Release.Name) }}
|
||||
{{- $tokenKey := "" }}
|
||||
{{- $tokenCert := "" }}
|
||||
@@ -76,6 +107,16 @@ stringData:
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: {{ $registrySecretName }}
|
||||
stringData:
|
||||
password: {{ $registryPassword | quote }}
|
||||
htpasswd: {{ $registryHtpasswd | quote }}
|
||||
|
||||
---
|
||||
|
||||
apiVersion: helm.toolkit.fluxcd.io/v2
|
||||
kind: HelmRelease
|
||||
metadata:
|
||||
@@ -134,6 +175,14 @@ spec:
|
||||
name: {{ .Release.Name }}-credentials
|
||||
valuesKey: redis-password
|
||||
targetPath: harbor.redis.external.password
|
||||
- kind: Secret
|
||||
name: {{ $registrySecretName }}
|
||||
valuesKey: password
|
||||
targetPath: harbor.registry.credentials.password
|
||||
- kind: Secret
|
||||
name: {{ $registrySecretName }}
|
||||
valuesKey: htpasswd
|
||||
targetPath: harbor.registry.credentials.htpasswdString
|
||||
# BucketInfo is the JSON blob populated by the COSI BucketAccess controller.
|
||||
# Merge it at the values root (no targetPath) so Flux unmarshals it as YAML
|
||||
# instead of running it through Helm's strvals parser, which would split the
|
||||
@@ -202,6 +251,8 @@ spec:
|
||||
{{- end }}
|
||||
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.core.resourcesPreset .Values.core.resources $) | nindent 10 }}
|
||||
registry:
|
||||
credentials:
|
||||
username: {{ $registryUser | quote }}
|
||||
registry:
|
||||
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.registry.resourcesPreset .Values.registry.resources $) | nindent 12 }}
|
||||
controller:
|
||||
|
||||
@@ -31,8 +31,8 @@ tests:
|
||||
issuer-name: letsencrypt-prod
|
||||
solver: http01
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: Secret
|
||||
path: metadata.name
|
||||
value: harbor-credentials
|
||||
asserts:
|
||||
- equal:
|
||||
path: stringData.url
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
suite: per-release inner registry credential
|
||||
|
||||
# The inner registry authenticates core and jobservice with htpasswd basic
|
||||
# auth. The vendored chart's default credential is published, so every release
|
||||
# must carry its own, and it must survive upgrades unchanged: a new bcrypt
|
||||
# hash on every render would restart the registry on every reconcile.
|
||||
|
||||
templates:
|
||||
- templates/harbor.yaml
|
||||
|
||||
release:
|
||||
name: harbor-test
|
||||
namespace: tenant-test
|
||||
|
||||
set:
|
||||
_namespace:
|
||||
host: example.org
|
||||
ingress: nginx
|
||||
gateway: ""
|
||||
seaweedfs: seaweedfs
|
||||
_cluster:
|
||||
solver: http01
|
||||
|
||||
tests:
|
||||
- it: generates a random password and a matching htpasswd line
|
||||
documentSelector:
|
||||
path: metadata.name
|
||||
value: harbor-test-registry-credentials
|
||||
asserts:
|
||||
- matchRegex:
|
||||
path: stringData.password
|
||||
pattern: ^[A-Za-z0-9]{32}$
|
||||
- matchRegex:
|
||||
path: stringData.htpasswd
|
||||
pattern: ^harbor_registry_user:\$2[aby]\$
|
||||
- notEqual:
|
||||
path: stringData.password
|
||||
value: harbor_registry_password
|
||||
|
||||
- it: keeps the registry credential out of the tenant-visible secrets
|
||||
documentSelector:
|
||||
path: metadata.name
|
||||
value: harbor-test-registry-credentials
|
||||
asserts:
|
||||
- notExists:
|
||||
path: metadata.labels
|
||||
|
||||
- it: passes the credential to the inner chart through valuesFrom
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: HelmRelease
|
||||
asserts:
|
||||
- contains:
|
||||
path: spec.valuesFrom
|
||||
content:
|
||||
kind: Secret
|
||||
name: harbor-test-registry-credentials
|
||||
valuesKey: password
|
||||
targetPath: harbor.registry.credentials.password
|
||||
- contains:
|
||||
path: spec.valuesFrom
|
||||
content:
|
||||
kind: Secret
|
||||
name: harbor-test-registry-credentials
|
||||
valuesKey: htpasswd
|
||||
targetPath: harbor.registry.credentials.htpasswdString
|
||||
- equal:
|
||||
path: spec.values.harbor.registry.credentials.username
|
||||
value: harbor_registry_user
|
||||
- notExists:
|
||||
path: spec.values.harbor.registry.credentials.password
|
||||
|
||||
- it: preserves an existing password and htpasswd pair
|
||||
kubernetesProvider:
|
||||
scheme:
|
||||
"v1/Secret":
|
||||
gvr:
|
||||
version: "v1"
|
||||
resource: "secrets"
|
||||
namespaced: true
|
||||
objects:
|
||||
- kind: Secret
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: harbor-test-registry-credentials
|
||||
namespace: tenant-test
|
||||
data:
|
||||
password: a2VwdC1wYXNzd29yZA==
|
||||
htpasswd: aGFyYm9yX3JlZ2lzdHJ5X3VzZXI6JDJhJDEwJGtlcHRrZXB0a2VwdGtlcHRrZXB0a08=
|
||||
documentSelector:
|
||||
path: metadata.name
|
||||
value: harbor-test-registry-credentials
|
||||
asserts:
|
||||
- equal:
|
||||
path: stringData.password
|
||||
value: kept-password
|
||||
- equal:
|
||||
path: stringData.htpasswd
|
||||
value: harbor_registry_user:$2a$10$keptkeptkeptkeptkeptkO
|
||||
|
||||
- it: regenerates both keys when the htpasswd line is missing
|
||||
kubernetesProvider:
|
||||
scheme:
|
||||
"v1/Secret":
|
||||
gvr:
|
||||
version: "v1"
|
||||
resource: "secrets"
|
||||
namespaced: true
|
||||
objects:
|
||||
- kind: Secret
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: harbor-test-registry-credentials
|
||||
namespace: tenant-test
|
||||
data:
|
||||
password: a2VwdC1wYXNzd29yZA==
|
||||
documentSelector:
|
||||
path: metadata.name
|
||||
value: harbor-test-registry-credentials
|
||||
asserts:
|
||||
- notEqual:
|
||||
path: stringData.password
|
||||
value: kept-password
|
||||
- matchRegex:
|
||||
path: stringData.htpasswd
|
||||
pattern: ^harbor_registry_user:\$2[aby]\$
|
||||
@@ -0,0 +1,78 @@
|
||||
{{- /*
|
||||
Only Harbor's own core, jobservice and trivy talk to the inner registry
|
||||
(5000) and its controller (8080); every client goes through core, which
|
||||
enforces projects, robot accounts, quotas and the audit log. The tenant
|
||||
base policy admits ingress from the whole namespace and from the cluster,
|
||||
and Cilium allow rules are additive, so a narrower allow here would change
|
||||
nothing: the rest has to be denied explicitly. Deny wins over allow in
|
||||
Cilium, and NotIn also matches a pod that lacks the key, so a pod without
|
||||
Harbor's labels falls under the deny.
|
||||
|
||||
The metrics port (8001) stays reachable for scraping, and kubelet probes
|
||||
come from the host entity, which no rule here selects.
|
||||
|
||||
Without Cilium there is no tenant base policy to narrow either; such a
|
||||
cluster gets no policy rather than a failed install.
|
||||
*/}}
|
||||
{{- if .Capabilities.APIVersions.Has "cilium.io/v2/CiliumNetworkPolicy" }}
|
||||
{{- $ports := list (dict "port" "5000" "protocol" "TCP") (dict "port" "8080" "protocol" "TCP") }}
|
||||
apiVersion: cilium.io/v2
|
||||
kind: CiliumNetworkPolicy
|
||||
metadata:
|
||||
name: {{ .Release.Name }}-registry-ingress
|
||||
spec:
|
||||
endpointSelector:
|
||||
matchLabels:
|
||||
release: {{ .Release.Name }}
|
||||
app: harbor
|
||||
component: registry
|
||||
ingressDeny:
|
||||
- fromEndpoints:
|
||||
# pods in any other namespace; Exists keeps the host identity, which has
|
||||
# no namespace label and would otherwise match NotIn, out of the deny
|
||||
- matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: Exists
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: NotIn
|
||||
values:
|
||||
- {{ .Release.Namespace }}
|
||||
# pods in this namespace that belong to another release or another app
|
||||
- matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: In
|
||||
values:
|
||||
- {{ .Release.Namespace }}
|
||||
- key: release
|
||||
operator: NotIn
|
||||
values:
|
||||
- {{ .Release.Name }}
|
||||
- matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: In
|
||||
values:
|
||||
- {{ .Release.Namespace }}
|
||||
- key: app
|
||||
operator: NotIn
|
||||
values:
|
||||
- harbor
|
||||
# Harbor components that have no business with the registry
|
||||
- matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: In
|
||||
values:
|
||||
- {{ .Release.Namespace }}
|
||||
- key: component
|
||||
operator: NotIn
|
||||
values:
|
||||
- core
|
||||
- jobservice
|
||||
- trivy
|
||||
toPorts:
|
||||
- ports: {{- toYaml $ports | nindent 6 }}
|
||||
- fromEntities:
|
||||
- world
|
||||
- remote-node
|
||||
toPorts:
|
||||
- ports: {{- toYaml $ports | nindent 6 }}
|
||||
{{- end }}
|
||||
@@ -0,0 +1,72 @@
|
||||
suite: inner registry reachable only from Harbor's own components
|
||||
|
||||
# The tenant base policy admits the whole cluster and Cilium allow rules are
|
||||
# additive, so only an explicit deny narrows who reaches the registry ports.
|
||||
|
||||
templates:
|
||||
- templates/registry-networkpolicy.yaml
|
||||
|
||||
release:
|
||||
name: harbor-test-system
|
||||
namespace: tenant-test
|
||||
|
||||
tests:
|
||||
- it: denies the registry ports to everything but core, jobservice and trivy of this release
|
||||
capabilities:
|
||||
apiVersions:
|
||||
- cilium.io/v2/CiliumNetworkPolicy
|
||||
asserts:
|
||||
- isKind:
|
||||
of: CiliumNetworkPolicy
|
||||
- equal:
|
||||
path: spec.endpointSelector.matchLabels
|
||||
value:
|
||||
release: harbor-test-system
|
||||
app: harbor
|
||||
component: registry
|
||||
- notExists:
|
||||
path: spec.ingress
|
||||
- contains:
|
||||
path: spec.ingressDeny[0].fromEndpoints
|
||||
content:
|
||||
matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: Exists
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: NotIn
|
||||
values: [tenant-test]
|
||||
- contains:
|
||||
path: spec.ingressDeny[0].fromEndpoints
|
||||
content:
|
||||
matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: In
|
||||
values: [tenant-test]
|
||||
- key: release
|
||||
operator: NotIn
|
||||
values: [harbor-test-system]
|
||||
- contains:
|
||||
path: spec.ingressDeny[0].fromEndpoints
|
||||
content:
|
||||
matchExpressions:
|
||||
- key: k8s:io.kubernetes.pod.namespace
|
||||
operator: In
|
||||
values: [tenant-test]
|
||||
- key: component
|
||||
operator: NotIn
|
||||
values: [core, jobservice, trivy]
|
||||
- equal:
|
||||
path: spec.ingressDeny[0].toPorts[0].ports
|
||||
value:
|
||||
- port: "5000"
|
||||
protocol: TCP
|
||||
- port: "8080"
|
||||
protocol: TCP
|
||||
- equal:
|
||||
path: spec.ingressDeny[1].fromEntities
|
||||
value: [world, remote-node]
|
||||
|
||||
- it: renders nothing without Cilium
|
||||
asserts:
|
||||
- hasDocuments:
|
||||
count: 0
|
||||
Reference in new issue
Block a user