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

Add API implementation for auth service #3178

Merged
merged 15 commits into from
Apr 13, 2022
Merged

Conversation

guy-har
Copy link
Contributor

@guy-har guy-har commented Apr 7, 2022

No description provided.

@guy-har guy-har requested a review from nopcoder April 7, 2022 15:19
Copy link
Contributor

@nopcoder nopcoder left a comment

Choose a reason for hiding this comment

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

some comments I had while going over the code - will continue sunday morning

}

// getNonRequiredString returns empty string in case of nil
func getNonRequiredString(s *string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

This one can be found in swag and api packages - we can move in a different pr all the api.StringValue functions into apiutil package

Copy link
Contributor

Choose a reason for hiding this comment

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

don't think we need this one - already have couple in our code and in packages we bring.

return *s
}

func toPagination(paginator Pagination) *model.Paginator {
Copy link
Contributor

Choose a reason for hiding this comment

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

same as the above function - looks like a set of functions we require to build api client or api user

return expectedStatusCode(resp, 201)
}

var ErrUnexpectedStatusCode = errors.New("unexpected status code")
Copy link
Contributor

Choose a reason for hiding this comment

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

errors.go?

if err != nil {
return InvalidUserID, err
}
if err := expectedStatusCode(resp, 201); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

As I see it we can have one function that handles all the remote api return values (errors) and map them to the local error. Alternative will be to have a switch case per status code to handle the local error mapping.

@@ -1,15 +1,20 @@
package auth

//go:generate oapi-codegen -package auth -generate "types,client,spec" -o client.gen.go api/swagger.yml
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should in placed on the /api directory.

cache Cache
}

func (a *APIAuthService) SecretStore() crypt.SecretStore {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the only thing the service needs to expose is the shared secret. Can be handled in a different PR.

}

func (a *APIAuthService) DeleteUser(ctx context.Context, username string) error {
u, err := a.GetUser(ctx, username)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to get the user first - this is not the remote implementation's work?

Copy link
Contributor

Choose a reason for hiding this comment

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

delete user is the only one that works with ID - the service can delete user by name

if err != nil {
return nil, err
}
logging.Default().Info("initialized Auth service")
Copy link
Contributor

Choose a reason for hiding this comment

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

consider accepting logger - or initialize one here with field "service" set to remote authorization service

Comment on lines 1564 to 1567
// avoid redirect automatically
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. not sure this one is required for this client
  2. needs a way to handle timeout or at least have a default set

Comment on lines 1488 to 1489
policies, _, err := a.listUserPolicies(ctx, username, params, true)
return policies, err
Copy link
Contributor

Choose a reason for hiding this comment

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

use a different names inside the scope so the reader will not get mixed. or just return the 3 of them and ignore the middle one outside

@guy-har guy-har requested a review from nopcoder April 11, 2022 15:11
Copy link
Contributor

@nopcoder nopcoder left a comment

Choose a reason for hiding this comment

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

🥇 added some comments, the main one is error handling and the second one is the authorized which looks like a client-side implementation and not server side implemented. which make you think it should be extracted out of the service that provides the authorization information.

format: byte
required:
- username
SetupState:
Copy link
Contributor

Choose a reason for hiding this comment

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

can remove this one?

default:
$ref: "#/components/responses/ServerError"

/auth/userbyname:
Copy link
Contributor

Choose a reason for hiding this comment

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

/auth/users?name={name}

default:
$ref: "#/components/responses/ServerError"

/auth/userbyemail:
Copy link
Contributor

Choose a reason for hiding this comment

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

/auth/users?email={email}


func NewAPIAuthService(apiEndpoint, token string, secretStore crypt.SecretStore, cacheConf params.ServiceCache) (*APIAuthService, error) {
// TODO(Guys): currently token is used, maybe we should consider using APIKey
basicAuthProvider, err := securityprovider.NewSecurityProviderBearerToken(token)
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. This is a bearer token - why assign it to basic auth? name
  2. At some point we should consider reload as we do for the logging level - so it will be easy to roll and update

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Done
  2. It will be done soon in a different PR

}
httpClient := &http.Client{
// avoid redirect automatically
CheckRedirect: func(req *http.Request, via []*http.Request) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Can remove this one - prefer to support the default redirect if needed.
  2. We should enable configurable timeout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 1087 to 1088
}
func (a *APIAuthService) ListUsers(ctx context.Context, params *model.PaginationParams) ([]*model.User, *model.Paginator, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
func (a *APIAuthService) ListUsers(ctx context.Context, params *model.PaginationParams) ([]*model.User, *model.Paginator, error) {
}
func (a *APIAuthService) ListUsers(ctx context.Context, params *model.PaginationParams) ([]*model.User, *model.Paginator, error) {

Comment on lines 1150 to 1161
func paginationPrefix(prefix string) *PaginationPrefix {
p := PaginationPrefix(prefix)
return &p
}
func paginationAfter(after string) *PaginationAfter {
p := PaginationAfter(after)
return &p
}
func paginationAmount(amount int) *PaginationAmount {
p := PaginationAmount(amount)
return &p
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func paginationPrefix(prefix string) *PaginationPrefix {
p := PaginationPrefix(prefix)
return &p
}
func paginationAfter(after string) *PaginationAfter {
p := PaginationAfter(after)
return &p
}
func paginationAmount(amount int) *PaginationAmount {
p := PaginationAmount(amount)
return &p
}
func paginationPrefix(prefix string) *PaginationPrefix {
p := PaginationPrefix(prefix)
return &p
}
func paginationAfter(after string) *PaginationAfter {
p := PaginationAfter(after)
return &p
}
func paginationAmount(amount int) *PaginationAmount {
p := PaginationAmount(amount)
return &p
}

}

func (a *APIAuthService) AddCredentials(ctx context.Context, username, accessKeyID, secretAccessKey string) (*model.Credential, error) {
// TODO(Guys): decide if we want to support this - We do! for ldap, we could not support in globalIAM
Copy link
Contributor

Choose a reason for hiding this comment

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

support/unsupport should be on the server side - return an unsupported error, but implement this one.

pkg/auth/service.go Show resolved Hide resolved
pkg/auth/service.go Show resolved Hide resolved
@guy-har guy-har requested a review from nopcoder April 12, 2022 10:07
@guy-har guy-har marked this pull request as ready for review April 12, 2022 10:31
Copy link
Contributor

@nopcoder nopcoder left a comment

Choose a reason for hiding this comment

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

lgtm! optional, service can delete user by name

}

func (a *APIAuthService) DeleteUser(ctx context.Context, username string) error {
u, err := a.GetUser(ctx, username)
Copy link
Contributor

Choose a reason for hiding this comment

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

delete user is the only one that works with ID - the service can delete user by name

@guy-har guy-har requested a review from nopcoder April 12, 2022 17:22
@guy-har guy-har added the include-changelog PR description should be included in next release changelog label Apr 13, 2022
@guy-har guy-har merged commit c1c6c0e into master Apr 13, 2022
@guy-har guy-har deleted the feature/auth-service-api branch April 13, 2022 09:08
@guy-har guy-har linked an issue Apr 13, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
include-changelog PR description should be included in next release changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for API Auth service
2 participants