generate rsa private/public key and return public key on jumper server
[why]
register the public key on testbox
enable user one click to login in the testbox
[how]
generate rsa private/public key for user on jumper server
return and register the public key to es
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
container/assign-account/get_account_info.rb | 32 ++++++++++++--------
1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/container/assign-account/get_account_info.rb b/container/assign-account/get_account_info.rb
index 137f5c6..b5a2388 100755
--- a/container/assign-account/get_account_info.rb
+++ b/container/assign-account/get_account_info.rb
@@ -94,30 +94,36 @@ class AccountStorage
end
def setup_jumper_account_info
- account_info = read_account_info
- jumper_info = read_jumper_info
+ login_name, password = read_account_info
+ jumper_host, jumper_port = read_jumper_info
pub_key = @data['my_ssh_pubkey'] unless @data['my_ssh_pubkey'].nil?
- login_name = account_info[0]
- password = if pub_key.nil?
- account_info[1]
- else
- 'Use pub_key to login'
- end
+ password = 'Use pub_key to login' unless pub_key.nil?
+
+ config_authorized_key(login_name, pub_key) unless pub_key.nil?
+ config_default_yaml(login_name)
+ my_jumper_pubkey = generate_ssh_key(login_name) if @data['my_jumper_pubkey'].eql? true
jumper_account_info = {
'my_login_name' => login_name,
'my_password' => password,
- 'jumper_host' => jumper_info[0].chomp,
- 'jumper_port' => jumper_info[1].chomp
+ 'jumper_host' => jumper_host,
+ 'jumper_port' => jumper_port,
+ 'my_jumper_pubkey' => my_jumper_pubkey
}
- config_authorized_key(login_name, pub_key) unless pub_key.nil?
- config_default_yaml(login_name)
-
return jumper_account_info
end
+ def generate_ssh_key(login_name)
+ ssh_dir = File.join('/home/', login_name, '.ssh')
+ Dir.mkdir ssh_dir, 0o700 unless File.exist? ssh_dir
+ hostname = File.read('/etc/hostname')
+ %x(ssh-keygen -f "#{ssh_dir}/id_rsa" -N '' -C "#{login_name}@#{hostname}")
+ %x(chown -R #{login_name}:#{login_name} #{ssh_dir})
+ File.read("/home/#{login_name}/.ssh/id_rsa.pub")
+ end
+
def config_default_yaml(login_name)
default_yaml_dir = File.join('/home', login_name, '.config/compass-ci/defaults')
FileUtils.mkdir_p default_yaml_dir
--
2.23.0