Skip to content

Commit 6ea4f03

Browse files
Add both uint64 and int64 to tlscommon types unpack methods (#198)
Add both uint64 and int64 to tlscommon types unpack methods
1 parent 8fcae27 commit 6ea4f03

File tree

6 files changed

+447
-0
lines changed

6 files changed

+447
-0
lines changed

transport/tlscommon/server_config_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,80 @@ func Test_ServerConfig_Repack(t *testing.T) {
173173
})
174174
}
175175
}
176+
177+
func Test_ServerConfig_RepackJSON(t *testing.T) {
178+
tests := []struct {
179+
name string
180+
json string
181+
auth *TLSClientAuth
182+
}{{
183+
name: "with client auth",
184+
json: `{
185+
"enabled": true,
186+
"verification_mode": "certificate",
187+
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
188+
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
189+
"certificate_authorities": ["/path/to/ca.crt"],
190+
"certificate": "/path/to/cert.crt",
191+
"key": "/path/to/key.crt",
192+
"curve_types": "P-521",
193+
"renegotiation": "freely",
194+
"ca_sha256": ["example"],
195+
"ca_trusted_fingerprint": "fingerprint",
196+
"client_authentication": "optional"
197+
}`,
198+
auth: &optional,
199+
}, {
200+
name: "nil client auth",
201+
json: `{
202+
"enabled": true,
203+
"verification_mode": "certificate",
204+
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
205+
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
206+
"certificate_authorities": ["/path/to/ca.crt"],
207+
"certificate": "/path/to/cert.crt",
208+
"key": "/path/to/key.crt",
209+
"curve_types": "P-521",
210+
"renegotiation": "freely",
211+
"ca_sha256": ["example"],
212+
"ca_trusted_fingerprint": "fingerprint"
213+
}`,
214+
auth: &required,
215+
}, {
216+
name: "nil client auth, no cas",
217+
json: `{
218+
"enabled": true,
219+
"verification_mode": "certificate",
220+
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
221+
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
222+
"certificate": "/path/to/cert.crt",
223+
"key": "/path/to/key.crt",
224+
"curve_types": "P-521",
225+
"renegotiation": "freely",
226+
"ca_sha256": ["example"]
227+
}`,
228+
auth: nil,
229+
}}
230+
231+
for _, tc := range tests {
232+
t.Run(tc.name, func(t *testing.T) {
233+
cfg := mustLoadServerConfigJSON(t, tc.json)
234+
if tc.auth != nil {
235+
require.Equal(t, *tc.auth, *cfg.ClientAuth)
236+
} else {
237+
require.Nil(t, cfg.ClientAuth)
238+
}
239+
240+
tmp, err := ucfg.NewFrom(cfg)
241+
require.NoError(t, err)
242+
243+
err = tmp.Unpack(&cfg)
244+
require.NoError(t, err)
245+
if tc.auth != nil {
246+
require.Equal(t, *tc.auth, *cfg.ClientAuth)
247+
} else {
248+
require.Nil(t, cfg.ClientAuth)
249+
}
250+
})
251+
}
252+
}

transport/tlscommon/tls_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929
"github.com/stretchr/testify/require"
3030

3131
"github.com/elastic/elastic-agent-libs/config"
32+
33+
ucfg "github.com/elastic/go-ucfg"
34+
"github.com/elastic/go-ucfg/json"
3235
)
3336

3437
const (
@@ -76,6 +79,50 @@ func mustLoad(t *testing.T, yamlStr string) *Config {
7679
return cfg
7780
}
7881

82+
// copied from config.fromConfig
83+
func cfgConvert(in *ucfg.Config) *config.C {
84+
return (*config.C)(in)
85+
}
86+
87+
func loadJSON(jsonStr string) (*Config, error) {
88+
var cfg Config
89+
uc, err := json.NewConfig([]byte(jsonStr), ucfg.PathSep("."), ucfg.VarExp)
90+
if err != nil {
91+
return nil, err
92+
}
93+
94+
c := cfgConvert(uc)
95+
96+
if err = c.Unpack(&cfg); err != nil {
97+
return nil, err
98+
}
99+
return &cfg, nil
100+
}
101+
102+
func loadServerConfigJSON(jsonStr string) (*ServerConfig, error) {
103+
var cfg ServerConfig
104+
uc, err := json.NewConfig([]byte(jsonStr), ucfg.PathSep("."), ucfg.VarExp)
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
c := cfgConvert(uc)
110+
111+
if err = c.Unpack(&cfg); err != nil {
112+
return nil, err
113+
}
114+
return &cfg, nil
115+
}
116+
117+
func mustLoadServerConfigJSON(t *testing.T, jsonStr string) *ServerConfig {
118+
t.Helper()
119+
cfg, err := loadServerConfigJSON(jsonStr)
120+
if err != nil {
121+
t.Fatal(err)
122+
}
123+
return cfg
124+
}
125+
79126
func writeTestFile(t *testing.T, content string) string {
80127
t.Helper()
81128
f, err := os.CreateTemp(t.TempDir(), "")
@@ -647,6 +694,7 @@ mrPVWmOCMtwHJrO7kF1ENDgHPkhoZFcpFhu3lzOY7mhpW5mPZPVs87ZmI75G7zMV
647694
AcV8KJqa/7XTTpvIzXePw9FtSSux5SkU6iKAKqwUt82D1E73bbppSg==
648695
-----END CERTIFICATE-----
649696
`
697+
//nolint:gosec // testing key
650698
key := `
651699
-----BEGIN RSA PRIVATE KEY-----
652700
Proc-Type: 4,ENCRYPTED

transport/tlscommon/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ func (m *TLSVerificationMode) Unpack(in interface{}) error {
177177
return fmt.Errorf("unknown verification mode '%v'", o)
178178
}
179179
*m = mode
180+
case int64:
181+
*m = TLSVerificationMode(o)
180182
case uint64:
181183
*m = TLSVerificationMode(o)
182184
default:
@@ -244,6 +246,8 @@ func (cs *CipherSuite) Unpack(i interface{}) error {
244246
}
245247

246248
*cs = suite
249+
case int64:
250+
*cs = CipherSuite(o)
247251
case uint64:
248252
*cs = CipherSuite(o)
249253
default:
@@ -270,6 +274,8 @@ func (ct *tlsCurveType) Unpack(i interface{}) error {
270274
}
271275

272276
*ct = t
277+
case int64:
278+
*ct = tlsCurveType(o)
273279
case uint64:
274280
*ct = tlsCurveType(o)
275281
default:
@@ -296,6 +302,8 @@ func (r *TLSRenegotiationSupport) Unpack(i interface{}) error {
296302
}
297303

298304
*r = t
305+
case int64:
306+
*r = TLSRenegotiationSupport(o)
299307
case uint64:
300308
*r = TLSRenegotiationSupport(o)
301309
default:

0 commit comments

Comments
 (0)