Skip to content

Commit e179e12

Browse files
authored
Merge pull request #1327 from tgauth/add-sshdconfig-match-to-export
Add sshdconfig match to export
2 parents 8503b80 + 78e6cad commit e179e12

File tree

8 files changed

+377
-17
lines changed

8 files changed

+377
-17
lines changed

dsc/tests/dsc_sshdconfig.tests.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ BeforeDiscovery {
55
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
66
$principal = [System.Security.Principal.WindowsPrincipal]::new($identity)
77
$isElevated = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
8-
$sshdExists = ($null -ne (Get-Command sshd -CommandType Application -ErrorAction Ignore))
9-
$skipTest = !$isElevated -or !$sshdExists
108
}
9+
else {
10+
$isElevated = (id -u) -eq 0
11+
}
12+
13+
$sshdExists = ($null -ne (Get-Command sshd -CommandType Application -ErrorAction Ignore))
14+
$skipTest = !$isElevated -or !$sshdExists
1115
}
1216

13-
Describe 'SSHDConfig resource tests' -Skip:(!$IsWindows -or $skipTest) {
17+
Describe 'SSHDConfig resource tests' -Skip:($skipTest) {
1418
BeforeAll {
1519
# set a non-default value in a temporary sshd_config file
1620
"LogLevel Debug3`nPasswordAuthentication no" | Set-Content -Path $TestDrive/test_sshd_config

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,26 @@ const PREC = {
1111
export default grammar({
1212
name: 'ssh_server_config',
1313

14-
extras: $ => [' ', '\t', '\r'],
14+
extras: $ => [' ', '\t', '\r', $.comment],
1515

1616
rules: {
17-
server_config: $ => seq(repeat(choice($._empty_line, $.comment, $.keyword)), repeat($.match)),
17+
server_config: $ => seq(repeat(choice($._new_line, $.keyword)), repeat($.match)),
1818

19-
// check for an empty line that is just a /n character
20-
_empty_line: $ => '\n',
21-
comment: $ => /#.*\n/,
19+
_new_line: $ => '\n',
20+
comment: $ => /#.*/,
2221

2322
keyword: $ => seq(
2423
field('keyword', $.alphanumeric),
2524
choice(seq(/[ \t]/, optional('=')), '='),
2625
optional(field('operator', $.operator)),
2726
field('arguments', $.arguments),
28-
"\n"
27+
$._new_line
2928
),
3029

3130
match: $ => seq(
3231
token(prec(PREC.MATCH, /match/i)),
33-
seq(repeat1($.criteria), $._empty_line),
34-
repeat1(choice($.comment, $.keyword)),
32+
seq(repeat1($.criteria), $._new_line),
33+
repeat1(choice($._new_line, $.keyword))
3534
),
3635

3736
criteria: $ => seq(
@@ -48,7 +47,7 @@ export default grammar({
4847
boolean: $ => choice('yes', 'no'),
4948
number: $ => /\d+/,
5049
operator: $ => token(prec(PREC.OPERATOR, /[-+\^]/)),
51-
string: $ => /[^\r\n,"'\s]+/, /* cannot contain spaces */
50+
string: $ => /[^\n\r\s,"'#]+/, /* cannot contain spaces */
5251

5352
_quotedString: $ => /[^\r\n,"']+/, /* can contain spaces */
5453
_doublequotedString: $ => seq('"', alias($._quotedString, $.string), repeat(seq(',', alias($._quotedString, $.string))), '"'),

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

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,199 @@ passwordauthentication no
429429
(alphanumeric)
430430
(arguments
431431
(boolean)))))
432+
====
433+
parse comments between keywords
434+
====
435+
port 2222 # use non-default port
436+
437+
---
438+
(server_config
439+
(keyword
440+
(alphanumeric)
441+
(arguments
442+
(number))
443+
(comment)))
444+
====
445+
parse comments in match blocks
446+
====
447+
port 2222
448+
passwordauthentication no
449+
450+
match User testuser # comment about criteria
451+
PasswordAuthentication yes
452+
AllowTcpForwarding no
453+
# comment between match blocks
454+
match Address 192.168.1.0/24
455+
X11Forwarding yes
456+
MaxAuthTries 3
457+
458+
---
459+
(server_config
460+
(keyword
461+
(alphanumeric)
462+
(arguments
463+
(number)))
464+
(keyword
465+
(alphanumeric)
466+
(arguments
467+
(boolean)))
468+
(match
469+
(criteria
470+
(alpha)
471+
(argument
472+
(string)))
473+
(comment)
474+
(keyword
475+
(alphanumeric)
476+
(arguments
477+
(boolean)))
478+
(keyword
479+
(alphanumeric)
480+
(arguments
481+
(boolean)))
482+
(comment))
483+
(match
484+
(criteria
485+
(alpha)
486+
(argument
487+
(string)))
488+
(keyword
489+
(alphanumeric)
490+
(arguments
491+
(boolean)))
492+
(keyword
493+
(alphanumeric)
494+
(arguments
495+
(number)))))
496+
====
497+
parse newlines between match blocks
498+
====
499+
port 2222
500+
passwordauthentication no
501+
502+
match User testuser
503+
PasswordAuthentication yes
504+
AllowTcpForwarding no
505+
506+
match Address 192.168.1.0/24
507+
X11Forwarding yes
508+
MaxAuthTries 3
509+
510+
---
511+
(server_config
512+
(keyword
513+
(alphanumeric)
514+
(arguments
515+
(number)))
516+
(keyword
517+
(alphanumeric)
518+
(arguments
519+
(boolean)))
520+
(match
521+
(criteria
522+
(alpha)
523+
(argument
524+
(string)))
525+
(keyword
526+
(alphanumeric)
527+
(arguments
528+
(boolean)))
529+
(keyword
530+
(alphanumeric)
531+
(arguments
532+
(boolean))))
533+
(match
534+
(criteria
535+
(alpha)
536+
(argument
537+
(string)))
538+
(keyword
539+
(alphanumeric)
540+
(arguments
541+
(boolean)))
542+
(keyword
543+
(alphanumeric)
544+
(arguments
545+
(number)))))
546+
====
547+
parse comment within match block
548+
====
549+
match user developer
550+
# Enable password authentication for developers - comment ignored
551+
passwordauthentication yes
552+
553+
---
554+
(server_config
555+
(match
556+
(criteria
557+
(alpha)
558+
(argument
559+
(string)))
560+
(comment)
561+
(keyword
562+
(alphanumeric)
563+
(arguments
564+
(boolean)))))
565+
====
566+
parse newlines between match blocks
567+
====
568+
match User testuser
569+
PasswordAuthentication yes
570+
AllowTcpForwarding no
571+
# comment line 1
572+
# comment line 2
573+
574+
575+
match Address 192.168.1.0/24
576+
X11Forwarding yes
577+
MaxAuthTries 3
578+
579+
---
580+
(server_config
581+
(match
582+
(criteria
583+
(alpha)
584+
(argument
585+
(string)))
586+
(keyword
587+
(alphanumeric)
588+
(arguments
589+
(boolean)))
590+
(keyword
591+
(alphanumeric)
592+
(arguments
593+
(boolean)))
594+
(comment)
595+
(comment))
596+
(match
597+
(criteria
598+
(alpha)
599+
(argument
600+
(string)))
601+
(keyword
602+
(alphanumeric)
603+
(arguments
604+
(boolean)))
605+
(keyword
606+
(alphanumeric)
607+
(arguments
608+
(number)))))
609+
====
610+
parse multiple comments and multiple lines
611+
====
612+
# comment line 1
613+
# comment line 2
614+
# comment line 3
615+
616+
617+
passwordauthentication yes
618+
619+
---
620+
(server_config
621+
(comment)
622+
(comment)
623+
(comment)
624+
(keyword
625+
(alphanumeric)
626+
(arguments
627+
(boolean))))

resources/sshdconfig/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ defaultShellCmdOptionMustBeString = "cmdOption must be a string"
2525
defaultShellEscapeArgsMustBe0Or1 = "'%{input}' must be a 0 or 1"
2626
defaultShellEscapeArgsMustBeDWord = "escapeArguments must be a DWord"
2727
defaultShellMustBeString = "shell must be a string"
28+
includeWarning = "Include directive found in sshd_config. This resource uses 'sshd -T' to process the overall configuration state, which merges all included files but does not return the Include directive itself"
2829
traceInput = "Get input:"
2930
windowsOnly = "Microsoft.OpenSSH.SSHD/Windows is only applicable to Windows"
3031

resources/sshdconfig/src/get.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use {
1010

1111
use rust_i18n::t;
1212
use serde_json::{Map, Value};
13-
use tracing::{debug, trace};
13+
use tracing::{debug, trace, warn};
1414

1515
use crate::args::Setting;
1616
use crate::error::SshdConfigError;
@@ -121,10 +121,15 @@ pub fn get_sshd_settings(cmd_info: &CommandInfo, is_get: bool) -> Result<Map<Str
121121
let mut defaults = extract_sshd_defaults()?;
122122

123123
// remove any explicit keys from default settings list
124-
for key in explicit_settings.keys() {
124+
for (key, value) in &explicit_settings {
125125
if defaults.contains_key(key) {
126126
defaults.remove(key);
127127
}
128+
if key == "include" {
129+
warn!("{}", t!("get.includeWarning").to_string());
130+
} else if key == "match" {
131+
result.insert(key.clone(), value.clone());
132+
}
128133
}
129134

130135
if cmd_info.include_defaults {

resources/sshdconfig/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ pub fn build_command_info(input: Option<&String>, is_get: bool) -> Result<Comman
280280
});
281281
if is_get && !sshd_config.is_empty() {
282282
warn!("{}", t!("util.getIgnoresInputFilters"));
283+
sshd_config.clear();
283284
}
284285
return Ok(CommandInfo {
285286
clobber,

0 commit comments

Comments
 (0)