diff --git a/src/ozones/Server/lib/OZones.rb b/src/ozones/Server/lib/OZones.rb index d75f060a36..6a08b01658 100644 --- a/src/ozones/Server/lib/OZones.rb +++ b/src/ozones/Server/lib/OZones.rb @@ -34,7 +34,6 @@ require 'base64' module OZones - CIPHER="aes-256-cbc" # ------------------------------------------------------------------------- # The Error Class represents a generic error in the OZones # library. It contains a readable representation of the error. @@ -72,37 +71,4 @@ module OZones def self.str_to_json(str) return JSON.pretty_generate({:message => str}) end - - def self.readKey - begin - credentials = File.read(ENV['OZONES_AUTH']).strip - return Digest::SHA1.hexdigest(credentials); - rescue - return ""; - end - end - - def self.encrypt(plain_txt) - #prepare cipher object - cipher = OpenSSL::Cipher.new(CIPHER) - cipher.encrypt - cipher.key = OZones.readKey - - enc_txt = cipher.update(plain_txt) - enc_txt << cipher.final - - Base64::encode64(enc_txt).strip.delete("\n") - end - - def self.decrypt(b64_txt) - #prepare cipher object - cipher = OpenSSL::Cipher.new(CIPHER) - cipher.decrypt - cipher.key = OZones.readKey - - enc_txt = Base64::decode64(b64_txt) - - plain_txt = cipher.update(enc_txt) - plain_txt << cipher.final - end -end +end \ No newline at end of file diff --git a/src/ozones/Server/lib/OZones/Zones.rb b/src/ozones/Server/lib/OZones/Zones.rb index b810792431..e652666742 100644 --- a/src/ozones/Server/lib/OZones/Zones.rb +++ b/src/ozones/Server/lib/OZones/Zones.rb @@ -68,10 +68,13 @@ module OZones def ONEPASS pw = super - OZones.decrypt(pw) + decrypt(pw) + end + + def ONEPASS=(plain_pw) + super(encrypt(plain_pw)) end - ####################################################################### # Zone Data Management ####################################################################### @@ -88,8 +91,6 @@ module OZones name = zone_data[:ONENAME] pass = zone_data[:ONEPASS] - zone_data[:ONEPASS] = OZones.encrypt(pass) - rc = OpenNebulaZone::check_oneadmin(name, pass, zone_data[:ENDPOINT]) @@ -113,6 +114,41 @@ module OZones return zone end + + ######################################################################## + # Encryption functions for the class + ######################################################################## + CIPHER = "aes-256-cbc" + + @@cipher = "" + + def self.cipher=(cipher) + @@cipher = cipher + end + + def encrypt(plain_txt) + #prepare cipher object + cipher = OpenSSL::Cipher.new(CIPHER) + cipher.encrypt + cipher.key = @@cipher + + enc_txt = cipher.update(plain_txt) + enc_txt << cipher.final + + Base64::encode64(enc_txt).strip.delete("\n") + end + + def decrypt(b64_txt) + #prepare cipher object + cipher = OpenSSL::Cipher.new(CIPHER) + cipher.decrypt + cipher.key = @@cipher + + enc_txt = Base64::decode64(b64_txt) + + plain_txt = cipher.update(enc_txt) + plain_txt << cipher.final + end end ########################################################################### diff --git a/src/ozones/Server/models/OzonesServer.rb b/src/ozones/Server/models/OzonesServer.rb index aca3c4b665..4c167c89cd 100644 --- a/src/ozones/Server/models/OzonesServer.rb +++ b/src/ozones/Server/models/OzonesServer.rb @@ -20,6 +20,11 @@ require 'JSONUtils' class OzonesServer include OpenNebulaJSON::JSONUtils + def initialize(cipher) + #Set cipher for Zone classes + OZones::Zones.cipher = cipher + end + ############################################################################ # Get methods for the Zones and VDC interface ############################################################################ diff --git a/src/ozones/Server/ozones-server.rb b/src/ozones/Server/ozones-server.rb index 85d93fd172..c5b64120f8 100755 --- a/src/ozones/Server/ozones-server.rb +++ b/src/ozones/Server/ozones-server.rb @@ -96,6 +96,7 @@ end ADMIN_NAME = @auth.name ADMIN_PASS = @auth.password +ADMIN_KEY = Digest::SHA1.hexdigest("ADMIN_NAME:ADMIN_PASS") begin OZones::ProxyRules.new("apache",config[:htaccess]) @@ -171,7 +172,7 @@ before do unless request.path=='/login' || request.path=='/' halt 401 unless authorized? - @OzonesServer = OzonesServer.new + @OzonesServer = OzonesServer.new(ADMIN_KEY) @pr = OZones::ProxyRules.new("apache",config[:htaccess]) end end