...

Source file src/golang.conradwood.net/go-easyops/authremote/context.go

Documentation: golang.conradwood.net/go-easyops/authremote

     1  package authremote
     2  
     3  /****************************************************************
     4  
     5  code in this file is not in use.
     6  
     7  
     8  
     9  ****************************************************************/
    10  
    11  import (
    12  	"context"
    13  	"fmt"
    14  	apb "golang.conradwood.net/apis/auth"
    15  	ge "golang.conradwood.net/apis/goeasyops"
    16  	"golang.conradwood.net/go-easyops/auth"
    17  	"golang.conradwood.net/go-easyops/cmdline"
    18  	"golang.conradwood.net/go-easyops/common"
    19  	"golang.conradwood.net/go-easyops/rpc"
    20  	"time"
    21  )
    22  
    23  // a local context value
    24  type CallStateV2 struct {
    25  	inctx *ge.InContext
    26  }
    27  
    28  func DIS_ContextV2WithTimeoutAndTags(t time.Duration, rt *ge.CTXRoutingTags) context.Context {
    29  	user := getLocalUserAccount()
    30  	ctx, cnc := DIS_ContextV2WithTimeoutAndTagsForUser(t, "no_req_id", user, rt)
    31  	go auto_cancel(cnc, t)
    32  	return ctx
    33  }
    34  
    35  // automatically cancel after duration
    36  func auto_cancel(cf context.CancelFunc, t time.Duration) {
    37  	time.Sleep(t)
    38  	cf()
    39  }
    40  
    41  /*
    42  creates a new context for a user, with a timeout and routing tags and a cancel function
    43  userid may be "" (empty).
    44  */
    45  func DIS_ContextV2WithTimeoutAndTagsForUser(t time.Duration, reqid string, user *apb.SignedUser, rt *ge.CTXRoutingTags) (context.Context, context.CancelFunc) {
    46  	if cmdline.IsStandalone() {
    47  		f := func() {}
    48  		return standalone_ContextWithTimeoutAndTags(t, rt), f
    49  	}
    50  	ctx, cnc := context.WithTimeout(context.Background(), t)
    51  	inctx := DIS_build_new_ctx_meta_struct(reqid, user, nil)
    52  	inctx.MCtx.Tags = rt
    53  	lm := &CallStateV2{inctx: inctx}
    54  	ctx = context.WithValue(ctx, rpc.LOCALCONTEXTNAMEV2, lm)
    55  	ctx = DIS_contextFromStruct(ctx, inctx)
    56  	return ctx, cnc
    57  }
    58  
    59  /*
    60  build the struct we need to add to the context. used to create new contexts (e.g. in h2gproxy or in command line clients)
    61  it will determine the service itself. user and sudo may be nil.
    62  this is intented to be used as outbound context to other services
    63  */
    64  func DIS_build_new_ctx_meta_struct(requestid string, user, sudo *apb.SignedUser) *ge.InContext {
    65  	fmt.Printf("[go-easyops] Building meta for user %s\n", auth.Description(common.VerifySignedUser(user)))
    66  	lsvc := GetLocalServiceAccount()
    67  	res := &ge.InContext{
    68  		ImCtx: &ge.ImmutableContext{
    69  			CreatorService: lsvc,
    70  			RequestID:      requestid,
    71  			User:           user,
    72  			SudoUser:       sudo,
    73  		},
    74  		MCtx: &ge.MutableContext{
    75  			CallingService: lsvc,
    76  		},
    77  	}
    78  	return res
    79  }
    80  

View as plain text