nethsm_cli/cli/
config.rs

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use std::{net::Ipv4Addr, path::PathBuf};

use chrono::{DateTime, Utc};
use clap::{Parser, Subcommand};
use expression_format::ex_format;
use nethsm::{BootMode, LogLevel, SystemState, TlsKeyType, UserRole::Administrator};
use strum::IntoEnumIterator;

use crate::passphrase_file::PassphraseFile;

#[derive(Debug, Subcommand)]
#[command(
    about = "Manage the configuration of a device",
    long_about = "Manage the configuration of a device

Allows adding, removing and listing of configuration items"
)]
pub enum ConfigCommand {
    #[command(subcommand)]
    Get(ConfigGetCommand),

    #[command(subcommand)]
    Set(ConfigSetCommand),
}

#[derive(Debug, Subcommand)]
#[command(about = "Get a configuration item for a device")]
pub enum ConfigGetCommand {
    BootMode(GetBootModeCommand),
    Logging(GetLoggingCommand),
    Network(GetNetworkCommand),
    Time(GetTimeCommand),
    TlsCertificate(GetTlsCertificateCommand),
    TlsCsr(GetTlsCsrCommand),
    TlsPublicKey(GetTlsPublicKeyCommand),
}

#[derive(Debug, Parser)]
#[command(
    about = "Get the unattended boot configuration",
    long_about = ex_format!("Get the unattended boot configuration

* \"{BootMode::Attended}\" if the device needs to be unlocked during boot
* \"{BootMode::Unattended}\" if the device does not need to be unlocked during boot

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetBootModeCommand {}

#[derive(Debug, Parser)]
#[command(
    about = "Get the logging configuration",
    long_about = ex_format!("Get the logging configuration

Shows IP address and port number of the host the target device logs to at a given log level.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetLoggingCommand {}

#[derive(Debug, Parser)]
#[command(
    about = "Get the network configuration",
    long_about = ex_format!("Get the network configuration

Shows IP address, netmask and gateway of the target device.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetNetworkCommand {}

#[derive(Debug, Parser)]
#[command(
    about = "Get the time",
    long_about = ex_format!("Get the time

Returns the current time as ISO 8601 formatted timestamp.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetTimeCommand {}

#[derive(Debug, Parser)]
#[command(
    about = "Get the certificate for the TLS connection",
    long_about = ex_format!("Get the certificate for the TLS connection

The X.509 certificate is returned in Privacy-enhanced Electronic Mail (PEM) format.
Unless a specific output file is chosen, the certificate is returned on stdout.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetTlsCertificateCommand {
    #[arg(
        env = "NETHSM_FORCE",
        help = "Write to output file even if it exists already",
        long,
        short
    )]
    pub force: bool,

    #[arg(
        env = "NETHSM_CONFIG_TLS_CERT_OUTPUT_FILE",
        help = "The optional path to a specific file that the certificate is written to",
        long,
        short
    )]
    pub output: Option<PathBuf>,
}

#[derive(Debug, Parser)]
#[command(
    about = "Get a Certificate Signing Request for the TLS certificate",
    long_about = ex_format!("Get a Certificate Signing Request for the TLS certificate

The PKCS#10 Certificate Signing Request (CSR) is returned in Privacy-enhanced Electronic Mail (PEM) format.
Unless a specific output file is chosen, the certificate is returned on stdout.

At a minimum, the \"Common Name\" (CN) attribute for the CSR has to be provided.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetTlsCsrCommand {
    #[arg(
        env = "NETHSM_TLS_CSR_COMMON_NAME",
        help = "The mandatory \"Common Name\" (CN) attribute for the CSR",
        long_help = "The mandatory \"Common Name\" (CN) attribute for the CSR

A fully qualified domain name (FQDN) that should be secured using the CSR."
    )]
    pub common_name: String,

    #[arg(
        env = "NETHSM_TLS_CSR_ORG_NAME",
        help = "The optional \"Organization Name\" (O) attribute for the CSR",
        long_help = "The optional \"Organization Name\" (O) attribute for the CSR

Usually the legal name of a company or entity and should include any suffixes such as Ltd., Inc., or Corp."
    )]
    pub org_name: Option<String>,

    #[arg(
        env = "NETHSM_TLS_CSR_ORG_UNIT",
        help = "The optional \"Organizational Unit\" (OU) attribute for the CSR",
        long_help = "The optional \"Organizational Unit\" (OU) attribute for the CSR

Internal organization department/division name."
    )]
    pub org_unit: Option<String>,

    #[arg(
        env = "NETHSM_TLS_CSR_LOCALITY",
        help = "The optional \"Locality\" (L) attribute for the CSR",
        long_help = "The optional \"Locality\" (L) attribute for the CSR

