Implement sq config inspect paths.

- Fixes #275.
This commit is contained in:
Justus Winter 2024-12-03 17:36:56 +01:00
parent 3bde91aeb4
commit ac0044a677
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
4 changed files with 124 additions and 4 deletions

View File

@ -5,6 +5,7 @@ use clap::{
Subcommand,
};
pub mod paths;
pub mod policy;
#[derive(Debug, Parser)]
@ -30,5 +31,6 @@ pub struct Command {
#[derive(Debug, Subcommand)]
#[non_exhaustive]
pub enum Subcommands {
Paths(paths::Command),
Policy(policy::Command),
}

View File

@ -0,0 +1,33 @@
//! Command-line parser for `sq config inspect paths`.
use clap::Args;
use crate::cli::examples::*;
#[derive(Debug, Args)]
#[clap(
name = "paths",
about = "Inspect relevant paths",
long_about = "\
Inspect relevant paths
Prints paths that are used by sq, such as the location of the home \
directory, the configuration file, the certificate store, the key \
store, etc. \
",
after_help = EXAMPLES,
)]
pub struct Command {
}
const EXAMPLES: Actions = Actions {
actions: &[
Action::example()
.comment("Inspect relevant paths.")
.command(&[
"sq", "config", "inspect", "paths",
])
.build(),
],
};
test_examples!(sq_config_inspect_paths, EXAMPLES);

View File

@ -1,5 +1,7 @@
//! Dispatches and implements `sq config inspect`.
use std::path::{Path, PathBuf};
use anyhow::Result;
use sequoia_policy_config::{
@ -10,16 +12,88 @@ use sequoia_policy_config::{
use crate::{
Sq,
cli::config::inspect,
config::ConfigFile,
};
pub fn dispatch(sq: Sq, cmd: inspect::Command)
-> Result<()>
{
match cmd.subcommand {
inspect::Subcommands::Paths(c) => paths(sq, c),
inspect::Subcommands::Policy(c) => policy(sq, c),
}
}
/// Implements `sq config inspect paths`.
fn paths(sq: Sq, _: inspect::paths::Command) -> Result<()> {
// Whether we have emitted anything.
let mut dirty = false;
// Formats a path.
let mut p = |path: &Path, name: &str, description: &str| -> Result<()> {
if dirty {
wprintln!();
}
dirty = true;
wprintln!(initial_indent = " - ", "{}", name);
wprintln!(initial_indent = " - ", "{}", path.display());
if ! path.exists() {
wprintln!(initial_indent = " - ", "does not exist");
}
wprintln!(initial_indent = " - ", "{}", description);
Ok(())
};
if let Some(home) = &sq.home {
p(home.location(), "home directory",
"This holds the configuration file, and, unless overridden, \
the certificate store and key store.",
)?;
p(&ConfigFile::file_name(&home), "config file",
"sq's configuration file.",
)?;
}
p(&PathBuf::from(ConfigFile::global_crypto_policy_file()),
"global cryptographic policy",
"This is the global cryptographic policy file. If it exists, it \
is read before reading in the policy in sq's configuration file, \
which will refine the global one.",
)?;
if let Some(policy_path) = sq.config.policy_path() {
p(policy_path,
"referenced cryptographic policy",
"This is the cryptographic policy file referenced in sq's \
configuration file. It is read after the global policy, \
and before the policy embedded in sq's configuration file, \
which will refine the global and referenced one.",
)?;
}
if let Some(cert_store) = sq.cert_store_base() {
p(&cert_store, "certificate store",
"This holds all the certificates, indices for faster lookup, \
and some additional certificates like the trust root.",
)?;
}
if let Ok(Some(key_store)) = sq.key_store_path() {
p(&key_store, "key store",
"This holds all the keys, either directly for those in the \
`softkeys` backend, or indirectly, using some configuration \
and metadata.",
)?;
}
Ok(())
}
/// Implements `sq config inspect policy`.
fn policy(sq: Sq, _: inspect::policy::Command) -> Result<()> {
let p = ConfiguredStandardPolicy::from_policy(sq.policy.clone());

View File

@ -168,6 +168,12 @@ impl Config {
&self.sign_signer_self
}
/// Returns the path to the referenced cryptographic policy, if
/// any.
pub fn policy_path(&self) -> Option<&Path> {
self.policy_path.as_deref()
}
/// Returns the cryptographic policy.
///
/// We read in the default policy configuration, the configuration
@ -346,10 +352,7 @@ impl ConfigFile {
.map(|c| c.lib().display().to_string())
.unwrap_or_else(|_| "<unknown>".into())
}),
&format!("{:?}",
std::env::var(ConfiguredStandardPolicy::ENV_VAR)
.unwrap_or_else(
|_| ConfiguredStandardPolicy::CONFIG_FILE.into())),
&format!("{:?}", Self::global_crypto_policy_file()),
&default_policy_inline.to_string(),
]))
}
@ -519,6 +522,14 @@ impl ConfigFile {
})
}
/// Returns the path to the global cryptographic policy
/// configuration file.
pub fn global_crypto_policy_file() -> String {
std::env::var(ConfiguredStandardPolicy::ENV_VAR)
.unwrap_or_else(
|_| ConfiguredStandardPolicy::CONFIG_FILE.into())
}
/// Returns the document tree.
pub fn as_item(&self) -> &Item {
self.doc.as_item()