certwarden-deploy/internal/configuration/validation.go

39 lines
953 B
Go
Raw Permalink Normal View History

2024-07-03 11:56:04 +02:00
package configuration
import (
"regexp"
)
2024-07-11 16:22:58 +02:00
// 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{}
2024-07-03 11:56:04 +02:00
2024-07-11 16:22:58 +02:00
if c.BaseURL == "" {
err.Add(`Field 'base_url' in config file is required!`)
2024-07-03 11:56:04 +02:00
}
2024-07-11 16:22:58 +02:00
for _, cert := range c.Certificates {
2024-07-03 11:56:04 +02:00
if cert.Name == "" {
cert.Name = "unnamed_certificate"
2024-07-11 16:22:58 +02:00
err.Add(`Field 'name' for certificates cannot be blank!`)
2024-07-03 11:56:04 +02:00
}
2024-07-11 16:22:58 +02:00
if cert.CertificateSecret == "" {
err.Add(`Field 'cert_secret' for certificate ` + cert.Name + " cannot be blank!")
2024-07-03 11:56:04 +02:00
}
2024-07-11 16:22:58 +02:00
if cert.CertificatePath == "" {
err.Add(`Field 'cert_path' for certificate ` + cert.Name + " cannot be blank!")
2024-07-03 11:56:04 +02:00
}
re := regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
if !re.MatchString(cert.Name) {
2024-07-11 16:22:58 +02:00
err.Add(`Field 'name' for certificate may only contain -_. and alphanumeric characters!`)
2024-07-03 11:56:04 +02:00
}
}
2024-07-11 16:22:58 +02:00
return err
2024-07-03 11:56:04 +02:00
}