{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if not .Values.auth.existingSecret }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 4 }}
  {{- end }}
type: Opaque
data:
  airflow-password: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "airflow-password" "providedValues" (list "auth.password") "context" $) }}
  # Airflow keys must be base64-encoded, hence we need to pipe to 'b64enc' twice
  # The auto-generation mechanism available at "common.secrets.passwords.manage" isn't compatible with encoding twice
  # Therefore, we can only use this function if the secret already exists
  {{- if or (include "common.secrets.exists" (dict "secret" (include "common.names.fullname" .) "context" $)) (not (empty .Values.auth.fernetKey)) }}
  airflow-fernet-key: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "airflow-fernet-key" "providedValues" (list "auth.fernetKey") "context" $) }}
  {{- else }}
  airflow-fernet-key: {{ randAlphaNum 32 | b64enc | b64enc | quote }}
  {{- end }}
  {{- if or (include "common.secrets.exists" (dict "secret" (include "common.names.fullname" .) "context" $)) (not (empty .Values.auth.secretKey)) }}
  airflow-secret-key: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "airflow-secret-key" "providedValues" (list "auth.secretKey") "context" $) }}
  {{- else }}
  airflow-secret-key: {{ randAlphaNum 32 | b64enc | b64enc | quote }}
  {{- end }}
  {{- if (include "airflow.isImageMajorVersion3" .) }}
  {{- if or (include "common.secrets.exists" (dict "secret" (include "common.names.fullname" .) "context" $)) (not (empty .Values.auth.jwtSecretKey)) }}
  airflow-jwt-secret-key: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "airflow-jwt-secret-key" "providedValues" (list "auth.jwtSecretKey") "failOnNew" false "context" $) }}
  {{- else }}
  airflow-jwt-secret-key: {{ randAlphaNum 32 | b64enc | b64enc | quote }}
  {{- end }}
  {{- end }}
{{- end }}
