Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Commit ee29103

Browse files
committed
add fleetdb client for oidc support
1 parent 838579f commit ee29103

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

cmd/server/server.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import (
55
"errors"
66
"log"
77
"net/http"
8+
"net/url"
89
"time"
910

11+
"github.com/coreos/go-oidc"
1012
"github.com/equinix-labs/otel-init-go/otelinit"
13+
"github.com/hashicorp/go-retryablehttp"
1114
fleetdb "github.com/metal-toolbox/fleetdb/pkg/api/v1"
15+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1216
"go.uber.org/zap"
17+
"golang.org/x/oauth2"
18+
"golang.org/x/oauth2/clientcredentials"
1319

1420
rootCmd "github.com/metal-toolbox/component-inventory/cmd"
1521
"github.com/metal-toolbox/component-inventory/internal/app"
@@ -19,13 +25,61 @@ import (
1925
"github.com/spf13/cobra"
2026
)
2127

22-
var shutdownTimeout = 10 * time.Second
28+
const (
29+
dialTimeout = 30 * time.Second
30+
shutdownTimeout = 10 * time.Second
31+
)
2332

2433
func getFleetDBClient(cfg *app.Configuration) (*fleetdb.Client, error) {
2534
if cfg.FleetDBOpts.DisableOAuth {
2635
return fleetdb.NewClient(cfg.FleetDBOpts.Endpoint, nil)
2736
}
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+
)
2983
}
3084

3185
// install server command

0 commit comments

Comments
 (0)