Skip to content

fix: Detect invalid API keys more carefully#792

Merged
atheriel merged 1 commit into
mainfrom
aj-more-careful-api-key-validation
Jun 25, 2026
Merged

fix: Detect invalid API keys more carefully#792
atheriel merged 1 commit into
mainfrom
aj-more-careful-api-key-validation

Conversation

@atheriel

Copy link
Copy Markdown
Contributor

Intent

When credentials come from the command line (or environment variables) rather than the store, we attempt to validate them by hitting the /v1/user endpoint, and error out if this fails.

It turns out that this doesn't work for machine credentials, which aren't associated with a user.

Thankfully the difference between "invalid API key" and "not a user API key" can be detected based on the error code from the response, so this commit draws this exact distinction.

Type of Change

  • Bug Fix
  • New Feature
  • Breaking Change

Automated Tests

Unit tests are included.

Directions for Reviewers

Checklist

  • I have updated CHANGELOG.md to cover notable changes.
  • I have updated all related GitHub issues to reflect their current state.
  • I have run the rsconnect-python-tests-at-night workflow in Connect against this feature branch.

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-06-25 17:59 UTC

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
7266 5951 82% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
rsconnect/api.py 80% 🟢
TOTAL 80% 🟢

updated for commit: bff5fd5 by action🐍

@atheriel atheriel force-pushed the aj-more-careful-api-key-validation branch from a13d522 to a5c8bac Compare June 23, 2026 21:15

@nealrichardson nealrichardson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I confirmed that this works, I just have some design questions. I'm ok to merge this and make a followup to refactor if there's no other simplification we can make here.

Comment thread rsconnect/api.py
Comment thread rsconnect/api.py
json_data = result.json_data if isinstance(result.json_data, dict) else {}
code = json_data.get("code")
# A service principal or machine identity authenticates successfully but has no
# associated user, so the v1/user endpoint rejects it with a 403 and error code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it necessary that we request the v1/user endpoint to validate the API key, or the OIDC token? I noticed that nothing else uses the .me() endpoint other than to check that an API key is valid.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also found this confusing. I believe the original point of it was to surface more user-friendly error messages, but I agree that I would personally prefer we just fail later on when attempting an actual operation. That felt like a more invasive behaviour change to make, though.

When credentials come from the command line (or environment variables)
rather than the store, we attempt to validate them by hitting the
`/v1/user` endpoint, and error out if this fails.

It turns out that this doesn't work for machine credentials, which
aren't associated with a user.

Thankfully the difference between "invalid API key" and "not a user API
key" can be detected based on the error code from the response, so this
commit draws this exact distinction.

Unit tests are included.

Signed-off-by: Aaron Jacobs <aaron.jacobs@posit.co>
@atheriel atheriel force-pushed the aj-more-careful-api-key-validation branch from a5c8bac to bff5fd5 Compare June 25, 2026 17:31
@atheriel atheriel merged commit 7f2933d into main Jun 25, 2026
24 checks passed
@atheriel atheriel deleted the aj-more-careful-api-key-validation branch June 25, 2026 17:59
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.

2 participants