Skip to content

Fix panic in tlsRoundTripper when CA file is absent #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,9 @@ func (t *tlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// using GetClientCertificate.
tlsConfig := t.tlsConfig.Clone()
if !updateRootCA(tlsConfig, caData) {
if t.settings.CA == nil {
return nil, errors.New("unable to use specified CA cert: none configured")
}
return nil, fmt.Errorf("unable to use specified CA cert %s", t.settings.CA.Description())
}
rt, err = t.newRT(tlsConfig)
Expand Down
42 changes: 42 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,48 @@ func TestModifyTLSCertificates(t *testing.T) {
}
}

func TestTLSRoundTripper_NoCAConfigured(t *testing.T) {
bs := getCertificateBlobs(t)

tmpDir, err := os.MkdirTemp("", "tlspanic")
require.NoErrorf(t, err, "Failed to create tmp dir")
defer os.RemoveAll(tmpDir)
cert, key := filepath.Join(tmpDir, "cert"), filepath.Join(tmpDir, "key")

handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, ExpectedMessage)
}
testServer, err := newTestServer(handler)
require.NoError(t, err)
defer testServer.Close()

cfg := HTTPClientConfig{
TLSConfig: TLSConfig{
CertFile: cert,
KeyFile: key,
InsecureSkipVerify: true,
},
}

writeCertificate(bs, ClientCertificatePath, cert)
writeCertificate(bs, ClientKeyNoPassPath, key)
c, err := NewClientFromConfig(cfg, "test")
require.NoErrorf(t, err, "Error creating HTTP Client: %v", err)

req, err := http.NewRequest(http.MethodGet, testServer.URL, nil)
require.NoErrorf(t, err, "Error creating HTTP request: %v", err)

r, err := c.Do(req)
require.NoErrorf(t, err, "Can't connect to the test server")
r.Body.Close()

err = os.WriteFile(cert, []byte("-----BEGIN GARBAGE-----\nabc\n-----END GARBAGE-----\n"), 0o664)
require.NoError(t, err)

_, err = c.Do(req)
require.ErrorContainsf(t, err, "unable to use specified CA cert: none configured", "Expected error to mention missing CA cert")
}

// loadHTTPConfigJSON parses the JSON input s into a HTTPClientConfig.
func loadHTTPConfigJSON(buf []byte) (*HTTPClientConfig, error) {
cfg := &HTTPClientConfig{}
Expand Down
Loading