Skip to content

Distinguish between authentication failures due to missing vs invalid credentials#12667

Closed
jtfmumm wants to merge 2 commits intomainfrom
jtfm/auth-error-messages
Closed

Distinguish between authentication failures due to missing vs invalid credentials#12667
jtfmumm wants to merge 2 commits intomainfrom
jtfm/auth-error-messages

Conversation

@jtfmumm
Copy link
Contributor

@jtfmumm jtfmumm commented Apr 4, 2025

Prior to this change, unless an index was configured as authenticate = "always", authentication failures would output the same error message whether the cause was missing credentials or incorrect credentials. This PR ensures that missing credentials lead to a distinct error message.

Once #12651 is merged,
Closes #12280

@jtfmumm jtfmumm added the error messages Messaging when something goes wrong label Apr 4, 2025
@jtfmumm
Copy link
Contributor Author

jtfmumm commented Apr 4, 2025

These snapshot tests will need to be updated to reflect #12651 once it is merged. So this PR should wait on #12651.

@jtfmumm jtfmumm force-pushed the jtfm/auth-error-messages branch from c2836c8 to 4368739 Compare April 4, 2025 10:43
Comment on lines -578 to 592

assert_eq!(
assert!(
client
.get(format!("{}/foo", server.uri()))
.send()
.await?
.status(),
401
.await
.is_err(),
"Requests should require credentials"
);
Copy link
Member

Choose a reason for hiding this comment

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

Why did this one change? There's no policy set 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.

Maybe there's a better way to do it, but the existing approach had been to throw an error for the missing credentials case. There's no policy set here, but the reason for the 401 is because of missing credentials.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we can throw an error there though, doesn't downstream logic rely on a successful request? (i.e., in #12667 (comment))

Perhaps I misunderstood the intent of this PR?

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 only tests that broke were cases where we'd want the message to indicate that missing credentials was the cause of failing to find the package. But if we are depending on getting the 401 out of this response downstream, then this approach won't work. In that case, I can try using reqwest extensions to add more context about the 401.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I understand the goal now, you want to make the case where no credentials were present distinct from the case where the credentials are wrong. Sorry it took me a while to get there. I think the snapshot changes should be like..

hint: An index URL (https://pypi-proxy.fly.dev/basic-auth/simple) could not be queried due to a lack of valid authentication credentials (401 Unauthorized).

to

hint: An index URL (https://pypi-proxy.fly.dev/basic-auth/simple) could not be queried due to missing credentials (401 Unauthorized)

and

hint: An index URL (https://pypi-proxy.fly.dev/basic-auth/simple) could not be queried due to invalid credentials (401 Unauthorized)

I think changing that error path entirely feels too risky and is hopefully unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

(fwiw, I think this would have been clearer if the title was something like "Distinguish between authentication failures due to missing vs invalid credentials")

hint: An index URL (https://pypi-proxy.fly.dev/basic-auth/simple) could not be queried due to a lack of valid authentication credentials (401 Unauthorized).
"###);
error: Failed to fetch: `https://pypi-proxy.fly.dev/basic-auth/simple/iniconfig/`
Caused by: Missing credentials for https://pypi-proxy.fly.dev/basic-auth/simple/iniconfig/
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a regression to me?

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 a case where the fetch failed because there were no credentials, which is the reason the package was not found (since we were never authenticated, we were not able to search for it). So this case is intentional. Does it still seem like a regression?

Copy link
Member

Choose a reason for hiding this comment

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

I think so? Doesn't this fail eagerly now? What if there was a second index with the package available?

Copy link
Member

Choose a reason for hiding this comment

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

(It should be easy to add a test for that, we should probably have coverage regardless)

Copy link
Contributor Author

@jtfmumm jtfmumm Apr 4, 2025

Choose a reason for hiding this comment

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

I'll add that test.

Comment on lines -7520 to +7528
uv_snapshot!(context.filters(), context.lock(), @r###"
uv_snapshot!(context.filters(), context.lock(), @r"
success: false
exit_code: 1
exit_code: 2
----- stdout -----

----- stderr -----
× No solution found when resolving dependencies:
╰─▶ Because iniconfig was not found in the package registry and child depends on iniconfig>=2, we can conclude that child's requirements are unsatisfiable.
And because your workspace requires child, we can conclude that your workspace's requirements are unsatisfiable.
"###);
error: Failed to fetch: `https://pypi-proxy.fly.dev/basic-auth/simple/iniconfig/`
Caused by: Missing credentials for https://pypi-proxy.fly.dev/basic-auth/simple/iniconfig/
");
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good question. I'll look into it

@jtfmumm jtfmumm changed the title Improve authentication failure error messages Distinguish between authentication failures due to missing vs invalid credentials Apr 5, 2025
@jtfmumm
Copy link
Contributor Author

jtfmumm commented Apr 25, 2025

Closing because we are going to use a different approach.

@jtfmumm jtfmumm closed this Apr 25, 2025
@jtfmumm jtfmumm deleted the jtfm/auth-error-messages branch June 1, 2025 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

error messages Messaging when something goes wrong

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide feedback and improve doc on keyring index authentication

2 participants