From cf33b7c9de10242419310690b82a21015f550ddb Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Fri, 8 Dec 2023 02:08:19 +0200 Subject: [PATCH] add validation --- .../templates/lib/certificate/_validation.tpl | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 library/common/templates/lib/certificate/_validation.tpl diff --git a/library/common/templates/lib/certificate/_validation.tpl b/library/common/templates/lib/certificate/_validation.tpl new file mode 100644 index 00000000..9e84d102 --- /dev/null +++ b/library/common/templates/lib/certificate/_validation.tpl @@ -0,0 +1,49 @@ +{{/* Certificate Validation */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.certificate.validation" (dict "rootCtx" $ "objectData" $objectData) -}} +objectData: + rootCtx: The root context of the chart. + objectData: The Certificate object. +*/}} + +{{- define "tc.v1.common.lib.certificate.validation" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- if not $objectData.certificateIssuer -}} + {{- fail "Cert Manager Certificate - Expected non-empty [certificateIssuer]" -}} + {{- end -}} + + {{- if not $objectData.hosts -}} + {{- fail "Cert Manager Certificate - Expected non-empty [hosts]" -}} + {{- end -}} + + {{- if not (kindIs "slice" $objectData.hosts) -}} + {{- fail (printf "Cert Manager Certificate - Expected [hosts] to be a [slice], but got [%s]" (kindOf $objectData.hosts)) -}} + {{- end -}} + + {{- range $h := $objectData.hosts -}} + {{- if not $h -}} + {{- fail "Cert Manager Certificate - Expected non-empty entry in [hosts]" -}} + {{- end -}} + + {{- $host := tpl $h $rootCtx -}} + {{- if (hasPrefix "http://" $host) -}} + {{- fail (printf "Cert Manager Certificate - Expected entry in [hosts] to not start with [http://], but got [%s]" $host) -}} + {{- end -}} + {{- if (hasPrefix "https://" $host) -}} + {{- fail (printf "Cert Manager Certificate - Expected entry in [hosts] to not start with [https://], but got [%s]" $host) -}} + {{- end -}} + {{- if (contains ":" $host) -}} + {{- fail (printf "Cert Manager Certificate - Expected entry in [hosts] to not contain [:], but got [%s]" $host) -}} + {{- end -}} + + {{- with $objectData.certificateSecretTemplate -}} + {{- if and (not .labels) (not .annotations) -}} + {{- fail "Cert Manager Certificate - Expected [certificateSecretTemplate] to have at least one of [labels, annotations]" -}} + {{- end -}} + + {{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData.certificateSecretTemplate "caller" "Cert Manager Certificate (certificateSecretTemplate)") -}} + {{- end -}} + {{- end -}} +{{- end -}}