vitess.io/vitess@v0.16.2/go/vt/callerid/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 callerid stores/retrieves CallerIDs (immediate CallerID
    18  // and effective CallerID) to/from the Context
    19  package callerid
    20  
    21  import (
    22  	"context"
    23  
    24  	querypb "vitess.io/vitess/go/vt/proto/query"
    25  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    26  )
    27  
    28  // The datatype for CallerID Context Keys
    29  type callerIDKey int
    30  
    31  var (
    32  	// internal Context key for immediate CallerID
    33  	immediateCallerIDKey callerIDKey
    34  	// internal Context key for effective CallerID
    35  	effectiveCallerIDKey callerIDKey = 1
    36  )
    37  
    38  // NewImmediateCallerID creates a querypb.VTGateCallerID initialized with username
    39  func NewImmediateCallerID(username string) *querypb.VTGateCallerID {
    40  	return &querypb.VTGateCallerID{Username: username}
    41  }
    42  
    43  // GetUsername returns the immediate caller of VTGate
    44  func GetUsername(im *querypb.VTGateCallerID) string {
    45  	if im == nil {
    46  		return ""
    47  	}
    48  	return im.Username
    49  }
    50  
    51  // NewEffectiveCallerID creates a new vtrpcpb.CallerID with principal, component and
    52  // subComponent
    53  func NewEffectiveCallerID(principal string, component string, subComponent string) *vtrpcpb.CallerID {
    54  	return &vtrpcpb.CallerID{Principal: principal, Component: component, Subcomponent: subComponent}
    55  }
    56  
    57  // GetPrincipal returns the effective user identifier, which is usually filled in
    58  // with whoever made the request to the appserver, if the request
    59  // came from an automated job or another system component.
    60  // If the request comes directly from the Internet, or if the Vitess client
    61  // takes action on its own accord, it is okay for this method to
    62  // return empty string.
    63  func GetPrincipal(ef *vtrpcpb.CallerID) string {
    64  	if ef == nil {
    65  		return ""
    66  	}
    67  	return ef.Principal
    68  }
    69  
    70  // GetComponent returns the running process of the effective caller.
    71  // It can for instance return hostname:port of the servlet initiating the
    72  // database call, or the container engine ID used by the servlet.
    73  func GetComponent(ef *vtrpcpb.CallerID) string {
    74  	if ef == nil {
    75  		return ""
    76  	}
    77  	return ef.Component
    78  }
    79  
    80  // GetSubcomponent returns a component inside the process of effective caller,
    81  // which is responsible for generating this request. Suggested values are a
    82  // servlet name or an API endpoint name.
    83  func GetSubcomponent(ef *vtrpcpb.CallerID) string {
    84  	if ef == nil {
    85  		return ""
    86  	}
    87  	return ef.Subcomponent
    88  }
    89  
    90  // NewContext adds the provided EffectiveCallerID(vtrpcpb.CallerID) and ImmediateCallerID(querypb.VTGateCallerID)
    91  // into the Context
    92  func NewContext(ctx context.Context, ef *vtrpcpb.CallerID, im *querypb.VTGateCallerID) context.Context {
    93  	ctx = context.WithValue(
    94  		context.WithValue(ctx, effectiveCallerIDKey, ef),
    95  		immediateCallerIDKey,
    96  		im,
    97  	)
    98  	return ctx
    99  }
   100  
   101  // EffectiveCallerIDFromContext returns the EffectiveCallerID(vtrpcpb.CallerID)
   102  // stored in the Context, if any
   103  func EffectiveCallerIDFromContext(ctx context.Context) *vtrpcpb.CallerID {
   104  	ef, ok := ctx.Value(effectiveCallerIDKey).(*vtrpcpb.CallerID)
   105  	if ok && ef != nil {
   106  		return ef
   107  	}
   108  	return nil
   109  }
   110  
   111  // ImmediateCallerIDFromContext returns the ImmediateCallerID(querypb.VTGateCallerID)
   112  // stored in the Context, if any
   113  func ImmediateCallerIDFromContext(ctx context.Context) *querypb.VTGateCallerID {
   114  	im, ok := ctx.Value(immediateCallerIDKey).(*querypb.VTGateCallerID)
   115  	if ok && im != nil {
   116  		return im
   117  	}
   118  	return nil
   119  }