| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package configtls |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"crypto/tls" |
| 9 |
"crypto/x509" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"maps" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"slices" |
| 16 |
"sync" |
| 17 |
"time" |
| 18 |
|
| 19 |
"go.opentelemetry.io/collector/config/configopaque" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
const defaultMinTLSVersion = tls.VersionTLS12 |
| 25 |
|
| 26 |
|
| 27 |
const defaultMaxTLSVersion = 0 |
| 28 |
|
| 29 |
var systemCertPool = x509.SystemCertPool |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
type Config struct { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
CAFile string `mapstructure:"ca_file,omitempty"` |
| 39 |
|
| 40 |
|
| 41 |
CAPem configopaque.String `mapstructure:"ca_pem,omitempty"` |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
IncludeSystemCACertsPool bool `mapstructure:"include_system_ca_certs_pool,omitempty"` |
| 46 |
|
| 47 |
|
| 48 |
CertFile string `mapstructure:"cert_file,omitempty"` |
| 49 |
|
| 50 |
|
| 51 |
CertPem configopaque.String `mapstructure:"cert_pem,omitempty"` |
| 52 |
|
| 53 |
|
| 54 |
KeyFile string `mapstructure:"key_file,omitempty"` |
| 55 |
|
| 56 |
|
| 57 |
KeyPem configopaque.String `mapstructure:"key_pem,omitempty"` |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
MinVersion string `mapstructure:"min_version,omitempty"` |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
MaxVersion string `mapstructure:"max_version,omitempty"` |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
CipherSuites []string `mapstructure:"cipher_suites,omitempty"` |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
ReloadInterval time.Duration `mapstructure:"reload_interval,omitempty"` |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
CurvePreferences []string `mapstructure:"curve_preferences,omitempty"` |
| 80 |
|
| 81 |
|
| 82 |
TPMConfig TPMConfig `mapstructure:"tpm,omitempty"` |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func NewDefaultConfig() Config { |
| 87 |
return Config{} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
type ClientConfig struct { |
| 94 |
|
| 95 |
Config `mapstructure:",squash"` |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Insecure bool `mapstructure:"insecure,omitempty"` |
| 104 |
|
| 105 |
InsecureSkipVerify bool `mapstructure:"insecure_skip_verify,omitempty"` |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
ServerName string `mapstructure:"server_name_override,omitempty"` |
| 110 |
|
| 111 |
_ struct{} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func NewDefaultClientConfig() ClientConfig { |
| 116 |
return ClientConfig{ |
| 117 |
Config: NewDefaultConfig(), |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
type ServerConfig struct { |
| 125 |
|
| 126 |
Config `mapstructure:",squash"` |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
ClientCAFile string `mapstructure:"client_ca_file,omitempty"` |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ReloadClientCAFile bool `mapstructure:"client_ca_file_reload,omitempty"` |
| 138 |
|
| 139 |
_ struct{} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
func NewDefaultServerConfig() ServerConfig { |
| 144 |
return ServerConfig{ |
| 145 |
Config: NewDefaultConfig(), |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
type certReloader struct { |
| 153 |
nextReload time.Time |
| 154 |
cert *tls.Certificate |
| 155 |
lock sync.RWMutex |
| 156 |
tls Config |
| 157 |
} |
| 158 |
|
| 159 |
func (c Config) newCertReloader() (*certReloader, error) { |
| 160 |
cert, err := c.loadCertificate() |
| 161 |
if err != nil { |
| 162 |
return nil, err |
| 163 |
} |
| 164 |
return &certReloader{ |
| 165 |
tls: c, |
| 166 |
nextReload: time.Now().Add(c.ReloadInterval), |
| 167 |
cert: &cert, |
| 168 |
}, nil |
| 169 |
} |
| 170 |
|
| 171 |
func (r *certReloader) GetCertificate() (*tls.Certificate, error) { |
| 172 |
now := time.Now() |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
r.lock.RLock() |
| 177 |
if r.tls.ReloadInterval != 0 && r.nextReload.Before(now) && (r.tls.hasCertFile() || r.tls.hasKeyFile()) { |
| 178 |
|
| 179 |
r.lock.RUnlock() |
| 180 |
r.lock.Lock() |
| 181 |
defer r.lock.Unlock() |
| 182 |
cert, err := r.tls.loadCertificate() |
| 183 |
if err != nil { |
| 184 |
return nil, fmt.Errorf("failed to load TLS cert and key: %w", err) |
| 185 |
} |
| 186 |
r.cert = &cert |
| 187 |
r.nextReload = now.Add(r.tls.ReloadInterval) |
| 188 |
return r.cert, nil |
| 189 |
} |
| 190 |
defer r.lock.RUnlock() |
| 191 |
return r.cert, nil |
| 192 |
} |
| 193 |
|
| 194 |
func (c Config) Validate() error { |
| 195 |
if c.hasCAFile() && c.hasCAPem() { |
| 196 |
return errors.New("provide either a CA file or the PEM-encoded string, but not both") |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
if c.hasCertFile() && c.hasCertPem() { |
| 201 |
return errors.New("provide either certificate file or PEM, but not both") |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
if c.hasKeyFile() && c.hasKeyPem() { |
| 206 |
return errors.New("provide either key file or PEM, but not both") |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if c.hasCert() != c.hasKey() { |
| 211 |
return errors.New("TLS configuration must include both certificate and key (CertFile/CertPem and KeyFile/KeyPem)") |
| 212 |
} |
| 213 |
|
| 214 |
minTLS, err := convertVersion(c.MinVersion, defaultMinTLSVersion) |
| 215 |
if err != nil { |
| 216 |
return fmt.Errorf("invalid TLS min_version: %w", err) |
| 217 |
} |
| 218 |
|
| 219 |
maxTLS, err := convertVersion(c.MaxVersion, defaultMaxTLSVersion) |
| 220 |
if err != nil { |
| 221 |
return fmt.Errorf("invalid TLS max_version: %w", err) |
| 222 |
} |
| 223 |
|
| 224 |
if maxTLS < minTLS && maxTLS != defaultMaxTLSVersion { |
| 225 |
return errors.New("invalid TLS configuration: min_version cannot be greater than max_version") |
| 226 |
} |
| 227 |
|
| 228 |
return nil |
| 229 |
} |
| 230 |
|
| 231 |
func (c ServerConfig) Validate() error { |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if !c.hasCert() && !c.hasKey() { |
| 236 |
return errors.New("TLS configuration must include both certificate and key for server connections") |
| 237 |
} |
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
func (c Config) loadTLSConfig() (*tls.Config, error) { |
| 244 |
certPool, err := c.loadCACertPool() |
| 245 |
if err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
|
| 249 |
var getCertificate func(*tls.ClientHelloInfo) (*tls.Certificate, error) |
| 250 |
var getClientCertificate func(*tls.CertificateRequestInfo) (*tls.Certificate, error) |
| 251 |
if c.hasCert() || c.hasKey() { |
| 252 |
var certReloader *certReloader |
| 253 |
certReloader, err = c.newCertReloader() |
| 254 |
if err != nil { |
| 255 |
return nil, fmt.Errorf("failed to load TLS cert and key: %w", err) |
| 256 |
} |
| 257 |
getCertificate = func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return certReloader.GetCertificate() } |
| 258 |
getClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) { return certReloader.GetCertificate() } |
| 259 |
} |
| 260 |
|
| 261 |
minTLS, err := convertVersion(c.MinVersion, defaultMinTLSVersion) |
| 262 |
if err != nil { |
| 263 |
return nil, fmt.Errorf("invalid TLS min_version: %w", err) |
| 264 |
} |
| 265 |
maxTLS, err := convertVersion(c.MaxVersion, defaultMaxTLSVersion) |
| 266 |
if err != nil { |
| 267 |
return nil, fmt.Errorf("invalid TLS max_version: %w", err) |
| 268 |
} |
| 269 |
cipherSuites, err := convertCipherSuites(c.CipherSuites) |
| 270 |
if err != nil { |
| 271 |
return nil, err |
| 272 |
} |
| 273 |
|
| 274 |
allowedCurves := slices.Collect(maps.Values(tlsCurveTypes)) |
| 275 |
curvePreferences := make([]tls.CurveID, 0, len(c.CurvePreferences)) |
| 276 |
for _, curve := range c.CurvePreferences { |
| 277 |
curveID, ok := tlsCurveTypes[curve] |
| 278 |
if !ok { |
| 279 |
return nil, fmt.Errorf("invalid curve type: %s. Expected values are %s", curveID, allowedCurves) |
| 280 |
} |
| 281 |
curvePreferences = append(curvePreferences, curveID) |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if len(curvePreferences) == 0 { |
| 288 |
curvePreferences = allowedCurves |
| 289 |
} |
| 290 |
|
| 291 |
return &tls.Config{ |
| 292 |
RootCAs: certPool, |
| 293 |
GetCertificate: getCertificate, |
| 294 |
GetClientCertificate: getClientCertificate, |
| 295 |
MinVersion: minTLS, |
| 296 |
MaxVersion: maxTLS, |
| 297 |
CipherSuites: cipherSuites, |
| 298 |
CurvePreferences: curvePreferences, |
| 299 |
}, nil |
| 300 |
} |
| 301 |
|
| 302 |
func convertCipherSuites(cipherSuites []string) ([]uint16, error) { |
| 303 |
var result []uint16 |
| 304 |
var errs []error |
| 305 |
for _, suite := range cipherSuites { |
| 306 |
found := false |
| 307 |
for _, supported := range tls.CipherSuites() { |
| 308 |
if suite == supported.Name { |
| 309 |
result = append(result, supported.ID) |
| 310 |
found = true |
| 311 |
break |
| 312 |
} |
| 313 |
} |
| 314 |
if !found { |
| 315 |
errs = append(errs, fmt.Errorf("invalid TLS cipher suite: %q", suite)) |
| 316 |
} |
| 317 |
} |
| 318 |
return result, errors.Join(errs...) |
| 319 |
} |
| 320 |
|
| 321 |
func (c Config) loadCACertPool() (*x509.CertPool, error) { |
| 322 |
|
| 323 |
|
| 324 |
var err error |
| 325 |
var certPool *x509.CertPool |
| 326 |
|
| 327 |
switch { |
| 328 |
case c.hasCAFile() && c.hasCAPem(): |
| 329 |
return nil, errors.New("failed to load CA CertPool: provide either a CA file or the PEM-encoded string, but not both") |
| 330 |
case c.hasCAFile(): |
| 331 |
|
| 332 |
certPool, err = c.loadCertFile(c.CAFile) |
| 333 |
if err != nil { |
| 334 |
return nil, fmt.Errorf("failed to load CA CertPool File: %w", err) |
| 335 |
} |
| 336 |
case c.hasCAPem(): |
| 337 |
|
| 338 |
certPool, err = c.loadCertPem([]byte(c.CAPem)) |
| 339 |
if err != nil { |
| 340 |
return nil, fmt.Errorf("failed to load CA CertPool PEM: %w", err) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return certPool, nil |
| 345 |
} |
| 346 |
|
| 347 |
func (c Config) loadCertFile(certPath string) (*x509.CertPool, error) { |
| 348 |
certPem, err := os.ReadFile(filepath.Clean(certPath)) |
| 349 |
if err != nil { |
| 350 |
return nil, fmt.Errorf("failed to load cert %s: %w", certPath, err) |
| 351 |
} |
| 352 |
|
| 353 |
return c.loadCertPem(certPem) |
| 354 |
} |
| 355 |
|
| 356 |
func (c Config) loadCertPem(certPem []byte) (*x509.CertPool, error) { |
| 357 |
certPool := x509.NewCertPool() |
| 358 |
if c.IncludeSystemCACertsPool { |
| 359 |
scp, err := systemCertPool() |
| 360 |
if err != nil { |
| 361 |
return nil, err |
| 362 |
} |
| 363 |
if scp != nil { |
| 364 |
certPool = scp |
| 365 |
} |
| 366 |
} |
| 367 |
if !certPool.AppendCertsFromPEM(certPem) { |
| 368 |
return nil, errors.New("failed to parse cert") |
| 369 |
} |
| 370 |
return certPool, nil |
| 371 |
} |
| 372 |
|
| 373 |
func (c Config) loadCertificate() (tls.Certificate, error) { |
| 374 |
switch { |
| 375 |
case c.hasCert() != c.hasKey(): |
| 376 |
return tls.Certificate{}, errors.New("for auth via TLS, provide both certificate and key, or neither") |
| 377 |
case !c.hasCert() && !c.hasKey(): |
| 378 |
return tls.Certificate{}, nil |
| 379 |
case c.hasCertFile() && c.hasCertPem(): |
| 380 |
return tls.Certificate{}, errors.New("for auth via TLS, provide either a certificate or the PEM-encoded string, but not both") |
| 381 |
case c.hasKeyFile() && c.hasKeyPem(): |
| 382 |
return tls.Certificate{}, errors.New("for auth via TLS, provide either a key or the PEM-encoded string, but not both") |
| 383 |
} |
| 384 |
|
| 385 |
var certPem, keyPem []byte |
| 386 |
var err error |
| 387 |
if c.hasCertFile() { |
| 388 |
certPem, err = os.ReadFile(c.CertFile) |
| 389 |
if err != nil { |
| 390 |
return tls.Certificate{}, err |
| 391 |
} |
| 392 |
} else { |
| 393 |
certPem = []byte(c.CertPem) |
| 394 |
} |
| 395 |
|
| 396 |
if c.hasKeyFile() { |
| 397 |
keyPem, err = os.ReadFile(c.KeyFile) |
| 398 |
if err != nil { |
| 399 |
return tls.Certificate{}, err |
| 400 |
} |
| 401 |
} else { |
| 402 |
keyPem = []byte(c.KeyPem) |
| 403 |
} |
| 404 |
|
| 405 |
if c.TPMConfig.Enabled { |
| 406 |
certificate, errTPM := c.TPMConfig.tpmCertificate(keyPem, certPem, openTPM(c.TPMConfig.Path)) |
| 407 |
if errTPM != nil { |
| 408 |
return tls.Certificate{}, fmt.Errorf("failed to load private key from TPM: %w", errTPM) |
| 409 |
} |
| 410 |
return certificate, nil |
| 411 |
} |
| 412 |
|
| 413 |
certificate, errKeyPair := tls.X509KeyPair(certPem, keyPem) |
| 414 |
if errKeyPair != nil { |
| 415 |
return tls.Certificate{}, fmt.Errorf("failed to load TLS cert and key PEMs: %w", errKeyPair) |
| 416 |
} |
| 417 |
return certificate, err |
| 418 |
} |
| 419 |
|
| 420 |
func (c Config) loadCert(caPath string) (*x509.CertPool, error) { |
| 421 |
caPEM, err := os.ReadFile(filepath.Clean(caPath)) |
| 422 |
if err != nil { |
| 423 |
return nil, fmt.Errorf("failed to load CA %s: %w", caPath, err) |
| 424 |
} |
| 425 |
|
| 426 |
var certPool *x509.CertPool |
| 427 |
if c.IncludeSystemCACertsPool { |
| 428 |
if certPool, err = systemCertPool(); err != nil { |
| 429 |
return nil, err |
| 430 |
} |
| 431 |
} |
| 432 |
if certPool == nil { |
| 433 |
certPool = x509.NewCertPool() |
| 434 |
} |
| 435 |
if !certPool.AppendCertsFromPEM(caPEM) { |
| 436 |
return nil, fmt.Errorf("failed to parse CA %s", caPath) |
| 437 |
} |
| 438 |
return certPool, nil |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
func (c ClientConfig) LoadTLSConfig(_ context.Context) (*tls.Config, error) { |
| 443 |
if c.Insecure && !c.hasCA() { |
| 444 |
return nil, nil |
| 445 |
} |
| 446 |
|
| 447 |
tlsCfg, err := c.loadTLSConfig() |
| 448 |
if err != nil { |
| 449 |
return nil, fmt.Errorf("failed to load TLS config: %w", err) |
| 450 |
} |
| 451 |
tlsCfg.ServerName = c.ServerName |
| 452 |
tlsCfg.InsecureSkipVerify = c.InsecureSkipVerify |
| 453 |
return tlsCfg, nil |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
func (c ServerConfig) LoadTLSConfig(_ context.Context) (*tls.Config, error) { |
| 458 |
tlsCfg, err := c.loadTLSConfig() |
| 459 |
if err != nil { |
| 460 |
return nil, fmt.Errorf("failed to load TLS config: %w", err) |
| 461 |
} |
| 462 |
if c.ClientCAFile != "" { |
| 463 |
reloader, err := newClientCAsReloader(c.ClientCAFile, &c) |
| 464 |
if err != nil { |
| 465 |
return nil, err |
| 466 |
} |
| 467 |
if c.ReloadClientCAFile { |
| 468 |
err = reloader.startWatching() |
| 469 |
if err != nil { |
| 470 |
return nil, err |
| 471 |
} |
| 472 |
tlsCfg.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) { return reloader.getClientConfig(tlsCfg) } |
| 473 |
} |
| 474 |
tlsCfg.ClientCAs = reloader.certPool |
| 475 |
tlsCfg.ClientAuth = tls.RequireAndVerifyClientCert |
| 476 |
} |
| 477 |
return tlsCfg, nil |
| 478 |
} |
| 479 |
|
| 480 |
func (c ServerConfig) loadClientCAFile() (*x509.CertPool, error) { |
| 481 |
return c.loadCert(c.ClientCAFile) |
| 482 |
} |
| 483 |
|
| 484 |
func (c Config) hasCA() bool { return c.hasCAFile() || c.hasCAPem() } |
| 485 |
func (c Config) hasCert() bool { return c.hasCertFile() || c.hasCertPem() } |
| 486 |
func (c Config) hasKey() bool { return c.hasKeyFile() || c.hasKeyPem() } |
| 487 |
|
| 488 |
func (c Config) hasCAFile() bool { return c.CAFile != "" } |
| 489 |
func (c Config) hasCAPem() bool { return len(c.CAPem) != 0 } |
| 490 |
|
| 491 |
func (c Config) hasCertFile() bool { return c.CertFile != "" } |
| 492 |
func (c Config) hasCertPem() bool { return len(c.CertPem) != 0 } |
| 493 |
|
| 494 |
func (c Config) hasKeyFile() bool { return c.KeyFile != "" } |
| 495 |
func (c Config) hasKeyPem() bool { return len(c.KeyPem) != 0 } |
| 496 |
|
| 497 |
func convertVersion(v string, defaultVersion uint16) (uint16, error) { |
| 498 |
|
| 499 |
if v == "" { |
| 500 |
return defaultVersion, nil |
| 501 |
} |
| 502 |
val, ok := tlsVersions[v] |
| 503 |
if !ok { |
| 504 |
return 0, fmt.Errorf("unsupported TLS version: %q", v) |
| 505 |
} |
| 506 |
return val, nil |
| 507 |
} |
| 508 |
|
| 509 |
var tlsVersions = map[string]uint16{ |
| 510 |
"1.0": tls.VersionTLS10, |
| 511 |
"1.1": tls.VersionTLS11, |
| 512 |
"1.2": tls.VersionTLS12, |
| 513 |
"1.3": tls.VersionTLS13, |
| 514 |
} |