Zanzana: Simple openfga client wrapper (#89430)

This commit is contained in:
Alexander Zobnin 2024-06-20 10:37:16 +02:00 committed by GitHub
parent df987fe2e5
commit ba16c37126
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 11 deletions

View File

@ -21,25 +21,23 @@ import (
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
type ZanzanaClient interface{}
// ProvideZanzana used to register ZanzanaClient. // ProvideZanzana used to register ZanzanaClient.
// It will also start an embedded ZanzanaSever if mode is set to "embedded". // It will also start an embedded ZanzanaSever if mode is set to "embedded".
func ProvideZanzana(cfg *setting.Cfg, db db.DB, features featuremgmt.FeatureToggles) (ZanzanaClient, error) { func ProvideZanzana(cfg *setting.Cfg, db db.DB, features featuremgmt.FeatureToggles) (zanzana.Client, error) {
if !features.IsEnabledGlobally(featuremgmt.FlagZanzana) { if !features.IsEnabledGlobally(featuremgmt.FlagZanzana) {
return zanzana.NoopClient{}, nil return zanzana.NoopClient{}, nil
} }
logger := log.New("zanzana") logger := log.New("zanzana")
var client *zanzana.Client var client zanzana.Client
switch cfg.Zanzana.Mode { switch cfg.Zanzana.Mode {
case setting.ZanzanaModeClient: case setting.ZanzanaModeClient:
conn, err := grpc.NewClient(cfg.Zanzana.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) conn, err := grpc.NewClient(cfg.Zanzana.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create zanzana client to remote server: %w", err) return nil, fmt.Errorf("failed to create zanzana client to remote server: %w", err)
} }
client = zanzana.NewClient(openfgav1.NewOpenFGAServiceClient(conn)) client = zanzana.NewClient(conn)
case setting.ZanzanaModeEmbedded: case setting.ZanzanaModeEmbedded:
store, err := zanzana.NewEmbeddedStore(cfg, db, logger) store, err := zanzana.NewEmbeddedStore(cfg, db, logger)
if err != nil { if err != nil {
@ -53,7 +51,7 @@ func ProvideZanzana(cfg *setting.Cfg, db db.DB, features featuremgmt.FeatureTogg
channel := &inprocgrpc.Channel{} channel := &inprocgrpc.Channel{}
openfgav1.RegisterOpenFGAServiceServer(channel, srv) openfgav1.RegisterOpenFGAServiceServer(channel, srv)
client = zanzana.NewClient(openfgav1.NewOpenFGAServiceClient(channel)) client = zanzana.NewClient(channel)
default: default:
return nil, fmt.Errorf("unsupported zanzana mode: %s", cfg.Zanzana.Mode) return nil, fmt.Errorf("unsupported zanzana mode: %s", cfg.Zanzana.Mode)
} }

View File

@ -1,16 +1,46 @@
package zanzana package zanzana
import ( import (
"context"
openfgav1 "github.com/openfga/api/proto/openfga/v1" openfgav1 "github.com/openfga/api/proto/openfga/v1"
"google.golang.org/grpc"
"github.com/grafana/grafana/pkg/infra/log"
) )
// FIXME(kalleep): Build out our wrapper client for openFGA // Client is a wrapper around OpenFGAServiceClient with only methods using in Grafana included.
type Client struct { type Client interface {
c openfgav1.OpenFGAServiceClient Check(ctx context.Context, in *openfgav1.CheckRequest, opts ...grpc.CallOption) (*openfgav1.CheckResponse, error)
ListObjects(ctx context.Context, in *openfgav1.ListObjectsRequest, opts ...grpc.CallOption) (*openfgav1.ListObjectsResponse, error)
} }
func NewClient(c openfgav1.OpenFGAServiceClient) *Client { type zanzanaClient struct {
return &Client{c} client openfgav1.OpenFGAServiceClient
logger log.Logger
}
func NewClient(cc grpc.ClientConnInterface) Client {
return &zanzanaClient{
client: openfgav1.NewOpenFGAServiceClient(cc),
logger: log.New("zanzana-client"),
}
}
func (c *zanzanaClient) Check(ctx context.Context, in *openfgav1.CheckRequest, opts ...grpc.CallOption) (*openfgav1.CheckResponse, error) {
return c.client.Check(ctx, in, opts...)
}
func (c *zanzanaClient) ListObjects(ctx context.Context, in *openfgav1.ListObjectsRequest, opts ...grpc.CallOption) (*openfgav1.ListObjectsResponse, error) {
return c.client.ListObjects(ctx, in, opts...)
} }
type NoopClient struct{} type NoopClient struct{}
func (nc NoopClient) Check(ctx context.Context, in *openfgav1.CheckRequest, opts ...grpc.CallOption) (*openfgav1.CheckResponse, error) {
return nil, nil
}
func (nc NoopClient) ListObjects(ctx context.Context, in *openfgav1.ListObjectsRequest, opts ...grpc.CallOption) (*openfgav1.ListObjectsResponse, error) {
return nil, nil
}