Skip to content

Commit 0d3aa06

Browse files
fix: increase default TLS Handshake Timeout to 63s (#1237)
* chore: add flag for TLS timeout Signed-off-by: Darren Murray <[email protected]> * refactor: set transport tls timeout as default Signed-off-by: Darren Murray <[email protected]> * style: linting Signed-off-by: Darren Murray <[email protected]> * refactor: address code review comments Signed-off-by: Darren Murray <[email protected]> --------- Signed-off-by: Darren Murray <[email protected]>
1 parent e9a4bd4 commit 0d3aa06

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

api/client.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import (
3232
"go.uber.org/zap"
3333
)
3434

35-
const defaultTimeout = 60 * time.Second
35+
const (
36+
defaultTimeout = 60 * time.Second
37+
defaultTLSTimeout = 63 * time.Second
38+
)
3639

3740
type Client struct {
3841
id string
@@ -99,7 +102,8 @@ func NewClient(account string, opts ...Option) (*Client, error) {
99102
auth: &authConfig{
100103
expiration: DefaultTokenExpiryTime,
101104
},
102-
c: &http.Client{Timeout: defaultTimeout},
105+
c: &http.Client{Timeout: defaultTimeout,
106+
Transport: &http.Transport{TLSHandshakeTimeout: defaultTLSTimeout}},
103107
}
104108

105109
c.V2 = NewV2Endpoints(c)
@@ -172,6 +176,14 @@ func WithTimeout(timeout time.Duration) Option {
172176
})
173177
}
174178

179+
// WithTransport changes the default transport to increase TLSHandshakeTimeout
180+
func WithTransport(transport *http.Transport) Option {
181+
return clientFunc(func(c *Client) error {
182+
c.c.Transport = transport
183+
return nil
184+
})
185+
}
186+
175187
// WithURL sets the base URL, this options is only available for test purposes
176188
func WithURL(baseURL string) Option {
177189
return clientFunc(func(c *Client) error {

api/client_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package api_test
2020

2121
import (
22+
"crypto/tls"
2223
"fmt"
2324
"net/http"
2425
"testing"
@@ -190,3 +191,44 @@ func TestNewClientWithoutOrgAccess(t *testing.T) {
190191
assert.Equal(t, false, c.OrgAccess(), "org access should be set to false")
191192

192193
}
194+
195+
func TestTLSHandshakeTimeout(t *testing.T) {
196+
fakeServer := lacework.MockUnstartedServer()
197+
fakeServer.Server.TLS = &tls.Config{InsecureSkipVerify: true}
198+
fakeServer.UseApiV2()
199+
apiPath := "AlertChannels"
200+
fakeServer.MockToken("TOKEN")
201+
fakeServer.Server.StartTLS()
202+
defer fakeServer.Close()
203+
204+
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) {
205+
time.Sleep(time.Second * 1)
206+
fmt.Fprintf(w, "{}")
207+
})
208+
209+
shortTimeout := &http.Transport{TLSHandshakeTimeout: time.Millisecond * 1,
210+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
211+
212+
client, err := api.NewClient("test",
213+
api.WithApiV2(),
214+
api.WithToken("TOKEN"),
215+
api.WithURL(fakeServer.URL()),
216+
api.WithTransport(shortTimeout),
217+
)
218+
219+
_, err = client.V2.AlertChannels.List()
220+
assert.ErrorContains(t, err, "TLS handshake timeout", "Expected TLS timeout error")
221+
222+
longTimeout := &http.Transport{TLSHandshakeTimeout: time.Second * 2,
223+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
224+
225+
clientWithTimeout, err := api.NewClient("test",
226+
api.WithApiV2(),
227+
api.WithToken("TOKEN"),
228+
api.WithURL(fakeServer.URL()),
229+
api.WithTransport(longTimeout),
230+
)
231+
232+
_, err = clientWithTimeout.V2.AlertChannels.List()
233+
assert.NoError(t, err)
234+
}

internal/lacework/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ func MockServer() *Mock {
5757
}
5858
}
5959

60+
func MockUnstartedServer() *Mock {
61+
mux := http.NewServeMux()
62+
return &Mock{
63+
Mux: mux,
64+
Server: httptest.NewUnstartedServer(mux),
65+
ApiVersion: "v2",
66+
}
67+
}
68+
6069
func (m *Mock) UseApiV2() {
6170
m.ApiVersion = "v2"
6271
}

0 commit comments

Comments
 (0)