v3_tls_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "context"
  17. "crypto/tls"
  18. "testing"
  19. "time"
  20. "go.etcd.io/etcd/clientv3"
  21. "go.etcd.io/etcd/pkg/testutil"
  22. "google.golang.org/grpc"
  23. )
  24. func TestTLSClientCipherSuitesValid(t *testing.T) { testTLSCipherSuites(t, true) }
  25. func TestTLSClientCipherSuitesMismatch(t *testing.T) { testTLSCipherSuites(t, false) }
  26. // testTLSCipherSuites ensures mismatching client-side cipher suite
  27. // fail TLS handshake with the server.
  28. func testTLSCipherSuites(t *testing.T, valid bool) {
  29. defer testutil.AfterTest(t)
  30. cipherSuites := []uint16{
  31. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  32. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  33. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  34. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  35. tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  36. tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  37. }
  38. srvTLS, cliTLS := testTLSInfo, testTLSInfo
  39. if valid {
  40. srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites, cipherSuites
  41. } else {
  42. srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites[:2], cipherSuites[2:]
  43. }
  44. clus := NewClusterV3(t, &ClusterConfig{Size: 1, ClientTLS: &srvTLS})
  45. defer clus.Terminate(t)
  46. cc, err := cliTLS.ClientConfig()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. cli, cerr := clientv3.New(clientv3.Config{
  51. Endpoints: []string{clus.Members[0].GRPCAddr()},
  52. DialTimeout: time.Second,
  53. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  54. TLS: cc,
  55. })
  56. if cli != nil {
  57. cli.Close()
  58. }
  59. if !valid && cerr != context.DeadlineExceeded {
  60. t.Fatalf("expected %v with TLS handshake failure, got %v", context.DeadlineExceeded, cerr)
  61. }
  62. if valid && cerr != nil {
  63. t.Fatalf("expected TLS handshake success, got %v", cerr)
  64. }
  65. }