Skip to content
This repository was archived by the owner on Mar 12, 2026. It is now read-only.

Commit 3f56f28

Browse files
authored
fix: save cli configuration if login/logout have been called (#4)
1 parent 2e6a6ff commit 3f56f28

13 files changed

Lines changed: 290 additions & 295 deletions

File tree

Cargo.lock

Lines changed: 123 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,18 @@ incremental = false
3838
opt-level = 0
3939

4040
[workspace.dependencies]
41-
actix-rt = "2.10"
42-
actix-server = { version = "2.5", default-features = false }
43-
actix-web = { version = "4.9.0", default-features = false }
44-
base64 = "0.21"
45-
chrono = "0.4"
4641
clap = { version = "4.5", default-features = false }
47-
cloudproof = "3.0"
4842
cosmian_kms_cli = { git = "https://www.github.com/Cosmian/kms", branch = "develop" }
4943
cosmian_findex_cli = { git = "https://www.github.com/Cosmian/findex-server", branch = "develop" }
5044
cosmian_config_utils = { git = "https://www.github.com/Cosmian/http_client_server", branch = "develop" }
5145
cosmian_http_client = { git = "https://www.github.com/Cosmian/http_client_server", branch = "develop" }
5246
cosmian_logger = { git = "https://www.github.com/Cosmian/http_client_server", branch = "develop" }
53-
der = { version = "0.7", default-features = false }
54-
env_logger = "0.11"
5547
hex = { version = "0.4", default-features = false }
56-
leb128 = "0.2.5"
57-
native-tls = "0.2"
58-
num_cpus = "1.13"
59-
num-bigint-dig = { version = "0.8", default-features = false }
6048
openssl = { version = "0.10", default-features = false }
61-
pem = "3.0"
62-
pyo3 = { version = "0.20", default-features = false }
63-
reqwest = { version = "0.11", default-features = false }
64-
rustls = "0.21"
6549
serde = "1.0"
66-
serde_json = "1.0"
67-
sha3 = { version = "0.10", default-features = false }
68-
strum = { version = "0.25", default-features = false }
6950
thiserror = "1.0"
70-
time = "0.3"
71-
tokio = { version = "1.39", default-features = false }
72-
tracing-subscriber = { version = "0.3", default-features = false }
51+
tokio = { version = "1.42", default-features = false }
7352
tracing = "0.1"
7453
url = "2.5"
75-
uuid = "1.10"
76-
x509-cert = { version = "0.2.5", default-features = false }
54+
uuid = "1.11"
7755
x509-parser = "0.16"
78-
zeroize = { version = "1.8", default-features = false }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ docker run -p 9998:9998 --rm --name kms ghcr.io/cosmian/kms
2525
```
2626

2727
> [!NOTE]:
28-
> The KMS server can also be installed using these [instructions](https://docs.cosmian./key_management_system/installation).
28+
> The KMS server can also be installed using these [instructions](https://docs.cosmian.com/key_management_system/single_server_mode/#quick-start).
2929
3030
In the same manner, you can run a Findex server (using Docker image) on `http://localhost:6668`:
3131

crate/cli/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cosmian_kms_cli = { workspace = true }
3535
cosmian_findex_cli = { workspace = true }
3636
cosmian_config_utils = { workspace = true }
3737
cosmian_logger = { workspace = true }
38-
csv = "1.3.0"
38+
csv = "1.3.1"
3939
hex = { workspace = true }
4040
thiserror = { workspace = true }
4141
serde = { workspace = true }
@@ -45,14 +45,14 @@ url = { workspace = true }
4545
uuid = { workspace = true }
4646

4747
[dev-dependencies]
48-
actix-rt = { workspace = true }
49-
actix-server = { workspace = true }
48+
actix-rt = "2.10"
49+
actix-server = { version = "2.5", default-features = false }
5050
assert_cmd = "2.0"
5151
const-oid = { version = "0.9", features = ["db"] }
5252
openssl = { workspace = true }
5353
predicates = "3.1"
54-
regex = { version = "1.10", default-features = false }
55-
tempfile = "3.11"
54+
regex = { version = "1.11", default-features = false }
55+
tempfile = "3.14"
5656
x509-parser = { workspace = true, features = ["verify"] }
5757

5858
# ------------------------------------------------------------------------------

crate/cli/src/actions/delete_datasets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct DeleteDatasetAction {
2121

2222
impl DeleteDatasetAction {
2323
#[allow(clippy::future_not_send, clippy::print_stdout)]
24-
pub(crate) async fn run(&self, findex_rest_client: FindexRestClient) -> CosmianResult<()> {
24+
pub(crate) async fn run(&self, findex_rest_client: &FindexRestClient) -> CosmianResult<()> {
2525
let uuids = Uuids::from(self.uuid.clone());
2626
findex_rest_client
2727
.delete_entries(&self.index_id, &uuids)

crate/cli/src/actions/encrypt_and_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl EncryptAndIndexAction {
269269
#[allow(clippy::future_not_send, clippy::print_stdout)]
270270
pub async fn run(
271271
&self,
272-
findex_rest_client: FindexRestClient,
272+
findex_rest_client: &FindexRestClient,
273273
kms_rest_client: &KmsClient,
274274
) -> CosmianResult<()> {
275275
let nonce = self

crate/cli/src/actions/findex.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ pub enum FindexActions {
1919

2020
impl FindexActions {
2121
/// Combine Findex with KMS encryption
22+
///
2223
/// # Errors
2324
/// Returns an error if the action fails
2425
#[allow(clippy::future_not_send)]
2526
pub async fn run(
2627
&self,
27-
findex_rest_client: FindexRestClient,
28+
findex_rest_client: &mut FindexRestClient,
2829
kms_rest_client: &KmsClient,
2930
) -> CosmianResult<()> {
3031
match self {

crate/cli/src/actions/search_and_decrypt.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,22 @@ impl SearchAndDecryptAction {
7171
#[allow(clippy::future_not_send, clippy::print_stdout)]
7272
pub(crate) async fn run(
7373
&self,
74-
findex_rest_client: FindexRestClient,
74+
findex_rest_client: &FindexRestClient,
7575
kms_rest_client: &KmsClient,
7676
) -> CosmianResult<()> {
77-
let results =
78-
instantiate_findex(findex_rest_client.clone(), &self.findex_parameters.index_id)
79-
.await?
80-
.search(
81-
&self.findex_parameters.user_key()?,
82-
&self.findex_parameters.label(),
83-
self.keyword
84-
.clone()
85-
.into_iter()
86-
.map(|word| Keyword::from(word.as_bytes()))
87-
.collect::<Keywords>(),
88-
&|_| async move { Ok(false) },
89-
)
90-
.await?;
77+
let results = instantiate_findex(findex_rest_client, &self.findex_parameters.index_id)
78+
.await?
79+
.search(
80+
&self.findex_parameters.user_key()?,
81+
&self.findex_parameters.label(),
82+
self.keyword
83+
.clone()
84+
.into_iter()
85+
.map(|word| Keyword::from(word.as_bytes()))
86+
.collect::<Keywords>(),
87+
&|_| async move { Ok(false) },
88+
)
89+
.await?;
9190
trace!("Index search results: {results}");
9291

9392
let mut uuids = Vec::new();

crate/cli/src/commands.rs

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::PathBuf;
22

33
use clap::{CommandFactory, Parser, Subcommand};
44
use cosmian_config_utils::ConfigUtils;
5-
use cosmian_findex_cli::reexports::cosmian_findex_client::FindexRestClient;
5+
use cosmian_findex_cli::{CoreFindexActions, reexports::cosmian_findex_client::FindexRestClient};
66
use cosmian_kms_cli::{KmsActions, reexport::cosmian_kms_client::KmsClient};
77
use cosmian_logger::log_init;
88
use tracing::{info, trace};
@@ -19,10 +19,10 @@ use crate::{
1919
pub struct Cli {
2020
/// Configuration file location
2121
///
22-
/// This is an alternative to the env variable `KMS_CLI_CONF`.
23-
/// Takes precedence over `KMS_CLI_CONF` env variable.
24-
#[arg(short, long)]
25-
conf: Option<PathBuf>,
22+
/// This is an alternative to the env variable `COSMIAN_CLI_CONF_PATH`.
23+
/// Takes precedence over `COSMIAN_CLI_CONF_PATH` env variable.
24+
#[arg(short, env = "COSMIAN_CLI_CONF_PATH", long)]
25+
conf_path: Option<PathBuf>,
2626

2727
#[command(subcommand)]
2828
pub command: CliCommands,
@@ -36,12 +36,12 @@ pub struct Cli {
3636
/// `accept_invalid_certs` is useful if the CLI needs to connect to an HTTPS KMS server
3737
/// running an invalid or insecure SSL certificate
3838
#[arg(long)]
39-
pub kms_accept_invalid_certs: Option<bool>,
39+
pub kms_accept_invalid_certs: bool,
4040

4141
/// Output the KMS JSON KMIP request and response.
4242
/// This is useful to understand JSON POST requests and responses
4343
/// required to programmatically call the KMS on the `/kmip/2_1` endpoint
44-
#[arg(long, default_value = "false")]
44+
#[arg(long)]
4545
pub kms_print_json: bool,
4646

4747
/// The URL of the Findex server
@@ -53,7 +53,7 @@ pub struct Cli {
5353
/// `accept_invalid_certs` is useful if the CLI needs to connect to an HTTPS KMS server
5454
/// running an invalid or insecure SSL certificate
5555
#[arg(long)]
56-
pub findex_accept_invalid_certs: Option<bool>,
56+
pub findex_accept_invalid_certs: bool,
5757
}
5858

5959
#[derive(Subcommand)]
@@ -84,65 +84,74 @@ pub enum CliCommands {
8484
/// - The command-line arguments cannot be parsed.
8585
/// - The configuration file cannot be located or loaded.
8686
/// - Any of the subcommands fail during their execution.
87-
#[allow(clippy::future_not_send)]
87+
#[allow(clippy::future_not_send, clippy::cognitive_complexity)]
8888
pub async fn cosmian_main() -> CosmianResult<()> {
8989
log_init(None);
9090
info!("Starting Cosmian CLI");
9191
let cli = Cli::parse();
9292

93-
let conf_path = ClientConf::location(cli.conf)?;
94-
let mut conf = ClientConf::from_toml(&conf_path)?;
93+
let conf_path = ClientConf::location(cli.conf_path)?;
94+
let mut config = ClientConf::from_toml(&conf_path)?;
9595

96-
// Override the configuration with the CLI arguments
97-
let mut has_been_overridden = false;
96+
// Handle KMS configuration
9897
if let Some(url) = cli.kms_url.clone() {
99-
conf.kms_config.http_config.server_url = url;
100-
has_been_overridden = true;
98+
config.kms_config.http_config.server_url = url;
10199
}
102-
if let Some(accept_invalid_certs) = cli.kms_accept_invalid_certs {
103-
conf.kms_config.http_config.accept_invalid_certs = accept_invalid_certs;
104-
has_been_overridden = true;
100+
if cli.kms_accept_invalid_certs {
101+
config.kms_config.http_config.accept_invalid_certs = true;
105102
}
106-
if let Some(url) = cli.findex_url.clone() {
107-
if let Some(findex_conf) = conf.findex_config.as_mut() {
108-
findex_conf.http_config.server_url = url;
109-
has_been_overridden = true;
103+
config.kms_config.print_json = Some(cli.kms_print_json);
104+
105+
// Handle Findex server configuration
106+
if let Some(findex_config) = config.findex_config.as_mut() {
107+
if let Some(url) = cli.findex_url.clone() {
108+
findex_config.http_config.server_url = url;
110109
}
111-
}
112-
if let Some(accept_invalid_certs) = cli.findex_accept_invalid_certs {
113-
if let Some(findex_conf) = conf.findex_config.as_mut() {
114-
findex_conf.http_config.accept_invalid_certs = accept_invalid_certs;
115-
has_been_overridden = true;
110+
if cli.findex_accept_invalid_certs {
111+
findex_config.http_config.accept_invalid_certs = true;
116112
}
117113
}
118-
conf.kms_config.print_json = Some(cli.kms_print_json);
119-
if has_been_overridden {
120-
conf.to_toml(&conf_path)?;
121-
}
122114

123-
trace!("Configuration: {conf:?}");
115+
trace!("Configuration: {config:?}");
124116

125-
// Instantiate the KMS and Findex clients
126-
let kms_rest_client = KmsClient::new(conf.kms_config)?;
117+
// Instantiate the KMS client
118+
let mut kms_rest_client = KmsClient::new(config.kms_config.clone())?;
127119

128-
match cli.command {
120+
match &cli.command {
129121
CliCommands::Markdown(action) => {
130122
let command = <Cli as CommandFactory>::command();
131123
action.process(&command)?;
132124
return Ok(())
133125
}
134126
CliCommands::Kms(kms_actions) => {
135-
kms_actions.process(&kms_rest_client).await?;
127+
kms_actions.process(&mut kms_rest_client).await?;
128+
config.kms_config = kms_rest_client.config.clone();
136129
}
137130
CliCommands::FindexServer(findex_actions) => {
138-
let findex_config = conf.findex_config.ok_or_else(|| {
131+
let findex_config = config.findex_config.as_ref().ok_or_else(|| {
139132
cli_error!("Findex server configuration is missing in the configuration file")
140133
})?;
141-
let findex_rest_client = FindexRestClient::new(findex_config)?;
134+
let mut findex_rest_client = FindexRestClient::new(findex_config.clone())?;
142135
findex_actions
143-
.run(findex_rest_client, &kms_rest_client)
136+
.run(&mut findex_rest_client, &kms_rest_client)
144137
.await?;
138+
config.findex_config = Some(findex_rest_client.config.clone());
139+
}
140+
}
141+
142+
// Save the configuration
143+
match cli.command {
144+
CliCommands::Kms(KmsActions::Login(_) | KmsActions::Logout(_)) => {
145+
config.to_toml(&conf_path)?;
146+
info!("Saving configuration to: {conf_path:?}");
147+
}
148+
CliCommands::FindexServer(FindexActions::Findex(
149+
CoreFindexActions::Login(_) | CoreFindexActions::Logout(_),
150+
)) => {
151+
config.to_toml(&conf_path)?;
152+
info!("Saving configuration to: {conf_path:?}");
145153
}
154+
_ => {}
146155
}
147156

148157
Ok(())

crate/cli/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
future_incompatible,
55
keyword_idents,
66
let_underscore,
7-
// rust_2024_compatibility,
87
unreachable_pub,
98
unused,
109
clippy::all,

0 commit comments

Comments
 (0)