Skip to content

Commit f5525f7

Browse files
Support of custom match configuration blocks
They are sometimes useful when you need to have user/group rectrictions or want to override some global configuration options Signed-off-by: Artem Sidorenko <[email protected]>
1 parent c5d1b50 commit f5525f7

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ override['ssh-hardening']['ssh']['server']['listen_to'] = node['ipaddress']
7979
* `['ssh-hardening']['ssh']['server']['sftp']['password_authentication']` - `false`. Set to `true` if password authentication should be enabled
8080
* `['ssh-hardening']['ssh']['server']['authorized_keys_path']` - `nil`. If not nil, full path to an authorized keys folder is expected
8181
* `['ssh-hardening']['ssh']['server']['extras']` - `{}`. Add extra configuration options, see [below](#extra-configuration-options) for details
82+
* `['ssh-hardening']['ssh']['server']['match_blocks']` - `{}`. Match configuration block, see [below](#match-configuration-options-for-sshd) for details
8283

8384
## Usage
8485

@@ -145,6 +146,24 @@ default['ssh-hardening']['ssh']['client']['extras'].tap do |extra|
145146
end
146147
```
147148

149+
## Match Configuration Options for sshd
150+
Match blocks have to be placed by the end of sshd_config. This can be achieved by using the `match_blocks` attribute tree:
151+
152+
```
153+
default['ssh-hardening']['ssh']['server']['match_blocks'].tap do |match|
154+
match['User root'] = <<~ROOT
155+
AuthorizedKeysFile .ssh/authorized_keys
156+
ROOT
157+
match['User git'] = <<~GIT
158+
Banner none
159+
AuthorizedKeysCommand /bin/false
160+
AuthorizedKeysFile .ssh/authorized_keys
161+
GSSAPIAuthentication no
162+
PasswordAuthentication no
163+
GIT
164+
end
165+
```
166+
148167
## Local Testing
149168

150169
Please install [chef-dk](https://downloads.chef.io/chefdk), [VirtualBox](https://www.virtualbox.org/) or VMware Workstation and [Vagrant](https://www.vagrantup.com/).

attributes/default.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
# extra server configuration options
114114
server['extras'] = {}
115115

116+
# server match configuration block
117+
server['match_blocks'] = {}
118+
116119
# sshd sftp options
117120
server['sftp']['enable'] = false
118121
server['sftp']['log_level'] = 'VERBOSE'

spec/recipes/server_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,35 @@
501501
end
502502
end
503503

504+
describe 'match configuration blocks' do
505+
context 'without custom extra config value' do
506+
cached(:chef_run) do
507+
ChefSpec::SoloRunner.new.converge(described_recipe)
508+
end
509+
510+
it 'does not have any match config blocks' do
511+
expect(chef_run).to render_file('/etc/ssh/sshd_config')
512+
expect(chef_run).not_to render_file('/etc/ssh/sshd_config').
513+
with_content(/^# Match Configuration Blocks/)
514+
end
515+
end
516+
517+
context 'with custom match config block value' do
518+
cached(:chef_run) do
519+
ChefSpec::SoloRunner.new do |node|
520+
node.normal['ssh-hardening']['ssh']['server']['match_blocks']['User root'] = <<~ROOT
521+
AuthorizedKeysFile .ssh/authorized_keys
522+
ROOT
523+
end.converge(described_recipe)
524+
end
525+
526+
it 'uses the match config blocks' do
527+
expect(chef_run).to render_file('/etc/ssh/sshd_config').with_content(/^# Match Configuration Blocks/)
528+
expect(chef_run).to render_file('/etc/ssh/sshd_config').with_content(/^Match User root/)
529+
end
530+
end
531+
end
532+
504533
it 'disables the challenge response authentication' do
505534
expect(chef_run).to render_file('/etc/ssh/sshd_config').
506535
with_content(/ChallengeResponseAuthentication no/)

templates/default/opensshd.conf.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ X11Forwarding no
245245
#PermitRootLogin no
246246
#X11Forwarding no
247247
<% end %>
248+
249+
<%- unless @node['ssh-hardening']['ssh']['server']['match_blocks'].empty? %>
250+
# Match Configuration Blocks
251+
<%- @node['ssh-hardening']['ssh']['server']['match_blocks'].each do |key, value| %>
252+
Match <%= key %>
253+
<%= value.split("\n").join("\n ") %>
254+
<% end -%>
255+
<% end -%>

0 commit comments

Comments
 (0)