MCPcopy Create free account
hub / github.com/prometheus/common / NewTLSConfigWithContext

Function NewTLSConfigWithContext

config/http_config.go:1263–1321  ·  view source on GitHub ↗

NewTLSConfigWithContext creates a new tls.Config from the given TLSConfig.

(ctx context.Context, cfg *TLSConfig, optFuncs ...TLSConfigOption)

Source from the content-addressed store, hash-verified

1261
1262// NewTLSConfigWithContext creates a new tls.Config from the given TLSConfig.
1263func NewTLSConfigWithContext(ctx context.Context, cfg *TLSConfig, optFuncs ...TLSConfigOption) (*tls.Config, error) {
1264 opts := tlsConfigOptions{}
1265 for _, opt := range optFuncs {
1266 opt.applyToTLSConfigOptions(&opts)
1267 }
1268
1269 if err := cfg.Validate(); err != nil {
1270 return nil, err
1271 }
1272
1273 tlsConfig := &tls.Config{
1274 InsecureSkipVerify: cfg.InsecureSkipVerify,
1275 MinVersion: uint16(cfg.MinVersion),
1276 MaxVersion: uint16(cfg.MaxVersion),
1277 }
1278
1279 if cfg.MaxVersion != 0 && cfg.MinVersion != 0 {
1280 if cfg.MaxVersion < cfg.MinVersion {
1281 return nil, errors.New("tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified")
1282 }
1283 }
1284
1285 // If a CA cert is provided then let's read it in so we can validate the
1286 // scrape target's certificate properly.
1287 caSecret, err := toSecret(opts.secretManager, Secret(cfg.CA), cfg.CAFile, cfg.CARef)
1288 if err != nil {
1289 return nil, fmt.Errorf("unable to use CA cert: %w", err)
1290 }
1291 if caSecret != nil {
1292 ca, err := caSecret.Fetch(ctx)
1293 if err != nil {
1294 return nil, fmt.Errorf("unable to read CA cert: %w", err)
1295 }
1296 if !updateRootCA(tlsConfig, []byte(ca)) {
1297 return nil, fmt.Errorf("unable to use specified CA cert %s", caSecret.Description())
1298 }
1299 }
1300
1301 if len(cfg.ServerName) > 0 {
1302 tlsConfig.ServerName = cfg.ServerName
1303 }
1304
1305 // If a client cert & key is provided then configure TLS config accordingly.
1306 if cfg.usingClientCert() && cfg.usingClientKey() {
1307 // Verify that client cert and key are valid.
1308 if _, err := cfg.getClientCertificate(ctx, opts.secretManager); err != nil {
1309 return nil, err
1310 }
1311 tlsConfig.GetClientCertificate = func(cri *tls.CertificateRequestInfo) (*tls.Certificate, error) {
1312 var ctx context.Context
1313 if cri != nil {
1314 ctx = cri.Context()
1315 }
1316 return cfg.getClientCertificate(ctx, opts.secretManager)
1317 }
1318 }
1319
1320 return tlsConfig, nil

Callers 1

NewTLSConfigFunction · 0.85

Calls 10

toSecretFunction · 0.85
SecretTypeAlias · 0.85
updateRootCAFunction · 0.85
usingClientCertMethod · 0.80
usingClientKeyMethod · 0.80
getClientCertificateMethod · 0.80
FetchMethod · 0.65
DescriptionMethod · 0.65
ValidateMethod · 0.45

Tested by

no test coverage detected