-
Notifications
You must be signed in to change notification settings - Fork 13
/
clients.go
164 lines (134 loc) · 3.81 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2021 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/auth"
)
type Clients interface {
Clusters() ClustersClient
Records() RecordsClient
RecordContexts() RecordContextsClient
Sboms() SbomsClient
Risks() RisksClient
Signatures() SignaturesClient
PolicyResults() PolicyResultsClient
VulnReports() VulnReportsClient
Nodes() NodesClient
Namespaces() NamespacesClient
Workloads() WorkloadsClient
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 tenant 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{
clusters: NewClustersClient(conn),
records: NewRecordsClient(conn),
recordContexts: NewRecordContextsClient(conn),
sboms: NewSbomsClient(conn),
vulnReports: NewVulnReportsClient(conn),
risks: NewRisksClient(conn),
signatures: NewSignaturesClient(conn),
nodes: NewNodesClient(conn),
namespaces: NewNamespacesClient(conn),
workloads: NewWorkloadsClient(conn),
policyResults: NewPolicyResultsClient(conn),
conn: conn,
}, nil
}
func NewClientsFromConnection(conn *grpc.ClientConn) Clients {
return &clients{
clusters: NewClustersClient(conn),
records: NewRecordsClient(conn),
recordContexts: NewRecordContextsClient(conn),
sboms: NewSbomsClient(conn),
vulnReports: NewVulnReportsClient(conn),
risks: NewRisksClient(conn),
signatures: NewSignaturesClient(conn),
policyResults: NewPolicyResultsClient(conn),
nodes: NewNodesClient(conn),
namespaces: NewNamespacesClient(conn),
workloads: NewWorkloadsClient(conn),
// conn is not set, this client struct does not own closing it.
}
}
type clients struct {
clusters ClustersClient
records RecordsClient
recordContexts RecordContextsClient
sboms SbomsClient
risks RisksClient
signatures SignaturesClient
policyResults PolicyResultsClient
vulnReports VulnReportsClient
nodes NodesClient
namespaces NamespacesClient
workloads WorkloadsClient
conn *grpc.ClientConn
}
func (c *clients) Clusters() ClustersClient {
return c.clusters
}
func (c *clients) Records() RecordsClient {
return c.records
}
func (c *clients) RecordContexts() RecordContextsClient {
return c.recordContexts
}
func (c *clients) Sboms() SbomsClient {
return c.sboms
}
func (c *clients) Risks() RisksClient {
return c.risks
}
func (c *clients) Signatures() SignaturesClient {
return c.signatures
}
func (c *clients) PolicyResults() PolicyResultsClient {
return c.policyResults
}
func (c *clients) Nodes() NodesClient {
return c.nodes
}
func (c *clients) Namespaces() NamespacesClient {
return c.namespaces
}
func (c *clients) Workloads() WorkloadsClient {
return c.workloads
}
func (c *clients) VulnReports() VulnReportsClient {
return c.vulnReports
}
func (c *clients) Close() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}