diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-22 17:35:49 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-22 17:35:49 -0600 |
| commit | 20ef0d92694465ac86b550df139e8366a0a2b4fa (patch) | |
| tree | 3f14589e1ce6eb9306a3af31c3a1f9e1af5ed637 /pkg/authz/grpc.go | |
| parent | 44e0d272c040cdc53a98b9f1dc58ae7da67752e6 (diff) | |
feat: connect to spicedb
Diffstat (limited to 'pkg/authz/grpc.go')
| -rw-r--r-- | pkg/authz/grpc.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/pkg/authz/grpc.go b/pkg/authz/grpc.go new file mode 100644 index 0000000..234208c --- /dev/null +++ b/pkg/authz/grpc.go @@ -0,0 +1,60 @@ +package authz + +import ( + "context" + "crypto/x509" + "net" + + "github.com/authzed/authzed-go/v1" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials" + "google.golang.org/grpc/credentials/insecure" +) + +func NewGrpcConnection(ctx context.Context, host string) *grpc.ClientConn { + connection, err := grpc.NewClient( + host, + grpc.WithTransportCredentials(credentialsFor(ctx, host)), + ) + if err != nil { + pls.LogErrorNow(ctx, err) + } + + return connection +} + +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 +} + +func credentialsFor(ctx context.Context, host string) credentials.TransportCredentials { + if host == "" { + return insecure.NewCredentials() + } + + _, port, err := net.SplitHostPort(host) + if err != nil { + pls.LogErrorNow(ctx, err) + return insecure.NewCredentials() + } + + if port != "443" { + return insecure.NewCredentials() + } + + pool, err := x509.SystemCertPool() + if err != nil { + return insecure.NewCredentials() + } + + return credentials.NewClientTLSFromCert(pool, "") +} |
