Skip to content

Commit 525cc7c

Browse files
authored
[FIPS] Ensure that TLS connections initiated by Agent use FIPS-compliant TLS versions (#7866)
* Adding test for unsupported TLS versions sent by client * Add TODO for second test * Add validate for client configuration * Add test case for multiple versions * Removing unused code * Running mage fmt * Add test for config hosts validation * Refactoring: extracting out utility function * Remove hosts validation
1 parent 73e5980 commit 525cc7c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

internal/pkg/remote/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ func newClient(
263263
cfg Config,
264264
clients ...*requestClient,
265265
) (*Client, error) {
266+
if err := cfg.Validate(); err != nil {
267+
return nil, fmt.Errorf("invalid configuration: %w", err)
268+
}
269+
266270
// Shuffle so all the agents don't access the hosts in the same order
267271
rand.Shuffle(len(clients), func(i, j int) {
268272
clients[i], clients[j] = clients[j], clients[i]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
//go:build requirefips
6+
7+
package remote
8+
9+
import (
10+
"fmt"
11+
"testing"
12+
13+
"github.com/stretchr/testify/require"
14+
15+
"github.com/elastic/elastic-agent-libs/transport/httpcommon"
16+
"github.com/elastic/elastic-agent-libs/transport/tlscommon"
17+
"github.com/elastic/elastic-agent/pkg/core/logger/loggertest"
18+
)
19+
20+
func TestClientWithUnsupportedTLSVersions(t *testing.T) {
21+
testLogger, _ := loggertest.New("TestClientWithUnsupportedTLSVersions")
22+
const unsupportedErrorMsg = "invalid configuration: unsupported tls version: %s"
23+
24+
cases := map[string]struct {
25+
versions []tlscommon.TLSVersion
26+
expectedErrMsg string
27+
}{
28+
"1.0": {
29+
versions: []tlscommon.TLSVersion{tlscommon.TLSVersion10},
30+
expectedErrMsg: fmt.Sprintf(unsupportedErrorMsg, tlscommon.TLSVersion10),
31+
},
32+
"1.1": {
33+
versions: []tlscommon.TLSVersion{tlscommon.TLSVersion11},
34+
expectedErrMsg: fmt.Sprintf(unsupportedErrorMsg, tlscommon.TLSVersion11),
35+
},
36+
"1.2": {
37+
versions: []tlscommon.TLSVersion{tlscommon.TLSVersion12},
38+
expectedErrMsg: "",
39+
},
40+
"1.3": {
41+
versions: []tlscommon.TLSVersion{tlscommon.TLSVersion13},
42+
expectedErrMsg: "",
43+
},
44+
"1.1,1.2": {
45+
versions: []tlscommon.TLSVersion{tlscommon.TLSVersion11, tlscommon.TLSVersion12},
46+
expectedErrMsg: fmt.Sprintf(unsupportedErrorMsg, tlscommon.TLSVersion11),
47+
},
48+
}
49+
50+
for name, test := range cases {
51+
t.Run(name, func(t *testing.T) {
52+
tlsEnabled := true
53+
config := Config{
54+
Transport: httpcommon.HTTPTransportSettings{
55+
TLS: &tlscommon.Config{
56+
Enabled: &tlsEnabled,
57+
Versions: test.versions,
58+
},
59+
},
60+
}
61+
62+
client, err := NewWithConfig(testLogger, config, nil)
63+
if test.expectedErrMsg == "" {
64+
require.NotNil(t, client)
65+
require.NoError(t, err)
66+
} else {
67+
require.Nil(t, client)
68+
require.Equal(t, test.expectedErrMsg, err.Error())
69+
}
70+
})
71+
}
72+
}

internal/pkg/remote/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,12 @@ func (c *Config) GetHosts() []string {
6666
}
6767
return []string{c.Host}
6868
}
69+
70+
// Validate returns an error if the configuration is invalid; nil, otherwise.
71+
func (c *Config) Validate() error {
72+
if c.Transport.TLS != nil {
73+
return c.Transport.TLS.Validate()
74+
}
75+
76+
return nil
77+
}

0 commit comments

Comments
 (0)