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

refactor: Improve AuthenticationStorage #1026

Merged
merged 24 commits into from
Jan 17, 2025

Conversation

pavelzw
Copy link
Contributor

@pavelzw pavelzw commented Jan 14, 2025

Description

While working on #1008 I noticed that AuthenticationStorage::default() doesn't take RATTLER_AUTH_FILE into account.
This PR fixes this and cleans up a bit.

Also, the credentials.lock wasn't cleaned up properly and since we never concurrently write to it, I removed the lock file and made the code a bit clearer/simpler.

Also, i switched the locking mechanism from creating a separate lockfile to using advisory locks on credentials.json.

@baszalmstra
Copy link
Collaborator

The cleanup looks good but what is the downside of keeping the lock file? It guards against multiple processes trying to access it. Maybe it doesnt happen often but I also dont see a downside it keeping it.

@@ -91,6 +85,12 @@ impl AuthenticationStorage {
self.backends.push(backend);
}

/// Add a new storage backend to the authentication storage at the given index
/// (backends are tried in the order they are added)
pub fn insert_backend(&mut self, index: usize, backend: Arc<dyn StorageBackend + Send + Sync>) {
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 will be needed for pixi's authentication-file-override in its global config.

My suggestion would be the following order:

  • file storage from $RATTLER_AUTH_FILE (if set)
  • file specified in authentication-file-override in pixi global config (if set) (pixi-only that's why we need to insert_backend(1, ...) in pixi)
  • file storage from the default location
  • keyring storage
  • netrc storage


/// Create a new authentication storage with just a file storage backend
pub fn from_file(path: &std::path::Path) -> Result<Self> {
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 was introduced in #645 but i don't think this is used anywhere so i don't think this brings us much benefit, especially now since RATTLER_AUTH_FILE is handled directly in AuthenticationStorage::default()

@pavelzw
Copy link
Contributor Author

pavelzw commented Jan 14, 2025

what is the downside of keeping the lock file

it currently is not working properly 😅
when i do pixi auth login, it creates the credentials.json as well as a credentials.lock but doesn't clean up after itself... at least i had quite many scenarios where there was some credentials.lock floating around in my files

@baszalmstra
Copy link
Collaborator

what is the downside of keeping the lock file

it currently is not working properly 😅 when i do pixi auth login, it creates the credentials.json as well as a credentials.lock but doesn't clean up after itself... at least i had quite many scenarios where there was some credentials.lock floating around in my files

That is expected behavior, otherwise deleting the file could create a race condition. However, instead of locking the credentials.lock file we could just lock the credentials.json itself!

@pavelzw
Copy link
Contributor Author

pavelzw commented Jan 14, 2025

However, instead of locking the credentials.lock file we could just lock the credentials.json itself

how would that work? AFAICT, lock.lock() and lock.unlock() both erase the contents of the file used as a lock 🤔

@pavelzw pavelzw requested a review from baszalmstra January 14, 2025 20:08
@pavelzw
Copy link
Contributor Author

pavelzw commented Jan 14, 2025

otherwise deleting the file could create a race condition

makes sense 🤔
although i personally found it more than once irritating that there is a random credentials.lock floating around on my system. also, i can't think of a scenario where you would want to write to a credential file multiple times at once...

IMO it being irritating weighs more than potential race conditions on pixi auth login/logout so i would be in favor of removing it but i'll let you decide :)
for now, i re-added the locking

@baszalmstra
Copy link
Collaborator

would

We can use an advisory lock on the file instead.

@baszalmstra
Copy link
Collaborator

You can use the async-fd-lock crate to achieve this locking behavior.

@pavelzw
Copy link
Contributor Author

pavelzw commented Jan 15, 2025

thanks! 6f93e41

@pavelzw
Copy link
Contributor Author

pavelzw commented Jan 15, 2025

Thanks for the pointers @baszalmstra! This is ready from my side

Copy link
Collaborator

@baszalmstra baszalmstra left a comment

Choose a reason for hiding this comment

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

Some final last minute small nitpitcks. We can merge after!

@baszalmstra baszalmstra enabled auto-merge (squash) January 17, 2025 10:42
@baszalmstra
Copy link
Collaborator

Thanks @pavelzw !

@baszalmstra baszalmstra merged commit 7591044 into conda:main Jan 17, 2025
15 checks passed
@pavelzw pavelzw deleted the authentication-storage branch January 17, 2025 10:48
@baszalmstra baszalmstra mentioned this pull request Jan 11, 2025
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