|
1 | 1 | require 'shellwords' |
| 2 | +require 'mixlib/shellout' |
| 3 | + |
2 | 4 | include Chef::SSH::Helpers |
3 | 5 |
|
| 6 | +use_inline_resources |
| 7 | + |
| 8 | +def whyrun_supported? |
| 9 | + true |
| 10 | +end |
| 11 | + |
4 | 12 | action :add do |
5 | | - ssh_user = new_resource.user || 'root' |
6 | | - known_hosts_path = default_or_user_path(node['ssh']['known_hosts_path'], ssh_user) |
7 | | - host, port = new_resource.host.split(':') |
8 | | - # set the port to the default (22) if it wasn't already set |
9 | | - port = new_resource.port unless port |
10 | | - |
11 | | - key = new_resource.key |
12 | | - if key.nil? |
13 | | - results = `ssh-keyscan #{new_resource.hashed ? '-H ' : ''} -p #{port.to_i} #{Shellwords.escape(host)}` |
14 | | - Chef::Application.fatal! results.strip if key =~ /getaddrinfo/ |
15 | | - key = results.strip |
16 | | - end |
| 13 | + unless @current_resource.exists? |
| 14 | + directory ::File.basename(new_resource.path) do |
| 15 | + action :create |
| 16 | + owner new_resource.user if new_resource.user |
| 17 | + group new_resource.group if new_resource.group |
| 18 | + mode new_resource.user ? 00700 : 00755 |
| 19 | + end |
17 | 20 |
|
18 | | - execute "add known_host entry for #{host}" do |
19 | | - not_if "ssh-keygen -H -F #{Shellwords.escape(host)} -f #{known_hosts_path} | grep 'Host #{host} found'" |
20 | | - command "echo '#{key}' >> #{known_hosts_path}" |
21 | | - user ssh_user |
22 | | - end |
| 21 | + file new_resource.path do |
| 22 | + action :create |
| 23 | + mode new_resource.user ? 00600 : 00644 |
| 24 | + owner new_resource.user if new_resource.user |
| 25 | + group new_resource.group if new_resource.group |
| 26 | + end |
23 | 27 |
|
24 | | - log "entry_for_#{host}_exists" do |
25 | | - message "An entry for #{host} already exists in #{known_hosts_path}." |
26 | | - level :debug |
27 | | - only_if "ssh-keygen -H -F #{Shellwords.escape(host)} -f #{known_hosts_path} | grep 'Host #{host} found'" |
| 28 | + execute "add known_host entry for #{new_resource.host}" do |
| 29 | + command "echo '#{new_resource.key}' >> #{new_resource.path}" |
| 30 | + user new_resource.user if new_resource.user |
| 31 | + umask new_resource.user ? 0077 : 0022 |
| 32 | + end |
28 | 33 | end |
29 | 34 | end |
30 | 35 |
|
31 | 36 | action :remove do |
32 | | - ssh_user = new_resource.user || 'root' |
33 | | - known_hosts_path = default_or_user_path(node['ssh']['known_hosts_path'], ssh_user) |
34 | | - execute "remove known_host entry for #{new_resource.host}" do |
35 | | - command "ssh-keygen -R #{Shellwords.escape(new_resource.host)}" |
36 | | - user ssh_user |
37 | | - umask '0600' |
| 37 | + if @current_resource.exists? |
| 38 | + execute "remove known_host entry for #{new_resource.host}" do |
| 39 | + command "ssh-keygen -R #{Shellwords.escape(new_resource.host)} -f #{new_resource.path}" |
| 40 | + user new_resource.user if new_resource.user |
| 41 | + umask new_resource.user ? 0077 : 0022 |
| 42 | + end |
38 | 43 | end |
39 | 44 | end |
| 45 | + |
| 46 | +def initialize(new_resource, run_context) |
| 47 | + super(new_resource, run_context) |
| 48 | + |
| 49 | + new_resource.path default_or_user_path(new_resource.user) unless new_resource.path |
| 50 | + if new_resource.host.match(/:/) |
| 51 | + host, port = new_resource.host.split(':') |
| 52 | + new_resource.host host |
| 53 | + new_resource.port port unless new_resource.port |
| 54 | + end |
| 55 | + |
| 56 | + new_resource.port 22 unless new_resource.port |
| 57 | + |
| 58 | + new_resource.group user_group(new_resource.user) unless new_resource.group |
| 59 | + |
| 60 | + load_current_resource |
| 61 | + load_key_if_needed |
| 62 | +end |
| 63 | + |
| 64 | +def load_key_if_needed |
| 65 | + return if @current_resource.exists? |
| 66 | + return if new_resource.key |
| 67 | + return if new_resource.action.is_a?(Array) ? new_resource.action.include?(:remove) : new_resource.action == :remove |
| 68 | + |
| 69 | + keyscan = Mixlib::ShellOut.new( |
| 70 | + "ssh-keyscan #{new_resource.hashed ? '-H ' : ''} -p #{new_resource.port.to_i} #{Shellwords.escape(new_resource.host)}" |
| 71 | + ) |
| 72 | + keyscan.run_command |
| 73 | + keyscan.error! # this will raise an error if the command failed for any reason. |
| 74 | + new_resource.key keyscan.stdout.strip |
| 75 | +end |
| 76 | + |
| 77 | +def load_current_resource |
| 78 | + search = Mixlib::ShellOut.new( |
| 79 | + "ssh-keygen -H -F #{Shellwords.escape(new_resource.host)} "\ |
| 80 | + "-f #{new_resource.path} | grep 'Host #{new_resource.host} found'" |
| 81 | + ) |
| 82 | + search.run_command |
| 83 | + @current_resource = Chef::Resource::SshKnownHosts.new(@new_resource.name) |
| 84 | + @current_resource.exists = search.status.success? |
| 85 | +end |
| 86 | + |
| 87 | +protected |
| 88 | + |
| 89 | +def default_or_user_path(username = nil) |
| 90 | + username ? "#{user_dir(username)}/.ssh/known_hosts" : node['ssh']['known_hosts_path'] |
| 91 | +end |
0 commit comments