Skip to content

Commit

Permalink
chore: improve docstring for ExtractClientCertificates and other twea…
Browse files Browse the repository at this point in the history
…ks (#5909)
  • Loading branch information
programmer04 authored Apr 26, 2024
1 parent c05b8a6 commit cd42108
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions internal/util/tls/tls_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

// ExtractClientCertificates extracts tls.Certificates from TLSClientConfig.
// It returns nil in case there was no client cert and/or client key provided.
// REVIEW: in case of no certs specified, return nil, nil, OR return non-nil error, OR add a boolean return value?
// In case of no certs specified, it returns nil, nil.
func ExtractClientCertificates(cert []byte, certFile string, key []byte, keyFile string) (*tls.Certificate, error) {
clientCert, err := ValueFromVariableOrFile(cert, certFile)
clientCert, err := valueFromVariableOrFile(cert, certFile)
if err != nil {
return nil, fmt.Errorf("could not extract TLS client cert")
}
clientKey, err := ValueFromVariableOrFile(key, keyFile)
clientKey, err := valueFromVariableOrFile(key, keyFile)
if err != nil {
return nil, fmt.Errorf("could not extract TLS client key")
}
Expand All @@ -30,20 +30,14 @@ func ExtractClientCertificates(cert []byte, certFile string, key []byte, keyFile
return nil, nil
}

// ValueFromVariableOrFile uses v value if it's not empty, and falls back to reading a file content when value is missing.
// valueFromVariableOrFile uses v value if it's not empty, and falls back to reading a file content when value is missing.
// When both are empty, nil is returned.
func ValueFromVariableOrFile(v []byte, file string) ([]byte, error) {
func valueFromVariableOrFile(v []byte, file string) ([]byte, error) {
if len(v) > 0 {
return v, nil
}
if file != "" {
b, err := os.ReadFile(file)
if err != nil {
return nil, err
}

return b, nil
return os.ReadFile(file)
}

return nil, nil
}

0 comments on commit cd42108

Please sign in to comment.