-
Notifications
You must be signed in to change notification settings - Fork 6
upki: add support for system-wide configuration/caching #106
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,10 +27,10 @@ impl Config { | |
| /// Load the configuration data from a file at `path`. | ||
| /// | ||
| /// If no file exists at `path`, a default configuration is returned. | ||
| pub fn from_file_or_user_default(path: &ConfigPath) -> Result<Self, Error> { | ||
| pub fn from_file_or_default(path: &ConfigPath) -> Result<Self, Error> { | ||
| match path { | ||
| ConfigPath::Default(path) if path.exists() => Self::from_file(path), | ||
| ConfigPath::Default(_) => Self::try_user_default(), | ||
| ConfigPath::Default(path, _) if path.exists() => Self::from_file(path), | ||
| ConfigPath::Default(_, kind) => Self::try_default(*kind), | ||
| ConfigPath::Specified(path) => Self::from_file(path), | ||
| } | ||
| } | ||
|
|
@@ -49,9 +49,9 @@ impl Config { | |
| } | ||
|
|
||
| /// Return a sensible default configuration. | ||
| pub fn try_user_default() -> Result<Self, Error> { | ||
| pub fn try_default(kind: PathKind) -> Result<Self, Error> { | ||
| Ok(Self { | ||
| cache_dir: user_cache_dir()?, | ||
| cache_dir: kind.cache_dir()?, | ||
| revocation: RevocationConfig::default(), | ||
| }) | ||
| } | ||
|
|
@@ -66,7 +66,7 @@ pub enum ConfigPath { | |
| /// The path was directly specified by a user. | ||
| Specified(PathBuf), | ||
| /// The path was determined automatically. | ||
| Default(PathBuf), | ||
| Default(PathBuf, PathKind), | ||
| } | ||
|
|
||
| impl ConfigPath { | ||
|
|
@@ -81,10 +81,10 @@ impl ConfigPath { | |
| /// | ||
| /// This function fails for platform-specific reasons, typically if `$HOME` is not | ||
| /// set, or another XDG environment variable is malformed. | ||
| pub fn new(specified: Option<PathBuf>) -> Result<Self, Error> { | ||
| pub fn new(specified: Option<PathBuf>, kind: PathKind) -> Result<Self, Error> { | ||
| match specified { | ||
| Some(f) => Ok(Self::Specified(f)), | ||
| None => user_config_file().map(ConfigPath::Default), | ||
| None => Ok(Self::Default(kind.config_dir()?.join(CONFIG_FILE), kind)), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -93,11 +93,32 @@ impl AsRef<Path> for ConfigPath { | |
| fn as_ref(&self) -> &Path { | ||
| match self { | ||
| Self::Specified(path) => path.as_ref(), | ||
| Self::Default(path) => path.as_ref(), | ||
| Self::Default(path, _) => path.as_ref(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// What kind of path is being determined. | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub enum PathKind { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about the name... |
||
| /// User-relative configuration and data. | ||
| User, | ||
| } | ||
|
|
||
| impl PathKind { | ||
| fn config_dir(self) -> Result<PathBuf, Error> { | ||
| Ok(match self { | ||
| Self::User => project_dirs()?.config_dir().to_owned(), | ||
| }) | ||
| } | ||
|
|
||
| fn cache_dir(self) -> Result<PathBuf, Error> { | ||
| Ok(match self { | ||
| Self::User => project_dirs()?.cache_dir().to_owned(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Errors for the upki library API. | ||
| #[non_exhaustive] | ||
| #[derive(Debug)] | ||
|
|
@@ -156,16 +177,6 @@ impl fmt::Display for Error { | |
| } | ||
| } | ||
|
|
||
| fn user_config_file() -> Result<PathBuf, Error> { | ||
| Ok(project_dirs()? | ||
| .config_dir() | ||
| .join(CONFIG_FILE)) | ||
| } | ||
|
|
||
| fn user_cache_dir() -> Result<PathBuf, Error> { | ||
| Ok(project_dirs()?.cache_dir().to_owned()) | ||
| } | ||
|
|
||
| fn project_dirs() -> Result<ProjectDirs, Error> { | ||
| ProjectDirs::from("dev", "rustls", PREFIX).ok_or(Error::NoValidHomeDirectory) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is the right level for this declaration? Because if
args.config_fileis none, thenPathKind::Userimplies the default should ignore the system-level one?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the current behavior, too, right? This PR intentionally punts on heuristics about when we should try what paths in what order (because it is a little bit tricky), in favor of at least encoding in the library the ability to have system-level config and cache.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, yes, but the desired and previous behaviour was to find a user-controlled config file (if one exists at a well-known location) before falling back to a system-wide config file. And that should happen without each call site having to do that work individually, else we end with some upki functions ignoring one location or the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. But then I think that means we have to reintroduce the
for_readvsfor_writedistinction, right? I think it's clear thatcheck()should first try the user config file and then the system-wide config file, but it's not obvious to me what the least surprising behavior forfetch()is. Maybe it would be better forfetch()config to require a specific path or level?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if someone doing
fetch()as a normal user, using a system-level config, should pretty quickly fail with a clear permission error. I think that's fine. eg, this is what apt-get update does in that situation: