summaryrefslogtreecommitdiff
path: root/pkg/authz/spice.go
blob: e563375a73803f2315776b370ad2dfb0637b85f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package authz

import (
	"context"
	"os"

	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
	"github.com/authzed/authzed-go/v1"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
	"google.golang.org/grpc"
)

func NewSpiceDBClient(ctx context.Context, host string, presharedKey string) *authzed.Client {
	client, err := authzed.NewClient(
		host,
		grpc.WithTransportCredentials(credentialsFor(ctx, host)),
		grpc.WithPerRPCCredentials(NewBearerToken(presharedKey)),
	)
	if err != nil {
		pls.LogErrorNow(ctx, err)
	}
	return client
}

type CheckPermissionService interface {
	CheckPermission(ctx context.Context, in *v1.CheckPermissionRequest, opts ...grpc.CallOption) (*v1.CheckPermissionResponse, error)
}

func WriteSchema(ctx context.Context, client *authzed.Client, path string) (*v1.WriteSchemaResponse, error) {
	content, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	return client.WriteSchema(ctx, &v1.WriteSchemaRequest{
		Schema: string(content),
	})
}