Skip to content

Adds keychain availability checks for all platforms#46

Merged
hrantzsch merged 2 commits intohrantzsch:masterfrom
Seeker1437:feature/isAvailable
Mar 15, 2026
Merged

Adds keychain availability checks for all platforms#46
hrantzsch merged 2 commits intohrantzsch:masterfrom
Seeker1437:feature/isAvailable

Conversation

@Seeker1437
Copy link
Contributor

Introduces isAvailable function to verify keychain accessibility on Linux, macOS, and Windows.

On Linux, checks Secret Service status
On macOS, ensures a default keychain exists
On Windows, always returns true since CredentialManager is always present.

Enhances platform-specific error handling for better diagnostics.

@Seeker1437 Seeker1437 marked this pull request as draft June 10, 2025 18:41
@Seeker1437 Seeker1437 marked this pull request as ready for review June 10, 2025 21:52
@codecov
Copy link

codecov bot commented Jun 11, 2025

Codecov Report

❌ Patch coverage is 65.51724% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.97%. Comparing base (775906c) to head (cd3f1ae).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/keychain_mac.cpp 51.85% 9 Missing and 4 partials ⚠️
src/keychain_linux.cpp 61.11% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #46      +/-   ##
==========================================
- Coverage   81.14%   77.97%   -3.17%     
==========================================
  Files           4        4              
  Lines         228      286      +58     
  Branches       68       92      +24     
==========================================
+ Hits          185      223      +38     
- Misses         28       43      +15     
- Partials       15       20       +5     
Flag Coverage Δ
unittests 77.97% <65.51%> (-3.17%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hrantzsch
Copy link
Owner

That looks very good and useful, thanks! I'll take a closer look as soon as I can.
Btw, don't worry about codecov being sad, it's not configured well.

@Seeker1437
Copy link
Contributor Author

Thanks!

Sorry for forgetting to mark the PR as WIP initially

Copy link
Owner

@hrantzsch hrantzsch left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this, very polished and thought-out. I did request a few changes. Don't hesitate to disagree though.


bool isAvailable(Error &err) {
err = Error{};

Copy link
Owner

Choose a reason for hiding this comment

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

I wouldn't consider any runtime failure here as an indicator of the keychain not being available, and I wouldn't shadow the lower-level errors with the Unavailable error type.

Can we use createQuery to make the query as usual, fail with any errors it might produce, and then only look at whether or not SecItemCopyMatching succeeds as an indicator of isAvailable?

I think we can do without the specific test cases for KEYCHAIN_TEST_FAIL_DICT and KEYCHAIN_TEST_FAIL_STRING too.

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 agree that runtime errors should not be masked with Unavailable actually. I am not sure what I was thinking there. However, createQuery can block forever if no keychain exists at the time. This is an attempt to detect the potential of this happening beforehand.

I am okay with removing these test hooks

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 think I needed to go back and review I do feel like you're right for everything

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My only real thing is that the way I did it is more of a proactive probe... it's confirming the keychain is usable and can help indicate if ceateQuery is not safe to use (due to how it may hang).

By breaking it down to the lowest level parts and testing each step in isolation we can help improve the use experience

@Seeker1437 Seeker1437 marked this pull request as draft June 21, 2025 19:48
@hrantzsch
Copy link
Owner

Looks great now! I think tests are failing because we need #if defined(KEYCHAIN_LINUX) && defined(SIMULATE_FAILURES) rather than #ifdef ... && ....

You can add the new build flag to the two Run cmake steps in .github/workflows/ci.yml.

Although seeing the errors from running without it, do you think it makes sense to run both configurations in the CI? We could add one more target to the matrix. For example

          - {
            image: ubuntu-24.04,
            coverage: no
            simulate-failures: no
          }

(and simulate-failures: yes for all the others)

@Seeker1437
Copy link
Contributor Author

Sorry for the delay on this, will be getting this squared tomorrow!

Seeker1437 and others added 2 commits March 15, 2026 12:37
Introduces `isAvailable` function to verify keychain accessibility on Linux,
macOS, and Windows.
On Linux, checks Secret Service status; on macOS, ensures a default keychain exists; on Windows, always returns true since CredentialManager is always present. Enhances platform-specific error handling for
better diagnostics.
@hrantzsch hrantzsch force-pushed the feature/isAvailable branch from 52458db to cd3f1ae Compare March 15, 2026 12:38
@hrantzsch hrantzsch marked this pull request as ready for review March 15, 2026 12:38
@hrantzsch hrantzsch merged commit ce3572a into hrantzsch:master Mar 15, 2026
11 of 13 checks passed
@hrantzsch
Copy link
Owner

I took the liberty to finish this up. Let me know if I missed something. I any case, thank you for your contribution!

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