vagrant-keymanager 1.0.6 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e10be7d86cef39986c9c2eec8ad99ee5fb70a79d
4
- data.tar.gz: 840ec20d30f5de57f82a60b89e96c66c21b3ea84
3
+ metadata.gz: f86ecb2c469be75655708a7b2f8297984df16779
4
+ data.tar.gz: a592b2082ac1fb8623ce285f505914128e216e4a
5
5
  SHA512:
6
- metadata.gz: 915d8801a877c8a8da00b1f29be02a54785943d7859e509b287c36b5a1a24acfadef10a27a5b093af54b9cd96e2b91f51021fd55325e4e852b3070826bff45d9
7
- data.tar.gz: 5f569ed4ab3004be13d5eb37a65c0067c169e7e02ed874fd64a7840ee8b9257851fcebed76dc8b842e54d8b9952968cc01415d301f8fc9e7eecca4cb13444563
6
+ metadata.gz: eddd74a862aedbeb8bef62c204cbccc84ff40c92120008bbbb1ac45d3c52a3ed0fab12e7090dc0c900cd707cc1cdbb2590cb5c782b7ad1d00f34e6bbe52dff5b
7
+ data.tar.gz: 5ed4769a76a9114c969f56ee3ceb47c45fab49475dbf243dc7e7d52f8f43b4afe98b896268bf8c4e3459b7d98397cb12c14e6b9afe2766f9f0e3d1d950970ffc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.8
4
+ ### Fixes
5
+ * Can now get ssh keys for a list of users. Supply it via param 'user_list'. Root user is implicit so no need to add it
6
+
7
+
3
8
  ## 1.0.6
4
9
  ### Fixes
5
10
  * Single-liner for get_user_key e get_root_key functions
data/Gemfile.lock CHANGED
@@ -18,7 +18,7 @@ GIT
18
18
  PATH
19
19
  remote: .
20
20
  specs:
21
- vagrant-keymanager (1.0.6)
21
+ vagrant-keymanager (1.0.8)
22
22
 
23
23
  GEM
24
24
  remote: https://rubygems.org/
@@ -1,17 +1,17 @@
1
1
  module VagrantPlugins
2
2
  module KeyManager
3
3
  class Config < Vagrant.plugin('2', :config)
4
- attr_accessor :extra_params
5
4
  attr_accessor :extra_steps
5
+ attr_accessor :user_list
6
6
 
7
7
  def initialize
8
- @extra_params = []
9
- @extra_params = Array.new
10
8
  @extra_steps = nil
9
+ @user_list = UNSET_VALUE
11
10
  end
12
11
 
13
12
  def finalize!
14
- @extra_params = [ @extra_params ].flatten
13
+ @user_list = [] if @user_list == UNSET_VALUE
14
+ @user_list = [ "root", @user_list ].flatten.uniq
15
15
  end
16
16
 
17
17
  def validate(machine)
@@ -19,15 +19,6 @@ module VagrantPlugins
19
19
  # errors << validate_bool('keymanager.enabled', @enabled)
20
20
  errors.compact!
21
21
 
22
- # check if extra_params option is an Array
23
- if !machine.config.keymanager.extra_params.kind_of?(Array) &&
24
- !machine.config.keymanager.extra_params.kind_of?(String)
25
- errors << I18n.t('vagrant_keymanager.config.not_an_array_or_string', {
26
- :config_key => 'keymanager.extra_params',
27
- :is_class => extra_params.class.to_s,
28
- })
29
- end
30
-
31
22
  if !machine.config.keymanager.extra_steps.nil? &&
32
23
  !machine.config.keymanager.extra_steps.kind_of?(Proc)
33
24
  errors << I18n.t('vagrant_keymanager.config.not_a_proc', {
@@ -36,6 +27,14 @@ module VagrantPlugins
36
27
  })
37
28
  end
38
29
 
30
+ if !machine.config.keymanager.user_list.kind_of?(Array) &&
31
+ !machine.config.keymanager.user_list.kind_of?(String)
32
+ errors << I18n.t('vagrant_keymanager.config.not_an_array_or_string', {
33
+ :config_key => 'keymanager.user_list',
34
+ :is_class => user_list.class.to_s,
35
+ })
36
+ end
37
+
39
38
  errors.compact!
40
39
  { "KeyManager configuration" => errors }
41
40
  end
@@ -2,103 +2,135 @@ require 'tempfile'
2
2
  require 'pp'
3
3
 
4
4
  module VagrantPlugins
