Skip to content

Commit 4f9a089

Browse files
committed
address review feedback
1 parent 4a65468 commit 4f9a089

File tree

9 files changed

+70
-42
lines changed

9 files changed

+70
-42
lines changed

sshdconfig/src/args.rs

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

4-
use clap::{Parser, Subcommand};
4+
use clap::{Parser, Subcommand, ValueEnum};
55
use schemars::JsonSchema;
66
use serde::{Deserialize, Serialize};
77

@@ -14,7 +14,10 @@ pub struct Args {
1414
#[derive(Subcommand)]
1515
pub enum Command {
1616
/// Get default shell, eventually to be used for `sshd_config` and repeatable keywords
17-
Get,
17+
Get {
18+
#[clap(short = 'r', long, hide = true)]
19+
resource: Resource,
20+
},
1821
/// Set default shell, eventually to be used for `sshd_config` and repeatable keywords
1922
Set {
2023
#[clap(short = 'i', long, help = "input to set in sshd_config")]
@@ -24,8 +27,8 @@ pub enum Command {
2427
Export,
2528
Schema {
2629
// Used to inform which schema to generate
27-
#[clap(long, hide = true)]
28-
as_global: bool,
30+
#[clap(short = 'r', long, hide = true)]
31+
resource: Resource,
2932
},
3033
}
3134

@@ -36,3 +39,9 @@ pub struct DefaultShell {
3639
pub escape_arguments: Option<bool>,
3740
pub shell_arguments: Option<Vec<String>>,
3841
}
42+
43+
#[derive(Clone, Debug, Eq, PartialEq, ValueEnum)]
44+
pub enum Resource {
45+
DefaultShell,
46+
SshdConfig,
47+
}

sshdconfig/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum SshdConfigError {
1313
Json(#[from] serde_json::Error),
1414
#[error("Language: {0}")]
1515
LanguageError(#[from] tree_sitter::LanguageError),
16+
#[error("Not Implemented: {0}")]
17+
NotImplemented(String),
1618
#[error("Parser: {0}")]
1719
ParserError(String),
1820
#[error("Parser Int: {0}")]

sshdconfig/src/export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::util::invoke_sshd_config_validation;
1313
pub fn invoke_export() -> Result<(), SshdConfigError> {
1414
let sshd_config_text = invoke_sshd_config_validation()?;
1515
let sshd_config: serde_json::Map<String, serde_json::Value> = parse_text_to_map(&sshd_config_text)?;
16-
let json = serde_json::to_string_pretty(&sshd_config)?;
16+
let json = serde_json::to_string(&sshd_config)?;
1717
println!("{json}");
1818
Ok(())
1919
}

sshdconfig/src/get.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#[cfg(windows)]
55
use {
66
registry_lib::{config::{Registry, RegistryValueData}, RegistryHelper},
7-
crate::args::DefaultShell
7+
crate::args::{DefaultShell, Resource},
8+
crate::metadata::{DEFAULT_SHELL, DEFAULT_SHELL_CMD_OPTION, DEFAULT_SHELL_ESCAPE_ARGS, REGISTRY_PATH},
89
};
910

1011
use crate::error::SshdConfigError;
@@ -14,56 +15,58 @@ use crate::error::SshdConfigError;
1415
/// # Errors
1516
///
1617
/// This function will return an error if the desired settings cannot be retrieved.
17-
pub fn invoke_get() -> Result<(), SshdConfigError> {
18-
// TODO: distinguish between get commands for default shell, repeatable keywords, and sshd_config
19-
get_default_shell()?;
20-
Ok(())
18+
pub fn invoke_get(resource: &Resource) -> Result<(), SshdConfigError> {
19+
match resource {
20+
&Resource::DefaultShell => get_default_shell(),
21+
&Resource::SshdConfig => Err(SshdConfigError::NotImplemented("get not yet implemented for Microsoft.OpenSSH.SSHD/sshd_config".to_string())),
22+
}
2123
}
2224

2325
#[cfg(windows)]
2426
fn get_default_shell() -> Result<(), SshdConfigError> {
25-
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some("DefaultShell".to_string()), None)?;
27+
let registry_helper = RegistryHelper::new(REGISTRY_PATH, Some(DEFAULT_SHELL.to_string()), None)?;
2628
let default_shell: Registry = registry_helper.get()?;
2729
let mut shell = None;
2830
let mut shell_arguments = None;
31+
// default_shell is a single string consisting of the shell exe path and, optionally, any arguments
2932
if let Some(value) = default_shell.value_data {
3033
match value {
3134
RegistryValueData::String(s) => {
3235
let parts: Vec<&str> = s.split_whitespace().collect();
3336
if parts.is_empty() {
34-
return Err(SshdConfigError::InvalidInput("DefaultShell cannot be empty".to_string()));
37+
return Err(SshdConfigError::InvalidInput(format!("{} cannot be empty", DEFAULT_SHELL)));
3538
}
3639
shell = Some(parts[0].to_string());
3740
if parts.len() > 1 {
3841
shell_arguments = Some(parts[1..].iter().map(|&s| s.to_string()).collect());
3942
}
4043
}
41-
_ => return Err(SshdConfigError::InvalidInput("DefaultShell must be a string".to_string())),
44+
_ => return Err(SshdConfigError::InvalidInput(format!("{} must be a string", DEFAULT_SHELL))),
4245
}
4346
}
4447

45-
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some("DefaultShellCommandOption".to_string()), None)?;
48+
let registry_helper = RegistryHelper::new(REGISTRY_PATH, Some(DEFAULT_SHELL_CMD_OPTION.to_string()), None)?;
4649
let option: Registry = registry_helper.get()?;
4750
let mut cmd_option = None;
4851
if let Some(value) = option.value_data {
4952
match value {
5053
RegistryValueData::String(s) => cmd_option = Some(s),
51-
_ => return Err(SshdConfigError::InvalidInput("DefaultShellCommandOption must be a string".to_string())),
54+
_ => return Err(SshdConfigError::InvalidInput(format!("{} must be a string", DEFAULT_SHELL_CMD_OPTION))),
5255
}
5356
}
5457

55-
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some("DefaultShellEscapeArguments".to_string()), None)?;
58+
let registry_helper = RegistryHelper::new(REGISTRY_PATH, Some(DEFAULT_SHELL_ESCAPE_ARGS.to_string()), None)?;
5659
let escape_args: Registry = registry_helper.get()?;
5760
let mut escape_arguments = None;
5861
if let Some(value) = escape_args.value_data {
5962
if let RegistryValueData::DWord(b) = value {
6063
if b == 0 || b == 1 {
6164
escape_arguments = if b == 1 { Some(true) } else { Some(false) };
6265
} else {
63-
return Err(SshdConfigError::InvalidInput("DefaultShellEscapeArguments must be a boolean".to_string()));
66+
return Err(SshdConfigError::InvalidInput(format!("{} must be a 0 or 1", DEFAULT_SHELL_ESCAPE_ARGS)));
6467
}
6568
} else {
66-
return Err(SshdConfigError::InvalidInput("DefaultShellEscapeArguments must be a boolean".to_string()));
69+
return Err(SshdConfigError::InvalidInput(format!("{} must be a DWord", DEFAULT_SHELL_ESCAPE_ARGS)));
6770
}
6871
}
6972

@@ -74,12 +77,12 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
7477
shell_arguments
7578
};
7679

77-
let output = serde_json::to_string_pretty(&result)?;
80+
let output = serde_json::to_string(&result)?;
7881
println!("{output}");
7982
Ok(())
8083
}
8184

8285
#[cfg(not(windows))]
8386
pub fn get_default_shell() -> Result<(), SshdConfigError> {
84-
Err(SshdConfigError::InvalidInput("Windows registry operations not applicable to this platform".to_string()))
87+
Err(SshdConfigError::InvalidInput("Microsoft.OpenSSH.SSHD/Windows is only applicable to Windows".to_string()))
8588
}

sshdconfig/src/main.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use clap::{Parser};
55
use schemars::schema_for;
66

7-
use args::{Args, Command, DefaultShell};
7+
use args::{Args, Command, DefaultShell, Resource};
88
use export::invoke_export;
99
use get::invoke_get;
1010
use parser::SshdConfigParser;
@@ -24,15 +24,18 @@ fn main() {
2424

2525
let result = match &args.command {
2626
Command::Export => invoke_export(),
27-
Command::Get => invoke_get(),
27+
Command::Get { resource } => invoke_get(resource),
2828
Command::Set { input } => invoke_set(input),
29-
Command::Schema { as_global } => {
30-
let schema = if *as_global {
31-
schema_for!(DefaultShell)
32-
} else {
33-
schema_for!(SshdConfigParser)
29+
Command::Schema { resource } => {
30+
let schema = match resource {
31+
Resource::DefaultShell => {
32+
schema_for!(DefaultShell)
33+
}
34+
Resource::SshdConfig => {
35+
schema_for!(SshdConfigParser)
36+
}
3437
};
35-
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
38+
println!("{}", serde_json::to_string(&schema).unwrap());
3639
Ok(())
3740
}
3841
};

sshdconfig/src/metadata.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ pub const MULTI_ARG_KEYWORDS: [&str; 7] = [
3636
"macs",
3737
"pubkeyacceptedalgorithms"
3838
];
39+
40+
pub const REGISTRY_PATH: &str = "HKLM\\SOFTWARE\\OpenSSH";
41+
pub const DEFAULT_SHELL: &str = "DefaultShell";
42+
pub const DEFAULT_SHELL_CMD_OPTION: &str = "DefaultShellCommandOption";
43+
pub const DEFAULT_SHELL_ESCAPE_ARGS: &str = "DefaultShellEscapeArguments";

sshdconfig/src/set.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#[cfg(windows)]
55
use {
66
std::path::Path,
7-
registry_lib::{config::RegistryValueData, RegistryHelper}
7+
registry_lib::{config::RegistryValueData, RegistryHelper},
8+
crate::metadata::{DEFAULT_SHELL, DEFAULT_SHELL_CMD_OPTION, DEFAULT_SHELL_ESCAPE_ARGS, REGISTRY_PATH},
89
};
910

1011
use crate::args::DefaultShell;
@@ -44,46 +45,46 @@ fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_a
4445
shell_data = format!("{shell} {args_str}");
4546
}
4647

47-
set_registry("DefaultShell", RegistryValueData::String(shell_data))?;
48+
set_registry(DEFAULT_SHELL, RegistryValueData::String(shell_data))?;
4849
} else {
49-
remove_registry("DefaultShell")?;
50+
remove_registry(DEFAULT_SHELL)?;
5051
}
5152

5253

5354
if let Some(cmd_option) = cmd_option {
54-
set_registry("DefaultShellCommandOption", RegistryValueData::String(cmd_option.clone()))?;
55+
set_registry(DEFAULT_SHELL_CMD_OPTION, RegistryValueData::String(cmd_option.clone()))?;
5556
} else {
56-
remove_registry("DefaultShellCommandOption")?;
57+
remove_registry(DEFAULT_SHELL_CMD_OPTION)?;
5758
}
5859

5960
if let Some(escape_args) = escape_arguments {
6061
let mut escape_data = 0;
6162
if escape_args {
6263
escape_data = 1;
6364
}
64-
set_registry("DefaultShellEscapeArguments", RegistryValueData::DWord(escape_data))?;
65+
set_registry(DEFAULT_SHELL_ESCAPE_ARGS, RegistryValueData::DWord(escape_data))?;
6566
} else {
66-
remove_registry("DefaultShellEscapeArguments")?;
67+
remove_registry(DEFAULT_SHELL_ESCAPE_ARGS)?;
6768
}
6869

6970
Ok(())
7071
}
7172

7273
#[cfg(not(windows))]
7374
pub fn set_default_shell(_shell: Option<String>, _cmd_option: Option<String>, _escape_arguments: Option<bool>, _shell_arguments: Option<Vec<String>>) -> Result<(), SshdConfigError> {
74-
Err(SshdConfigError::InvalidInput("Windows registry operations not applicable to this platform".to_string()))
75+
Err(SshdConfigError::InvalidInput("Microsoft.OpenSSH.SSHD/Windows is only applicable to Windows".to_string()))
7576
}
7677

7778
#[cfg(windows)]
7879
fn set_registry(name: &str, data: RegistryValueData) -> Result<(), SshdConfigError> {
79-
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some(name.to_string()), Some(data))?;
80+
let registry_helper = RegistryHelper::new(REGISTRY_PATH, Some(name.to_string()), Some(data))?;
8081
registry_helper.set()?;
8182
Ok(())
8283
}
8384

8485
#[cfg(windows)]
8586
fn remove_registry(name: &str) -> Result<(), SshdConfigError> {
86-
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some(name.to_string()), None)?;
87+
let registry_helper = RegistryHelper::new(REGISTRY_PATH, Some(name.to_string()), None)?;
8788
registry_helper.remove()?;
8889
Ok(())
8990
}

sshdconfig/sshd.dsc.resource.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3-
"type": "Microsoft.Windows.OpenSSH/SSHD",
3+
"type": "Microsoft.OpenSSH.SSHD/Windows",
44
"description": "Manage SSH Server Configuration Global Settings",
55
"tags": [
66
"Windows"
@@ -9,7 +9,9 @@
99
"get": {
1010
"executable": "sshdconfig",
1111
"args": [
12-
"get"
12+
"get",
13+
"-r",
14+
"default-shell"
1315
]
1416
},
1517
"set": {
@@ -27,7 +29,8 @@
2729
"executable": "sshdconfig",
2830
"args": [
2931
"schema",
30-
"--as-global"
32+
"-r",
33+
"default-shell"
3134
]
3235
}
3336
}

sshdconfig/sshd_config.dsc.resource.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"command": {
1414
"executable": "sshdconfig",
1515
"args": [
16-
"schema"
16+
"schema",
17+
"-r",
18+
"sshd-config"
1719
]
1820
}
1921
}

0 commit comments

Comments
 (0)