Skip to content

Commit f083932

Browse files
committed
add tracing and expand consts definition
1 parent d4470dc commit f083932

File tree

9 files changed

+187
-64
lines changed

9 files changed

+187
-64
lines changed

sshdconfig/Cargo.lock

Lines changed: 49 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sshdconfig/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ lto = true
1717
atty = { version = "0.2" }
1818
chrono = { version = "0.4" }
1919
clap = { version = "4.5", features = ["derive"] }
20+
crossterm = { version = "0.27" }
2021
rust-i18n = { version = "3.1" }
2122
schemars = "0.9"
2223
serde = { version = "1.0", features = ["derive"] }

sshdconfig/locales/en-us.toml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,34 @@ parseInt = "Parse Integer"
1414
registry = "Registry"
1515

1616
[get]
17-
notImplemented = "get not yet implemented for Microsoft.OpenSSH.SSHD/sshd_config"
18-
defaultShellMustBeString = "shell must be a string"
1917
defaultShellCmdOptionMustBeString = "cmdOption must be a string"
2018
defaultShellEscapeArgsMustBe0Or1 = "'%{input}' must be a 0 or 1"
2119
defaultShellEscapeArgsMustBeDWord = "escapeArguments must be a DWord"
20+
defaultShellMustBeString = "shell must be a string"
21+
notImplemented = "get not yet implemented for Microsoft.OpenSSH.SSHD/sshd_config"
2222
windowsOnly = "Microsoft.OpenSSH.SSHD/Windows is only applicable to Windows"
2323

24-
[set]
25-
failedToParseInput = "failed to parse input as DefaultShell with error: '%{error}'"
26-
shellPathMustNotBeRelative = "shell path must not be relative"
27-
shellPathDoesNotExist = "shell path does not exist: '%{shell}'"
28-
2924
[parser]
3025
failedToParse = "failed to parse: '%{input}'"
31-
failedToParseRoot = "failed to parse root: '%{input}'"
32-
invalidConfig = "invalid config: '%{input}'"
33-
unknownNodeType = "unknown node type: '%{node}'"
34-
failedToParseChildNode = "failed to parse child node: '%{input}'"
35-
missingValueInChildNode = "missing value in child node: '%{input}'"
36-
missingKeyInChildNode = "missing key in child node: '%{input}'"
3726
failedToParseAsArray = "value is not an array"
27+
failedToParseChildNode = "failed to parse child node: '%{input}'"
3828
failedToParseNode = "failed to parse '%{input}'"
29+
failedToParseRoot = "failed to parse root: '%{input}'"
30+
invalidConfig = "invalid config: '%{input}'"
31+
invalidMultiArgNode = "multi-arg node '%{input}' is not valid"
32+
invalidValue = "operator is an invalid value for node"
3933
keyNotFound = "key '%{key}' not found"
4034
keyNotRepeatable = "key '%{key}' is not repeatable"
41-
invalidValue = "operator is an invalid value for node"
35+
missingValueInChildNode = "missing value in child node: '%{input}'"
36+
missingKeyInChildNode = "missing key in child node: '%{input}'"
4237
unknownNode = "unknown node: '%{kind}'"
38+
unknownNodeType = "unknown node type: '%{node}'"
39+
40+
[set]
41+
failedToParseInput = "failed to parse input as DefaultShell with error: '%{error}'"
42+
shellPathDoesNotExist = "shell path does not exist: '%{shell}'"
43+
shellPathMustNotBeRelative = "shell path must not be relative"
4344

4445
[util]
4546
sshdElevation = "elevated security context required"
47+
tracingInitError = "Failed to initialize tracing"

sshdconfig/src/get.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ use {
88
crate::metadata::windows::{DEFAULT_SHELL, DEFAULT_SHELL_CMD_OPTION, DEFAULT_SHELL_ESCAPE_ARGS, REGISTRY_PATH},
99
};
1010

11+
use rust_i18n::t;
12+
use tracing::debug;
13+
1114
use crate::args::Setting;
1215
use crate::error::SshdConfigError;
13-
use rust_i18n::t;
1416

