apiVersion: v1
kind: Namespace
metadata:
  name: passbolt
---
apiVersion: v1
kind: Secret
metadata:
  name: passbolt-secrets
  namespace: passbolt
type: Opaque
stringData:
  DATASOURCES_DEFAULT_PASSWORD: "change-me-db-password"
  DATASOURCES_DEFAULT_USERNAME: "passbolt"
  DATASOURCES_DEFAULT_DATABASE: "passbolt"
  MYSQL_ROOT_PASSWORD: "change-me-root-password"
  MYSQL_PASSWORD: "change-me-db-password"
  MYSQL_USER: "passbolt"
  MYSQL_DATABASE: "passbolt"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mariadb-pvc
  namespace: passbolt
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 5Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: passbolt-gpg-pvc
  namespace: passbolt
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: passbolt-jwt-pvc
  namespace: passbolt
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
  namespace: passbolt
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
        - name: mariadb
          image: mariadb:10.11
          envFrom:
            - secretRef:
                name: passbolt-secrets
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mariadb-data
              mountPath: /var/lib/mysql
      volumes:
        - name: mariadb-data
          persistentVolumeClaim:
            claimName: mariadb-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: mariadb
  namespace: passbolt
spec:
  selector:
    app: mariadb
  ports:
    - port: 3306
      targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: passbolt
  namespace: passbolt
spec:
  replicas: 1
  selector:
    matchLabels:
      app: passbolt
  template:
    metadata:
      labels:
        app: passbolt
    spec:
      containers:
        - name: passbolt
          image: passbolt/passbolt:latest-ce
          env:
            - name: APP_FULL_BASE_URL
              value: "https://pb.roysland.net"
            # Must match the server key on the gpg PVC (uid passbolt@yourdomain.com).
            # Without it passbolt's config fingerprint mismatches the actual key and
            # the GPG login handshake fails (web login loops back to the login page).
            - name: PASSBOLT_GPG_SERVER_KEY_FINGERPRINT
              value: "1471F6B122637CC7A95E63DCA214B492C0F7DF56"
            - name: DATASOURCES_DEFAULT_HOST
              value: "mariadb"
            - name: DATASOURCES_DEFAULT_PORT
              value: "3306"
            - name: DATASOURCES_DEFAULT_USERNAME
              valueFrom:
                secretKeyRef:
                  name: passbolt-secrets
                  key: DATASOURCES_DEFAULT_USERNAME
            - name: DATASOURCES_DEFAULT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: passbolt-secrets
                  key: DATASOURCES_DEFAULT_PASSWORD
            - name: DATASOURCES_DEFAULT_DATABASE
              valueFrom:
                secretKeyRef:
                  name: passbolt-secrets
                  key: DATASOURCES_DEFAULT_DATABASE
            # TLS is terminated by Traefik, which forwards plain HTTP to :80.
            # The container must NOT force its own HTTP->HTTPS redirect or it
            # creates an infinite redirect loop. APP_FULL_BASE_URL stays https
            # so generated links remain https.
            - name: PASSBOLT_SSL_FORCE
              value: "false"
          ports:
            - containerPort: 80
            - containerPort: 443
          volumeMounts:
            - name: gpg-data
              mountPath: /etc/passbolt/gpg
            - name: jwt-data
              mountPath: /etc/passbolt/jwt
      volumes:
        - name: gpg-data
          persistentVolumeClaim:
            claimName: passbolt-gpg-pvc
        - name: jwt-data
          persistentVolumeClaim:
            claimName: passbolt-jwt-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: passbolt
  namespace: passbolt
spec:
  selector:
    app: passbolt
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: https-redirect
  namespace: passbolt
spec:
  redirectScheme:
    scheme: https
    permanent: true
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: passbolt
  namespace: passbolt
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt
    # Force HTTP->HTTPS at Traefik (proxy-level, no app redirect loop).
    traefik.ingress.kubernetes.io/router.middlewares: passbolt-https-redirect@kubernetescrd
spec:
  ingressClassName: traefik
  tls:
    - hosts:
        - pb.roysland.net
      secretName: passbolt-tls
  rules:
    - host: pb.roysland.net
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: passbolt
                port:
                  number: 80
