MCPcopy Create free account
hub / github.com/prometheus/common / Validate

Method Validate

config/http_config.go:1378–1396  ·  view source on GitHub ↗

Validate validates the TLSConfig to check that only one of the inlined or file-based fields for the TLS CA, client certificate, and client key are used.

()

Source from the content-addressed store, hash-verified

1376// file-based fields for the TLS CA, client certificate, and client key are
1377// used.
1378func (c *TLSConfig) Validate() error {
1379 if nonZeroCount(len(c.CA) > 0, len(c.CAFile) > 0, len(c.CARef) > 0) > 1 {
1380 return errors.New("at most one of ca, ca_file & ca_ref must be configured")
1381 }
1382 if nonZeroCount(len(c.Cert) > 0, len(c.CertFile) > 0, len(c.CertRef) > 0) > 1 {
1383 return errors.New("at most one of cert, cert_file & cert_ref must be configured")
1384 }
1385 if nonZeroCount(len(c.Key) > 0, len(c.KeyFile) > 0, len(c.KeyRef) > 0) > 1 {
1386 return errors.New("at most one of key and key_file must be configured")
1387 }
1388
1389 if c.usingClientCert() && !c.usingClientKey() {
1390 return errors.New("exactly one of key or key_file must be configured when a client certificate is configured")
1391 } else if c.usingClientKey() && !c.usingClientCert() {
1392 return errors.New("exactly one of cert or cert_file must be configured when a client key is configured")
1393 }
1394
1395 return nil
1396}
1397
1398func (c *TLSConfig) usingClientCert() bool {
1399 return len(c.Cert) > 0 || len(c.CertFile) > 0 || len(c.CertRef) > 0

Callers 5

UnmarshalYAMLMethod · 0.95
UnmarshalYAMLMethod · 0.45
UnmarshalJSONMethod · 0.45
ValidateMethod · 0.45
NewTLSConfigWithContextFunction · 0.45

Calls 3

usingClientCertMethod · 0.95
usingClientKeyMethod · 0.95
nonZeroCountFunction · 0.85

Tested by

no test coverage detected