Skip to content

Commit

Permalink
Merge pull request #8 from ZoeySimone/ident_interface
Browse files Browse the repository at this point in the history
Use IdentityAPI interface for issuer token generation
  • Loading branch information
eamonnotoole authored Aug 13, 2021
2 parents 0851ef8 + f3d2975 commit 8dc65dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
26 changes: 23 additions & 3 deletions pkg/token/issuertoken/issuertoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ type TokenResponse struct {
Scope string `json:"scope"`
}

type Client struct {
identityServiceURL string
httpClient httpClient
}

type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}

// New creates a new identity Client object
func New(identityServiceURL string) *Client {
client := &http.Client{Timeout: 10 * time.Second}
identityServiceURL = strings.TrimRight(identityServiceURL, "/")

return &Client{
identityServiceURL: identityServiceURL,
httpClient: client,
}
}

func doRetries(call func() (*http.Response, error), retries int) (*http.Response, error) {
var resp *http.Response
var err error
Expand All @@ -45,22 +65,22 @@ func doRetries(call func() (*http.Response, error), retries int) (*http.Response
return resp, nil
}

func GenerateIssuerToken(ctx context.Context, issuerURL, clientID, clientSecret string) (string, error) {
func (c *Client) GenerateToken(ctx context.Context, tenantID, clientID, clientSecret string) (string, error) {
params := url.Values{}
params.Add("client_id", clientID)
params.Add("client_secret", clientSecret)
params.Add("grant_type", "client_credentials")
params.Add("scope", "hpe-tenant")

url := fmt.Sprintf("%s/v1/token", issuerURL)
url := fmt.Sprintf("%s/v1/token", c.identityServiceURL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(params.Encode()))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := doRetries(func() (*http.Response, error) {
return http.DefaultClient.Do(req)
return c.httpClient.Do(req)
}, retryLimit)
if err != nil {
return "", err
Expand Down
23 changes: 14 additions & 9 deletions pkg/token/serviceclient/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const retryLimit = 3
// Assert that Handler implements common.TokenChannelInterface
var _ common.TokenChannelInterface = (*Handler)(nil)

type IdentityAPI interface {
GenerateToken(context.Context, string, string, string) (string, error)
}

// Handler the handler for service-client creds
type Handler struct {
iamServiceURL string
Expand All @@ -30,16 +34,16 @@ type Handler struct {
clientSecret string
vendedServiceClient bool
numRetries int
client identityclient.IdentityAPI
client IdentityAPI
resultCh chan common.Result
exitCh chan int
}

// CreateOpt - function option definition
type CreateOpt func(h *Handler)

// WithIdentityAPI override the identityclient.IdentityAPI in Handler
func WithIdentityAPI(i identityclient.IdentityAPI) CreateOpt {
// WithIdentityAPI override the IdentityAPI in Handler
func WithIdentityAPI(i IdentityAPI) CreateOpt {
return func(h *Handler) {
h.client = i
}
Expand All @@ -51,12 +55,17 @@ func NewHandler(d *schema.ResourceData, opts ...CreateOpt) (common.TokenChannelI

// set Handler fields
h.iamServiceURL = d.Get("iam_service_url").(string)
h.client = identityclient.New(h.iamServiceURL)
h.tenantID = d.Get("tenant_id").(string)
h.clientID = d.Get("user_id").(string)
h.clientSecret = d.Get("user_secret").(string)
h.vendedServiceClient = d.Get("api_vended_service_client").(bool)

if h.vendedServiceClient {
h.client = issuertoken.New(h.iamServiceURL)
} else {
h.client = identityclient.New(h.iamServiceURL)
}

// run overrides
for _, opt := range opts {
if opt != nil {
Expand Down Expand Up @@ -165,11 +174,7 @@ func (h *Handler) generateToken() (string, bool, error) {
var err error

// TODO pass a context down to here
if h.vendedServiceClient {
token, err = issuertoken.GenerateIssuerToken(context.Background(), h.iamServiceURL, h.clientID, h.clientSecret)
} else {
token, err = h.client.GenerateToken(context.Background(), h.tenantID, h.clientID, h.clientSecret)
}
token, err = h.client.GenerateToken(context.Background(), h.tenantID, h.clientID, h.clientSecret)

// If this is a retryable error check to see if we've reached our retryLimit or not, if we can retry again
// return true
Expand Down

0 comments on commit 8dc65dc

Please sign in to comment.