summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge/src/cli/certificate/cli.rs
blob: 7ef78fb50246de38c79b146ea6cc189e1022be70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use super::{create::CreateCertCmd, remove::RemoveCertCmd, show::ShowCertCmd, upload::*};

use crate::command::{BuildCommand, BuildContext, Command};
use crate::ConfigError;

use structopt::StructOpt;
use tedge_config::*;

#[derive(StructOpt, Debug)]
pub enum TEdgeCertCli {
    /// Create a self-signed device certificate
    Create {
        /// The device identifier to be used as the common name for the certificate
        #[structopt(long = "device-id")]
        id: String,
    },

    /// Show the device certificate, if any
    Show,

    /// Remove the device certificate
    Remove,

    /// Upload root certificate
    Upload(UploadCertCli),
}

impl BuildCommand for TEdgeCertCli {
    fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
        let config = context.config_repository.load()?;

        let cmd = match self {
            TEdgeCertCli::Create { id } => {
                let cmd = CreateCertCmd {
                    id,
                    cert_path: config.query(DeviceCertPathSetting)?,
                    key_path: config.query(DeviceKeyPathSetting)?,
                    user_manager: context.user_manager,
                };
                cmd.into_boxed()
            }

            TEdgeCertCli::Show => {
                let cmd = ShowCertCmd {
                    cert_path: config.query(DeviceCertPathSetting)?,
                };
                cmd.into_boxed()
            }

            TEdgeCertCli::Remove => {
                let cmd = RemoveCertCmd {
                    cert_path: config.query(DeviceCertPathSetting)?,
                    key_path: config.query(DeviceKeyPathSetting)?,
                    user_manager: context.user_manager,
                };
                cmd.into_boxed()
            }

            TEdgeCertCli::Upload(cmd) => {
                let cmd = match cmd {
                    UploadCertCli::C8y { username } => UploadCertCmd {
                        device_id: config.query(DeviceIdSetting)?,
                        path: config.query(DeviceCertPathSetting)?,
                        host: config.query(C8yUrlSetting)?,
                        username,
                    },
                };
                cmd.into_boxed()
            }
        };

        Ok(cmd)
    }
}

#[derive(StructOpt, Debug)]
pub enum UploadCertCli {
    /// Upload root certificate to Cumulocity
    ///
    /// The command will upload root certificate to Cumulocity.
    C8y {
        #[structopt(long = "user")]
        /// Provided username should be a Cumulocity user with tenant management permissions.
        /// The password is requested on /dev/tty, unless the $C8YPASS env var is set to the user password.
        username: String,
    },
}