v3_tls_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestTLSClientCipherSuitesValid(t *testing.T) { testTLSCipherSuites(t, true) }
  24. func TestTLSClientCipherSuitesMismatch(t *testing.T) { testTLSCipherSuites(t, false) }
  25. // testTLSCipherSuites ensures mismatching client-side cipher suite
  26. // fail TLS handshake with the server.
  27. func testTLSCipherSuites(t *testing.T, valid bool) {
  28. defer testutil.AfterTest(t)
  29. cipherSuites := []uint16{
  30. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  31. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  32. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  33. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  34. tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  35. tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  36. }
  37. srvTLS, cliTLS := testTLSInfo, testTLSInfo
  38. if valid {
  39. srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites, cipherSuites
  40. } else {
  41. srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites[:2], cipherSuites[2:]
  42. }
  43. clus := NewClusterV3(t, &ClusterConfig{Size: 1, ClientTLS: &srvTLS})
  44. defer clus.Terminate(t)
  45. cc, err := cliTLS.ClientConfig()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. cli, cerr := clientv3.New(clientv3.Config{
  50. Endpoints: []string{clus.Members[0].GRPCAddr()},
  51. DialTimeout: time.Second,
  52. TLS: cc,
  53. })
  54. if cli != nil {
  55. cli.Close()
  56. }
  57. if !valid && cerr != context.DeadlineExceeded {
  58. t.Fatalf("expected %v with TLS handshake failure, got %v", context.DeadlineExceeded, cerr)
  59. }
  60. if valid && cerr != nil {
  61. t.Fatalf("expected TLS handshake success, got %v", cerr)
  62. }
  63. }