Skip to content

Commit 87a9681

Browse files
authored
Merge pull request #3316 from rina23q/fix/3315/cert-create-csr-should-use-cloud-profile-cn
fix: tedge cert create-csr should use cloud profile's CN
2 parents db8bf7e + a4d99a4 commit 87a9681

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

crates/core/tedge/src/cli/certificate/cli.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::cli::common::CloudArg;
33
use camino::Utf8PathBuf;
44
use tedge_config::OptionalConfigError;
55
use tedge_config::ProfileName;
6+
use tedge_config::ReadError;
7+
use tedge_config::TEdgeConfig;
68

79
use super::create::CreateCertCmd;
810
use super::create_csr::CreateCsrCmd;
@@ -93,11 +95,14 @@ impl BuildCommand for TEdgeCertCli {
9395
cloud,
9496
} => {
9597
let cloud: Option<Cloud> = cloud.map(<_>::try_into).transpose()?;
98+
9699
// Use the current device id if no id is provided
97-
let id = match id {
98-
Some(id) => id,
99-
None => config.device.id()?.clone(),
100+
let id = if let Some(id) = id {
101+
id
102+
} else {
103+
get_device_id_from_config(&config, &cloud)?
100104
};
105+
101106
let cmd = CreateCsrCmd {
102107
id,
103108
key_path: config.device_key_path(cloud.as_ref())?.to_owned(),
@@ -186,3 +191,17 @@ pub enum UploadCertCli {
186191
profile: Option<ProfileName>,
187192
},
188193
}
194+
195+
fn get_device_id_from_config(
196+
config: &TEdgeConfig,
197+
cloud: &Option<Cloud>,
198+
) -> Result<String, ReadError> {
199+
let id = match cloud {
200+
None => config.device.id(),
201+
Some(Cloud::C8y(profile)) => config.c8y.try_get(profile.as_deref())?.device.id(),
202+
Some(Cloud::Azure(profile)) => config.az.try_get(profile.as_deref())?.device.id(),
203+
Some(Cloud::Aws(profile)) => config.aws.try_get(profile.as_deref())?.device.id(),
204+
}?
205+
.to_owned();
206+
Ok(id)
207+
}

tests/RobotFramework/tests/tedge/certificate_signing_request.robot

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ Generate CSR without an existing certificate and private key
5252
... openssl req -in /etc/tedge/device-certs/tedge.csr -pubkey -noout | openssl md5
5353
Should Be Equal ${output_private_key_md5} ${output_csr_md5}
5454

55+
Generate CSR using the device-id from an existing certificate and private key of cloud profile
56+
[Tags] \#3315
57+
[Setup] Setup With Self-Signed Certificate
58+
59+
${second_device_sn}= Catenate SEPARATOR=_ ${DEVICE_SN} second
60+
Execute Command
61+
... tedge config set c8y.device.cert_path --profile second /etc/tedge/device-certs/tedge@second-certificate.pem
62+
Execute Command
63+
... tedge config set c8y.device.key_path --profile second /etc/tedge/device-certs/tedge@second-key.pem
64+
Execute Command tedge cert create --device-id ${second_device_sn} c8y --profile second
65+
66+
${hash_before_cert}= Execute Command md5sum /etc/tedge/device-certs/tedge@second-certificate.pem
67+
${hash_before_private_key}= Execute Command md5sum /etc/tedge/device-certs/tedge@second-key.pem
68+
69+
Execute Command sudo tedge cert create-csr c8y --profile second
70+
71+
${output_cert_subject}= Execute Command
72+
... openssl x509 -noout -subject -in /etc/tedge/device-certs/tedge@second-certificate.pem
73+
${output_csr_subject}= Execute Command
74+
... openssl req -noout -subject -in /etc/tedge/device-certs/tedge.csr
75+
Should Be Equal ${output_cert_subject} ${output_csr_subject}
76+
77+
${output_private_key_md5}= Execute Command
78+
... openssl pkey -in /etc/tedge/device-certs/tedge@second-key.pem -pubout | openssl md5
79+
${output_csr_md5}= Execute Command
80+
... openssl req -in /etc/tedge/device-certs/tedge.csr -pubkey -noout | openssl md5
81+
Should Be Equal ${output_private_key_md5} ${output_csr_md5}
82+
83+
${hash_after_cert}= Execute Command md5sum /etc/tedge/device-certs/tedge@second-certificate.pem
84+
${hash_after_private_key}= Execute Command md5sum /etc/tedge/device-certs/tedge@second-key.pem
85+
Should Be Equal ${hash_before_cert} ${hash_after_cert}
86+
Should Be Equal ${hash_before_private_key} ${hash_after_private_key}
87+
5588

5689
*** Keywords ***
5790
Setup With Self-Signed Certificate

0 commit comments

Comments
 (0)