credentials.go 4.9 KB

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