Allow option to skip the validation of config

This commit is contained in:
Mariano Cano 2022-08-16 14:04:04 -07:00
parent ae76d943c9
commit 5e0be92273
2 changed files with 17 additions and 7 deletions

View file

@ -72,6 +72,7 @@ type Config struct {
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
Templates *templates.Templates `json:"templates,omitempty"` Templates *templates.Templates `json:"templates,omitempty"`
CommonName string `json:"commonName,omitempty"` CommonName string `json:"commonName,omitempty"`
SkipValidation bool `json:"-"`
} }
// ASN1DN contains ASN1.DN attributes that are used in Subject and Issuer // ASN1DN contains ASN1.DN attributes that are used in Subject and Issuer
@ -201,6 +202,8 @@ func (c *Config) Save(filename string) error {
// Validate validates the configuration. // Validate validates the configuration.
func (c *Config) Validate() error { func (c *Config) Validate() error {
switch { switch {
case c.SkipValidation:
return nil
case c.Address == "": case c.Address == "":
return errors.New("address cannot be empty") return errors.New("address cannot be empty")
case len(c.DNSNames) == 0: case len(c.DNSNames) == 0:

View file

@ -35,9 +35,16 @@ func TestConfigValidate(t *testing.T) {
type ConfigValidateTest struct { type ConfigValidateTest struct {
config *Config config *Config
err error err error
tls TLSOptions tls *TLSOptions
} }
tests := map[string]func(*testing.T) ConfigValidateTest{ tests := map[string]func(*testing.T) ConfigValidateTest{
"skip-validation": func(t *testing.T) ConfigValidateTest {
return ConfigValidateTest{
config: &Config{
SkipValidation: true,
},
}
},
"empty-address": func(t *testing.T) ConfigValidateTest { "empty-address": func(t *testing.T) ConfigValidateTest {
return ConfigValidateTest{ return ConfigValidateTest{
config: &Config{ config: &Config{
@ -128,7 +135,7 @@ func TestConfigValidate(t *testing.T) {
Password: "pass", Password: "pass",
AuthorityConfig: ac, AuthorityConfig: ac,
}, },
tls: DefaultTLSOptions, tls: &DefaultTLSOptions,
} }
}, },
"empty-TLS-values": func(t *testing.T) ConfigValidateTest { "empty-TLS-values": func(t *testing.T) ConfigValidateTest {
@ -143,7 +150,7 @@ func TestConfigValidate(t *testing.T) {
AuthorityConfig: ac, AuthorityConfig: ac,
TLS: &TLSOptions{}, TLS: &TLSOptions{},
}, },
tls: DefaultTLSOptions, tls: &DefaultTLSOptions,
} }
}, },
"custom-tls-values": func(t *testing.T) ConfigValidateTest { "custom-tls-values": func(t *testing.T) ConfigValidateTest {
@ -165,7 +172,7 @@ func TestConfigValidate(t *testing.T) {
Renegotiation: true, Renegotiation: true,
}, },
}, },
tls: TLSOptions{ tls: &TLSOptions{
CipherSuites: CipherSuites{ CipherSuites: CipherSuites{
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305", "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
}, },
@ -209,9 +216,9 @@ func TestConfigValidate(t *testing.T) {
} }
} else { } else {
if assert.Nil(t, tc.err) { if assert.Nil(t, tc.err) {
fmt.Printf("tc.tls = %+v\n", tc.tls) fmt.Printf("tc.tls = %v\n", tc.tls)
fmt.Printf("*tc.config.TLS = %+v\n", *tc.config.TLS) fmt.Printf("*tc.config.TLS = %v\n", tc.config.TLS)
assert.Equals(t, *tc.config.TLS, tc.tls) assert.Equals(t, tc.config.TLS, tc.tls)
} }
} }
}) })