certwarden-deploy/internal/configuration/validation.go
2024-07-11 16:22:58 +02:00

38 lines
953 B
Go

package configuration
import (
"regexp"
)
// IsValid tests if the config read from file has all required parameters set.
//
// Exits the app if errors are detected
func (c *ConfigFileData) IsValid() ConfigValidationError {
err := ConfigValidationError{}
if c.BaseURL == "" {
err.Add(`Field 'base_url' in config file is required!`)
}
for _, cert := range c.Certificates {
if cert.Name == "" {
cert.Name = "unnamed_certificate"
err.Add(`Field 'name' for certificates cannot be blank!`)
}
if cert.CertificateSecret == "" {
err.Add(`Field 'cert_secret' for certificate ` + cert.Name + " cannot be blank!")
}
if cert.CertificatePath == "" {
err.Add(`Field 'cert_path' for certificate ` + cert.Name + " cannot be blank!")
}
re := regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
if !re.MatchString(cert.Name) {
err.Add(`Field 'name' for certificate may only contain -_. and alphanumeric characters!`)
}
}
return err
}