crypt and decrypt complete

This commit is contained in:
Sergio authored and Tino Vázquez committed 2017-05-29 16:33:31 +02:00
1 parent 2b05cde92e
commit c5dc6a3c2a
2 files changed
+66 -3

No files matched your search

+43
View File
@@ -37,8 +37,10 @@ EOT
if ONE_LOCATION
TABLE_CONF_PATH=ONE_LOCATION+"/etc/cli"
VAR_LOCATION=ONE_LOCATION+"/var" if !defined?(VAR_LOCATION)
else
TABLE_CONF_PATH="/etc/one/cli"
VAR_LOCATION="/var/lib/one" if !defined?(VAR_LOCATION)
end
EDITOR_PATH='/usr/bin/vi'
@@ -489,6 +491,47 @@ EOT
end
end
# receive a object key => value format
# returns hashed values
def encrypt(opts)
res = {}
key_one= File.read(VAR_LOCATION+'/.one/one_key')
opts.each do |key, value|
cipher = OpenSSL::Cipher::AES.new(256,:CBC)
cipher.encrypt.key = key_one
puts "cifrando #{key}"
encrypted = cipher.update(value) + cipher.final
#res.merge!({key => value})
res[key] = Base64::encode64(encrypted)
puts "encriptado es: "+encrypted
end
return res
end
def decrypt(res)
opts = {}
key_one= File.read(VAR_LOCATION+'/.one/one_key')
res.each do |key, encrypted_value|
decipher = OpenSSL::Cipher::AES.new(256,:CBC)
decipher.decrypt
decipher.key = key_one
puts "desencriptando #{key}"
plain = decipher.update(Base64::decode64(encrypted_value)) + decipher.final
puts "una vez desencriptado es "+plain
opts[key] = plain
end
return opts
end
def list_pool(options, top=false, filter_flag=nil)
if options[:describe]
table = format_pool(options)
+23 -3
View File
@@ -32,7 +32,6 @@ $: << RUBY_LIB_LOCATION+"/cli"
require 'command_parser'
require 'one_helper/onehost_helper'
require 'one_helper/onecluster_helper'
require 'ec2_driver'
cmd=CommandParser::CmdParser.new(ARGV) do
usage "`onehost` <command> [<args>] [<options>]"
@@ -124,16 +123,37 @@ cmd=CommandParser::CmdParser.new(ARGV) do
STDERR.puts "\t -v hypervisor driver"
exit -1
end
ec2_host = !options[:ec2access].nil? && !options[:ec2secret].nil?
encrypt(options)
if ec2_host
ec2_opts = {
:access => options[:ec2access],
:secret => options[:ec2secret]
}
encrypt_opts = helper.encrypt(ec2_opts)
helper.decrypt(encrypt_opts)
end
cid = options[:cluster] || ClusterPool::NONE_CLUSTER_ID
helper.create_resource(options) do |host|
host.allocate(args[0],
options[:im],
options[:vm],
cid)
#template=""
#encrypt_opts.each do |key, value|
# template << "#{key}=\"+value\"\n"\
#end
template = "EC2_ACCESS=\"#{encrypt_opts[:access]}\"\n"\
"EC2_SECRET=\"#{encrypt_opts[:secret]}\"\n"\
host.update(template, true)
end
end