vitess.io/vitess@v0.16.2/go/vt/vtadmin/vtsql/vtsql_test.go (about)

     1  /*
     2  Copyright 2020 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 vtsql
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/vt/callerid"
    27  	"vitess.io/vitess/go/vt/grpcclient"
    28  
    29  	querypb "vitess.io/vitess/go/vt/proto/query"
    30  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    31  )
    32  
    33  func assertImmediateCaller(t *testing.T, im *querypb.VTGateCallerID, expected string) {
    34  	t.Helper()
    35  
    36  	require.NotNil(t, im, "immediate caller cannot be nil")
    37  	assert.Equal(t, im.Username, expected, "immediate caller username mismatch")
    38  }
    39  
    40  func assertEffectiveCaller(t *testing.T, ef *vtrpcpb.CallerID, principal string, component string, subcomponent string) {
    41  	t.Helper()
    42  
    43  	require.NotNil(t, ef, "effective caller cannot be nil")
    44  	assert.Equal(t, ef.Principal, principal, "effective caller principal mismatch")
    45  	assert.Equal(t, ef.Component, component, "effective caller component mismatch")
    46  	assert.Equal(t, ef.Subcomponent, subcomponent, "effective caller subcomponent mismatch")
    47  }
    48  
    49  func Test_getQueryContext(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	ctx := context.Background()
    53  
    54  	creds := &StaticAuthCredentials{
    55  		EffectiveUser: "efuser",
    56  		StaticAuthClientCreds: &grpcclient.StaticAuthClientCreds{
    57  			Username: "imuser",
    58  		},
    59  	}
    60  	db := &VTGateProxy{creds: creds}
    61  
    62  	outctx := db.getQueryContext(ctx)
    63  	assert.NotEqual(t, ctx, outctx, "getQueryContext should return a modified context when credentials are set")
    64  	assertEffectiveCaller(t, callerid.EffectiveCallerIDFromContext(outctx), "efuser", "vtadmin", "")
    65  	assertImmediateCaller(t, callerid.ImmediateCallerIDFromContext(outctx), "imuser")
    66  
    67  	db.creds = nil
    68  	outctx = db.getQueryContext(ctx)
    69  	assert.Equal(t, ctx, outctx, "getQueryContext should not modify the context when credentials are not set")
    70  
    71  	callerctx := callerid.NewContext(
    72  		ctx,
    73  		callerid.NewEffectiveCallerID("other principal", "vtctld", ""),
    74  		callerid.NewImmediateCallerID("other_user"),
    75  	)
    76  	db.creds = creds
    77  
    78  	outctx = db.getQueryContext(callerctx)
    79  	assert.NotEqual(t, callerctx, outctx, "getQueryContext should override an existing callerid in the context")
    80  	assertEffectiveCaller(t, callerid.EffectiveCallerIDFromContext(outctx), "efuser", "vtadmin", "")
    81  	assertImmediateCaller(t, callerid.ImmediateCallerIDFromContext(outctx), "imuser")
    82  }