Name of town, city, village, etc."
    )]
    pub locality: Option<String>,

    #[arg(
        env = "NETHSM_TLS_CSR_STATE",
        help = "The optional \"State\" (ST) attribute for the CSR",
        long_help = "The optional \"State\" (ST) attribute for the CSR

Province, region, county or state."
    )]
    pub state: Option<String>,

    #[arg(
        env = "NETHSM_TLS_CSR_COUNTRY",
        help = "The optional \"Country\" (C) attribute for the CSR",
        long_help = "The optional \"Country\" (C) attribute for the CSR

The two-letter ISO code for the country where the \"Organization\" (O) is located."
    )]
    pub country: Option<String>,

    #[arg(
        env = "NETHSM_TLS_CSR_EMAIL",
        help = "The optional \"Email Address\" (EMAIL) attribute for the CSR",
        long_help = "The optional \"Email Address\" (EMAIL) attribute for the CSR

The organization contact, usually of the certificate administrator or IT department."
    )]
    pub email: Option<String>,

    #[arg(
        env = "NETHSM_FORCE",
        help = "Write to output file even if it exists already",
        long,
        short
    )]
    pub force: bool,

    #[arg(
        env = "NETHSM_CONFIG_TLS_CSR_OUTPUT_FILE",
        help = "The optional path to a specific file that the certificate is written to",
        long,
        short
    )]
    pub output: Option<PathBuf>,
}

#[derive(Debug, Parser)]
#[command(
    about = "Get the public key for the TLS connection",
    long_about = ex_format!("Get the public key for the TLS connection

The X.509 public key certificate is returned in Privacy-enhanced Electronic Mail (PEM) format.
Unless a specific output file is chosen, the certificate is returned on stdout.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct GetTlsPublicKeyCommand {
    #[arg(
        env = "NETHSM_FORCE",
        help = "Write to output file even if it exists already",
        long,
        short
    )]
    pub force: bool,

    #[arg(
        env = "NETHSM_CONFIG_TLS_PUBKEY_OUTPUT_FILE",
        help = "The optional path to a specific file that the certificate is written to",
        long,
        short
    )]
    pub output: Option<PathBuf>,
}

#[derive(Debug, Subcommand)]
#[command(about = "Set a configuration item for a device")]
pub enum ConfigSetCommand {
    BackupPassphrase(SetBackupPassphraseCommand),
    BootMode(SetBootModeCommand),
    Logging(SetLoggingCommand),
    Network(SetNetworkCommand),
    Time(SetTimeCommand),
    TlsCertificate(SetTlsCertificateCommand),
    TlsGenerate(SetTlsGenerateCommand),
    UnlockPassphrase(SetUnlockPassphraseCommand),
}

#[derive(Debug, Parser)]
#[command(
    about = "Set the backup passphrase",
    long_about = ex_format!("Set the backup passphrase

The initial backup passphrase is the empty string.

The new passphrase must be >= 10 and <= 200 characters.

By default the passphrases are prompted for interactively, but they can each be provided using a dedicated passphrase file instead.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetBackupPassphraseCommand {
    #[arg(
        env = "NETHSM_NEW_PASSPHRASE_FILE",
        help = "The path to a file containing the new passphrase",
        long_help = "The path to a file containing the new passphrase

The passphrase must be >= 10 and <= 200 characters long.",
        long,
        short
    )]
    pub new_passphrase_file: Option<PassphraseFile>,

    #[arg(
        env = "NETHSM_OLD_PASSPHRASE_FILE",
        help = "The path to a file containing the old passphrase",
        long_help = "The path to a file containing the old passphrase

The passphrase must be >= 10 and <= 200 characters long.",
        long,
        short
    )]
    pub old_passphrase_file: Option<PassphraseFile>,
}

#[derive(Debug, Parser)]
#[command(
    about = "Set the unattended boot mode",
    long_about = ex_format!("Set the unattended boot mode

Sets whether the device boots into state \"{SystemState::Locked}\" (using boot mode \"{BootMode::Attended}\") or \"{SystemState::Operational}\" (using boot mode \"{BootMode::Unattended}\").

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    ),
)]
pub struct SetBootModeCommand {
    #[arg(
        env = "NETHSM_BOOT_MODE",
        help = "The boot mode to use",
        long_help = format!("The boot mode to use

One of {:?} (no default).",
            BootMode::iter().map(Into::into).collect::<Vec<&'static str>>()
        )
    )]
    pub boot_mode: BootMode,
}

