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

fix: add device.csr_path to cloud profiles #3441

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ define_tedge_config! {
cert_path: Utf8PathBuf,

/// Path where the device's certificate signing request is stored
#[tedge_config(example = "/etc/tedge/device-certs/tedge.csr", default(function = "default_device_csr"))]
#[tedge_config(example = "/etc/tedge/device-certs/tedge.csr", default(function = "default_device_csr"), reader(private))]
#[doku(as = "PathBuf")]
csr_path: Utf8PathBuf,

Expand Down Expand Up @@ -204,6 +204,11 @@ define_tedge_config! {
#[tedge_config(example = "/etc/tedge/device-certs/tedge-certificate.pem", default(from_key = "device.cert_path"))]
#[doku(as = "PathBuf")]
cert_path: Utf8PathBuf,

/// Path where the device's certificate signing request is stored
#[tedge_config(example = "/etc/tedge/device-certs/tedge.csr", default(from_key = "device.csr_path"))]
#[doku(as = "PathBuf")]
csr_path: Utf8PathBuf,
},

smartrest: {
Expand Down Expand Up @@ -399,6 +404,11 @@ define_tedge_config! {
#[tedge_config(example = "/etc/tedge/device-certs/tedge-certificate.pem", default(from_key = "device.cert_path"))]
#[doku(as = "PathBuf")]
cert_path: Utf8PathBuf,

/// Path where the device's certificate signing request is stored
#[tedge_config(example = "/etc/tedge/device-certs/tedge.csr", default(from_key = "device.csr_path"))]
#[doku(as = "PathBuf")]
csr_path: Utf8PathBuf,
},

mapper: {
Expand Down Expand Up @@ -468,6 +478,11 @@ define_tedge_config! {
#[tedge_config(example = "/etc/tedge/device-certs/tedge-certificate.pem", default(from_key = "device.cert_path"))]
#[doku(as = "PathBuf")]
cert_path: Utf8PathBuf,

/// Path where the device's certificate signing request is stored
#[tedge_config(example = "/etc/tedge/device-certs/tedge.csr", default(from_key = "device.csr_path"))]
#[doku(as = "PathBuf")]
csr_path: Utf8PathBuf,
},

mapper: {
Expand Down Expand Up @@ -902,6 +917,18 @@ impl TEdgeConfigReader {
})
}

pub fn device_csr_path<'a>(
&self,
cloud: Option<impl Into<Cloud<'a>>>,
) -> Result<&Utf8Path, MultiError> {
Ok(match cloud.map(<_>::into) {
None => &self.device.csr_path,
Some(Cloud::C8y(profile)) => &self.c8y.try_get(profile)?.device.csr_path,
Some(Cloud::Az(profile)) => &self.az.try_get(profile)?.device.csr_path,
Some(Cloud::Aws(profile)) => &self.aws.try_get(profile)?.device.csr_path,
})
}

pub fn device_id<'a>(&self, cloud: Option<impl Into<Cloud<'a>>>) -> Result<&str, ReadError> {
Ok(match cloud.map(<_>::into) {
None => self.device.id()?,
Expand Down
15 changes: 2 additions & 13 deletions crates/core/tedge/src/cli/certificate/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use super::show::ShowCertCmd;
use super::upload::*;

use anyhow::anyhow;
use camino::Utf8PathBuf;
use clap::ValueHint;
use tedge_config::OptionalConfigError;
use tedge_config::ProfileName;
use tedge_config::TEdgeConfig;
Expand Down Expand Up @@ -37,10 +35,6 @@ pub enum TEdgeCertCli {
#[clap(long = "device-id", global = true)]
id: Option<String>,

/// Path where a Certificate signing request will be stored
#[clap(long = "output-path", global = true, value_hint = ValueHint::FilePath)]
output_path: Option<Utf8PathBuf>,
Comment on lines -41 to -42
Copy link
Member

Choose a reason for hiding this comment

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

Why --output_path option is removed?


#[clap(subcommand)]
cloud: Option<CloudArg>,
},
Expand Down Expand Up @@ -91,18 +85,13 @@ impl BuildCommand for TEdgeCertCli {
cmd.into_boxed()
}

TEdgeCertCli::CreateCsr {
id,
output_path,
cloud,
} => {
TEdgeCertCli::CreateCsr { id, cloud } => {
let cloud: Option<Cloud> = cloud.map(<_>::try_into).transpose()?;

let cmd = CreateCsrCmd {
id: get_device_id(id, &config, &cloud)?,
key_path: config.device_key_path(cloud.as_ref())?.to_owned(),
// Use output file instead of csr_path from tedge config if provided
csr_path: output_path.unwrap_or_else(|| config.device.csr_path.clone()),
csr_path: config.device_csr_path(cloud.as_ref())?.to_owned(),
user: user.to_owned(),
group: group.to_owned(),
};
Expand Down
Loading