Skip to content

Commit 0c96867

Browse files
authored
Add capability to pass in certs dynamically from memory. (#60)
1 parent b8d1085 commit 0c96867

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pkg/certificate/certificate.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,33 @@ func GenerateSignedCert(ca *KeyPair, hostnames HostNames, commonName string) (*K
165165
return &keyPair, nil
166166
}
167167

168+
// GenerateSignedCertFromFiles generates a new signed certificate signed by the input CA key/cert pair.
169+
func GenerateSignedCertFromFiles(caCertFile string, caKeyFile string,
170+
hostnames HostNames, commonName string,
171+
) (*KeyPair, error) {
172+
cert, err := tls.LoadX509KeyPair(caCertFile, caKeyFile)
173+
if err != nil {
174+
return nil, fmt.Errorf("can't load certificate from `%s` because: %w", caCertFile, err)
175+
}
176+
177+
// Get the certificate bytes (DER)
178+
certBytes := cert.Certificate[0]
179+
180+
// Get the private key bytes (for RSA)
181+
rsaKey, ok := cert.PrivateKey.(*rsa.PrivateKey)
182+
if !ok {
183+
return nil, fmt.Errorf("can't load private from `%s` because: %w", caKeyFile, err)
184+
}
185+
keyBytes := x509.MarshalPKCS1PrivateKey(rsaKey)
186+
187+
caKeyPair := &KeyPair{
188+
Certificate: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes}),
189+
PrivateKey: pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyBytes}),
190+
}
191+
192+
return GenerateSignedCert(caKeyPair, hostnames, commonName)
193+
}
194+
168195
// GenerateCRL will generate a blank Certificate revocation List from the provided issuer certificate.
169196
func GenerateCRL(ca *KeyPair) ([]byte, error) {
170197
tlsKeyPair, err := tls.X509KeyPair(ca.Certificate, ca.PrivateKey)

pkg/service/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33

44
import (
55
"context"
6+
"crypto/tls"
67
"errors"
78
"fmt"
89
"net/http"
@@ -59,6 +60,7 @@ type MiddlewareHandler struct {
5960
type ServerCertificateConfig struct {
6061
CertificateFile string // The TLS certificate file.
6162
KeyFile string // The TLS private key file.
63+
Certificate *tls.Certificate
6264
}
6365

6466
// RateLimitConfig specifies the rate limiting config.
@@ -307,6 +309,16 @@ func (s *Service) Run() error {
307309

308310
go func() {
309311
if s.config.CertConfig != nil {
312+
var tlsConfig *tls.Config
313+
if s.config.CertConfig.Certificate != nil {
314+
tlsConfig = &tls.Config{
315+
MinVersion: tls.VersionTLS12,
316+
Certificates: []tls.Certificate{*s.config.CertConfig.Certificate},
317+
}
318+
}
319+
320+
s.Server.TLSConfig = tlsConfig
321+
310322
err := s.Server.ListenAndServeTLS(s.config.CertConfig.CertificateFile, s.config.CertConfig.KeyFile)
311323
if !errors.Is(err, http.ErrServerClosed) {
312324
logrus.Fatalf("Failed to start query service: %s\n", err)

0 commit comments

Comments
 (0)