Skip to content

Commit 4475ddf

Browse files
committed
fix: openid login commands
1 parent 5b0bfb3 commit 4475ddf

File tree

9 files changed

+166
-158
lines changed

9 files changed

+166
-158
lines changed

Cargo.lock

Lines changed: 11 additions & 12 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ base64 = "0.21"
4545
chrono = "0.4"
4646
clap = { version = "4.5", default-features = false }
4747
cloudproof = "3.0"
48-
cosmian_kms_cli = { git = "https://www.github.com/Cosmian/kms", branch = "develop" }
49-
cosmian_findex_cli = { git = "https://www.github.com/Cosmian/findex-server", branch = "develop" }
48+
cosmian_kms_cli = { git = "https://www.github.com/Cosmian/kms", branch = "fix/openid_login" }
49+
cosmian_findex_cli = { git = "https://www.github.com/Cosmian/findex-server", branch = "fix/openid_login" }
5050
cosmian_config_utils = { git = "https://www.github.com/Cosmian/http_client_server", branch = "develop" }
5151
cosmian_http_client = { git = "https://www.github.com/Cosmian/http_client_server", branch = "develop" }
5252
cosmian_logger = { git = "https://www.github.com/Cosmian/http_client_server", branch = "develop" }

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(())

0 commit comments

Comments
 (0)