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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package authz
import (
"context"
"crypto/x509"
"net"
"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"
"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 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, "")
}
func LoadSpiceSchema(ctx context.Context, client *authzed.Client, path string) error {
content, err := os.ReadFile(path)
_, err = client.WriteSchema(ctx, &v1.WriteSchemaRequest{Schema: string(content)})
if err != nil {
return err
}
return nil
}
|