Skip to main content

nethsm/cli/command/
env.rs

1use clap::{Parser, Subcommand};
2use expression_format::ex_format;
3use strum::IntoEnumIterator;
4
5use crate::{ConnectionSecurity, Url, UserId, UserRole, cli::PassphraseFile};
6
7/// The "netshm env" command.
8#[derive(Debug, Subcommand)]
9#[command(about = "Manage environments in the configuration file")]
10pub enum EnvCommand {
11    /// The "netshm env add" command.
12    #[command(subcommand)]
13    Add(EnvAddCommand),
14
15    /// The "netshm env delete" command.
16    #[command(subcommand)]
17    Delete(EnvDeleteCommand),
18
19    /// The "netshm env list" command.
20    #[command(about = "List all items in the configuration file")]
21    List,
22}
23
24/// The "netshm env add" command.
25#[derive(Debug, Subcommand)]
26#[command(
27    about = "Add a configuration item",
28    long_about = "Add a configuration item
29
30Add a new device, or credentials for an existing one."
31)]
32pub enum EnvAddCommand {
33    /// The "netshm env add credentials" command.
34    Credentials(CredentialsAddCommand),
35    /// The "netshm env add device" command.
36    Device(DeviceAddCommand),
37}
38
39#[derive(Debug, Parser)]
40#[command(
41    about = "Add credentials for a device in the configuration",
42    long_about = "Add credentials for a device in the configuration
43
44By default credentials in the configuration file only contain user name and role.
45In this scenario the passphrase of a user is prompted for interactively, once it is needed.
46
47Optionally, it is possible to also persist a passphrase for a given user name to allow non-interactive use.
48This use is discouraged as it persists the passphrase in an unencrypted configuration file."
49)]
50pub struct CredentialsAddCommand {
51    #[arg(
52        env = "NETHSM_USER_NAME",
53        help = "The name of the user on the target device"
54    )]
55    pub name: UserId,
56
57    #[arg(
58        env = "NETHSM_PASSPHRASE_FILE",
59        help = "The path to a file containing the passphrase",
60        long,
61        short
62    )]
63    pub passphrase_file: Option<PassphraseFile>,
64
65    #[arg(
66        env = "NETHSM_USER_ROLE",
67        help = "The optional role of the user on the target device",
68        long_help = format!("The optional role of the user on the target device
69
70One of {:?} (defaults to \"{}\").", UserRole::iter().map(Into::into).collect::<Vec<&'static str>>(), UserRole::default())
71    )]
72    pub role: Option<UserRole>,
73
74    #[arg(
75        env = "NETHSM_WITH_PASSPHRASE",
76        help = "Whether to prompt for and store a passphrase for the user",
77        long_help = "Whether to prompt for and store a passphrase for the user
78
79The passphrase is persisted in the configuration file. Use with caution!",
80        long,
81        short
82    )]
83    pub with_passphrase: bool,
84}
85
86#[derive(Debug, Parser)]
87#[command(
88    about = "Add a device to the configuration",
89    long_about = "Add a device to the configuration
90
91Device entries are added with a URL and settings for the TLS connection security.
92
93For this command it is required to provide a label that identifies the device."
94)]
95pub struct DeviceAddCommand {
96    #[arg(env = "NETHSM_URL", help = "The URL of the device API")]
97    pub url: Url,
98
99    #[arg(
100        env = "NETHSM_TLS_SECURITY",
101        help = "The TLS connection security for the device",
102        long_help = ex_format!("The TLS connection security for the device
103
104One of the following:
105* \"{:?ConnectionSecurity::Unsafe}\": the TLS connection is not validated by authenticating the target host certificate
106* \"{:?ConnectionSecurity::Native}\": the TLS connection is validated by authenticating the target host certificate against the caller's native system-wide certificate store
107* the pinned SHA-256 checksum of the target host key (prefixed with \"sha256:\") is used to validate the target host certificate"),
108    )]
109    pub tls_security: ConnectionSecurity,
110}
111
112/// The "nethsm env delete" command.
113#[derive(Debug, Subcommand)]
114#[command(
115    about = "Delete a configuration item",
116    long_about = "Delete a configuration item
117
118Delete credentials for an existing device or a device."
119)]
120pub enum EnvDeleteCommand {
121    /// The "nethsm env credentials" command.
122    Credentials(CredentialsDeleteCommand),
123    /// The "nethsm env device" command.
124    Device(DeviceDeleteCommand),
125}
126
127#[derive(Debug, Parser)]
128#[command(about = "Delete credentials for a device")]
129pub struct CredentialsDeleteCommand {
130    #[arg(
131        env = "NETHSM_USER_NAME",
132        help = "The user name matching the credentials to be deleted"
133    )]
134    pub name: UserId,
135}
136
137#[derive(Debug, Parser)]
138#[command(
139    about = "Delete a device",
140    long_about = "Delete a device from the configuration
141
142For this command it is required to provide a label that identifies the device."
143)]
144pub struct DeviceDeleteCommand {}