Skip to content

Commit

Permalink
Merge pull request #1 from tuxinaut/master
Browse files Browse the repository at this point in the history
Add posibility to keep existing ssh pub keys
  • Loading branch information
nickola committed Apr 24, 2013
2 parents 2cf174b + 395dfab commit c94e94d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default['ssh_keys']['keep_existing_keys'] = true
21 changes: 19 additions & 2 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,44 @@
if user and user['dir'] and user['dir'] != "/dev/null"
# Preparing SSH keys
ssh_keys = []

Array(bag_users).each do |bag_user|
data = data_bag_item('users', bag_user)
if data and data['ssh_keys']
ssh_keys += Array(data['ssh_keys'])
end
end

if node['ssh_keys']['keep_existing_keys']
authorized_keys_file = "#{user['dir']}/.ssh/authorized_keys"

if File.exist?(authorized_keys_file)
Chef::Log.info("Keep authorized keys from #{authorized_keys_file}")

File.open(authorized_keys_file).each do |l|
if l.start_with?("ssh")
ssh_keys += Array(l.delete "\n")
end
end

ssh_keys.uniq!
end
end

# Saving SSH keys
if ssh_keys.length > 0
home_dir = user['dir']

# Creating ".ssh" directory
directory "#{home_dir}/.ssh" do
owner user['id']
owner user['uid']
group user['gid'] || user['id']
mode "0700"
end

# Creating "authorized_keys"
template "#{home_dir}/.ssh/authorized_keys" do
owner user['id']
owner user['uid']
group user['gid'] || user['id']
mode "0600"
variables :ssh_keys => ssh_keys
Expand Down

0 comments on commit c94e94d

Please sign in to comment.