-
Notifications
You must be signed in to change notification settings - Fork 807
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
credentials: Refactor GetCredentials and CheckCredentials #1683
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR.
From initial look maybe I am missing something and need many more coffees but this doesn't seem to expand functionality. Could please add a proof (test) to show the expanded functionality.
You brought up the lock and you are correct that doesn't look like its needed at all and can be removed.
Also want to mention return nil instead of &account.Credentials{}
is more appropriate on error but a few instances need to change across the wrapper implementations.
exchanges/credentials.go
Outdated
if !b.API.AuthenticatedSupport && !b.API.AuthenticatedWebsocketSupport && !isContext { | ||
return fmt.Errorf("%s %w", b.Name, ErrAuthenticationSupportNotEnabled) | ||
} | ||
|
||
// Check to see if the user has enabled AuthenticatedSupport, but has | ||
// invalid API credentials set and loaded by config | ||
return b.VerifyAPICredentials(creds) | ||
if b.API.AuthenticatedSupport || b.API.AuthenticatedWebsocketSupport || isContext { | ||
return b.VerifyAPICredentials(creds) | ||
} | ||
|
||
return fmt.Errorf("%s %w", b.Name, ErrAuthenticationSupportNotEnabled) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert; as its the same thing. You also purged a line of commentary.
@@ -117,7 +117,8 @@ func (b *Base) GetCredentials(ctx context.Context) (*account.Credentials, error) | |||
if !ok { | |||
// NOTE: Return empty credentials on error to limit panic on | |||
// websocket handling. | |||
return &account.Credentials{}, errContextCredentialsFailure | |||
return nil, fmt.Errorf("context credentials store type assertion failed for %T: %w", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning nil causes panics in the CI builds, can you please rectify them. Can also change the error wrapping to fmt.Errorf("%w: %w", common.GetTypeAssertError(params...),errContextCredentialsFailure)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, crap I wasn't supposed to return nil (since the comment mentioned that)
*** I should remove tabnine from my IDE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can leave it as nil but you will need to do a bit more work. What you have done is more correct on what should happen. I will leave it up to you on what you want to do.
if SubAccountOverride, ok := ctx.Value(account.ContextSubAccountFlag).(string); ok { | ||
b.API.credMu.RLock() | ||
creds.SubAccount = SubAccountOverride | ||
b.API.credMu.RUnlock() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if SubAccountOverride, ok := ctx.Value(account.ContextSubAccountFlag).(string); ok { | |
b.API.credMu.RLock() | |
creds.SubAccount = SubAccountOverride | |
b.API.credMu.RUnlock() | |
} | |
if subAccountOverride, ok := ctx.Value(account.ContextSubAccountFlag).(string); ok { | |
b.API.credMu.RLock() | |
creds.SubAccount = subAccountOverride | |
b.API.credMu.RUnlock() | |
} |
Reorganize conditions for the authentication support validation process. Improve error handling and context credential store type assertion for better clarity and maintainability. Ensure sub-account override logic is consistently applied with mutex.
This pull request introduces several important changes to the
exchanges/credentials.go
file, focusing on improving error handling and credential management. The most significant changes include modifying theCheckCredentials
function to handle different authentication support scenarios, enhancing error messages inGetCredentials
, and adding a fallback mechanism for default credentials.Error Handling Improvements:
func (b *Base) GetCredentials(ctx context.Context) (*account.Credentials, error)
: Enhanced the error message for context credentials store type assertion failures to provide more detailed information.Credential Management Enhancements:
func (b *Base) CheckCredentials(creds *account.Credentials, isContext bool) error
: Reorganized the logicfunc (b *Base) GetCredentials(ctx context.Context) (*account.Credentials, error)
: small readability change^^^ I mean, if copilot says so
Test do pass
also
Is that lock required?
EDIT: I was experimenting around to understand the code base.
Will have to edit the commit, it wasn't supposed to return nil. Not sure, maybe IDE did it automatically since I did saw the comment
My main question was also about the lock being required. (Don't think it will lead to a noticeable performance gain when removed but can)
Type of change
Please delete options that are not relevant and add an
x
in[]
as item is complete.How has this been tested
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and
also consider improving test coverage whilst working on a certain feature or package.
Checklist