Skip to content

Commit ccf3628

Browse files
authored
Merge pull request #1284 from tgauth/expand-sshdconfig-parser
Expand sshdconfig parser
2 parents 4573b3e + 31dc767 commit ccf3628

File tree

7 files changed

+405
-80
lines changed

7 files changed

+405
-80
lines changed

dsc/tests/dsc_sshdconfig.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ resources:
4040
$out.resources.count | Should -Be 1
4141
$out.resources[0].properties | Should -Not -BeNullOrEmpty
4242
$out.resources[0].properties.port | Should -BeNullOrEmpty
43-
$out.resources[0].properties.passwordAuthentication | Should -Be 'no'
43+
$out.resources[0].properties.passwordAuthentication | Should -Be $false
4444
$out.resources[0].properties._inheritedDefaults | Should -BeNullOrEmpty
4545
} else {
4646
$out.results.count | Should -Be 1
4747
$out.results.result.actualState | Should -Not -BeNullOrEmpty
4848
$out.results.result.actualState.port[0] | Should -Be 22
49-
$out.results.result.actualState.passwordAuthentication | Should -Be 'no'
49+
$out.results.result.actualState.passwordAuthentication | Should -Be $false
5050
$out.results.result.actualState._inheritedDefaults | Should -Contain 'port'
5151
}
5252
}
@@ -69,7 +69,7 @@ resources:
6969
$LASTEXITCODE | Should -Be 0
7070
$out.resources.count | Should -Be 1
7171
($out.resources[0].properties.psobject.properties | Measure-Object).count | Should -Be 1
72-
$out.resources[0].properties.passwordAuthentication | Should -Be 'no'
72+
$out.resources[0].properties.passwordAuthentication | Should -Be $false
7373
}
7474

7575
It '<command> with _includeDefaults specified works' -TestCases @(
@@ -128,7 +128,7 @@ resources:
128128
$out.results.count | Should -Be 1
129129
$out.results.result.actualState | Should -Not -BeNullOrEmpty
130130
$out.results.result.actualState.port | Should -Be 22
131-
$out.results.result.actualState.passwordAuthentication | Should -Be 'yes'
131+
$out.results.result.actualState.passwordAuthentication | Should -Be $true
132132
$out.results.result.actualState._inheritedDefaults | Should -Not -Contain 'port'
133133
}
134134
}

