vitess.io/vitess@v0.16.2/go/cmd/vtgateclienttest/services/callerid.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 services 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "strings" 23 24 "context" 25 26 "google.golang.org/protobuf/proto" 27 28 "vitess.io/vitess/go/sqltypes" 29 "vitess.io/vitess/go/vt/callerid" 30 "vitess.io/vitess/go/vt/vtgate/vtgateservice" 31 32 querypb "vitess.io/vitess/go/vt/proto/query" 33 vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" 34 vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" 35 ) 36 37 // CallerIDPrefix is the prefix to send with queries so they go 38 // through this test suite. 39 const CallerIDPrefix = "callerid://" 40 41 // callerIDClient implements vtgateservice.VTGateService, and checks 42 // the received callerid matches the one passed out of band by the client. 43 type callerIDClient struct { 44 fallbackClient 45 } 46 47 func newCallerIDClient(fallback vtgateservice.VTGateService) *callerIDClient { 48 return &callerIDClient{ 49 fallbackClient: newFallbackClient(fallback), 50 } 51 } 52 53 // checkCallerID will see if this module is handling the request, and 54 // if it is, check the callerID from the context. Returns false if 55 // the query is not for this module. Returns true and the error to 56 // return with the call if this module is handling the request. 57 func (c *callerIDClient) checkCallerID(ctx context.Context, received string) (bool, error) { 58 if !strings.HasPrefix(received, CallerIDPrefix) { 59 return false, nil 60 } 61 62 jsonCallerID := []byte(received[len(CallerIDPrefix):]) 63 expectedCallerID := &vtrpcpb.CallerID{} 64 if err := json.Unmarshal(jsonCallerID, expectedCallerID); err != nil { 65 return true, fmt.Errorf("cannot unmarshal provided callerid: %v", err) 66 } 67 68 receivedCallerID := callerid.EffectiveCallerIDFromContext(ctx) 69 if receivedCallerID == nil { 70 return true, fmt.Errorf("no callerid received in the query") 71 } 72 73 if !proto.Equal(receivedCallerID, expectedCallerID) { 74 return true, fmt.Errorf("callerid mismatch, got %v expected %v", receivedCallerID, expectedCallerID) 75 } 76 77 return true, fmt.Errorf("SUCCESS: callerid matches") 78 } 79 80 func (c *callerIDClient) Execute(ctx context.Context, session *vtgatepb.Session, sql string, bindVariables map[string]*querypb.BindVariable) (*vtgatepb.Session, *sqltypes.Result, error) { 81 if ok, err := c.checkCallerID(ctx, sql); ok { 82 return session, nil, err 83 } 84 return c.fallbackClient.Execute(ctx, session, sql, bindVariables) 85 } 86 87 func (c *callerIDClient) ExecuteBatch(ctx context.Context, session *vtgatepb.Session, sqlList []string, bindVariablesList []map[string]*querypb.BindVariable) (*vtgatepb.Session, []sqltypes.QueryResponse, error) { 88 if len(sqlList) == 1 { 89 if ok, err := c.checkCallerID(ctx, sqlList[0]); ok { 90 return session, nil, err 91 } 92 } 93 return c.fallbackClient.ExecuteBatch(ctx, session, sqlList, bindVariablesList) 94 } 95 96 func (c *callerIDClient) StreamExecute(ctx context.Context, session *vtgatepb.Session, sql string, bindVariables map[string]*querypb.BindVariable, callback func(*sqltypes.Result) error) error { 97 if ok, err := c.checkCallerID(ctx, sql); ok { 98 return err 99 } 100 return c.fallbackClient.StreamExecute(ctx, session, sql, bindVariables, callback) 101 }