-
Notifications
You must be signed in to change notification settings - Fork 15
/
clients.go
81 lines (64 loc) · 1.74 KB
/
clients.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Copyright 2023 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package v1
import (
"context"
"fmt"
"net/url"
"time"
delegate "chainguard.dev/go-grpc-kit/pkg/options"
"google.golang.org/grpc"
"knative.dev/pkg/logging"
"chainguard.dev/sdk/pkg/auth"
)
type Clients interface {
SecurityAdvisory() SecurityAdvisoryClient
Close() error
}
func NewClients(ctx context.Context, addr string, token string) (Clients, error) {
uri, err := url.Parse(addr)
if err != nil {
return nil, fmt.Errorf("failed to parse advisory service address, must be a url: %w", err)
}
target, opts := delegate.GRPCOptions(*uri)
// TODO: we may want to require transport security at some future point.
if cred := auth.NewFromToken(ctx, token, false); cred != nil {
opts = append(opts, grpc.WithPerRPCCredentials(cred))
} else {
logging.FromContext(ctx).Warn("No authentication provided, this may end badly.")
}
var cancel context.CancelFunc
if _, timeoutSet := ctx.Deadline(); !timeoutSet {
ctx, cancel = context.WithTimeout(ctx, 300*time.Second)
defer cancel()
}
conn, err := grpc.DialContext(ctx, target, opts...)
if err != nil {
return nil, fmt.Errorf("failed to connect to the iam server: %w", err)
}
return &clients{
advisory: NewSecurityAdvisoryClient(conn),
conn: conn,
}, nil
}
func NewClientsFromConnection(conn *grpc.ClientConn) Clients {
return &clients{
advisory: NewSecurityAdvisoryClient(conn),
// conn is not set, this client struct does not own closing it.
}
}
type clients struct {
advisory SecurityAdvisoryClient
conn *grpc.ClientConn
}
func (c *clients) SecurityAdvisory() SecurityAdvisoryClient {
return c.advisory
}
func (c *clients) Close() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}