5
- module KeyManager
6
- module HostsFile
7
- def get_guest_keys(machine)
8
- machines = get_machines
9
-
10
- sshkeys = Hash.new
11
- sshrootkeys = Hash.new
12
-
13
- #puts "MACHINES"
14
- #pp machines
15
- machines.each do |curr_machine|
16
- #pp curr_machine
17
- curr_machine_name=curr_machine.name.to_s
18
- puts "Getting SSH keys from "+curr_machine_name
19
- sshkey=get_user_key(curr_machine)
20
- #puts "SSH key: "+sshkey
21
- sshkeys[curr_machine_name] = sshkey
22
- sshrootkey=get_root_key(curr_machine)
23
- #puts "SSH root key: "+sshrootkey
24
- sshrootkeys[curr_machine_name] = sshrootkey
25
- end
26
-
27
- machines.each do |curr_machine|
28
- curr_machine_name=curr_machine.name.to_s
29
- curr_machine.communicate.sudo("rm -f /tmp/.all_keys.txt /tmp/.all_root_keys.txt /tmp/add_ssh_keys.sh")
30
-
31
- puts "Saving public SSH keys to "+curr_machine_name
32
- ssh_keys_to_save=sshkeys.reject{|k,v| k == curr_machine_name}.values.join.gsub("\n\n", '\n')
33
- curr_machine.communicate.execute("echo '"+ssh_keys_to_save+"' >/tmp/.all_keys.txt")
34
- #puts "Saved /tmp/.all_keys.txt"
35
-
36
- ssh_root_keys_to_save=sshrootkeys.reject{|k,v| k == curr_machine_name}.values.join.gsub("\n\n", '\n')
37
- curr_machine.communicate.execute("echo '"+ssh_root_keys_to_save+"' >/tmp/.all_root_keys.txt")
38
-
39
- #puts "Saved /tmp/.all_root_keys.txt"
40
-
41
- # We must save locally a bash script that computes and applies diff and always exits with 0 or vagrant plugin will exit with an error
42
- curr_machine.communicate.execute("echo 'diff --changed-group-format=\"%>\" --unchanged-group-format=\"\" ~/.ssh/authorized_keys $1 >>~/.ssh/authorized_keys;exit 0' >/tmp/add_ssh_keys.sh")
43
-
44
- curr_machine.communicate.execute("sh /tmp/add_ssh_keys.sh /tmp/.all_keys.txt")
45
- curr_machine.communicate.execute("sh /tmp/add_ssh_keys.sh /tmp/.all_root_keys.txt")
46
- #puts "Saved user keys"
47
-
48
- curr_machine.communicate.sudo("sh /tmp/add_ssh_keys.sh /tmp/.all_keys.txt")
49
- curr_machine.communicate.sudo("sh /tmp/add_ssh_keys.sh /tmp/.all_root_keys.txt")
50
- #puts "Saved root keys"
51
- end
52
-
53
- machines.each do |curr_machine|
54
- call_extra_user_steps(curr_machine)
55
- end
56
-
57
- machines.each do |curr_machine|
58
- curr_machine.communicate.sudo("rm -f /tmp/.all_keys.txt /tmp/.all_root_keys.txt /tmp/add_ssh_keys.sh")
59
- end
60
- end
61
-
62
- private
63
-
64
- def get_user_key(machine)
65
- sshresult=""
66
- machine.communicate.execute("if [ ! -e ~/.ssh/id_rsa ] || [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -q -f ~/.ssh/id_rsa -P ''; fi; cat ~/.ssh/id_rsa.pub") do |type, data|
67
- sshresult << data if type == :stdout
68
- end
69
- return sshresult
70
- end
71
-
72
- def get_root_key(machine)
73
- sshrootresult = ""
74
- machine.communicate.sudo("if [ ! -e ~/.ssh/id_rsa ] || [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -q -f ~/.ssh/id_rsa -P ''; fi; cat ~/.ssh/id_rsa.pub") do |type, data|
75
- sshrootresult << data if type == :stdout
76
- end
77
- return sshrootresult
78
- end
79
-
80
- def call_extra_user_steps(resolving_machine)
81
- extra_user_steps = machine.config.keymanager.extra_steps
82
- if extra_user_steps
83
- machines = @global_env.machine_names
84
- machines.map { |machine| extra_user_steps.call(machine, resolving_machine) }
85
- end
86
- end
87
-
88
- def get_machines
89
- machines = @global_env.machine_names
90
- # Collect only machines that exist for the current provider
91
- machines.collect do |name|
92
- begin
93
- machine = @global_env.machine(name, @provider)
94
- rescue Vagrant::Errors::MachineNotFound
95
- # ignore
96
- end
97
- machine
98
- end
99
- .reject(&:nil?)
100
- end
101
-
102
- end
103
- end
5
+ module KeyManager
6
+ module HostsFile
7
+ def get_guest_keys(machine)
8
+ machines = get_machines
9
+ #running_machines = machines.reject {|m| m.state.short_description.to_s != "running"}
10
+ running_machines = machines.select {|m| m.communicate.ready? }
11
+
12
+ machines.each do |curr_machine|
13
+ #if curr_machine.state.short_description.to_s != "running"
14
+ if ! curr_machine.communicate.ready?
15
+ puts "Skipping machine "+curr_machine.name.to_s+ ". It's not in running state"
16
+ end
17
+ end
18
+
19
+ sshkeys = Hash.new
20
+ required_users = machine.config.keymanager.user_list
21
+
22
+ puts "REQUIRED USERS:"
23
+ pp required_users
24
+
25
+ running_machines.each do |curr_machine|
26
+ curr_machine.communicate.sudo("rm -f /tmp/add_ssh_keys.sh /tmp/get_user_keys.sh")
27
+ # TODO: save these 2 bash script with curr_machine.communicate.upload
28
+ # We must save locally a bash script that computes and applies diff and always exits with 0 or vagrant plugin will exit with an error
29
+ curr_machine.communicate.execute("echo -e 'diff --changed-group-format=\"%>\" --unchanged-group-format=\"\" ~/.ssh/authorized_keys $1 >>~/.ssh/authorized_keys\nexit 0' >/tmp/add_ssh_keys.sh")
30
+ # We must save locally a bash script that gets ssh keys from any user (will passed as a aparameter)
31
+ curr_machine.communicate.execute("echo -e 'if [ ! -e ~/.ssh/id_rsa ] || [ ! -e ~/.ssh/id_rsa.pub ]; then\n\tssh-keygen -q -f ~/.ssh/id_rsa -P \"\"\nfi\ncat ~/.ssh/id_rsa.pub' >/tmp/get_user_keys.sh")
32
+
33
+ curr_machine_name=curr_machine.name.to_s
34
+ puts "Getting SSH keys from "+curr_machine_name
35
+
36
+ required_users.each do |curr_user|
37
+ if !check_user_existence(curr_machine, curr_user)
38
+ create_user(curr_machine, curr_user)
39
+ end
40
+
41
+ if !sshkeys[curr_user]
42
+ sshkeys[curr_user] = Hash.new
43
+ end
44
+ sshkey=get_user_key(curr_machine, curr_user)
45
+ #puts "SSH key for "+curr_user+": "+sshkey
46
+ sshkeys[curr_user][curr_machine_name] = sshkey
47
+ end
48
+ end
49
+
50
+ #pp sshkeys
51
+
52
+ running_machines.each do |curr_machine|
53
+ curr_machine_name=curr_machine.name.to_s
54
+
55
+ puts "Saving public SSH keys to "+curr_machine_name
56
+
57
+ required_users.each do |curr_user|
58
+ curr_machine.communicate.sudo("rm -f /tmp/.all_"+curr_user+"_keys.txt")
59
+
60
+ ssh_keys_to_save=sshkeys[curr_user].reject{|k,v| k == curr_machine_name}.values.join.gsub("\n\n", '\n')
61
+ curr_machine.communicate.execute("sudo -u "+curr_user+" -H echo '"+ssh_keys_to_save+"' >/tmp/.all_"+curr_user+"_keys.txt")
62
+ #puts "Saved /tmp/.all_"+curr_user+"_keys.txt"
63
+ end
64
+
65
+ required_users.each do |curr_user|
66
+ required_users.each do |source_user|
67
+ curr_machine.communicate.execute("sudo -u "+curr_user+" -H sh /tmp/add_ssh_keys.sh /tmp/.all_"+source_user+"_keys.txt")
68
+ end
69
+ #puts "Saved user "+curr_user+"keys"
70
+ end
71
+ end
72
+
73
+ running_machines.each do |curr_machine|
74
+ call_extra_user_steps(curr_machine)
75
+ end
76
+
77
+ running_machines.each do |curr_machine|
78
+ curr_machine.communicate.sudo("rm -f /tmp/add_ssh_keys.sh /tmp/get_user_keys.sh")
79
+ required_users.each do |curr_user|
80
+ curr_machine.communicate.sudo("rm -f /tmp/.all_"+curr_user+"_keys.txt")
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def check_user_existence(machine, username)
88
+ if username != "root"
89
+ user_id = ""
90
+ machine.communicate.execute("id -u "+username+" 2>/dev/null; exit 0") do |type, data|
91
+ user_id << data if type == :stdout
92
+ end
93
+ return user_id != ""
94
+ else
95
+ return true
96
+ end
97
+ end
98
+
99
+ def create_user(machine, username)
100
+ puts "Creating user "+username+" on "+machine.name.to_s
101
+ machine.communicate.sudo("adduser "+username)
102
+ end
103
+
104
+ def get_user_key(machine, username)
105
+ sshresult=""
106
+ machine.communicate.execute("sudo -u "+username+" -H sh /tmp/get_user_keys.sh") do |type, data|
107
+ sshresult << data if type == :stdout
108
+ end
109
+ return sshresult
110
+ end
111
+
112
+ def call_extra_user_steps(resolving_machine)
113
+ extra_user_steps = machine.config.keymanager.extra_steps
114
+ if extra_user_steps
115
+ machines = @global_env.machine_names
116
+ machines.map { |machine| extra_user_steps.call(machine, resolving_machine) }
117
+ end
118
+ end
119
+
120
+ def get_machines
121
+ machines = @global_env.machine_names
122
+ # Collect only machines that exist for the current provider
123
+ machines.collect do |name|
124
+ begin
125
+ machine = @global_env.machine(name, @provider)
126
+ rescue Vagrant::Errors::MachineNotFound
127
+ # ignore
128
+ end
129
+ machine
130
+ end
131
+ .reject(&:nil?)
132
+ end
133
+
134
+ end
135
+ end
104
136
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module KeyManager
3
- VERSION = '1.0.6'
3
+ VERSION = '1.0.8'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-keymanager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giorgio Baldaccini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-05 00:00:00.000000000 Z
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler