@@ -223,13 +223,15 @@ func (c *Server) getHandler(ctx context.Context, backendMapper BackendMapper, ec
223223 backendModeConfigInitDone : false ,
224224 }
225225
226- h .HandleFunc ("/authenticate" , h .authenticateEndpoint )
226+ h .HandleFunc ("/authenticate" , func (w http.ResponseWriter , r * http.Request ) {
227+ h .authenticateEndpoint (ctx , w , r )
228+ })
227229 h .Handle ("/metrics" , promhttp .Handler ())
228230 h .HandleFunc ("/healthz" , func (w http.ResponseWriter , r * http.Request ) {
229231 fmt .Fprintf (w , "ok" )
230232 })
231233 logrus .Infof ("Starting the h.ec2Provider.startEc2DescribeBatchProcessing " )
232- go h .ec2Provider .StartEc2DescribeBatchProcessing ()
234+ go h .ec2Provider .StartEc2DescribeBatchProcessing (ctx )
233235 if strings .TrimSpace (c .DynamicBackendModePath ) != "" {
234236 fileutil .StartLoadDynamicFile (c .DynamicBackendModePath , h , stopCh )
235237 }
@@ -303,7 +305,7 @@ func (h *handler) isLoggableIdentity(identity *token.Identity) bool {
303305 return true
304306}
305307
306- func (h * handler ) authenticateEndpoint (w http.ResponseWriter , req * http.Request ) {
308+ func (h * handler ) authenticateEndpoint (ctx context. Context , w http.ResponseWriter , req * http.Request ) {
307309 start := time .Now ()
308310 log := logrus .WithFields (logrus.Fields {
309311 "path" : req .URL .Path ,
@@ -372,7 +374,7 @@ func (h *handler) authenticateEndpoint(w http.ResponseWriter, req *http.Request)
372374 log = log .WithField ("arn" , identity .CanonicalARN )
373375 }
374376
375- username , groups , err := h .doMapping (identity )
377+ username , groups , err := h .doMapping (ctx , identity )
376378 if err != nil {
377379 metrics .Get ().Latency .WithLabelValues (metrics .Unknown ).Observe (duration (start ))
378380 log .WithError (err ).Warn ("access denied" )
@@ -429,14 +431,14 @@ func ReservedPrefixExists(username string, reservedList []string) bool {
429431 return false
430432}
431433
432- func (h * handler ) doMapping (identity * token.Identity ) (string , []string , error ) {
434+ func (h * handler ) doMapping (ctx context. Context , identity * token.Identity ) (string , []string , error ) {
433435 var errs []error
434436
435437 for _ , m := range h .backendMapper .mappers {
436438 mapping , err := m .Map (identity )
437439 if err == nil {
438440 // Mapping found, try to render any templates like {{EC2PrivateDNSName}}
439- username , groups , err := h .renderTemplates (* mapping , identity )
441+ username , groups , err := h .renderTemplates (ctx , * mapping , identity )
440442 if err != nil {
441443 return "" , nil , fmt .Errorf ("mapper %s renderTemplates error: %v" , m .Name (), err )
442444 }
@@ -461,19 +463,19 @@ func (h *handler) doMapping(identity *token.Identity) (string, []string, error)
461463 return "" , nil , errutil .ErrNotMapped
462464}
463465
464- func (h * handler ) renderTemplates (mapping config.IdentityMapping , identity * token.Identity ) (string , []string , error ) {
466+ func (h * handler ) renderTemplates (ctx context. Context , mapping config.IdentityMapping , identity * token.Identity ) (string , []string , error ) {
465467 var username string
466468 groups := []string {}
467469 var err error
468470
469471 userPattern := mapping .Username
470- username , err = h .renderTemplate (userPattern , identity )
472+ username , err = h .renderTemplate (ctx , userPattern , identity )
471473 if err != nil {
472474 return "" , nil , fmt .Errorf ("error rendering username template %q: %s" , userPattern , err .Error ())
473475 }
474476
475477 for _ , groupPattern := range mapping .Groups {
476- group , err := h .renderTemplate (groupPattern , identity )
478+ group , err := h .renderTemplate (ctx , groupPattern , identity )
477479 if err != nil {
478480 return "" , nil , fmt .Errorf ("error rendering group template %q: %s" , groupPattern , err .Error ())
479481 }
@@ -483,13 +485,13 @@ func (h *handler) renderTemplates(mapping config.IdentityMapping, identity *toke
483485 return username , groups , nil
484486}
485487
486- func (h * handler ) renderTemplate (template string , identity * token.Identity ) (string , error ) {
488+ func (h * handler ) renderTemplate (ctx context. Context , template string , identity * token.Identity ) (string , error ) {
487489 // Private DNS requires EC2 API call
488490 if strings .Contains (template , "{{EC2PrivateDNSName}}" ) {
489491 if ! instanceIDPattern .MatchString (identity .SessionName ) {
490492 return "" , fmt .Errorf ("SessionName did not contain an instance id" )
491493 }
492- privateDNSName , err := h .ec2Provider .GetPrivateDNSName (context . Background () , identity .SessionName )
494+ privateDNSName , err := h .ec2Provider .GetPrivateDNSName (ctx , identity .SessionName )
493495 if err != nil {
494496 return "" , err
495497 }
0 commit comments