grammars/tree-sitter-ssh-server-config/grammar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ module.exports = grammar({
3535
),
3636

3737
criteria: $ => seq(
38-
field('criteria', $.alpha),
38+
field('keyword', $.alpha),
3939
choice(seq(/[ \t]/, optional('=')), '='),
40-
field('argument', $._argument)
40+
field('argument', alias($._argument, $.argument))
4141
),
4242

4343
_argument: $ => choice($.boolean, $.number, $.string, $._commaSeparatedString, $._doublequotedString, $._singlequotedString),

grammars/tree-sitter-ssh-server-config/test/corpus/valid_expressions.txt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ authorizedkeysfile "path to authorized keys file"
3131
(match
3232
(criteria
3333
(alpha)
34-
(string))
34+
(argument
35+
(string)))
3536
(keyword
3637
(alphanumeric)
3738
(arguments
@@ -53,7 +54,8 @@ authorizedkeysfile "path to authorized keys file"
5354
(match
5455
(criteria
5556
(alpha)
56-
(string))
57+
(argument
58+
(string)))
5759
(keyword
5860
(alphanumeric)
5961
(arguments
@@ -229,15 +231,17 @@ passwordauthentication yes
229231
(match
230232
(criteria
231233
(alpha)
232-
(string))
234+
(argument
235+
(string)))
233236
(keyword
234237
(alphanumeric)
235238
(arguments
236239
(boolean))))
237240
(match
238241
(criteria
239242
(alpha)
240-
(string))
243+
(argument
244+
(string)))
241245
(keyword
242246
(alphanumeric)
243247
(arguments
@@ -307,7 +311,8 @@ passwordauthentication yes
307311
(match
308312
(criteria
309313
(alpha)
310-
(string))
314+
(argument
315+
(string)))
311316
(keyword
312317
(alphanumeric)
313318
(arguments
@@ -320,7 +325,8 @@ passwordauthentication yes
320325
(match
321326
(criteria
322327
(alpha)
323-
(string))
328+
(argument
329+
(string)))
324330
(keyword
325331
(alphanumeric)
326332
(arguments
@@ -412,11 +418,13 @@ passwordauthentication no
412418
(match
413419
(criteria
414420
(alpha)
421+
(argument
415422
(string)
416-
(string))
423+
(string)))
417424
(criteria
418425
(alpha)
419-
(string))
426+
(argument
427+
(string)))
420428
(keyword
421429
(alphanumeric)
422430
(arguments

resources/sshdconfig/locales/en-us.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ parser = "Parser"
1717
parseInt = "Parse Integer"
1818
persist = "Persist"
1919
registry = "Registry"
20+
stringUtf8 = "String UTF-8"
2021

2122
[get]
2223
debugSetting = "Get setting:"
@@ -35,17 +36,18 @@ set = "Set command: '%{input}'"
3536
[parser]
3637
failedToParse = "failed to parse: '%{input}'"
3738
failedToParseAsArray = "value is not an array"
38-
failedToParseChildNode = "failed to parse child node: '%{input}'"
3939
failedToParseNode = "failed to parse '%{input}'"
4040
failedToParseRoot = "failed to parse root: '%{input}'"
4141
invalidConfig = "invalid config: '%{input}'"
4242
invalidMultiArgNode = "multi-arg node '%{input}' is not valid"
43-
invalidValue = "operator is an invalid value for node"
4443
keyNotFound = "key '%{key}' not found"
4544
keyNotRepeatable = "key '%{key}' is not repeatable"
46-
keywordDebug = "Parsing keyword: '%{text}'"
47-
missingValueInChildNode = "missing value in child node: '%{input}'"
45+
missingCriteriaInMatch = "missing criteria field in match block: '%{input}'"
4846
missingKeyInChildNode = "missing key in child node: '%{input}'"
47+
missingKeyInCriteria = "missing key in criteria node: '%{input}'"
48+
missingValueInCriteria = "missing value in criteria node: '%{input}'"
49+
missingValueInChildNode = "missing value in child node: '%{input}'"
50+
noArgumentsFound = "no arguments found in node: '%{input}'"
4951
valueDebug = "Parsed argument value:"
5052
unknownNode = "unknown node: '%{kind}'"
5153
unknownNodeType = "unknown node type: '%{node}'"

resources/sshdconfig/src/error.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use thiserror::Error;
99
pub enum SshdConfigError {
1010
#[error("{t}: {0}", t = t!("error.command"))]
1111
CommandError(String),
12+
#[error("{t}: {0}", t = t!("error.envVar"))]
13+
EnvVarError(#[from] std::env::VarError),
1214
#[error("{t}: {0}", t = t!("error.fmt"))]
1315
FmtError(#[from] std::fmt::Error),
1416
#[error("{t}: {0}", t = t!("error.invalidInput"))]
@@ -28,6 +30,6 @@ pub enum SshdConfigError {
2830
#[cfg(windows)]
2931
#[error("{t}: {0}", t = t!("error.registry"))]
3032
RegistryError(#[from] dsc_lib_registry::error::RegistryError),
31-
#[error("{t}: {0}", t = t!("error.envVar"))]
32-
EnvVarError(#[from] std::env::VarError),
33+
#[error("{t}: {0}", t = t!("error.stringUtf8"))]
34+
StringUtf8Error(#[from] std::str::Utf8Error),
3335
}

resources/sshdconfig/src/metadata.rs

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

4-
// keywords that can have multiple arguments 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; 17] = [
4+
// note that it is possible for a keyword to be in one, neither, or both of the multi-arg and repeatable lists below.
5+
6+
// keywords that can have multiple comma-separated arguments per line and should be represented as arrays.
7+
pub const MULTI_ARG_KEYWORDS: [&str; 22] = [
8+
"acceptenv",
9+
"allowgroups",
10+
"allowusers",
711
"authenticationmethods",
812
"authorizedkeysfile",
913
"casignaturealgorithms",
1014
"channeltimeout",
1115
"ciphers",
16+
"denygroups",
17+
"denyusers",
1218
"hostbasedacceptedalgorithms",
1319
"hostkeyalgorithms",
1420
"ipqos",
@@ -24,7 +30,6 @@ pub const MULTI_ARG_KEYWORDS: [&str; 17] = [
2430
];
2531

2632
// keywords that can be repeated over multiple lines and should be represented as arrays.
27-
// note that some keywords can be both multi-arg and repeatable, in which case they only need to be listed here
2833
pub const REPEATABLE_KEYWORDS: [&str; 12] = [
2934
"acceptenv",
3035
"allowgroups",

0 commit comments

Comments
 (0)