Add Tailscale certificate resolver

Co-authored-by: Mathieu Lonjaret <mathieu.lonjaret@gmail.com>
This commit is contained in:
Kevin Pollet 2022-09-30 15:20:08 +02:00 committed by GitHub
parent 033fccccc7
commit 38d7011487
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 957 additions and 48 deletions

View file

@ -162,21 +162,32 @@ func (c *Certificate) AppendCertificate(certs map[string]map[string]*tls.Certifi
return err
}
// GetCertificate retrieves Certificate as tls.Certificate.
// GetCertificate returns a tls.Certificate matching the configured CertFile and KeyFile.
func (c *Certificate) GetCertificate() (tls.Certificate, error) {
certContent, err := c.CertFile.Read()
if err != nil {
return tls.Certificate{}, fmt.Errorf("unable to read CertFile : %w", err)
return tls.Certificate{}, fmt.Errorf("unable to read CertFile: %w", err)
}
keyContent, err := c.KeyFile.Read()
if err != nil {
return tls.Certificate{}, fmt.Errorf("unable to read KeyFile : %w", err)
return tls.Certificate{}, fmt.Errorf("unable to read KeyFile: %w", err)
}
cert, err := tls.X509KeyPair(certContent, keyContent)
if err != nil {
return tls.Certificate{}, fmt.Errorf("unable to generate TLS certificate : %w", err)
return tls.Certificate{}, fmt.Errorf("unable to parse TLS certificate: %w", err)
}
return cert, nil
}
// GetCertificateFromBytes returns a tls.Certificate matching the configured CertFile and KeyFile.
// It assumes that the configured CertFile and KeyFile are of byte type.
func (c *Certificate) GetCertificateFromBytes() (tls.Certificate, error) {
cert, err := tls.X509KeyPair([]byte(c.CertFile), []byte(c.KeyFile))
if err != nil {
return tls.Certificate{}, fmt.Errorf("unable to parse TLS certificate: %w", err)
}
return cert, nil