Skip to content

Commit 0f3c348

Browse files
authored
Merge pull request #908 from machine424/ttlsco
fix(http_config): fix client cert rotation when no CA is configured
2 parents ce9215c + 732a9cf commit 0f3c348

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

config/http_config.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,18 +1476,15 @@ func (t *tlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
14761476
rt := t.rt
14771477
t.mtx.RUnlock()
14781478
if equal {
1479-
// The CA cert hasn't changed, use the existing RoundTripper.
1479+
// The TLS materials (CA, cert, key) haven't changed, use the existing RoundTripper.
14801480
return rt.RoundTrip(req)
14811481
}
14821482

14831483
// Create a new RoundTripper.
14841484
// The cert and key files are read separately by the client
14851485
// using GetClientCertificate.
14861486
tlsConfig := t.tlsConfig.Clone()
1487-
if !updateRootCA(tlsConfig, caData) {
1488-
if t.settings.CA == nil {
1489-
return nil, errors.New("unable to use specified CA cert: none configured")
1490-
}
1487+
if t.settings.CA != nil && !updateRootCA(tlsConfig, caData) {
14911488
return nil, fmt.Errorf("unable to use specified CA cert %s", t.settings.CA.Description())
14921489
}
14931490
rt, err = t.newRT(tlsConfig)

config/http_config_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,11 +2064,13 @@ func TestModifyTLSCertificates(t *testing.T) {
20642064
}
20652065
}
20662066

2067+
// TestTLSRoundTripper_NoCAConfigured verifies that client cert rotation works
2068+
// when no ca_file is configured. Regression test for prometheus/prometheus#16622.
20672069
func TestTLSRoundTripper_NoCAConfigured(t *testing.T) {
20682070
bs := getCertificateBlobs(t)
20692071

2070-
tmpDir, err := os.MkdirTemp("", "tlspanic")
2071-
require.NoErrorf(t, err, "Failed to create tmp dir")
2072+
tmpDir, err := os.MkdirTemp("", "tlsnocacert")
2073+
require.NoError(t, err)
20722074
defer os.RemoveAll(tmpDir)
20732075
cert, key := filepath.Join(tmpDir, "cert"), filepath.Join(tmpDir, "key")
20742076

@@ -2090,20 +2092,24 @@ func TestTLSRoundTripper_NoCAConfigured(t *testing.T) {
20902092
writeCertificate(bs, ClientCertificatePath, cert)
20912093
writeCertificate(bs, ClientKeyNoPassPath, key)
20922094
c, err := NewClientFromConfig(cfg, "test")
2093-
require.NoErrorf(t, err, "Error creating HTTP Client: %v", err)
2095+
require.NoError(t, err)
20942096

20952097
req, err := http.NewRequest(http.MethodGet, testServer.URL, nil)
2096-
require.NoErrorf(t, err, "Error creating HTTP request: %v", err)
2098+
require.NoError(t, err)
20972099

20982100
r, err := c.Do(req)
2099-
require.NoErrorf(t, err, "Can't connect to the test server")
2101+
require.NoErrorf(t, err, "request should succeed before cert rotation")
21002102
r.Body.Close()
21012103

2102-
err = os.WriteFile(cert, []byte("-----BEGIN GARBAGE-----\nabc\n-----END GARBAGE-----\n"), 0o664)
2103-
require.NoError(t, err)
2104+
// Rotate the cert/key files to different (but still valid) certs.
2105+
// Tthe next RoundTrip should rebuild the transport.
2106+
writeCertificate(bs, ServerCertificatePath, cert)
2107+
writeCertificate(bs, ServerKeyPath, key)
21042108

2105-
_, err = c.Do(req)
2106-
require.ErrorContainsf(t, err, "unable to use specified CA cert: none configured", "Expected error to mention missing CA cert")
2109+
// The request still succeeds after cert rotation.
2110+
r, err = c.Do(req)
2111+
require.NoErrorf(t, err, "request should succeed after cert rotation without ca_file")
2112+
r.Body.Close()
21072113
}
21082114

21092115
// loadHTTPConfigJSON parses the JSON input s into a HTTPClientConfig.

0 commit comments

Comments
 (0)