Skip to content

Commit 7b8c4ee

Browse files
committed
internal/access: add helper functions for testing authentication
This change adds functions which assists in the testing of authentication. Updates golang/go#48729 Change-Id: Ibff9c758e97c5fd3584c6e68612e4d9db99baa5e Reviewed-on: https://go-review.googlesource.com/c/build/+/371396 Trust: Carlos Amedee <[email protected]> Run-TryBot: Carlos Amedee <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c4e615e commit 7b8c4ee

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

internal/access/access.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ const (
3434
// IAPFields contains the values for the headers retrieved from Identity Aware
3535
// Proxy.
3636
type IAPFields struct {
37+
// Email contains the user's email address
38+
// For example, "accounts.google.com:[email protected]"
3739
Email string
38-
ID string
40+
// ID contains a unique identifier for the user
41+
// For example, "accounts.google.com:userIDvalue"
42+
ID string
3943
}
4044

4145
// IAPFromContext retrieves the IAPFields stored in the context if it exists.
@@ -96,7 +100,12 @@ func contextWithIAPMD(ctx context.Context, md metadata.MD) (context.Context, err
96100
if iap.ID, err = retrieveFn(md, iapHeaderID); err != nil {
97101
return ctx, fmt.Errorf("unable to retrieve metadata field: %s", iapHeaderID)
98102
}
99-
return context.WithValue(ctx, contextIAP, iap), nil
103+
return ContextWithIAP(ctx, iap), nil
104+
}
105+
106+
// ContextWithIAP adds the iap fields to the context.
107+
func ContextWithIAP(ctx context.Context, iap IAPFields) context.Context {
108+
return context.WithValue(ctx, contextIAP, iap)
100109
}
101110

102111
// RequireIAPAuthUnaryInterceptor creates an authentication interceptor for a GRPC
@@ -132,3 +141,28 @@ func IAPAudienceGCE(projectNumber int64, serviceID string) string {
132141
func IAPAudienceAppEngine(projectNumber int64, projectID string) string {
133142
return fmt.Sprintf("/projects/%d/apps/%s", projectNumber, projectID)
134143
}
144+
145+
// FakeContextWithOutgoingIAPAuth adds the iap fields to the metadata of an outgoing GRPC request and
146+
// should only be used for testing.
147+
func FakeContextWithOutgoingIAPAuth(ctx context.Context, iap IAPFields) context.Context {
148+
md := metadata.New(map[string]string{
149+
iapHeaderEmail: iap.Email,
150+
iapHeaderID: iap.ID,
151+
iapHeaderJWT: "test-jwt",
152+
})
153+
return metadata.NewOutgoingContext(ctx, md)
154+
}
155+
156+
// FakeIAPAuthFunc provides a fake IAP authentication validation and should only be used for testing.
157+
func FakeIAPAuthFunc() grpcauth.AuthFunc {
158+
return iapAuthFunc("TESTING", func(ctx context.Context, token, audiance string) (*idtoken.Payload, error) { return nil, nil })
159+
}
160+
161+
// FakeIAPAuthInterceptorOptions provides the GRPC server options for fake IAP authentication
162+
// and should only be used for testing.
163+
func FakeIAPAuthInterceptorOptions() []grpc.ServerOption {
164+
return []grpc.ServerOption{
165+
grpc.UnaryInterceptor(grpcauth.UnaryServerInterceptor(FakeIAPAuthFunc())),
166+
grpc.StreamInterceptor(grpcauth.StreamServerInterceptor(FakeIAPAuthFunc())),
167+
}
168+
}

0 commit comments

Comments
 (0)