getClientCertificate reads the pair of client cert and key and returns a tls.Certificate.
(ctx context.Context, secretManager SecretManager)
| 1427 | |
| 1428 | // getClientCertificate reads the pair of client cert and key and returns a tls.Certificate. |
| 1429 | func (c *TLSConfig) getClientCertificate(ctx context.Context, secretManager SecretManager) (*tls.Certificate, error) { |
| 1430 | var ( |
| 1431 | certData, keyData string |
| 1432 | err error |
| 1433 | ) |
| 1434 | |
| 1435 | certSecret, err := toSecret(secretManager, Secret(c.Cert), c.CertFile, c.CertRef) |
| 1436 | if err != nil { |
| 1437 | return nil, fmt.Errorf("unable to use client cert: %w", err) |
| 1438 | } |
| 1439 | if certSecret != nil { |
| 1440 | certData, err = certSecret.Fetch(ctx) |
| 1441 | if err != nil { |
| 1442 | return nil, fmt.Errorf("unable to read specified client cert: %w", err) |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | keySecret, err := toSecret(secretManager, c.Key, c.KeyFile, c.KeyRef) |
| 1447 | if err != nil { |
| 1448 | return nil, fmt.Errorf("unable to use client key: %w", err) |
| 1449 | } |
| 1450 | if keySecret != nil { |
| 1451 | keyData, err = keySecret.Fetch(ctx) |
| 1452 | if err != nil { |
| 1453 | return nil, fmt.Errorf("unable to read specified client key: %w", err) |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | cert, err := tls.X509KeyPair([]byte(certData), []byte(keyData)) |
| 1458 | if err != nil { |
| 1459 | return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %w", certSecret.Description(), keySecret.Description(), err) |
| 1460 | } |
| 1461 | |
| 1462 | return &cert, nil |
| 1463 | } |
| 1464 | |
| 1465 | // updateRootCA parses the given byte slice as a series of PEM encoded certificates and updates tls.Config.RootCAs. |
| 1466 | func updateRootCA(cfg *tls.Config, b []byte) bool { |
no test coverage detected