Skip to content

Conversation

@Sinjo
Copy link
Member

@Sinjo Sinjo commented Jan 9, 2022

While URLs do support passing Basic Auth credentials using the
http://user:[email protected]/ 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
@Sinjo Sinjo force-pushed the sinjo-fix-basic-auth branch from 2ae084f to e7111cd Compare January 9, 2022 17:03
@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:[email protected]/` 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 <[email protected]>
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

4 participants