Skip to content

Commit 295d905

Browse files
committed
internal/gomote: add authenticate endpoint implementation
This change adds the authenticate implementation for the gomote service. The endpoint will be used to verify that the user is authenticated and authorized to use the service. Updates golang/go#48742 Change-Id: Ic6ab4cfa7eeccc37c12c6c0d002464053f1708dc Reviewed-on: https://go-review.googlesource.com/c/build/+/371719 Trust: Carlos Amedee <[email protected]> Run-TryBot: Carlos Amedee <[email protected]> Reviewed-by: Alex Rakoczy <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 12521bb commit 295d905

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

internal/gomote/gomote.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ func New(rsp *remote.SessionPool, sched *schedule.Scheduler) *Server {
4949
}
5050
}
5151

52+
// Authenticate will allow the caller to verify that they are properly authenticated and authorized to interact with the
53+
// Service.
54+
func (s *Server) Authenticate(ctx context.Context, req *protos.AuthenticateRequest) (*protos.AuthenticateResponse, error) {
55+
_, err := access.IAPFromContext(ctx)
56+
if err != nil {
57+
log.Printf("Authenticate access.IAPFromContext(ctx) = nil, %s", err)
58+
return nil, status.Errorf(codes.Unauthenticated, "request does not contain the required authentication")
59+
}
60+
return &protos.AuthenticateResponse{}, nil
61+
}
62+
5263
// CreateInstance will create a gomote instance for the authenticated user.
5364
func (s *Server) CreateInstance(req *protos.CreateInstanceRequest, stream protos.GomoteService_CreateInstanceServer) error {
5465
ctx, cancel := context.WithTimeout(stream.Context(), 5*time.Minute)
@@ -131,7 +142,7 @@ func (s *Server) CreateInstance(req *protos.CreateInstanceRequest, stream protos
131142
}
132143
}
133144

134-
// isPrivilegedUser returns true if the user is using a Google account.
145+
// isPrivilagedUser returns true if the user is using a Google account.
135146
// The user has to be a part of the appropriate IAM group.
136147
func isPrivilegedUser(email string) bool {
137148
if strings.HasSuffix(email, "@google.com") {

internal/gomote/gomote_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ func setupGomoteTest(t *testing.T, ctx context.Context) protos.GomoteServiceClie
6161
return gc
6262
}
6363

64+
func TestAuthenticate(t *testing.T) {
65+
ctx := access.FakeContextWithOutgoingIAPAuth(context.Background(), fakeIAP())
66+
client := setupGomoteTest(t, context.Background())
67+
got, err := client.Authenticate(ctx, &protos.AuthenticateRequest{})
68+
if err != nil {
69+
t.Fatalf("client.Authenticate(ctx, request) = %v, %s; want no error", got, err)
70+
}
71+
}
72+
73+
func TestAuthenticateError(t *testing.T) {
74+
wantCode := codes.Unauthenticated
75+
client := setupGomoteTest(t, context.Background())
76+
_, err := client.Authenticate(context.Background(), &protos.AuthenticateRequest{})
77+
if status.Code(err) != wantCode {
78+
t.Fatalf("client.Authenticate(ctx, request) = _, %s; want %s", status.Code(err), wantCode)
79+
}
80+
}
81+
6482
func TestCreateInstance(t *testing.T) {
6583
ctx := access.FakeContextWithOutgoingIAPAuth(context.Background(), fakeIAP())
6684
req := &protos.CreateInstanceRequest{BuilderType: "linux-amd64"}

0 commit comments

Comments
 (0)