Skip to content

Commit 31db083

Browse files
authored
Setup cert-watcher for TA server cert (#264)
1 parent 64e55f3 commit 31db083

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

cmd/amazon-cloudwatch-agent-target-allocator/config/config.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
package config
55

66
import (
7+
"context"
78
"crypto/tls"
89
"crypto/x509"
910
"errors"
1011
"fmt"
1112
"io/fs"
1213
"os"
14+
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
1315
"time"
1416

1517
"github.com/go-logr/logr"
@@ -207,26 +209,31 @@ func Load() (*Config, string, error) {
207209

208210
// ValidateConfig validates the cli and file configs together.
209211
func ValidateConfig(config *Config) error {
210-
scrapeConfigsPresent := (config.PromConfig != nil && len(config.PromConfig.ScrapeConfigs) > 0)
212+
scrapeConfigsPresent := config.PromConfig != nil && len(config.PromConfig.ScrapeConfigs) > 0
211213
if !(config.PrometheusCR.Enabled || scrapeConfigsPresent) {
212214
return fmt.Errorf("at least one scrape config must be defined, or Prometheus CR watching must be enabled")
213215
}
214216
return nil
215217
}
216218

217-
func (c HTTPSServerConfig) NewTLSConfig() (*tls.Config, error) {
218-
cert, err := tls.LoadX509KeyPair(c.TLSCertFilePath, c.TLSKeyFilePath)
219+
func (c HTTPSServerConfig) NewTLSConfig(ctx context.Context) (*tls.Config, error) {
220+
tlsConfig := &tls.Config{
221+
MinVersion: tls.VersionTLS13,
222+
}
223+
224+
certWatcher, err := certwatcher.New(c.TLSCertFilePath, c.TLSKeyFilePath)
219225
if err != nil {
220226
return nil, err
221227
}
222-
tlsConfig := &tls.Config{
223-
Certificates: []tls.Certificate{cert},
224-
ClientAuth: tls.NoClientCert,
225-
MinVersion: tls.VersionTLS12,
226-
}
228+
tlsConfig.GetCertificate = certWatcher.GetCertificate
229+
go func() {
230+
_ = certWatcher.Start(ctx)
231+
}()
232+
227233
if c.CAFilePath == "" {
228234
return tlsConfig, nil
229235
}
236+
230237
caCert, err := os.ReadFile(c.CAFilePath)
231238
caCertPool := x509.NewCertPool()
232239
if err != nil {

cmd/amazon-cloudwatch-agent-target-allocator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080
}
8181

8282
httpOptions := []server.Option{}
83-
tlsConfig, confErr := cfg.HTTPS.NewTLSConfig()
83+
tlsConfig, confErr := cfg.HTTPS.NewTLSConfig(ctx)
8484
if confErr != nil {
8585
setupLog.Error(confErr, "Unable to initialize TLS configuration", "Config", cfg.HTTPS)
8686
os.Exit(1)

0 commit comments

Comments
 (0)