#[derive(Debug, Parser)]
#[command(
    about = "Set the logging configuration",
    long_about = ex_format!("Set the logging configuration

Provide IP address and port of a host to send syslog to at a specified log level.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetLoggingCommand {
    #[arg(
        env = "NETHSM_LOGGING_IP_ADDRESS",
        help = "The IPv4 address of the host to send syslog to"
    )]
    pub ip_address: Ipv4Addr,

    #[arg(
        env = "NETHSM_LOGGING_PORT",
        help = "The port of the host to send syslog to"
    )]
    pub port: u32,

    #[arg(
        env = "NETHSM_LOGGING_LOG_LEVEL",
        help = "The log level at which to log",
        long_help = format!("The log level at which to log

One of {:?} (defaults to \"{:?}\").",
            LogLevel::iter().map(Into::into).collect::<Vec<&'static str>>(),
            LogLevel::default(),
        )
    )]
    pub log_level: Option<LogLevel>,
}

#[derive(Debug, Parser)]
#[command(
    about = "Set the network configuration",
    long_about = ex_format!("Set the network configuration

Provide IPv4 address, netmask and Ipv4 gateway address for the device to use.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetNetworkCommand {
    #[arg(
        env = "NETHSM_NETWORK_IP_ADDRESS",
        help = "The IPv4 address the device is to use"
    )]
    pub ip_address: Ipv4Addr,

    #[arg(
        env = "NETHSM_NETWORK_NETMASK",
        help = "The IPv4 netmask the device is to use"
    )]
    pub netmask: String,

    #[arg(
        env = "NETHSM_NETWORK_GATEWAY",
        help = "The IPv4 gateway the device is to use"
    )]
    pub gateway: Ipv4Addr,
}

#[derive(Debug, Parser)]
#[command(
    about = "Set the time",
    long_about = ex_format!("Set the time

The time must be provided as ISO 8601 formatted UTC timestamp.
If no timestamp is provided, the caller's current system time is used to construct a UTC timestamp.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetTimeCommand {
    #[arg(
        env = "NETHSM_SYSTEM_TIME",
        help = "An optional ISO 8601 formatted UTC timestamp",
        long_help = "An optional ISO 8601 formatted UTC timestamp

If no timestamp is provided, the caller's current system time is used."
    )]
    pub system_time: Option<DateTime<Utc>>,
}

#[derive(Debug, Parser)]
#[command(
    about = "Set a new TLS certificate",
    long_about = ex_format!("Set a new TLS certificate

The X.509 certificate must be provided in Privacy-enhanced Electronic Mail (PEM) format.

The certificate is only accepted if it is created using a Certificate Signing Request (CSR) generated by the target device.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetTlsCertificateCommand {
    #[arg(
        env = "NETHSM_TLS_CERT",
        help = "A new TLS certificate file",
        long_help = "A new TLS certificate file

The X.509 certificate must be provided in Privacy-enhanced Electronic Mail (PEM) format."
    )]
    pub tls_cert: PathBuf,
}

#[derive(Debug, Parser)]
#[command(
    about = "Generate a new TLS certificate",
    long_about = ex_format!("Generate a new TLS certificate

The current TLS certificate is replaced by the newly generated one.
Optionally, the type of key and its length can be specified.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetTlsGenerateCommand {
    #[arg(
        env = "NETHSM_TLS_KEY_TYPE",
        help = "The optional key type of the TLS certificate to generate",
        long_help = format!("The optional key type of the TLS certificate to generate

One of {:?} (defaults to \"{}\").",
            TlsKeyType::iter().map(Into::into).collect::<Vec<&'static str>>(),
            TlsKeyType::default(),
        ),
    )]
    pub tls_key_type: Option<TlsKeyType>,

    #[arg(
        env = "NETHSM_TLS_KEY_LENGTH",
        help = "The bit length of the TLS key to generate",
        long_help = ex_format!("The optional bit length of the TLS key to generate

The bit length must be compatible with the chosen key type.

Requires authentication of a user in the \"{Administrator}\" role.")
    )]
    pub tls_key_length: Option<u32>,
}

#[derive(Debug, Parser)]
#[command(
    about = "Set the unlock passphrase",
    long_about = ex_format!("Set the unlock passphrase

The initial unlock passphrase is set during provisioning.

The new passphrase must be >= 10 and <= 200 characters.

By default the passphrases are prompted for interactively, but they can each be provided using a dedicated passphrase file instead.

Requires authentication of a system-wide user in the \"{Administrator}\" role."
    )
)]
pub struct SetUnlockPassphraseCommand {
    #[arg(
        env = "NETHSM_NEW_PASSPHRASE_FILE",
        help = "The path to a file containing the new passphrase",
        long_help = "The path to a file containing the new passphrase

The passphrase must be >= 10 and <= 200 characters long.",
        long,
        short
    )]
    pub new_passphrase_file: Option<PassphraseFile>,

    #[arg(
        env = "NETHSM_OLD_PASSPHRASE_FILE",
        help = "The path to a file containing the old passphrase",
        long_help = "The path to a file containing the old passphrase

The passphrase must be >= 10 and <= 200 characters long.",
        long,
        short
    )]
    pub old_passphrase_file: Option<PassphraseFile>,
}