mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #16358 from ahrtr/remove_creds_bundle_20230802
clientv3: remove the experimental gRPC API grpccredentials.Bundle
This commit is contained in:
commit
10c7e81cac
@ -69,7 +69,7 @@ type Client struct {
|
|||||||
Username string
|
Username string
|
||||||
// Password is a password for authentication.
|
// Password is a password for authentication.
|
||||||
Password string
|
Password string
|
||||||
authTokenBundle credentials.Bundle
|
authTokenBundle credentials.PerRPCCredentialsBundle
|
||||||
|
|
||||||
callOpts []grpc.CallOption
|
callOpts []grpc.CallOption
|
||||||
|
|
||||||
@ -338,7 +338,7 @@ func (c *Client) credentialsForEndpoint(ep string) grpccredentials.TransportCred
|
|||||||
if c.creds != nil {
|
if c.creds != nil {
|
||||||
return c.creds
|
return c.creds
|
||||||
}
|
}
|
||||||
return credentials.NewBundle(credentials.Config{}).TransportCredentials()
|
return credentials.NewTransportCredential(nil)
|
||||||
default:
|
default:
|
||||||
panic(fmt.Errorf("unsupported CredsRequirement: %v", r))
|
panic(fmt.Errorf("unsupported CredsRequirement: %v", r))
|
||||||
}
|
}
|
||||||
@ -350,7 +350,7 @@ func newClient(cfg *Config) (*Client, error) {
|
|||||||
}
|
}
|
||||||
var creds grpccredentials.TransportCredentials
|
var creds grpccredentials.TransportCredentials
|
||||||
if cfg.TLS != nil {
|
if cfg.TLS != nil {
|
||||||
creds = credentials.NewBundle(credentials.Config{TLSConfig: cfg.TLS}).TransportCredentials()
|
creds = credentials.NewTransportCredential(cfg.TLS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// use a temporary skeleton client to bootstrap first connection
|
// use a temporary skeleton client to bootstrap first connection
|
||||||
@ -389,7 +389,7 @@ func newClient(cfg *Config) (*Client, error) {
|
|||||||
if cfg.Username != "" && cfg.Password != "" {
|
if cfg.Username != "" && cfg.Password != "" {
|
||||||
client.Username = cfg.Username
|
client.Username = cfg.Username
|
||||||
client.Password = cfg.Password
|
client.Password = cfg.Password
|
||||||
client.authTokenBundle = credentials.NewBundle(credentials.Config{})
|
client.authTokenBundle = credentials.NewPerRPCCredentialBundle()
|
||||||
}
|
}
|
||||||
if cfg.MaxCallSendMsgSize > 0 || cfg.MaxCallRecvMsgSize > 0 {
|
if cfg.MaxCallSendMsgSize > 0 || cfg.MaxCallRecvMsgSize > 0 {
|
||||||
if cfg.MaxCallRecvMsgSize > 0 && cfg.MaxCallSendMsgSize > cfg.MaxCallRecvMsgSize {
|
if cfg.MaxCallRecvMsgSize > 0 && cfg.MaxCallSendMsgSize > cfg.MaxCallRecvMsgSize {
|
||||||
|
@ -19,7 +19,6 @@ package credentials
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
grpccredentials "google.golang.org/grpc/credentials"
|
grpccredentials "google.golang.org/grpc/credentials"
|
||||||
@ -27,85 +26,44 @@ import (
|
|||||||
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
|
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config defines gRPC credential configuration.
|
func NewTransportCredential(cfg *tls.Config) grpccredentials.TransportCredentials {
|
||||||
type Config struct {
|
return grpccredentials.NewTLS(cfg)
|
||||||
TLSConfig *tls.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bundle defines gRPC credential interface.
|
// PerRPCCredentialsBundle defines gRPC credential interface.
|
||||||
type Bundle interface {
|
type PerRPCCredentialsBundle interface {
|
||||||
grpccredentials.Bundle
|
|
||||||
UpdateAuthToken(token string)
|
UpdateAuthToken(token string)
|
||||||
|
PerRPCCredentials() grpccredentials.PerRPCCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBundle constructs a new gRPC credential bundle.
|
func NewPerRPCCredentialBundle() PerRPCCredentialsBundle {
|
||||||
func NewBundle(cfg Config) Bundle {
|
return &perRPCCredentialBundle{
|
||||||
return &bundle{
|
rc: &perRPCCredential{},
|
||||||
tc: newTransportCredential(cfg.TLSConfig),
|
|
||||||
rc: newPerRPCCredential(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// bundle implements "grpccredentials.Bundle" interface.
|
// perRPCCredentialBundle implements `PerRPCCredentialsBundle` interface.
|
||||||
type bundle struct {
|
type perRPCCredentialBundle struct {
|
||||||
tc *transportCredential
|
|
||||||
rc *perRPCCredential
|
rc *perRPCCredential
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bundle) TransportCredentials() grpccredentials.TransportCredentials {
|
func (b *perRPCCredentialBundle) UpdateAuthToken(token string) {
|
||||||
return b.tc
|
if b.rc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.rc.UpdateAuthToken(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bundle) PerRPCCredentials() grpccredentials.PerRPCCredentials {
|
func (b *perRPCCredentialBundle) PerRPCCredentials() grpccredentials.PerRPCCredentials {
|
||||||
return b.rc
|
return b.rc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bundle) NewWithMode(mode string) (grpccredentials.Bundle, error) {
|
// perRPCCredential implements `grpccredentials.PerRPCCredentials` interface.
|
||||||
// no-op
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// transportCredential implements "grpccredentials.TransportCredentials" interface.
|
|
||||||
type transportCredential struct {
|
|
||||||
gtc grpccredentials.TransportCredentials
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTransportCredential(cfg *tls.Config) *transportCredential {
|
|
||||||
return &transportCredential{
|
|
||||||
gtc: grpccredentials.NewTLS(cfg),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *transportCredential) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, grpccredentials.AuthInfo, error) {
|
|
||||||
return tc.gtc.ClientHandshake(ctx, authority, rawConn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *transportCredential) ServerHandshake(rawConn net.Conn) (net.Conn, grpccredentials.AuthInfo, error) {
|
|
||||||
return tc.gtc.ServerHandshake(rawConn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *transportCredential) Info() grpccredentials.ProtocolInfo {
|
|
||||||
return tc.gtc.Info()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *transportCredential) Clone() grpccredentials.TransportCredentials {
|
|
||||||
return &transportCredential{
|
|
||||||
gtc: tc.gtc.Clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *transportCredential) OverrideServerName(serverNameOverride string) error {
|
|
||||||
return tc.gtc.OverrideServerName(serverNameOverride)
|
|
||||||
}
|
|
||||||
|
|
||||||
// perRPCCredential implements "grpccredentials.PerRPCCredentials" interface.
|
|
||||||
type perRPCCredential struct {
|
type perRPCCredential struct {
|
||||||
authToken string
|
authToken string
|
||||||
authTokenMu sync.RWMutex
|
authTokenMu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPerRPCCredential() *perRPCCredential { return &perRPCCredential{} }
|
|
||||||
|
|
||||||
func (rc *perRPCCredential) RequireTransportSecurity() bool { return false }
|
func (rc *perRPCCredential) RequireTransportSecurity() bool { return false }
|
||||||
|
|
||||||
func (rc *perRPCCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
|
func (rc *perRPCCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
|
||||||
@ -118,13 +76,6 @@ func (rc *perRPCCredential) GetRequestMetadata(ctx context.Context, s ...string)
|
|||||||
return map[string]string{rpctypes.TokenFieldNameGRPC: authToken}, nil
|
return map[string]string{rpctypes.TokenFieldNameGRPC: authToken}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bundle) UpdateAuthToken(token string) {
|
|
||||||
if b.rc == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b.rc.UpdateAuthToken(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rc *perRPCCredential) UpdateAuthToken(token string) {
|
func (rc *perRPCCredential) UpdateAuthToken(token string) {
|
||||||
rc.authTokenMu.Lock()
|
rc.authTokenMu.Lock()
|
||||||
rc.authToken = token
|
rc.authToken = token
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdateAuthToken(t *testing.T) {
|
func TestUpdateAuthToken(t *testing.T) {
|
||||||
bundle := NewBundle(Config{})
|
bundle := NewPerRPCCredentialBundle()
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
metadataBeforeUpdate, _ := bundle.PerRPCCredentials().GetRequestMetadata(ctx)
|
metadataBeforeUpdate, _ := bundle.PerRPCCredentials().GetRequestMetadata(ctx)
|
||||||
|
@ -25,24 +25,16 @@ import (
|
|||||||
|
|
||||||
type dummyAuthTokenBundle struct{}
|
type dummyAuthTokenBundle struct{}
|
||||||
|
|
||||||
func (d dummyAuthTokenBundle) TransportCredentials() grpccredentials.TransportCredentials {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d dummyAuthTokenBundle) PerRPCCredentials() grpccredentials.PerRPCCredentials {
|
func (d dummyAuthTokenBundle) PerRPCCredentials() grpccredentials.PerRPCCredentials {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dummyAuthTokenBundle) NewWithMode(mode string) (grpccredentials.Bundle, error) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d dummyAuthTokenBundle) UpdateAuthToken(token string) {
|
func (d dummyAuthTokenBundle) UpdateAuthToken(token string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClientShouldRefreshToken(t *testing.T) {
|
func TestClientShouldRefreshToken(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
authTokenBundle credentials.Bundle
|
authTokenBundle credentials.PerRPCCredentialsBundle
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
err error
|
err error
|
||||||
|
@ -797,8 +797,7 @@ func (e *Etcd) grpcGatewayDial(splitHttp bool) (grpcDial func(ctx context.Contex
|
|||||||
dtls := tlscfg.Clone()
|
dtls := tlscfg.Clone()
|
||||||
// trust local server
|
// trust local server
|
||||||
dtls.InsecureSkipVerify = true
|
dtls.InsecureSkipVerify = true
|
||||||
bundle := credentials.NewBundle(credentials.Config{TLSConfig: dtls})
|
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTransportCredential(dtls)))
|
||||||
opts = append(opts, grpc.WithTransportCredentials(bundle.TransportCredentials()))
|
|
||||||
} else {
|
} else {
|
||||||
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,7 @@ func Server(s *etcdserver.EtcdServer, tls *tls.Config, interceptor grpc.UnarySer
|
|||||||
var opts []grpc.ServerOption
|
var opts []grpc.ServerOption
|
||||||
opts = append(opts, grpc.CustomCodec(&codec{}))
|
opts = append(opts, grpc.CustomCodec(&codec{}))
|
||||||
if tls != nil {
|
if tls != nil {
|
||||||
bundle := credentials.NewBundle(credentials.Config{TLSConfig: tls})
|
opts = append(opts, grpc.Creds(credentials.NewTransportCredential(tls)))
|
||||||
opts = append(opts, grpc.Creds(bundle.TransportCredentials()))
|
|
||||||
}
|
}
|
||||||
chainUnaryInterceptors := []grpc.UnaryServerInterceptor{
|
chainUnaryInterceptors := []grpc.UnaryServerInterceptor{
|
||||||
newLogUnaryInterceptor(s),
|
newLogUnaryInterceptor(s),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user