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

Move to explicit method call for Basic Auth credentials in push client #242

Merged
merged 1 commit into from
Jan 9, 2022

Conversation

Sinjo
Copy link
Member

@Sinjo Sinjo commented Jan 9, 2022

While URLs do support passing Basic Auth credentials using the
http://user:password@example.com/ syntax, the username and password in
that syntax have to follow the usual rules for URL encoding of
characters per RFC 3986
(https://datatracker.ietf.org/doc/html/rfc3986#section-2.1).

Rather than place the burden of correctly performing that encoding on
users of this gem, we decided to have a separate method for supplying
Basic Auth credentials, with no requirement to URL encode the characters
in them.


Since the tests around HTTP requests are extremely heavily mocked, I tested this locally with a real pushgateway configured with one user with @ in the password and another with : in it, as well as a more boring password that only used basic latin alphabet characters. All three worked.

Maybe at some point we should have some integration tests, but that feels like its own piece of work.

Fixes #170

@Sinjo Sinjo requested a review from dmagliola January 9, 2022 17:02
@Sinjo Sinjo added this to the v3.0.0 milestone Jan 9, 2022

@http = Net::HTTP.new(@uri.host, @uri.port)
@http.use_ssl = (@uri.scheme == 'https')
@http.open_timeout = kwargs[:open_timeout] if kwargs[:open_timeout]
@http.read_timeout = kwargs[:read_timeout] if kwargs[:read_timeout]
end

def basic_auth(user, password)
Copy link
Member Author

Choose a reason for hiding this comment

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

I went with the same method name as Net::HTTP as it seems like a reasonable one, though I'd also be open to something like set_basic_auth to make it clearer if you'd prefer.

def validate_no_basic_auth!(uri)
if uri.user || uri.password
raise ArgumentError, <<~EOF
Setting Basic Auth credentials in the gateway URL is not supported, please call the `basic_auth` method

Choose a reason for hiding this comment

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

Driveby 😄 maybe a quick usage example here that a developer could just copy-paste?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a really good idea. Just added it in!

Also hi Nick 👋🏻

Copy link
Collaborator

@dmagliola dmagliola left a comment

Choose a reason for hiding this comment

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

Beautiful error message

@Sinjo Sinjo force-pushed the sinjo-fix-basic-auth branch 2 times, most recently from 79d119d to 9ab21cd Compare January 9, 2022 17:40
While URLs do support passing Basic Auth credentials using the
`http://user:password@example.com/` syntax, the username and password in
that syntax have to follow the usual rules for URL encoding of
characters per RFC 3986
(https://datatracker.ietf.org/doc/html/rfc3986#section-2.1).

Rather than place the burden of correctly performing that encoding on
users of this gem, we decided to have a separate method for supplying
Basic Auth credentials, with no requirement to URL encode the characters
in them.

Signed-off-by: Chris Sinjakli <chris@sinjakli.co.uk>
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.

Pushgateway basic auth with special characters in password
3 participants