credentials.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2019 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 credentials implements gRPC credential interface with etcd specific logic.
  15. // e.g., client handshake with custom authority parameter
  16. package credentials
  17. import (
  18. "context"
  19. "crypto/tls"
  20. "net"
  21. "sync"
  22. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  23. grpccredentials "google.golang.org/grpc/credentials"
  24. )
  25. // Config defines gRPC credential configuration.
  26. type Config struct {
  27. TLSConfig *tls.Config
  28. AuthToken string
  29. }
  30. // NewBundle constructs a new gRPC credential bundle.
  31. func NewBundle(cfg Config) grpccredentials.Bundle {
  32. return &bundle{
  33. tc: newTransportCredential(cfg.TLSConfig),
  34. rc: newPerRPCCredential(cfg.AuthToken),
  35. }
  36. }
  37. // bundle implements "grpccredentials.Bundle" interface.
  38. type bundle struct {
  39. tc *transportCredential
  40. rc *perRPCCredential
  41. }
  42. func (b *bundle) TransportCredentials() grpccredentials.TransportCredentials {
  43. return b.tc
  44. }
  45. func (b *bundle) PerRPCCredentials() grpccredentials.PerRPCCredentials {
  46. return b.rc
  47. }
  48. func (b *bundle) NewWithMode(mode string) (grpccredentials.Bundle, error) {
  49. // no-op
  50. return nil, nil
  51. }
  52. // transportCredential implements "grpccredentials.TransportCredentials" interface.
  53. type transportCredential struct {
  54. gtc grpccredentials.TransportCredentials
  55. }
  56. func newTransportCredential(cfg *tls.Config) *transportCredential {
  57. return &transportCredential{
  58. gtc: grpccredentials.NewTLS(cfg),
  59. }
  60. }
  61. func (tc *transportCredential) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, grpccredentials.AuthInfo, error) {
  62. // Only overwrite when authority is an IP address!
  63. // Let's say, a server runs SRV records on "etcd.local" that resolves
  64. // to "m1.etcd.local", and its SAN field also includes "m1.etcd.local".
  65. // But what if SAN does not include its resolved IP address (e.g. 127.0.0.1)?
  66. // Then, the server should only authenticate using its DNS hostname "m1.etcd.local",
  67. // instead of overwriting it with its IP address.
  68. // And we do not overwrite "localhost" either. Only overwrite IP addresses!
  69. if isIP(authority) {
  70. target := rawConn.RemoteAddr().String()
  71. if authority != target {
  72. // When user dials with "grpc.WithDialer", "grpc.DialContext" "cc.parsedTarget"
  73. // update only happens once. This is problematic, because when TLS is enabled,
  74. // retries happen through "grpc.WithDialer" with static "cc.parsedTarget" from
  75. // the initial dial call.
  76. // If the server authenticates by IP addresses, we want to set a new endpoint as
  77. // a new authority. Otherwise
  78. // "transport: authentication handshake failed: x509: certificate is valid for 127.0.0.1, 192.168.121.180, not 192.168.223.156"
  79. // when the new dial target is "192.168.121.180" whose certificate host name is also "192.168.121.180"
  80. // but client tries to authenticate with previously set "cc.parsedTarget" field "192.168.223.156"
  81. authority = target
  82. }
  83. }
  84. return tc.gtc.ClientHandshake(ctx, authority, rawConn)
  85. }
  86. // return true if given string is an IP.
  87. func isIP(ep string) bool {
  88. return net.ParseIP(ep) != nil
  89. }
  90. func (tc *transportCredential) ServerHandshake(rawConn net.Conn) (net.Conn, grpccredentials.AuthInfo, error) {
  91. return tc.gtc.ServerHandshake(rawConn)
  92. }
  93. func (tc *transportCredential) Info() grpccredentials.ProtocolInfo {
  94. return tc.gtc.Info()
  95. }
  96. func (tc *transportCredential) Clone() grpccredentials.TransportCredentials {
  97. return &transportCredential{
  98. gtc: tc.gtc.Clone(),
  99. }
  100. }
  101. func (tc *transportCredential) OverrideServerName(serverNameOverride string) error {
  102. return tc.gtc.OverrideServerName(serverNameOverride)
  103. }
  104. // perRPCCredential implements "grpccredentials.PerRPCCredentials" interface.
  105. type perRPCCredential struct {
  106. authToken string
  107. authTokenMu sync.RWMutex
  108. }
  109. func newPerRPCCredential(authToken string) *perRPCCredential {
  110. if authToken == "" {
  111. return nil
  112. }
  113. return &perRPCCredential{
  114. authToken: authToken,
  115. }
  116. }
  117. func (rc *perRPCCredential) RequireTransportSecurity() bool { return false }
  118. func (rc *perRPCCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
  119. rc.authTokenMu.RLock()
  120. authToken := rc.authToken
  121. rc.authTokenMu.RUnlock()
  122. return map[string]string{rpctypes.TokenFieldNameGRPC: authToken}, nil
  123. }