Skip to content

Commit 39f6a75

Browse files
committed
add tls min/max version to grpc proxy
This adds the min and max TLS version support from #13506 and #15156 to the grpc proxy. Fixes #13506 Signed-off-by: Thomas Jungblut <[email protected]>
1 parent 507c0de commit 39f6a75

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

server/etcdmain/grpc_proxy.go

+58-15
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,16 @@ var (
7878

7979
// tls for clients connecting to proxy
8080

81-
grpcProxyListenCA string
82-
grpcProxyListenCert string
83-
grpcProxyListenKey string
84-
grpcProxyListenCipherSuites []string
85-
grpcProxyListenAutoTLS bool
86-
grpcProxyListenCRL string
87-
selfSignedCertValidity uint
81+
grpcProxyListenCA string
82+
grpcProxyListenCert string
83+
grpcProxyListenKey string
84+
grpcProxyListenCipherSuites []string
85+
grpcProxyListenAutoTLS bool
86+
grpcProxyListenCRL string
87+
grpcProxyListenTLSMinVersion string
88+
grpcProxyListenTLSMaxVersion string
89+
90+
selfSignedCertValidity uint
8891

8992
grpcProxyAdvertiseClientURL string
9093
grpcProxyResolverPrefix string
@@ -166,6 +169,8 @@ func newGRPCProxyStartCommand() *cobra.Command {
166169
cmd.Flags().BoolVar(&grpcProxyListenAutoTLS, "auto-tls", false, "proxy TLS using generated certificates")
167170
cmd.Flags().StringVar(&grpcProxyListenCRL, "client-crl-file", "", "proxy client certificate revocation list file.")
168171
cmd.Flags().UintVar(&selfSignedCertValidity, "self-signed-cert-validity", 1, "The validity period of the proxy certificates, unit is year")
172+
cmd.Flags().StringVar(&grpcProxyListenTLSMinVersion, "tls-min-version", string(tlsutil.TLSVersion12), "Minimum TLS version supported by grpc proxy. Possible values: TLS1.2, TLS1.3.")
173+
cmd.Flags().StringVar(&grpcProxyListenTLSMaxVersion, "tls-max-version", string(tlsutil.TLSVersionDefault), "Maximum TLS version supported by grpc proxy. Possible values: TLS1.2, TLS1.3 (empty defers to Go).")
169174

170175
// experimental flags
171176
cmd.Flags().BoolVar(&grpcProxyEnableOrdering, "experimental-serializable-ordering", false, "Ensure serializable reads have monotonically increasing store revisions across endpoints.")
@@ -197,13 +202,6 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
197202
// The empty CN is required for grpcProxyCert.
198203
// Please see https://github.com/etcd-io/etcd/issues/11970#issuecomment-687875315 for more context.
199204
tlsInfo := newTLS(grpcProxyListenCA, grpcProxyListenCert, grpcProxyListenKey, false)
200-
if len(grpcProxyListenCipherSuites) > 0 {
201-
cs, err := tlsutil.GetCipherSuites(grpcProxyListenCipherSuites)
202-
if err != nil {
203-
log.Fatal(err)
204-
}
205-
tlsInfo.CipherSuites = cs
206-
}
207205
if tlsInfo == nil && grpcProxyListenAutoTLS {
208206
host := []string{"https://" + grpcProxyListenAddr}
209207
dir := filepath.Join(grpcProxyDataDir, "fixtures", "proxy")
@@ -213,10 +211,32 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
213211
}
214212
tlsInfo = &autoTLS
215213
}
216-
217214
if tlsInfo != nil {
215+
if len(grpcProxyListenCipherSuites) > 0 {
216+
cs, err := tlsutil.GetCipherSuites(grpcProxyListenCipherSuites)
217+
if err != nil {
218+
log.Fatal(err)
219+
}
220+
tlsInfo.CipherSuites = cs
221+
}
222+
if grpcProxyListenTLSMinVersion != "" {
223+
version, err := tlsutil.GetTLSVersion(grpcProxyListenTLSMinVersion)
224+
if err != nil {
225+
log.Fatal(err)
226+
}
227+
tlsInfo.MinVersion = version
228+
}
229+
if grpcProxyListenTLSMaxVersion != "" {
230+
version, err := tlsutil.GetTLSVersion(grpcProxyListenTLSMaxVersion)
231+
if err != nil {
232+
log.Fatal(err)
233+
}
234+
tlsInfo.MaxVersion = version
235+
}
236+
218237
lg.Info("gRPC proxy server TLS", zap.String("tls-info", fmt.Sprintf("%+v", tlsInfo)))
219238
}
239+
220240
m := mustListenCMux(lg, tlsInfo)
221241
grpcl := m.Match(cmux.HTTP2())
222242
defer func() {
@@ -290,6 +310,29 @@ func checkArgs() {
290310
fmt.Fprintln(os.Stderr, fmt.Errorf("selfSignedCertValidity is invalid,it should be greater than 0"))
291311
os.Exit(1)
292312
}
313+
314+
minVersion, err := tlsutil.GetTLSVersion(grpcProxyListenTLSMinVersion)
315+
if err != nil {
316+
fmt.Fprintln(os.Stderr, fmt.Errorf("tls-min-version is invalid: %w", err))
317+
os.Exit(1)
318+
}
319+
maxVersion, err := tlsutil.GetTLSVersion(grpcProxyListenTLSMaxVersion)
320+
if err != nil {
321+
fmt.Fprintln(os.Stderr, fmt.Errorf("tls-max-version is invalid: %w", err))
322+
os.Exit(1)
323+
}
324+
325+
// maxVersion == 0 means that Go selects the highest available version.
326+
if maxVersion != 0 && minVersion > maxVersion {
327+
fmt.Fprintln(os.Stderr, fmt.Errorf("min version (%s) is greater than max version (%s)", grpcProxyListenTLSMinVersion, grpcProxyListenTLSMaxVersion))
328+
os.Exit(1)
329+
}
330+
331+
// Check if user attempted to configure ciphers for TLS1.3 only: Go does not support that currently.
332+
if minVersion == tls.VersionTLS13 && len(grpcProxyListenCipherSuites) > 0 {
333+
fmt.Fprintln(os.Stderr, fmt.Errorf("cipher suites cannot be configured when only TLS1.3 is enabled"))
334+
os.Exit(1)
335+
}
293336
}
294337

295338
func mustNewClient(lg *zap.Logger) *clientv3.Client {

tests/e2e/etcd_grpcproxy_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"testing"
2424
"time"
2525

26+
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/require"
2728
"go.etcd.io/etcd/api/v3/etcdserverpb"
2829
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
@@ -126,6 +127,38 @@ func TestGrpcProxyAutoSync(t *testing.T) {
126127
require.NoError(t, proxyProc.Stop())
127128
}
128129

130+
func TestGrpcProxyTLSVersions(t *testing.T) {
131+
e2e.SkipInShortMode(t)
132+
133+
epc, err := e2e.NewEtcdProcessCluster(t, e2e.NewConfigClientBoth())
134+
require.NoError(t, err)
135+
defer func() {
136+
assert.NoError(t, epc.Close())
137+
}()
138+
139+
var (
140+
node1ClientURL = epc.Procs[0].Config().ClientHttpUrl
141+
proxyClientURL = "127.0.0.1:42379"
142+
)
143+
144+
// Run independent grpc-proxy instance
145+
proxyProc, err := e2e.SpawnCmd([]string{e2e.BinDir + "/etcd", "grpc-proxy", "start",
146+
"--advertise-client-url", proxyClientURL,
147+
"--listen-addr", proxyClientURL,
148+
"--endpoints", node1ClientURL,
149+
"--endpoints-auto-sync-interval", "1s",
150+
"--cert-file", e2e.CertPath2,
151+
"--key-file", e2e.PrivateKeyPath2,
152+
"--tls-min-version", "TLS1.2",
153+
"--tls-max-version", "TLS1.3",
154+
}, nil)
155+
require.NoError(t, err)
156+
defer proxyProc.Stop()
157+
158+
_, err = proxyProc.Expect("listening for gRPC proxy client requests")
159+
require.NoError(t, err)
160+
}
161+
129162
func runEtcdNode(name, dataDir, clientURL, peerURL, clusterState, initialCluster string) (*expect.ExpectProcess, error) {
130163
proc, err := e2e.SpawnCmd([]string{e2e.BinPath,
131164
"--name", name,

0 commit comments

Comments
 (0)