-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
484 lines (397 loc) · 12.6 KB
/
client.go
File metadata and controls
484 lines (397 loc) · 12.6 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package diode
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/netboxlabs/diode-sdk-go/diode/v1/diodepb"
)
const (
// SDKName is the name of the Diode SDK
SDKName = "diode-sdk-go"
// SDKVersion is the version of the Diode SDK
SDKVersion = "0.2.0"
// DiodeClientIDEnvVarName is the environment variable name for the Diode Client ID
DiodeClientIDEnvVarName = "DIODE_CLIENT_ID"
// DiodeClientSecretEnvVarName is the environment variable name for the Diode Client Secret
DiodeClientSecretEnvVarName = "DIODE_CLIENT_SECRET"
// DiodeSDKLogLevelEnvVarName is the environment variable name for the Diode SDK log level
DiodeSDKLogLevelEnvVarName = "DIODE_SDK_LOG_LEVEL"
// DiodeMaxAuthRetriesEnvVarName is the environment variable name for the maximum number of authentication retries
DiodeMaxAuthRetriesEnvVarName = "DIODE_MAX_AUTH_RETRIES"
defaultStreamName = "latest"
)
var allowedSchemesRe = regexp.MustCompile(`grpc|grpcs`)
// loadCerts loads the system x509 cert pool
func loadCerts() *x509.CertPool {
certPool, _ := x509.SystemCertPool()
return certPool
}
// parseTarget parses the target string into authority, path, and tlsVerify
func parseTarget(target string) (string, string, bool, error) {
u, err := url.Parse(target)
if err != nil {
return "", "", false, err
}
if !allowedSchemesRe.MatchString(u.Scheme) {
return "", "", false, errors.New("target should start with grpc:// or grpcs://")
}
authority := u.Host
if u.Port() == "" {
authority += ":443"
}
path := u.Path
if path == "/" {
path = ""
}
tlsVerify := u.Scheme == "grpcs"
return authority, path, tlsVerify, nil
}
// getClientID returns the client ID either from provided value or environment variable
func getClientID(clientID string) (string, error) {
if clientID == "" {
clientID = os.Getenv(DiodeClientIDEnvVarName)
}
if clientID == "" {
return "", fmt.Errorf("client_id param or %s environment variable required", DiodeClientIDEnvVarName)
}
return clientID, nil
}
// getClientSecret returns the client secret either from provided value or environment variable
func getClientSecret(clientSecret string) (string, error) {
if clientSecret == "" {
clientSecret = os.Getenv(DiodeClientSecretEnvVarName)
}
if clientSecret == "" {
return "", fmt.Errorf("client_secret param or %s environment variable required", DiodeClientSecretEnvVarName)
}
return clientSecret, nil
}
// getAuthRetries returns the maximum number of authentication retries
func getAuthRetries(maxAuthRetries int) (int, error) {
maxAuthRetriesStr := os.Getenv(DiodeMaxAuthRetriesEnvVarName)
if maxAuthRetriesStr != "" {
retries, err := strconv.Atoi(maxAuthRetriesStr)
if err != nil {
return 0, fmt.Errorf("invalid value for %s: %w", DiodeMaxAuthRetriesEnvVarName, err)
}
maxAuthRetries = retries
}
if maxAuthRetries <= 0 {
return 0, fmt.Errorf("max_auth_retries param or %s environment variable must be greater than 0", DiodeMaxAuthRetriesEnvVarName)
}
return maxAuthRetries, nil
}
// Client is an interface that defines the methods available from Diode API
type Client interface {
// Close closes the connection to the API service
Close() error
// Ingest sends an ingest request to the ingester service
Ingest(context.Context, []Entity) (*diodepb.IngestResponse, error)
}
// GRPCClient is a gRPC implementation of the ingester service
type GRPCClient struct {
// The logger for the client
logger *slog.Logger
// gRPC virtual connection
conn *grpc.ClientConn
// The gRPC API client
client diodepb.IngesterServiceClient
// Producer's application name
appName string
// Producer's application version
appVersion string
// The client ID for the API
clientID string
// The client secret for the API
clientSecret string
// The maximum number of authentication retries
maxAuthRetries int
// GRPC target
target string
// GRPC path
path string
// TLS verify
tlsVerify bool
// Platform name
platform string
// Go version
goVersion string
// Metadata
metadata metadata.MD
}
// ClientOption is a functional option for the GRPCClient
type ClientOption func(*GRPCClient)
// WithClientID sets the client ID for the GRPCClient
func WithClientID(clientID string) ClientOption {
return func(c *GRPCClient) {
c.clientID = clientID
}
}
// WithClientSecret sets the client secret for the GRPCClient
func WithClientSecret(clientSecret string) ClientOption {
return func(c *GRPCClient) {
c.clientSecret = clientSecret
}
}
// authenticate fetches an OAuth2 token using client credentials and updates the metadata with the token.
func (g *GRPCClient) authenticate() error {
authClient := newDiodeAuthentication(g.target, g.path, g.tlsVerify, g.clientID, g.clientSecret)
accessToken, err := authClient.authenticate(g.logger)
if err != nil {
return fmt.Errorf("authentication failed: %w", err)
}
// Update metadata with the new authorization token
g.metadata.Set("authorization", fmt.Sprintf("Bearer %s", accessToken))
return nil
}
// DiodeAuthentication handles OAuth2 authentication for the Diode API.
type diodeAuthentication struct {
target string
path string
tlsVerify bool
clientID string
clientSecret string
}
// NewDiodeAuthentication creates a new instance of DiodeAuthentication.
func newDiodeAuthentication(target string, path string, tlsVerify bool, clientID, clientSecret string) *diodeAuthentication {
return &diodeAuthentication{
target: target,
path: path,
tlsVerify: tlsVerify,
clientID: clientID,
clientSecret: clientSecret,
}
}
// Authenticate requests an OAuth2 token using client credentials and returns it.
func (d *diodeAuthentication) authenticate(logger *slog.Logger) (string, error) {
scheme := "http"
if d.tlsVerify {
scheme = "https"
}
authURL := fmt.Sprintf("%s://%s/auth/token", scheme, d.target)
if d.path != "" {
authURL = fmt.Sprintf("%s://%s%s/auth/token", scheme, d.target, d.path)
}
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Set("client_id", d.clientID)
data.Set("client_secret", d.clientSecret)
req, err := http.NewRequest(http.MethodPost, authURL, strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
if !d.tlsVerify {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to send request: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Error("failed to close response body", "error", err)
}
}()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("authentication failed: %s", resp.Status)
}
var result struct {
AccessToken string `json:"access_token"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", fmt.Errorf("failed to parse response: %w", err)
}
if result.AccessToken == "" {
return "", errors.New("access token not found in response")
}
return result.AccessToken, nil
}
// NewClient creates a new diode client based on gRPC
func NewClient(target string, appName string, appVersion string, opts ...ClientOption) (Client, error) {
logger := newLogger()
if appName == "" {
return nil, fmt.Errorf("app name is required")
}
if appVersion == "" {
return nil, fmt.Errorf("app version is required")
}
target, path, tlsVerify, err := parseTarget(target)
if err != nil {
return nil, err
}
dialOpts := []grpc.DialOption{
grpc.WithUserAgent(userAgent()),
}
if path != "" {
logger.Debug("Setting up gRPC interceptor for path", "path", path)
dialOpts = append(dialOpts, methodUnaryInterceptor(path))
}
if tlsVerify {
logger.Debug("Setting up gRPC secure channel")
rootCAs := loadCerts()
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{RootCAs: rootCAs})))
} else {
logger.Debug("Setting up gRPC insecure channel")
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
conn, err := grpc.NewClient(target, dialOpts...)
if err != nil {
return nil, err
}
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
goVersion := runtime.Version()
c := &GRPCClient{
logger: logger,
conn: conn,
client: diodepb.NewIngesterServiceClient(conn),
appName: appName,
appVersion: appVersion,
target: target,
path: path,
tlsVerify: tlsVerify,
platform: platform,
goVersion: goVersion,
maxAuthRetries: 3,
}
var clientID string
var clientSecret string
for _, o := range opts {
o(c)
}
c.metadata = metadata.Pairs("platform", platform, "go-version", goVersion)
c.maxAuthRetries, err = getAuthRetries(c.maxAuthRetries)
if err != nil {
return nil, err
}
clientID, err = getClientID(c.clientID)
if err != nil {
return nil, err
}
clientSecret, err = getClientSecret(c.clientSecret)
if err != nil {
return nil, err
}
c.clientID = clientID
c.clientSecret = clientSecret
if err = c.authenticate(); err != nil {
return nil, err
}
return c, nil
}
// Close closes the connection to the API service
func (g *GRPCClient) Close() error {
if g.conn != nil {
return g.conn.Close()
}
return nil
}
// Ingest sends an ingest request to the ingester service
func (g *GRPCClient) Ingest(ctx context.Context, entities []Entity) (*diodepb.IngestResponse, error) {
stream := defaultStreamName
protoEntities := convertEntitiesToProto(entities)
req := &diodepb.IngestRequest{
Id: uuid.NewString(),
Entities: protoEntities,
Stream: stream,
ProducerAppName: g.appName,
ProducerAppVersion: g.appVersion,
SdkName: SDKName,
SdkVersion: SDKVersion,
}
ctx = metadata.NewOutgoingContext(ctx, g.metadata)
var err error
var res *diodepb.IngestResponse
attempt := 0
for {
res, err = g.client.Ingest(ctx, req)
if err != nil {
if status.Code(err) == codes.Unauthenticated {
attempt++
if attempt >= g.maxAuthRetries {
return nil, fmt.Errorf("authentication failed after %d attempts: %w", attempt, err)
}
g.logger.Debug("Authentication failed, retrying...", "attempt", attempt)
if err := g.authenticate(); err != nil {
g.logger.Error("Failed to re-authenticate", "error", err)
}
continue
}
return nil, err
}
break
}
return res, nil
}
// convertEntitiesToProto converts entities to proto entities
func convertEntitiesToProto(entities []Entity) []*diodepb.Entity {
protoEntities := make([]*diodepb.Entity, 0)
for _, entity := range entities {
entityPb := entity.ConvertToProtoEntity()
entityPb.Timestamp = timestamppb.New(time.Now().UTC())
protoEntities = append(protoEntities, entityPb)
}
return protoEntities
}
// methodUnaryInterceptor returns a gRPC dial option with a unary interceptor
//
// It's used to intercept the client calls and modify the method details.
//
// Diode's default method generated from Protocol Buffers definition is /diode.v1.IngesterService/Ingest and in order
// to use Diode targets with path (i.e. localhost:8081/this/is/custom/path), this interceptor is used to modify the
// method details, by prepending the generated method name with the path extracted from initial target.
func methodUnaryInterceptor(path string) grpc.DialOption {
return grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
method = fmt.Sprintf("%s%s", path, method)
return invoker(ctx, method, req, reply, cc, opts...)
})
}
// userAgent returns the user agent string for the SDK
func userAgent() string {
return fmt.Sprintf("%s/%s", SDKName, SDKVersion)
}
// newLogger creates a new logger for the SDK
func newLogger() *slog.Logger {
level, ok := os.LookupEnv(DiodeSDKLogLevelEnvVarName)
if !ok {
level = "INFO"
}
var l slog.Level
switch strings.ToUpper(level) {
case "DEBUG":
l = slog.LevelDebug
case "INFO":
l = slog.LevelInfo
case "WARN":
l = slog.LevelWarn
case "ERROR":
l = slog.LevelError
default:
l = slog.LevelDebug
}
h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: l, AddSource: false})
return slog.New(h)
}