Skip to content

Allow to specify an alternate AuthorizedKeysFile inside the Match block #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ override['ssh-hardening']['ssh']['server']['listen_to'] = node['ipaddress']
* `['ssh-hardening']['ssh']['server']['sftp']['enable']` - `false`. Set to `true` to enable the SFTP feature of OpenSSH daemon
* `['ssh-hardening']['ssh']['server']['sftp']['group']` - `sftponly`. Sets the `Match Group` option of SFTP to allow SFTP only for dedicated users
* `['ssh-hardening']['ssh']['server']['sftp']['chroot']` - `/home/%u`. Sets the directory where the SFTP user should be chrooted
* `['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
* `['ssh-hardening']['ssh']['server']['sftp']['password_authentication']` - `false`. Set to `true` if password authentication should be enabled
* `['ssh-hardening']['ssh']['server']['authorized_keys_path']` - `nil`. If not nil, full path to an authorized keys folder is expected
* `['ssh-hardening']['ssh']['server']['authorized_keys_path']` - `nil`. If not nil, full path to one or multipe space-separated authorized keys file is expected.
* `['ssh-hardening']['ssh']['server']['extras']` - `{}`. Add extra configuration options, see [below](#extra-configuration-options) for details
* `['ssh-hardening']['ssh']['server']['match_blocks']` - `{}`. Match configuration block, see [below](#match-configuration-options-for-sshd) for details

Expand Down
3 changes: 2 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
server['password_authentication'] = false
server['log_level'] = 'verbose'
server['accept_env'] = ['LANG', 'LC_*', 'LANGUAGE']
server['authorized_keys_path'] = nil # if not nil, full path to an authorized keys folder is expected
server['authorized_keys_path'] = nil # if not nil, full path to one or multipe space-separated authorized keys file is expected

# extra server configuration options
server['extras'] = {}
Expand All @@ -121,5 +121,6 @@
server['sftp']['log_level'] = 'VERBOSE'
server['sftp']['group'] = 'sftponly'
server['sftp']['chroot'] = '/home/%u'
server['sftp']['authorized_keys_path'] = nil # if not nil, full path to one or multipe space-separated authorized keys file is expected
server['sftp']['password_authentication'] = false
end
20 changes: 17 additions & 3 deletions spec/recipes/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@

it 'sets the SFTP chroot correctly' do
expect(chef_run).to render_file('/etc/ssh/sshd_config').
with_content(/^ChrootDirectory test_home_dir$/)
with_content(/^[[:space:]]*ChrootDirectory test_home_dir$/)
end
end

Expand Down Expand Up @@ -772,11 +772,11 @@

it 'does not have AuthorizedKeysFile configured' do
expect(chef_run).not_to render_file('/etc/ssh/sshd_config').
with_content('AuthorizedKeysFile')
with_content(/^[[:space:]]*AuthorizedKeysFile/)
end
end

context 'with customized AuthorizedKeysFile' do
context 'with customized global AuthorizedKeysFile' do
cached(:chef_run) do
ChefSpec::SoloRunner.new do |node|
node.normal['ssh-hardening']['ssh']['server']['authorized_keys_path'] = '/some/authorizedkeysfile'
Expand All @@ -788,5 +788,19 @@
with_content('AuthorizedKeysFile /some/authorizedkeysfile')
end
end

context 'with customized sftponly AuthorizedKeysFile' do
cached(:chef_run) do
ChefSpec::SoloRunner.new do |node|
node.normal['ssh-hardening']['ssh']['server']['sftp']['enable'] = true
node.normal['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path'] = '/some/authorizedkeysfile'
end.converge(described_recipe)
end

it 'has AuthorizedKeysFile configured' do
expect(chef_run).to render_file('/etc/ssh/sshd_config').
with_content('AuthorizedKeysFile /some/authorizedkeysfile')
end
end
end
end
32 changes: 18 additions & 14 deletions templates/default/opensshd.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,16 @@ Subsystem sftp internal-sftp -l <%= @node['ssh-hardening']['ssh']['server']['sft

## These lines must appear at the *end* of sshd_config
Match Group <%= @node['ssh-hardening']['ssh']['server']['sftp']['group'] %>
ForceCommand internal-sftp -l <%= @node['ssh-hardening']['ssh']['server']['sftp']['log_level'] %>
ChrootDirectory <%= @node['ssh-hardening']['ssh']['server']['sftp']['chroot'] %>
AllowTcpForwarding no
AllowAgentForwarding no
PasswordAuthentication <%= ((@node['ssh-hardening']['ssh']['server']['sftp']['password_authentication']) ? 'yes' : 'no' ) %>
PermitRootLogin no
X11Forwarding no
ForceCommand internal-sftp -l <%= @node['ssh-hardening']['ssh']['server']['sftp']['log_level'] %>
ChrootDirectory <%= @node['ssh-hardening']['ssh']['server']['sftp']['chroot'] %>
<% if @node['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path'] %>
AuthorizedKeysFile <%= @node['ssh-hardening']['ssh']['server']['sftp']['authorized_keys_path'] %>
<% end %>
AllowTcpForwarding no
AllowAgentForwarding no
PasswordAuthentication <%= ((@node['ssh-hardening']['ssh']['server']['sftp']['password_authentication']) ? 'yes' : 'no' ) %>
PermitRootLogin no
X11Forwarding no
<% else %>
# Configuration, in case SFTP is used
## override default of no subsystems
Expand All @@ -237,13 +240,14 @@ X11Forwarding no
#
## These lines must appear at the *end* of sshd_config
#Match Group sftponly
#ForceCommand internal-sftp -l VERBOSE
#ChrootDirectory /sftpchroot/home/%u
#AllowTcpForwarding no
#AllowAgentForwarding no
#PasswordAuthentication no
#PermitRootLogin no
#X11Forwarding no
#ForceCommand internal-sftp -l VERBOSE
#ChrootDirectory /sftpchroot/home/%u
#AuthorizedKeysFile /sftpchroot/home/%u/.ssh/authorized_keys
#AllowTcpForwarding no
#AllowAgentForwarding no
#PasswordAuthentication no
#PermitRootLogin no
#X11Forwarding no
<% end %>

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