vitess.io/vitess@v0.16.2/go/vt/servenv/grpc_server_auth_mtls.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package servenv 18 19 import ( 20 "context" 21 "strings" 22 23 "github.com/spf13/pflag" 24 "google.golang.org/grpc/codes" 25 "google.golang.org/grpc/credentials" 26 "google.golang.org/grpc/peer" 27 "google.golang.org/grpc/status" 28 29 "vitess.io/vitess/go/vt/log" 30 ) 31 32 var ( 33 // clientCertSubstrings list of substrings of at least one of the client certificate names to use during authorization 34 clientCertSubstrings string 35 // MtlsAuthPlugin implements AuthPlugin interface 36 _ Authenticator = (*MtlsAuthPlugin)(nil) 37 ) 38 39 func registerGRPCServerAuthMTLSFlags(fs *pflag.FlagSet) { 40 fs.StringVar(&clientCertSubstrings, "grpc_auth_mtls_allowed_substrings", clientCertSubstrings, "List of substrings of at least one of the client certificate names (separated by colon).") 41 } 42 43 // MtlsAuthPlugin implements static username/password authentication for grpc. It contains an array of username/passwords 44 // that will be authorized to connect to the grpc server. 45 type MtlsAuthPlugin struct { 46 clientCertSubstrings []string 47 } 48 49 // Authenticate implements Authenticator interface. This method will be used inside a middleware in grpc_server to authenticate 50 // incoming requests. 51 func (ma *MtlsAuthPlugin) Authenticate(ctx context.Context, fullMethod string) (context.Context, error) { 52 p, ok := peer.FromContext(ctx) 53 if !ok { 54 return nil, status.Errorf(codes.Unauthenticated, "no peer connection info") 55 } 56 tlsInfo, ok := p.AuthInfo.(credentials.TLSInfo) 57 if !ok { 58 return nil, status.Errorf(codes.Unauthenticated, "not connected via TLS") 59 } 60 for _, substring := range ma.clientCertSubstrings { 61 for _, cert := range tlsInfo.State.PeerCertificates { 62 if strings.Contains(cert.Subject.String(), substring) { 63 return ctx, nil 64 } 65 } 66 } 67 return nil, status.Errorf(codes.Unauthenticated, "client certificate not authorized") 68 } 69 70 func mtlsAuthPluginInitializer() (Authenticator, error) { 71 mtlsAuthPlugin := &MtlsAuthPlugin{ 72 clientCertSubstrings: strings.Split(clientCertSubstrings, ":"), 73 } 74 log.Infof("mtls auth plugin have initialized successfully with allowed client cert name substrings of %v", clientCertSubstrings) 75 return mtlsAuthPlugin, nil 76 } 77 78 // ClientCertSubstrings returns the value of the 79 // `--grpc_auth_mtls_allowed_substrings` flag. 80 func ClientCertSubstrings() string { 81 return clientCertSubstrings 82 } 83 84 func init() { 85 RegisterAuthPlugin("mtls", mtlsAuthPluginInitializer) 86 grpcAuthServerFlagHooks = append(grpcAuthServerFlagHooks, registerGRPCServerAuthMTLSFlags) 87 }