mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
Merge commit from fork
[Backport release-1.7] security(harbor): give the inner registry its own credential and port policy
This commit is contained in:
5 files changed
+370
-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:
|
||||
@@ -123,6 +164,28 @@ spec:
|
||||
retryInterval: 30s
|
||||
remediation:
|
||||
retries: -1
|
||||
{{- /* jobservice mounts a ReadWriteOnce job-log volume, and the upstream
|
||||
chart rolls it with a surge: a replacement scheduled on another node
|
||||
waits on the attach for good and the upgrade fails as stalled. With
|
||||
no surge the old pod goes first and releases the volume. The chart's
|
||||
own switch, updateStrategy=Recreate, also covers the registry, which
|
||||
keeps its data in S3 and should keep rolling without an outage. */}}
|
||||
postRenderers:
|
||||
- kustomize:
|
||||
patches:
|
||||
- target:
|
||||
group: apps
|
||||
version: v1
|
||||
kind: Deployment
|
||||
name: {{ .Release.Name }}-jobservice
|
||||
patch: |
|
||||
- op: replace
|
||||
path: /spec/strategy
|
||||
value:
|
||||
type: RollingUpdate
|
||||
rollingUpdate:
|
||||
maxSurge: 0
|
||||
maxUnavailable: 1
|
||||
valuesFrom:
|
||||
- kind: Secret
|
||||
name: cozystack-values
|
||||
@@ -134,6 +197,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 +273,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,145 @@
|
||||
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]\$
|
||||
|
||||
# Rotating the credential restarts jobservice on every existing release;
|
||||
# with a surge its ReadWriteOnce job-log volume can pin the replacement on
|
||||
# another node and fail the upgrade as stalled.
|
||||
- it: rolls jobservice without a surge
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: HelmRelease
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.postRenderers[0].kustomize.patches[0].target
|
||||
value:
|
||||
group: apps
|
||||
version: v1
|
||||
kind: Deployment
|
||||
name: harbor-test-jobservice
|
||||
- matchRegex:
|
||||
path: spec.postRenderers[0].kustomize.patches[0].patch
|
||||
pattern: "maxSurge: 0"
|
||||
@@ -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