Skip to content

Commit 54dd2ce

Browse files
committed
Allow to specify an alternate AuthorizedKeysFile inside the Match block
Signed-off-by: Hervé Werner <[email protected]>
1 parent bc6d53e commit 54dd2ce

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ override['ssh-hardening']['ssh']['server']['listen_to'] = node['ipaddress']
7676
* `['ssh-hardening']['ssh']['server']['sftp']['enable']` - `false`. Set to `true` to enable the SFTP feature of OpenSSH daemon
7777
* `['ssh-hardening']['ssh']['server']['sftp']['group']` - `sftponly`. Sets the `Match Group` option of SFTP to allow SFTP only for dedicated users
7878
* `['ssh-hardening']['ssh']['server']['sftp']['chroot']` - `/home/%u`. Sets the directory where the SFTP user should be chrooted
79+
* `['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path']` - `nil`. If not nil, full path to one or multipe space-separated authorized keys file that will be set inside the `Match Group` for SFTP-only access
7980
* `['ssh-hardening']['ssh']['server']['sftp']['password_authentication']` - `false`. Set to `true` if password authentication should be enabled
80-
* `['ssh-hardening']['ssh']['server']['authorized_keys_path']` - `nil`. If not nil, full path to an authorized keys folder is expected
81+
* `['ssh-hardening']['ssh']['server']['authorized_keys_path']` - `nil`. If not nil, full path to one or multipe space-separated authorized keys file is expected.
8182
* `['ssh-hardening']['ssh']['server']['extras']` - `{}`. Add extra configuration options, see [below](#extra-configuration-options) for details
8283
* `['ssh-hardening']['ssh']['server']['match_blocks']` - `{}`. Match configuration block, see [below](#match-configuration-options-for-sshd) for details
8384

attributes/default.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
server['password_authentication'] = false
109109
server['log_level'] = 'verbose'
110110
server['accept_env'] = ['LANG', 'LC_*', 'LANGUAGE']
111-
server['authorized_keys_path'] = nil # if not nil, full path to an authorized keys folder is expected
111+
server['authorized_keys_path'] = nil # if not nil, full path to one or multipe space-separated authorized keys file is expected
112112

113113
# extra server configuration options
114114
server['extras'] = {}
@@ -121,5 +121,6 @@
121121
server['sftp']['log_level'] = 'VERBOSE'
122122
server['sftp']['group'] = 'sftponly'
123123
server['sftp']['chroot'] = '/home/%u'
124+
server['sftp']['authorized_keys_path'] = nil # if not nil, full path to one or multipe space-separated authorized keys file is expected
124125
server['sftp']['password_authentication'] = false
125126
end

spec/recipes/server_spec.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@
707707

708708
it 'sets the SFTP chroot correctly' do
709709
expect(chef_run).to render_file('/etc/ssh/sshd_config').
710-
with_content(/^ChrootDirectory test_home_dir$/)
710+
with_content(/^[[:space:]]*ChrootDirectory test_home_dir$/)
711711
end
712712
end
713713

@@ -772,11 +772,11 @@
772772

773773
it 'does not have AuthorizedKeysFile configured' do
774774
expect(chef_run).not_to render_file('/etc/ssh/sshd_config').
775-
with_content('AuthorizedKeysFile')
775+
with_content(/^[[:space:]]*AuthorizedKeysFile/)
776776
end
777777
end
778778

779-
context 'with customized AuthorizedKeysFile' do
779+
context 'with customized global AuthorizedKeysFile' do
780780
cached(:chef_run) do
781781
ChefSpec::SoloRunner.new do |node|
782782
node.normal['ssh-hardening']['ssh']['server']['authorized_keys_path'] = '/some/authorizedkeysfile'
@@ -788,5 +788,19 @@
788788
with_content('AuthorizedKeysFile /some/authorizedkeysfile')
789789
end
790790
end
791+
792+
context 'with customized sftponly AuthorizedKeysFile' do
793+
cached(:chef_run) do
794+
ChefSpec::SoloRunner.new do |node|
795+
node.normal['ssh-hardening']['ssh']['server']['sftp']['enable'] = true
796+
node.normal['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path'] = '/some/authorizedkeysfile'
797+
end.converge(described_recipe)
798+
end
799+
800+
it 'has AuthorizedKeysFile configured' do
801+
expect(chef_run).to render_file('/etc/ssh/sshd_config').
802+
with_content('AuthorizedKeysFile /some/authorizedkeysfile')
803+
end
804+
end
791805
end
792806
end

templates/default/opensshd.conf.erb

+18-14
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,16 @@ Subsystem sftp internal-sftp -l <%= @node['ssh-hardening']['ssh']['server']['sft
222222

223223
## These lines must appear at the *end* of sshd_config
224224
Match Group <%= @node['ssh-hardening']['ssh']['server']['sftp']['group'] %>
225-
ForceCommand internal-sftp -l <%= @node['ssh-hardening']['ssh']['server']['sftp']['log_level'] %>
226-
ChrootDirectory <%= @node['ssh-hardening']['ssh']['server']['sftp']['chroot'] %>
227-
AllowTcpForwarding no
228-
AllowAgentForwarding no
229-
PasswordAuthentication <%= ((@node['ssh-hardening']['ssh']['server']['sftp']['password_authentication']) ? 'yes' : 'no' ) %>
230-
PermitRootLogin no
231-
X11Forwarding no
225+
ForceCommand internal-sftp -l <%= @node['ssh-hardening']['ssh']['server']['sftp']['log_level'] %>
226+
ChrootDirectory <%= @node['ssh-hardening']['ssh']['server']['sftp']['chroot'] %>
227+
<% if @node['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path'] %>
228+
AuthorizedKeysFile <%= @node['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path'] %>
229+
<% end %>
230+
AllowTcpForwarding no
231+
AllowAgentForwarding no
232+
PasswordAuthentication <%= ((@node['ssh-hardening']['ssh']['server']['sftp']['password_authentication']) ? 'yes' : 'no' ) %>
233+
PermitRootLogin no
234+
X11Forwarding no
232235
<% else %>
233236
# Configuration, in case SFTP is used
234237
## override default of no subsystems
@@ -237,13 +240,14 @@ X11Forwarding no
237240
#
238241
## These lines must appear at the *end* of sshd_config
239242
#Match Group sftponly
240-
#ForceCommand internal-sftp -l VERBOSE
241-
#ChrootDirectory /sftpchroot/home/%u
242-
#AllowTcpForwarding no
243-
#AllowAgentForwarding no
244-
#PasswordAuthentication no
245-
#PermitRootLogin no
246-
#X11Forwarding no
243+
#ForceCommand internal-sftp -l VERBOSE
244+
#ChrootDirectory /sftpchroot/home/%u
245+
#AuthorizedKeysFile /sftpchroot/home/%u/.ssh/authorized_keys
246+
#AllowTcpForwarding no
247+
#AllowAgentForwarding no
248+
#PasswordAuthentication no
249+
#PermitRootLogin no
250+
#X11Forwarding no
247251
<% end %>
248252

249253
<%- unless @node['ssh-hardening']['ssh']['server']['match_blocks'].empty? %>

0 commit comments

Comments
 (0)