github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/fakeauth/fake_auth.go (about)

     1  // Package fakeauth provides middlewares thats injects a fake userID, so the rest of the code
     2  // can continue to be multitenant.
     3  package fakeauth
     4  
     5  import (
     6  	"context"
     7  	"net/http"
     8  
     9  	"github.com/weaveworks/common/middleware"
    10  	"github.com/weaveworks/common/server"
    11  	"github.com/weaveworks/common/user"
    12  	"google.golang.org/grpc"
    13  )
    14  
    15  // SetupAuthMiddleware for the given server config.
    16  func SetupAuthMiddleware(config *server.Config, enabled bool, noGRPCAuthOn []string) middleware.Interface {
    17  	if enabled {
    18  		ignoredMethods := map[string]bool{}
    19  		for _, m := range noGRPCAuthOn {
    20  			ignoredMethods[m] = true
    21  		}
    22  
    23  		config.GRPCMiddleware = append(config.GRPCMiddleware, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
    24  			if ignoredMethods[info.FullMethod] {
    25  				return handler(ctx, req)
    26  			}
    27  			return middleware.ServerUserHeaderInterceptor(ctx, req, info, handler)
    28  		})
    29  
    30  		config.GRPCStreamMiddleware = append(config.GRPCStreamMiddleware,
    31  			func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    32  				if ignoredMethods[info.FullMethod] {
    33  					return handler(srv, ss)
    34  				}
    35  				return middleware.StreamServerUserHeaderInterceptor(srv, ss, info, handler)
    36  			},
    37  		)
    38  
    39  		return middleware.AuthenticateUser
    40  	}
    41  
    42  	config.GRPCMiddleware = append(config.GRPCMiddleware,
    43  		fakeGRPCAuthUniaryMiddleware,
    44  	)
    45  	config.GRPCStreamMiddleware = append(config.GRPCStreamMiddleware,
    46  		fakeGRPCAuthStreamMiddleware,
    47  	)
    48  	return fakeHTTPAuthMiddleware
    49  }
    50  
    51  var fakeHTTPAuthMiddleware = middleware.Func(func(next http.Handler) http.Handler {
    52  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    53  		ctx := user.InjectOrgID(r.Context(), "fake")
    54  		next.ServeHTTP(w, r.WithContext(ctx))
    55  	})
    56  })
    57  
    58  var fakeGRPCAuthUniaryMiddleware = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    59  	ctx = user.InjectOrgID(ctx, "fake")
    60  	return handler(ctx, req)
    61  }
    62  
    63  var fakeGRPCAuthStreamMiddleware = func(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    64  	ctx := user.InjectOrgID(ss.Context(), "fake")
    65  	return handler(srv, serverStream{
    66  		ctx:          ctx,
    67  		ServerStream: ss,
    68  	})
    69  }
    70  
    71  type serverStream struct {
    72  	ctx context.Context
    73  	grpc.ServerStream
    74  }
    75  
    76  func (ss serverStream) Context() context.Context {
    77  	return ss.ctx
    78  }