credentials.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/api/v3/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. return tc.gtc.ClientHandshake(ctx, authority, rawConn)
  67. }
  68. func (tc *transportCredential) ServerHandshake(rawConn net.Conn) (net.Conn, grpccredentials.AuthInfo, error) {
  69. return tc.gtc.ServerHandshake(rawConn)
  70. }
  71. func (tc *transportCredential) Info() grpccredentials.ProtocolInfo {
  72. return tc.gtc.Info()
  73. }
  74. func (tc *transportCredential) Clone() grpccredentials.TransportCredentials {
  75. return &transportCredential{
  76. gtc: tc.gtc.Clone(),
  77. }
  78. }
  79. func (tc *transportCredential) OverrideServerName(serverNameOverride string) error {
  80. return tc.gtc.OverrideServerName(serverNameOverride)
  81. }
  82. // perRPCCredential implements "grpccredentials.PerRPCCredentials" interface.
  83. type perRPCCredential struct {
  84. authToken string
  85. authTokenMu sync.RWMutex
  86. }
  87. func newPerRPCCredential() *perRPCCredential { return &perRPCCredential{} }
  88. func (rc *perRPCCredential) RequireTransportSecurity() bool { return false }
  89. func (rc *perRPCCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
  90. rc.authTokenMu.RLock()
  91. authToken := rc.authToken
  92. rc.authTokenMu.RUnlock()
  93. if authToken == "" {
  94. return nil, nil
  95. }
  96. return map[string]string{rpctypes.TokenFieldNameGRPC: authToken}, nil
  97. }
  98. func (b *bundle) UpdateAuthToken(token string) {
  99. if b.rc == nil {
  100. return
  101. }
  102. b.rc.UpdateAuthToken(token)
  103. }
  104. func (rc *perRPCCredential) UpdateAuthToken(token string) {
  105. rc.authTokenMu.Lock()
  106. rc.authToken = token
  107. rc.authTokenMu.Unlock()
  108. }