Skip to content

Commit 8fcae27

Browse files
Add repack support for ServerConfig.ClientAuth (#197)
Extend the ClientAuthType unpack method to allow integer values and repack support. Add unit tests to verify behaviour.
1 parent e6d574b commit 8fcae27

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

transport/tlscommon/server_config_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package tlscommon
2020
import (
2121
"testing"
2222

23+
"github.com/elastic/go-ucfg"
2324
"github.com/stretchr/testify/require"
2425
"gopkg.in/yaml.v2"
2526
)
@@ -92,3 +93,83 @@ func Test_ServerConfig_Serialization_ClientAuth(t *testing.T) {
9293
})
9394
}
9495
}
96+
97+
func Test_ServerConfig_Repack(t *testing.T) {
98+
tests := []struct {
99+
name string
100+
yaml string
101+
auth *TLSClientAuth
102+
}{{
103+
name: "with client auth",
104+
yaml: `
105+
enabled: true
106+
verification_mode: certificate
107+
supported_protocols: [TLSv1.1, TLSv1.2]
108+
cipher_suites:
109+
- RSA-AES-256-CBC-SHA
110+
certificate_authorities:
111+
- /path/to/ca.crt
112+
certificate: /path/to/cert.cry
113+
key: /path/to/key/crt
114+
curve_types:
115+
- P-521
116+
client_authentication: optional
117+
ca_sha256:
118+
- example`,
119+
auth: &optional,
120+
}, {
121+
name: "nil client auth",
122+
yaml: `
123+
enabled: true
124+
verification_mode: certificate
125+
supported_protocols: [TLSv1.1, TLSv1.2]
126+
cipher_suites:
127+
- RSA-AES-256-CBC-SHA
128+
certificate_authorities:
129+
- /path/to/ca.crt
130+
certificate: /path/to/cert.cry
131+
key: /path/to/key/crt
132+
curve_types:
133+
- P-521
134+
ca_sha256:
135+
- example`,
136+
auth: &required,
137+
}, {
138+
name: "nil client auth, no cas",
139+
yaml: `
140+
enabled: true
141+
verification_mode: certificate
142+
supported_protocols: [TLSv1.1, TLSv1.2]
143+
cipher_suites:
144+
- RSA-AES-256-CBC-SHA
145+
certificate: /path/to/cert.cry
146+
key: /path/to/key/crt
147+
curve_types:
148+
- P-521
149+
ca_sha256:
150+
- example`,
151+
auth: nil,
152+
}}
153+
154+
for _, tc := range tests {
155+
t.Run(tc.name, func(t *testing.T) {
156+
cfg := mustLoadServerConfig(t, tc.yaml)
157+
if tc.auth != nil {
158+
require.Equal(t, *tc.auth, *cfg.ClientAuth)
159+
} else {
160+
require.Nil(t, cfg.ClientAuth)
161+
}
162+
163+
tmp, err := ucfg.NewFrom(cfg)
164+
require.NoError(t, err)
165+
166+
err = tmp.Unpack(&cfg)
167+
require.NoError(t, err)
168+
if tc.auth != nil {
169+
require.Equal(t, *tc.auth, *cfg.ClientAuth)
170+
} else {
171+
require.Nil(t, cfg.ClientAuth)
172+
}
173+
})
174+
}
175+
}

transport/tlscommon/types.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ func (m *TLSVerificationMode) Unpack(in interface{}) error {
165165
*m = VerifyFull
166166
return nil
167167
}
168-
169168
switch o := in.(type) {
170169
case string:
171170
if o == "" {
@@ -207,17 +206,30 @@ func (m TLSClientAuth) MarshalText() ([]byte, error) {
207206
return nil, fmt.Errorf("could not marshal '%+v' to text", m)
208207
}
209208

210-
func (m *TLSClientAuth) Unpack(s string) error {
211-
if s == "" {
209+
func (m *TLSClientAuth) Unpack(in interface{}) error {
210+
if in == nil {
212211
*m = TLSClientAuthNone
213212
return nil
214213
}
215-
mode, found := tlsClientAuthTypes[s]
216-
if !found {
217-
return fmt.Errorf("unknown client authentication mode '%v'", s)
218-
}
214+
switch o := in.(type) {
215+
case string:
216+
if o == "" {
217+
*m = TLSClientAuthNone
218+
return nil
219+
}
220+
mode, found := tlsClientAuthTypes[o]
221+
if !found {
222+
return fmt.Errorf("unknown client authentication mode '%v'", o)
223+
}
219224

220-
*m = mode
225+
*m = mode
226+
case uint64:
227+
*m = TLSClientAuth(o)
228+
case int64: // underlying type is int so we need both uint64 and int64 as options for TLSClientAuth
229+
*m = TLSClientAuth(o)
230+
default:
231+
return fmt.Errorf("client auth mode is an unknown type: %T", o)
232+
}
221233
return nil
222234
}
223235

0 commit comments

Comments
 (0)