|
| 1 | +include Chef::SSH::Helpers |
| 2 | + |
| 3 | +use_inline_resources |
| 4 | + |
| 5 | +def whyrun_supported? |
| 6 | + true |
| 7 | +end |
| 8 | + |
| 9 | +action :add do |
| 10 | + validate_options(new_resource.options, "Resource #{new_resource.name}") if new_resource.options |
| 11 | + validate_type(new_resource.type, "Resource #{new_resource.name}") |
| 12 | + if @current_resource.exists? |
| 13 | + action_update |
| 14 | + else |
| 15 | + @lines << { :options => new_resource.options, |
| 16 | + :type => new_resource.type, |
| 17 | + :key => new_resource.key, |
| 18 | + :comment => new_resource.comment } |
| 19 | + update_file |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +action :update do |
| 24 | + return unless @current_resource.exists? |
| 25 | + current = @lines.find { |line| line[:key] == new_resource.key } |
| 26 | + current[:options] = new_resource.options |
| 27 | + current[:type] = new_resource.type |
| 28 | + current[:comment] = new_resource.comment |
| 29 | + update_file |
| 30 | +end |
| 31 | + |
| 32 | +action :remove do |
| 33 | + return unless @current_resource.exists? |
| 34 | + @lines.reject! { |line| line[:key] == new_resource.key } |
| 35 | + update_file |
| 36 | +end |
| 37 | + |
| 38 | +def update_file |
| 39 | + directory ::File.dirname(@path) do |
| 40 | + action :create |
| 41 | + owner new_resource.user |
| 42 | + mode 00700 |
| 43 | + end |
| 44 | + |
| 45 | + file @path do |
| 46 | + action :create |
| 47 | + mode 00600 |
| 48 | + owner new_resource.user |
| 49 | + content format_lines |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +def format_lines |
| 54 | + @lines.collect do |line| |
| 55 | + joined = '' |
| 56 | + if line[:options] |
| 57 | + joined << line[:options].collect do |key, value| |
| 58 | + if value.nil? || value.empty? |
| 59 | + key.to_s |
| 60 | + elsif value.include?(' ') && !value.include?('"') |
| 61 | + "#{key}=\"#{value}\"" |
| 62 | + else |
| 63 | + "#{key}=#{value}" |
| 64 | + end |
| 65 | + end.join(',') |
| 66 | + joined << ' ' |
| 67 | + end |
| 68 | + joined << line[:type] << ' ' << line[:key] |
| 69 | + line[:comment] && (joined << ' ' << line[:comment]) |
| 70 | + joined |
| 71 | + end.join("\n") + "\n" |
| 72 | +end |
| 73 | + |
| 74 | +def initialize(new_resource, run_context) |
| 75 | + super(new_resource, run_context) |
| 76 | + |
| 77 | + @path = ::File.join(user_dir(new_resource.user), '.ssh', 'authorized_keys') |
| 78 | + |
| 79 | + load_current_resource |
| 80 | +end |
| 81 | + |
| 82 | +def load_current_resource |
| 83 | + @lines = ::File.exist?(@path) ? parse(::IO.readlines(@path)) : [] |
| 84 | + |
| 85 | + current_line = @lines.find { |line| line[:key] == @new_resource.key } |
| 86 | + @current_resource = Chef::Resource::SshKnownHosts.new(@new_resource.name) |
| 87 | + @current_resource.exists = current_line |
| 88 | +end |
| 89 | + |
| 90 | +protected |
| 91 | + |
| 92 | +def parse(current) |
| 93 | + current.reduce([]) do |memo, row| |
| 94 | + line = {} |
| 95 | + fields = extract_fields(row) |
| 96 | + line[:options] = parse_options(fields.shift) unless types.include? fields[0] |
| 97 | + validate_type(fields[0], @path) |
| 98 | + line[:type] = fields[0] |
| 99 | + line[:key] = fields[1] |
| 100 | + line[:comment] = fields[2..-1].join(' ') if row[2] |
| 101 | + memo << line |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +def extract_fields(row) |
| 106 | + return :comment => row if row.empty? || row[0] == '#' |
| 107 | + |
| 108 | + quotes = 0 |
| 109 | + fields = [] |
| 110 | + row.scan(/\S+/) do |match| |
| 111 | + if quotes.even? || quotes == 0 |
| 112 | + fields << match |
| 113 | + else |
| 114 | + fields[-1] << " #{match}" |
| 115 | + end |
| 116 | + quotes += match.count('"') |
| 117 | + end |
| 118 | + fields |
| 119 | +end |
| 120 | + |
| 121 | +def parse_options(text) |
| 122 | + options = {} |
| 123 | + split = text.split(',') |
| 124 | + split.each do |group| |
| 125 | + validate_options(group, @path) |
| 126 | + group = group.split('=') |
| 127 | + options[group[0]] = group[1] |
| 128 | + end |
| 129 | + options |
| 130 | +end |
| 131 | + |
| 132 | +def types |
| 133 | + %w(id-rsa ecdsa-sha2-nistp256 ecdsa-sha2-nistp384 ecdsa-sha2-nistp521 ssh-ed25519 ssh-dss) |
| 134 | +end |
| 135 | + |
| 136 | +def validate_type(type, source) |
| 137 | + valid = types |
| 138 | + fail "Invalid Type #{type} in #{source}" unless valid.include? type.to_s |
| 139 | +end |
| 140 | + |
| 141 | +def validate_options(option, source) |
| 142 | + if option.is_a? Hash |
| 143 | + option.each { |o| validate_options o, source } |
| 144 | + return |
| 145 | + elsif option.is_a? String |
| 146 | + option = option.split('=') |
| 147 | + end |
| 148 | + |
| 149 | + binary_options = %w(cert-authority no-agent-forwarding no-port-forwarding no-pty no-user-rc no-X11-forwarding) |
| 150 | + other_options = %w(command environment from permitopen principals tunnel) |
| 151 | + |
| 152 | + if option[1].nil? || option[1].empty? |
| 153 | + fail "Invalid Option in #{source}: #{option}" unless binary_options.include? option[0].to_s |
| 154 | + else |
| 155 | + fail "Invalid Option in #{source}: #{option}" unless other_options.include? option[0].to_s |
| 156 | + end |
| 157 | +end |
0 commit comments