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 authentication support #31

Merged
merged 2 commits into from
Sep 5, 2023
Merged

Add authentication support #31

merged 2 commits into from
Sep 5, 2023

Conversation

setrofim
Copy link
Contributor

@setrofim setrofim commented Aug 29, 2023

Add support for authentication with the remote service. Basic HTTP and
OAuth2 schemes are supported (as well as "passthrough" which disables
authentication).

This is implemented by associating an IAuthorizer interface with the Client, which
sets the Authorization HTTP header in Client's requests.

(Note: while not directly depending on it, this only makes sense in the context of veraison/services#186)

The error message in this case appears to have changed not to include
the DNS resolver address, resulting in the expected error failure.

To get around that and make the test more robust, check just for the "no
such host" fragment inside the error message, rather than attempting to
match the entire message.

Signed-off-by: Sergei Trofimov <[email protected]>
auth/basic.go Outdated Show resolved Hide resolved
auth/basic.go Outdated Show resolved Hide resolved
}

// Type returns the string representing the type name (used by pflag).
func (o *Method) Type() 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 is not clear to me the purpose and scope of this method

Copy link
Contributor

Choose a reason for hiding this comment

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

it's a method required by the pflag.Value interface, which the auth.Method type implements

Copy link
Contributor

Choose a reason for hiding this comment

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

We should clarify a bit more in the comment to make it better!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment already states that it's used by pflag


o.ClientID = decoded.ClientID
o.ClientSecret = decoded.ClientSecret
o.TokenURL = decoded.TokenURL
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we not check whether the URL is a valid URL before setting it?

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe mapstructure:"token_url", valid:"url" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar with the valid tag, and by google-foo seems to be failing me. Could you provide a link to an explanation of how that works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Validation at the moment happends inside validate() which is called just below. While it may techincally be more go-ish ("gogonic"?) to not until valudation, it would make the code less neat (we'd ether have to mix validation code with rest making the method less readable, or expose the currently interannly-defied temporary struct so that we can attach the validate method to it), and I don't see a concrete benefit?

auth/oauth2.go Outdated
}

if len(decoded.Rest) > 0 {
return errors.New("unexpected fields in config")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why should we hard fail here if the Rest bytes are supplied.

We should just Warn and continue if the required essential parameters are valid and satisfy the constraints which we set.

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 is library code, not application code -- we should let the calling code handle it (it may have ideosyncratic mechansims for logging/warning).

Please note that the input here does not come directly from the end-user, it is a map generated by the calling code, so it's on that code to make sure the map is correct. It is up to the calling code to decide whether to allow additional fields in the input (and just ignore them when populating the map, or remove them if the map is generated from end-user input), or to report the error and fail. We should not be making that decision in the library code.

o.ClientSecret = decoded.ClientSecret
o.TokenURL = decoded.TokenURL
o.Username = decoded.Username
o.Password = decoded.Password
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a requirement on Password encoding, like minimum characters and specific characters?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess any such policy should be set on the service side rather than here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct -- this would be up to the particular OAuth2 service, we should not enforce anything here.

package auth

type IAuthenticator interface {
Configure(cfg map[string]interface{}) error
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, we should have a Validate Method on the IAuthenticator Interface

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To what end? Validation is performed as part of Configure() which is the only way for the user to alter the state of an IAuthenticator

Copy link
Contributor

Choose a reason for hiding this comment

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

True that this is part of Configure that you call Validator.
I am ok with this!

thomas-fossati
thomas-fossati previously approved these changes Sep 5, 2023
Copy link
Contributor

@thomas-fossati thomas-fossati left a comment

Choose a reason for hiding this comment

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

LGTM, modulo a couple of docstring adjustments that Yogesh spotted in his review

Copy link
Contributor

@yogeshbdeshpande yogeshbdeshpande left a comment

Choose a reason for hiding this comment

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

ok, left one more simple thing, in general good to go!

Add support for authentication with the remote service. Basic HTTP and
OAuth2 schemes are supported (as well as "passthrough" which disables
authentication).

This is implemented by associating an IAuthorizer with the Client, which
sets the Authrization HTTP header in Client's requests.

Signed-off-by: Sergei Trofimov <[email protected]>
@setrofim
Copy link
Contributor Author

setrofim commented Sep 5, 2023

Comments addressed.

Copy link
Contributor

@yogeshbdeshpande yogeshbdeshpande left a comment

Choose a reason for hiding this comment

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

LGTM!

@setrofim setrofim merged commit bb0fcef into main Sep 5, 2023
@setrofim setrofim deleted the auth branch September 5, 2023 13:54
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.

3 participants