1517
/// Invoke the get command.
1618
///
1719
/// # Errors
1820
///
1921
/// This function will return an error if the desired settings cannot be retrieved.
2022
pub fn invoke_get(setting: &Setting) -> Result<(), SshdConfigError> {
23+
debug!("Get setting: {:?}", setting);
2124
match *setting {
2225
Setting::SshdConfig => Err(SshdConfigError::NotImplemented(t!("get.notImplemented").to_string())),
2326
Setting::WindowsGlobal => get_default_shell()
@@ -78,4 +81,4 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
7881
#[cfg(not(windows))]
7982
fn get_default_shell() -> Result<(), SshdConfigError> {
8083
Err(SshdConfigError::InvalidInput(t!("get.windowsOnly").to_string()))
81-
}
84+
}

sshdconfig/src/main.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
use clap::{Parser};
55
use rust_i18n::i18n;
66
use schemars::schema_for;
7+
use std::process::exit;
8+
use tracing::{debug, error};
79

810
use args::{Args, Command, DefaultShell, Setting};
911
use export::invoke_export;
1012
use get::invoke_get;
1113
use parser::SshdConfigParser;
1214
use set::invoke_set;
15+
use util::enable_tracing;
1316

1417
mod args;
1518
mod error;
@@ -22,14 +25,25 @@ mod util;
2225

2326
i18n!("locales", fallback = "en-us");
2427

28+
const EXIT_SUCCESS: i32 = 0;
29+
const EXIT_FAILURE: i32 = 1;
30+
2531
fn main() {
32+
enable_tracing();
33+
2634
let args = Args::parse();
2735

2836
let result = match &args.command {
29-
Command::Export => invoke_export(),
30-
Command::Get { setting } => invoke_get(setting),
31-
Command::Set { input } => invoke_set(input),
37+
Command::Export => {
38+
debug!("Export command");
39+
invoke_export()
40+
},
41+
Command::Get { setting } => {
42+
debug!("Get command: setting={:?}", setting);
43+
invoke_get(setting)
44+
},
3245
Command::Schema { setting } => {
46+
debug!("Schema command: setting={:?}", setting);
3347
let schema = match setting {
3448
Setting::SshdConfig => {
3549
schema_for!(SshdConfigParser)
@@ -40,11 +54,17 @@ fn main() {
4054
};
4155
println!("{}", serde_json::to_string(&schema).unwrap());
4256
Ok(())
43-
}
57+
},
58+
Command::Set { input } => {
59+
debug!("Set command: input={}", input);
60+
invoke_set(input)
61+
},
4462
};
4563

4664
if let Err(e) = result {
47-
eprintln!("{e}");
48-
std::process::exit(1);
65+
error!("{e}");
66+
exit(EXIT_FAILURE);
4967
}
68+
69+
exit(EXIT_SUCCESS);
5070
}

sshdconfig/src/metadata.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use clap::ValueEnum;
5-
6-
// TODO: ensure lists are complete
7-
8-
// keywords that can be repeated over multiple lines and should be represented as arrays
9-
pub const REPEATABLE_KEYWORDS: [&str; 6] = [
10-
"hostkey",
11-
"include",
12-
"listenaddress",
13-
"port",
14-
"setenv",
15-
"subsystem"
16-
];
17-
18-
#[derive(Clone, Debug, Eq, PartialEq, ValueEnum)]
19-
pub enum RepeatableKeyword {
20-
HostKey,
21-
Include,
22-
ListenAddress,
23-
Port,
24-
SetEnv,
25-
Subsystem,
26-
}
27-
28-
// keywords that can have multiple argments per line and should be represented as arrays
29-
// but cannot be repeated over multiple lines, as subsequent entries are ignored
30-
pub const MULTI_ARG_KEYWORDS: [&str; 7] = [
4+
// keywords that can have multiple argments per line but cannot be repeated over multiple lines,
5+
// as subsequent entries are ignored, should be represented as arrays
6+
pub const MULTI_ARG_KEYWORDS: [&str; 16] = [
7+
"authenticationmethods",
8+
"authorizedkeysfile",
319
"casignaturealgorithms",
10+
"channeltimeout",
3211
"ciphers",
3312
"hostbasedacceptedalgorithms",
3413
"hostkeyalgorithms",
14+
"ipqos",
3515
"kexalgorithms",
3616
"macs",
17+
"permitlisten",
18+
"permitopen",
19+
"permituserenvironment",
20+
"persourcepenalties",
21+
"persourcepenaltyexemptlist",
3722
"pubkeyacceptedalgorithms"
3823
];
3924

25+
// keywords that can be repeated over multiple lines and should be represented as arrays.
26+
// note that some keywords can be both multi-arg and repeatable, in which case they only need to be listed here
27+
pub const REPEATABLE_KEYWORDS: [&str; 12] = [
28+
"acceptenv",
29+
"allowgroups",
30+
"allowusers",
31+
"denygroups",
32+
"denyusers",
33+
"hostkey",
34+
"include",
35+
"listenaddress",
36+
"match",
37+
"port",
38+
"setenv",
39+
"subsystem"
40+
];
41+
4042
#[cfg(windows)]
4143
pub mod windows {
4244
pub const REGISTRY_PATH: &str = "HKLM\\SOFTWARE\\OpenSSH";

0 commit comments

Comments
 (0)