Skip to content

Commit

Permalink
wip: fix oauth2 token handling for lbaas
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Dec 14, 2023
1 parent 5ead676 commit 8c14bc7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
40 changes: 26 additions & 14 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cli

import (
"context"
"fmt"
"log"
"net/http"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/oauth2"

v1 "github.com/equinix/metal-cli/internal/loadbalancers/api/v1"
"github.com/equinix/metal-cli/internal/loadbalancers/infrastructure"
Expand Down Expand Up @@ -118,16 +120,32 @@ func (c *Client) metalApiConnect(httpClient *http.Client) error {
return nil
}

func (c *Client) lbaasApiConnect(httpClient *http.Client) error {
func (c *Client) lbaasApiConnect(header http.Header) error {
ctx := context.Background()
config := oauth2.Config{
Endpoint: oauth2.Endpoint{
TokenURL: "https://iam.metalctrl.io/token",
},
}
ts := infrastructure.NewTokenExchanger(c.Token(), nil)
token, err := ts.Token()
if err != nil {
return err
}
client := &http.Client{
Transport: &headerTransport{
header: header,
},
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
client = config.Client(ctx, token)

configuration := v1.NewConfiguration()
configuration.Debug = checkEnvForDebug()
token := infrastructure.NewTokenExchanger(c.Token(), httpClient)
token.Token()
configuration.AddDefaultHeader("X-Auth-Token", c.Token())
configuration.HTTPClient = httpClient
configuration.HTTPClient = client
configuration.UserAgent = fmt.Sprintf(uaFormat, c.Version, configuration.UserAgent)
client := v1.NewAPIClient(configuration)
c.lbaasApiClient = client

c.lbaasApiClient = v1.NewAPIClient(configuration)
return nil
}

Expand Down Expand Up @@ -234,13 +252,7 @@ func (c *Client) LoadbalancerAPI(cmd *cobra.Command) *v1.APIClient {
}

if c.lbaasApiClient == nil {
httpClient := &http.Client{
Transport: &headerTransport{
header: getAdditionalHeaders(cmd),
},
}

err := c.lbaasApiConnect(httpClient)
err := c.lbaasApiConnect(getAdditionalHeaders(cmd))
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 9 additions & 4 deletions internal/loadbalancers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ func (c *Client) Create() *cobra.Command {
}

req := c.projectService.CreateLoadBalancer(context.Background(), projectID)
req.LoadBalancerCreate(*lbaas.NewLoadBalancerCreate(name, locationId, portIds, providerId))
// opts := lbaas.NewLoadBalancerCreate(name, locationId, portIds, providerId)
opts := &lbaas.LoadBalancerCreate{
Name: name,
LocationId: locationId,
PortIds: portIds,
ProviderId: providerId,
}
req = req.LoadBalancerCreate(*opts)
lb, _, err := req.Execute()
if err != nil {
return fmt.Errorf("Could not create LoadBalancer: %w", err)
Expand All @@ -76,11 +83,9 @@ func (c *Client) Create() *cobra.Command {
createLoadBalancerCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable.")
createLoadBalancerCmd.Flags().StringVarP(&locationId, "location", "l", "", "The location's ID.")
createLoadBalancerCmd.Flags().StringVarP(&providerId, "provider", "r", ProviderID, "The provider ID.")
createLoadBalancerCmd.Flags().StringSliceVarP(&portIds, "port", "o", []string{}, "The port's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PORT_ID environment variable.")
createLoadBalancerCmd.Flags().StringSliceVar(&portIds, "port", []string{}, "The port's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PORT_ID environment variable.")

// TODO(displague) Not sure if this is needed
_ = createLoadBalancerCmd.MarkFlagRequired("port")
_ = createLoadBalancerCmd.MarkFlagRequired("provider")
_ = createLoadBalancerCmd.MarkFlagRequired("location")
_ = createLoadBalancerCmd.MarkFlagRequired("project-id")
_ = createLoadBalancerCmd.MarkFlagRequired("name")
Expand Down
2 changes: 1 addition & 1 deletion internal/loadbalancers/infrastructure/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
lbaas "github.com/equinix/metal-cli/internal/loadbalancers/api/v1"
)

const ProviderID = ""
const ProviderID = "loadpvd-gOB_-byp5ebFo7A3LHv2B"

var LBMetros = map[string]string{
"da": "lctnloc--uxs0GLeAELHKV8GxO_AI",
Expand Down
7 changes: 6 additions & 1 deletion internal/loadbalancers/infrastructure/token_exchanger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ type TokenExchanger struct {
client *http.Client
}

var _ oauth2.TokenSource = (*TokenExchanger)(nil)

func NewTokenExchanger(metalAPIKey string, client *http.Client) *TokenExchanger {
if client == nil {
client = http.DefaultClient
}
return &TokenExchanger{
metalAPIKey: metalAPIKey,
client: client,
}
}

func (m *TokenExchanger) Token() (*oauth2.Token, error) {
func (m TokenExchanger) Token() (*oauth2.Token, error) {
tokenExchangeURL := "https://iam.metalctrl.io/api-keys/exchange"
tokenExchangeRequest, err := http.NewRequest("POST", tokenExchangeURL, nil)
if err != nil {
Expand Down

0 comments on commit 8c14bc7

Please sign in to comment.