Skip to content

Commit 3619c21

Browse files
committedApr 21, 2025·
internal/access,internal/relui: use IAPFields instead of one-of context key
For gRPC, we have a nice IAPFields type and IAPFromContext helper. Use that for HTTP as well rather than a one-off undocumented context key. Change-Id: I6a6a636c1a48f7bf194a7e15fcbfaec77808646a Reviewed-on: https://go-review.googlesource.com/c/build/+/666497 Reviewed-by: Roland Shoemaker <roland@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent cfa5357 commit 3619c21

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed
 

‎internal/access/access.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ func RequireIAPAuthHandler(h http.Handler, audience string) http.Handler {
7373
log.Printf("JWT validation error: %v", err)
7474
return
7575
}
76-
ctx := context.WithValue(r.Context(), "subject", iap.ID)
77-
ctx = context.WithValue(ctx, "email", iap.Email)
76+
ctx := ContextWithIAP(r.Context(), iap)
7877
h.ServeHTTP(w, r.WithContext(ctx))
7978
})
8079
}

‎internal/relui/web.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/google/uuid"
2626
"github.com/jackc/pgx/v4"
2727
"github.com/julienschmidt/httprouter"
28+
"golang.org/x/build/internal/access"
2829
"golang.org/x/build/internal/criadb"
2930
"golang.org/x/build/internal/metrics"
3031
"golang.org/x/build/internal/relui/db"
@@ -676,16 +677,16 @@ func (s *Server) authorizedForWorkflow(ctx context.Context, d *workflow.Definiti
676677
return true
677678
}
678679

679-
email := ctx.Value("email")
680-
if email == nil {
681-
log.Printf("request context did not contain expected 'email' value from IAP JWT")
680+
iap, err := access.IAPFromContext(ctx)
681+
if err != nil {
682+
log.Printf("Error getting IAP fields from context: %v", err)
682683
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
683684
return false
684685
}
685686

686-
isMember, err := s.cria.IsMemberOfAny(ctx, fmt.Sprintf("user:%s", email), authorizedGroups)
687+
isMember, err := s.cria.IsMemberOfAny(ctx, fmt.Sprintf("user:%s", iap.Email), authorizedGroups)
687688
if err != nil {
688-
log.Printf("cria.IsMemberOfAny(user:%s) failed: %s", email, err)
689+
log.Printf("cria.IsMemberOfAny(user:%s) failed: %s", iap.Email, err)
689690
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
690691
return false
691692
}

‎internal/relui/web_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/jackc/pgx/v4"
3030
"github.com/jackc/pgx/v4/pgxpool"
3131
"github.com/julienschmidt/httprouter"
32+
"golang.org/x/build/internal/access"
3233
"golang.org/x/build/internal/criadb"
3334
"golang.org/x/build/internal/releasetargets"
3435
"golang.org/x/build/internal/relui/db"
@@ -844,6 +845,11 @@ func testWorkflowACL(t *testing.T, acld bool, authorized bool, wantSucceed bool)
844845
}
845846
s := NewServer(p, worker, nil, SiteHeader{}, nil, criadb.NewTestDatabase(memberships))
846847

848+
iap := access.IAPFields{
849+
Email: "test@google.com",
850+
ID: "testid",
851+
}
852+
847853
hourAgo := time.Now().Add(-1 * time.Hour)
848854
q := db.New(p)
849855

@@ -854,7 +860,7 @@ func testWorkflowACL(t *testing.T, acld bool, authorized bool, wantSucceed bool)
854860
"workflow.schedule": []string{string(ScheduleImmediate)},
855861
}.Encode()))
856862
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
857-
req = req.WithContext(context.WithValue(req.Context(), "email", "test@google.com"))
863+
req = req.WithContext(access.ContextWithIAP(req.Context(), iap))
858864
rec := httptest.NewRecorder()
859865

860866
s.createWorkflowHandler(rec, req)
@@ -892,7 +898,7 @@ func testWorkflowACL(t *testing.T, acld bool, authorized bool, wantSucceed bool)
892898
params := httprouter.Params{{Key: "id", Value: wfID.String()}, {Key: "name", Value: "beep"}}
893899
req := httptest.NewRequest(http.MethodPost, path.Join("/workflows/", wfID.String(), "tasks", "beep", "retry"), nil)
894900
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
895-
req = req.WithContext(context.WithValue(req.Context(), "email", "test@google.com"))
901+
req = req.WithContext(access.ContextWithIAP(req.Context(), iap))
896902
rec := httptest.NewRecorder()
897903

898904
s.retryTaskHandler(rec, req, params)
@@ -933,7 +939,7 @@ func testWorkflowACL(t *testing.T, acld bool, authorized bool, wantSucceed bool)
933939
params := httprouter.Params{{Key: "id", Value: wfID.String()}, {Key: "name", Value: "approve"}}
934940
req := httptest.NewRequest(http.MethodPost, path.Join("/workflows/", wfID.String(), "tasks", "approve", "approve"), nil)
935941
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
936-
req = req.WithContext(context.WithValue(req.Context(), "email", "test@google.com"))
942+
req = req.WithContext(access.ContextWithIAP(req.Context(), iap))
937943
rec := httptest.NewRecorder()
938944

939945
s.approveTaskHandler(rec, req, params)
@@ -965,7 +971,7 @@ func testWorkflowACL(t *testing.T, acld bool, authorized bool, wantSucceed bool)
965971
params := httprouter.Params{{Key: "id", Value: wfID.String()}}
966972
req := httptest.NewRequest(http.MethodPost, path.Join("/workflows/", wfID.String(), "stop"), nil)
967973
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
968-
req = req.WithContext(context.WithValue(req.Context(), "email", "test@google.com"))
974+
req = req.WithContext(access.ContextWithIAP(req.Context(), iap))
969975
rec := httptest.NewRecorder()
970976

971977
s.stopWorkflowHandler(rec, req, params)
@@ -989,7 +995,7 @@ func testWorkflowACL(t *testing.T, acld bool, authorized bool, wantSucceed bool)
989995
params := httprouter.Params{{Key: "id", Value: strconv.Itoa(int(sched.ID))}}
990996
req := httptest.NewRequest(http.MethodPost, path.Join("/schedules/", strconv.Itoa(int(sched.ID)), "delete"), nil)
991997
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
992-
req = req.WithContext(context.WithValue(req.Context(), "email", "test@google.com"))
998+
req = req.WithContext(access.ContextWithIAP(req.Context(), iap))
993999
rec := httptest.NewRecorder()
9941000

9951001
s.deleteScheduleHandler(rec, req, params)

0 commit comments

Comments
 (0)
Please sign in to comment.