]> git.openstreetmap.org Git - chef.git/blob - cookbooks/openssh/recipes/default.rb
Use systemd to manage sshd on Ubuntu 15.10
[chef.git] / cookbooks / openssh / recipes / default.rb
1 #
2 # Cookbook Name:: openssh
3 # Recipe:: default
4 #
5 # Copyright 2010, OpenStreetMap Foundation.
6 # Copyright 2008-2009, Opscode, Inc.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 include_recipe "networking"
22
23 package "openssh-client"
24 package "openssh-server"
25
26 service "ssh" do
27   if node[:lsb][:release].to_f >= 15.10
28     provider Chef::Provider::Service::Systemd
29   elsif node[:lsb][:release].to_f >= 14.04
30     provider Chef::Provider::Service::Upstart
31   end
32   action [:enable, :start]
33   supports :status => true, :restart => true, :reload => true
34 end
35
36 hosts = search(:node, "networking:interfaces").sort_by { |n| n[:hostname] }.collect do |node|
37   names = [node[:hostname]]
38
39   node.interfaces(:role => :external).each do |interface|
40     names |= ["#{node[:hostname]}.openstreetmap.org"]
41     names |= ["#{node[:hostname]}.#{interface[:zone]}.openstreetmap.org"]
42   end
43
44   unless node.interfaces(:role => :internal).empty?
45     names |= ["#{node[:hostname]}.#{node[:networking][:roles][:external][:zone]}.openstreetmap.org"]
46   end
47
48   keys = {
49     "rsa" => node[:keys][:ssh][:host_rsa_public],            # ~FC039
50     "dsa" => node[:keys][:ssh][:host_dsa_public]             # ~FC039
51   }
52
53   if node[:keys][:ssh][:host_ecdsa_public]                   # ~FC039
54     ecdsa_type = node[:keys][:ssh][:host_ecdsa_type]         # ~FC039
55
56     keys[ecdsa_type] = node[:keys][:ssh][:host_ecdsa_public] # ~FC039
57   end
58
59   Hash[
60     :names => names.sort,
61     :addresses => node.ipaddresses.sort,
62     :keys => keys
63   ]
64 end
65
66 template "/etc/ssh/ssh_config" do
67   source "ssh_config.erb"
68   mode 0644
69   owner "root"
70   group "root"
71 end
72
73 template "/etc/ssh/ssh_known_hosts" do
74   source "ssh_known_hosts.erb"
75   mode 0444
76   owner "root"
77   group "root"
78   backup false
79   variables(
80     :hosts => hosts
81   )
82 end
83
84 firewall_rule "accept-ssh" do
85   action :accept
86   source "net"
87   dest "fw"
88   proto "tcp:syn"
89   dest_ports "ssh"
90 end