Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use original API request context for auth token requests #847

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ctreatma
Copy link
Contributor

@ctreatma ctreatma commented Jan 29, 2025

#844 updated the Config object to create a new auth client every time a new Fabric API client was created for SDK resources. If we stuck with this pattern, we would have to duplicate that behavior for every Equinix API client except for Metal. In addition, this pattern means that the provider will make a separate auth token request per resource rather than reusing valid tokens across all resources that use the same provider (and therefor auth) config.

This reverts the auth client change by introducing a custom HTTP transport and OAuth token source that enables passing the original request context for an API request to the auth token client.

This change requires these customizations be made here (or potentially in a non-generated section of equinix-sdk-go) because the oauth2 library does not accept a context at the time a token is obtained; the context can only be specified when a token source is instantiated. This change also relies on a branch of equinix-sdk-go that supports the Equinix access token API. As a result, we no longer need to use equinix/oauth2-go.

@codecov-commenter
Copy link

codecov-commenter commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 79.83193% with 24 lines in your changes missing coverage. Please review.

Project coverage is 65.74%. Comparing base (ed852bc) to head (748452f).

Files with missing lines Patch % Lines
internal/authtoken/transport.go 65.85% 11 Missing and 3 partials ⚠️
internal/authtoken/oauth2.go 80.85% 6 Missing and 3 partials ⚠️
internal/config/config.go 96.77% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #847       +/-   ##
===========================================
+ Coverage   34.52%   65.74%   +31.21%     
===========================================
  Files         192      194        +2     
  Lines       25208    25293       +85     
===========================================
+ Hits         8704    16628     +7924     
+ Misses      16357     7837     -8520     
- Partials      147      828      +681     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ctreatma ctreatma force-pushed the shared-auth branch 3 times, most recently from f78ef0c to 2e16ea4 Compare February 4, 2025 14:58
@ctreatma ctreatma marked this pull request as ready for review February 4, 2025 15:06
@ctreatma ctreatma requested a review from a team as a code owner February 4, 2025 15:06
@ctreatma ctreatma changed the title chore: [WIP] investigating options for sharing authClient across resources & services chore: use original API request context for auth token requests Feb 4, 2025
Copy link
Contributor

@thogarty thogarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, @ctreatma !

I'm still working through the lower level details but your implementation looks solid from a decent high level understanding.

I'm curious if this removes the API Request/Response from the log output though with a different round tripper. If it does then I'll add it back in with a follow up after looking into it.

@thogarty
Copy link
Contributor

thogarty commented Feb 5, 2025

Awesome, @ctreatma !

I'm still working through the lower level details but your implementation looks solid from a decent high level understanding.

I'm curious if this removes the API Request/Response from the log output though with a different round tripper. If it does then I'll add it back in with a follow up after looking into it.

Answered my own question. It doesn't because we still wrap this in logging.NewTransport which handles that for us with an additional wrapper. Good to go 👍

@ctreatma
Copy link
Contributor Author

@displague also interested to get your take on this one.

@@ -0,0 +1,96 @@
package authtoken
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This context-aware tokensource stuff should probably be moved to the Go SDK as well so that any tooling based on the Go SDK can easily implement correct auth logic.

That could also include logic to read from and write to standard env vars and/or a standard config file location (similar to how AWS/Azure/Google Cloud tools work)

@displague displague requested a review from Copilot February 20, 2025 17:48
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • go.mod: Language not supported
Comments suppressed due to low confidence (2)

internal/authtoken/oauth2.go:90

  • The error message 'oauth2: server response missing access token' could be more descriptive. Consider including additional context about the server response.
return nil, fmt.Errorf("oauth2: server response missing access token")

internal/config/config.go:83

  • The new behavior introduced in 'newAuthClient' function is not covered by tests. Ensure that this function is properly tested.
c.authClient = c.newAuthClient()


token, err := t.Source.TokenWithContext(req.Context())
if err != nil {
fmt.Println("error: ", err)
Copy link
Preview

Copilot AI Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fmt.Println for logging errors is not ideal. Consider using a proper logging library instead.

Suggested change
fmt.Println("error: ", err)
log.Println("error: ", err)

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@@ -3,7 +3,7 @@ module github.com/equinix/terraform-provider-equinix
go 1.23

require (
github.com/equinix/equinix-sdk-go v0.48.0
github.com/equinix/equinix-sdk-go v0.48.1-0.20250130155308-f4934ab22f00
github.com/equinix/ne-go v1.19.0
github.com/equinix/oauth2-go v1.0.0
Copy link
Member

@displague displague Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can oauth2-go be removed in this PR?

@@ -0,0 +1,93 @@
// Copyright 2014 The Go Authors. All rights reserved.
Copy link
Member

@displague displague Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this sourced? (oauth2-go or upstream?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants