@@ -5,11 +5,17 @@ import (
5
5
"errors"
6
6
"log"
7
7
"net/http"
8
+ "net/url"
8
9
"time"
9
10
11
+ "github.com/coreos/go-oidc"
10
12
"github.com/equinix-labs/otel-init-go/otelinit"
13
+ "github.com/hashicorp/go-retryablehttp"
11
14
fleetdb "github.com/metal-toolbox/fleetdb/pkg/api/v1"
15
+ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
12
16
"go.uber.org/zap"
17
+ "golang.org/x/oauth2"
18
+ "golang.org/x/oauth2/clientcredentials"
13
19
14
20
rootCmd "github.com/metal-toolbox/component-inventory/cmd"
15
21
"github.com/metal-toolbox/component-inventory/internal/app"
@@ -19,13 +25,61 @@ import (
19
25
"github.com/spf13/cobra"
20
26
)
21
27
22
- var shutdownTimeout = 10 * time .Second
28
+ const (
29
+ dialTimeout = 30 * time .Second
30
+ shutdownTimeout = 10 * time .Second
31
+ )
23
32
24
33
func getFleetDBClient (cfg * app.Configuration ) (* fleetdb.Client , error ) {
25
34
if cfg .FleetDBOpts .DisableOAuth {
26
35
return fleetdb .NewClient (cfg .FleetDBOpts .Endpoint , nil )
27
36
}
28
- return nil , errors .New ("OIDC integration not implemented" )
37
+
38
+ ctx := context .Background ()
39
+
40
+ // init retryable http client
41
+ retryableClient := retryablehttp .NewClient ()
42
+
43
+ // set retryable HTTP client to be the otel http client to collect telemetry
44
+ retryableClient .HTTPClient = otelhttp .DefaultClient
45
+
46
+ // setup oidc provider
47
+ provider , err := oidc .NewProvider (ctx , cfg .FleetDBOpts .IssuerEndpoint )
48
+ if err != nil {
49
+ return nil , err
50
+ }
51
+
52
+ clientID := "component-inventory"
53
+
54
+ if cfg .FleetDBOpts .ClientID != "" {
55
+ clientID = cfg .FleetDBOpts .ClientID
56
+ }
57
+
58
+ // setup oauth configuration
59
+ oauthConfig := clientcredentials.Config {
60
+ ClientID : clientID ,
61
+ ClientSecret : cfg .FleetDBOpts .ClientSecret ,
62
+ TokenURL : provider .Endpoint ().TokenURL ,
63
+ Scopes : cfg .FleetDBOpts .ClientScopes ,
64
+ EndpointParams : url.Values {"audience" : []string {cfg .FleetDBOpts .AudienceEndpoint }},
65
+ // with this the oauth client spends less time identifying the client grant mechanism.
66
+ AuthStyle : oauth2 .AuthStyleInParams ,
67
+ }
68
+
69
+ // wrap OAuth transport, cookie jar in the retryable client
70
+ oAuthclient := oauthConfig .Client (ctx )
71
+
72
+ retryableClient .HTTPClient .Transport = oAuthclient .Transport
73
+ retryableClient .HTTPClient .Jar = oAuthclient .Jar
74
+
75
+ httpClient := retryableClient .StandardClient ()
76
+ httpClient .Timeout = dialTimeout
77
+
78
+ return fleetdb .NewClientWithToken (
79
+ cfg .FleetDBOpts .ClientSecret ,
80
+ cfg .FleetDBOpts .Endpoint ,
81
+ httpClient ,
82
+ )
29
83
}
30
84
31
85
// install server command
0 commit comments