-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshkey.rb
66 lines (57 loc) · 1.5 KB
/
sshkey.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Encoding: utf-8
# Cookbook Name:: rancid-git
# Provider:: sshkey
# Author:: Bao Nguyen
# License:: Apache 2.0
#
# Copyright 2014, Bao Nguyen
use_inline_resources
action :create do
# Install sshkey gem into chef
chef_gem 'sshkey'
name = new_resource.name
directory node['rancid']['key_dir'] do
owner node['rancid']['user']
group node['rancid']['group']
mode 00700
end
pkey = "#{node[:rancid][:install_dir]}/keys/#{name}"
unless ::File.exists?(pkey)
# Generate a keypair with Ruby
require 'sshkey'
hostname = "localhost"
sshkey = SSHKey.generate(
type: 'RSA',
bits: 4096,
comment: "#{node['rancid']['user']}@#{hostname}"
)
# Store private key on disk
file pkey do
action :create_if_missing
owner node['rancid']['user']
group node['rancid']['group']
mode "0600"
content sshkey.private_key
end
# Store public key on disk
file "#{pkey}.pub" do
action :create_if_missing
owner node['rancid']['user']
group node['rancid']['group']
mode "0600"
content sshkey.ssh_public_key
end
new_resource.updated_by_last_action(true)
end
end
action :remove do
pkey = "#{node[:rancid][:install_dir]}/keys/#{new_resource.name}"
Chef::Log.info "Removing sshkey #{new_resource.name.to_s}: to #{pkey}"
if ::File.exists?(pkey)
Chef::Log.info "SSH key #{new_resource.name.to_s}: to #{pkey}"
file pkey do
action :delete
end
new_resource.updated_by_last_action(true)
end
end