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

credentials: Refactor GetCredentials and CheckCredentials #1683

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

romanornr
Copy link
Contributor

@romanornr romanornr commented Oct 17, 2024

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 the CheckCredentials function to handle different authentication support scenarios, enhancing error messages in GetCredentials, 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 logic

  • func (b *Base) GetCredentials(ctx context.Context) (*account.Credentials, error): small readability change

^^^ I mean, if copilot says so
Test do pass

also

if SubAccountOverride, ok := ctx.Value(account.ContextSubAccountFlag).(string); ok {
		b.API.credMu.RLock()
		creds.SubAccount = SubAccountOverride
		b.API.credMu.RUnlock()
	}

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.

  • Unnecessary improvement attempt

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.

  • go test ./... -race
  • golangci-lint run
  • Test X

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • [x ] I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • [ x] My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Github Actions with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link
Collaborator

@shazbert shazbert left a 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.

Comment on lines 84 to 90
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)
Copy link
Collaborator

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",
Copy link
Collaborator

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)

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Comment on lines +138 to 142
if SubAccountOverride, ok := ctx.Value(account.ContextSubAccountFlag).(string); ok {
b.API.credMu.RLock()
creds.SubAccount = SubAccountOverride
b.API.credMu.RUnlock()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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()
}

@shazbert shazbert added the review me This pull request is ready for review label Oct 17, 2024
@thrasher- thrasher- changed the title Refactor credential support checks in exchanges module credentials: Refactor GetCredentials and CheckCredentials Oct 17, 2024
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.
@thrasher- thrasher- added low priority This enhancement or update will be implemented at a later date. pending response awaiting a response from the author builds require fixing and removed review me This pull request is ready for review labels Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds require fixing low priority This enhancement or update will be implemented at a later date. pending response awaiting a response from the author
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

3 participants