PeerDB-io / peerdb

Fast, Simple and a cost effective tool to replicate data from Postgres to Data Warehouses, Queues and Storage
https://peerdb.io
Other
2.17k stars 88 forks source link

support grpc oauth #1980

Closed serprex closed 3 weeks ago

serprex commented 1 month ago

Enables using OAuth2 for calls to the Flow API

Screenshot 2024-09-09 at 17 36 13

Requires the following env vars:

Health Endpoints are explicitly excluded from auth.

iamKunalGupta commented 1 month ago

@serprex we need to exclude the health route from auth

serprex commented 1 month ago

@serprex we need to exclude the health route from auth

https://pkg.go.dev/google.golang.org/grpc#UnaryClientInterceptor

method is the RPC name

can check that for skips

iamKunalGupta commented 1 month ago

@serprex we need to exclude the health route from auth

https://pkg.go.dev/google.golang.org/grpc#UnaryClientInterceptor

method is the RPC name

can check that for skips

Something I have done in the past:

func CreateAuthServerInterceptor(authConfig *AuthConfig, unauthenticatedMethods []string) grpc.UnaryServerInterceptor {
    unauthenticatedMethodsMap := make(map[string]struct{}, len(unauthenticatedMethods))
    for _, method := range unauthenticatedMethods {
        unauthenticatedMethodsMap[method] = struct{}{}
    }
    if authConfig.Disabled {
        logging.Log().Warn("Authentication is disabled for the current server")
        return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
            return handler(ctx, req)
        }
    }
    // Can add configuration and use auth accordingly
    return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
        if _, unauthorized := unauthenticatedMethodsMap[info.FullMethod]; !unauthorized {
            // TODO add recover in case of panics here
            var err error
            ctx, err = Authorize(ctx, authConfig)
            if err != nil {
                return nil, err
            }
        }
        resp, err = handler(ctx, req)
        return resp, err
    }
}
pjhampton commented 3 weeks ago

LGTM with some additional testing