From 29322d952e23360ac522bbd14e32aa364656c575 Mon Sep 17 00:00:00 2001 From: Yan Date: Thu, 16 Nov 2017 20:46:03 +0630 Subject: [PATCH] license cloud done --- app/controllers/application_controller.rb | 15 ++++-- app/controllers/install_controller.rb | 2 +- app/models/license.rb | 59 +++++++++++++++-------- app/models/my_aes_crypt.rb | 16 ++++-- config/secrets.yml | 2 +- 5 files changed, 64 insertions(+), 30 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2163d885..91103651 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,8 +22,8 @@ class ApplicationController < ActionController::Base { locale: I18n.locale } end - def lookup_domain - if request.subdomain.present? && request.subdomain != "www" + def lookup_domain + if request.subdomain.present? && request.subdomain != "www" @license = cache_license(ENV["SX_PROVISION_URL"], request.subdomain.downcase) # request.subdomain.downcase if (!@license.nil?) # logger.info "Location - " + @license.name @@ -54,10 +54,17 @@ class ApplicationController < ActionController::Base end def cache_license(url, lookup) + flag = ENV["AES_IV"] @license = License.new(url, lookup) # Export for Key - aes = MyAesCrypt.new - aes_key, aes_iv = aes.export_key(lookup) + + if flag == "<%= ENV['AES_IV'] %>" + aes = MyAesCrypt.new + aes_key, aes_iv = aes.export_key(lookup) + else + aes_key = ENV["AES_KEY"] + aes_iv = ENV["AES_IV"] + end if (@license.detail_with_local_cache(lookup, aes_key, aes_iv) == true) return @license diff --git a/app/controllers/install_controller.rb b/app/controllers/install_controller.rb index 870a10e0..52ecc296 100755 --- a/app/controllers/install_controller.rb +++ b/app/controllers/install_controller.rb @@ -16,7 +16,7 @@ class InstallController < BaseController # Export for Key aes = MyAesCrypt.new - aes_key, aes_iv = aes.export_key(lookup) + aes_key, aes_iv = aes.export_key(license_key) @license = License.new(ENV["SX_PROVISION_URL"]) response = @license.license_activate(aes_key, aes_iv, license_key, db_host, db_schema, db_user, db_password) diff --git a/app/models/license.rb b/app/models/license.rb index c75eded5..32f1b48a 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -41,9 +41,11 @@ class License cache_license = nil ##Get redis connection from connection pool - Redis.current do |conn| - cache_license = conn.get(cache_key) - end + redis = Redis.new + cache_license = redis.get(cache_key) + # Redis.current do |conn| + # cache_license = conn.get(cache_key) + # end Rails.logger.info "Cache key - " + cache_key.to_s if cache_license.nil? @@ -54,22 +56,27 @@ class License @license = response.parsed_response if (@license["status"] == true) - assign() Rails.logger.info "License - " + response.parsed_response.to_s - - Redis.current do |conn| - ##Remote - store the remote response in local redis cache - conn.set(cache_key, Marshal.dump(@license)) - ##ADD to List to remove later - conn.sadd("License:cache:keys", cache_key) - end + + redis = Redis.new + redis.set(cache_key, Marshal.dump(@license)) + # redis.sadd("License:cache:keys", cache_key) + # Redis.current do |conn| + # ##Remote - store the remote response in local redis cache + # conn.set(cache_key, Marshal.dump(@license)) + # ##ADD to List to remove later + # conn.sadd("License:cache:keys", cache_key) + # end return true end - - Rails.logger.info 'API License' + else + @license = Marshal.load(cache_license) + assign() + Rails.logger.info 'API License' + return true end end @@ -223,7 +230,7 @@ class License File.open("config/license.yml").each do |line| if line.include? (key) decrypted_line_array = line.split(":") - decrypted_line = AESCrypt.decrypt_data(decrypted_line_array[1], ENV['AES_KEY'], ENV['AES_IV'], ENV['CIPHER_TYPE']) + decrypted_line = AESCrypt.decrypt_data(decode_str(decrypted_line_array[1]), decode_str(ENV['AES_KEY']), decode_str(ENV['AES_IV']), ENV['CIPHER_TYPE']) end end end @@ -231,6 +238,10 @@ class License private + def decode_str(str) + return Base64.decode64(str) + end + # License File Creation def create_license_file(response_data) if check_license_file @@ -341,13 +352,21 @@ class License # self.plan_max_products = @license["plan_max_products"].to_i # self.plan_max_customers = @license["plan_max_customers"].to_i # self.plan_active_connections = @license["plan_active_connections"].to_i - salt = @license["secret_key"] + # salt = @license["secret_key"] - if (@license["dbhost"] || @license["dbschema"] || @license["dbusername"] || @license["dbpassword"] ) - self.dbhost = AESCrypt.decrypt(@license["dbhost"], salt) - self.dbschema = AESCrypt.decrypt(@license["dbschema"], salt) - self.dbusername = AESCrypt.decrypt(@license["dbusername"], salt) - self.dbpassword = AESCrypt.decrypt(@license["dbpassword"], salt) + key = Base64.decode64(ENV['AES_KEY']) + iv = Base64.decode64(ENV['AES_IV']) + + if (@license["dbhost"] || @license["dbschema"] || @license["dbusername"] || @license["dbpassword"] ) + host = Base64.decode64(@license["dbhost"]) + dbschema = Base64.decode64(@license["dbschema"]) + dbusername = Base64.decode64(@license["dbusername"]) + dbpassword = Base64.decode64(@license["dbpassword"]) + + self.dbhost = AESCrypt.decrypt_data(host, key, iv, ENV['CIPHER_TYPE']) + self.dbschema = AESCrypt.decrypt_data(dbschema, key, iv, ENV['CIPHER_TYPE']) + self.dbusername = AESCrypt.decrypt_data(dbusername, key, iv, ENV['CIPHER_TYPE']) + self.dbpassword = AESCrypt.decrypt_data(dbpassword, key, iv, ENV['CIPHER_TYPE']) end # self.exchange_unqiue_id = @license["exchange_unqiue_id"] diff --git a/app/models/my_aes_crypt.rb b/app/models/my_aes_crypt.rb index 7427a000..acba4974 100644 --- a/app/models/my_aes_crypt.rb +++ b/app/models/my_aes_crypt.rb @@ -1,3 +1,6 @@ +require 'openssl' +require 'base64' + class MyAesCrypt @cipher = "" @@ -7,11 +10,16 @@ class MyAesCrypt def export_key(passphrase) # We want a 256 bit key symetric key based on passphrase + # for cloud is lookup + # for local is license_key + # iv_salt = passphrase+"c2l" digest = Digest::SHA256.new - key = digest.update(passphrase) - key = digest.digest - ENV['AES_KEY'] = cipher_key = key # stores the key in key, and also sets the generated key on the @cipher - ENV['AES_IV'] = cipher_iv = @cipher.random_iv # stores the iv in iv, and also sets the generated iv on the @cipher + key_digest = digest.update(passphrase) + # iv_digest = digest.update(iv_salt) + key = key_digest.digest + # iv = iv_digest.digest + ENV['AES_KEY'] = cipher_key = Base64.encode64(key) # stores the key in key, and also sets the generated key on the @cipher + ENV['AES_IV'] = cipher_iv = Base64.encode64(@cipher.random_iv) # stores the iv in iv, and also sets the generated iv on the @cipher return cipher_key, cipher_iv end diff --git a/config/secrets.yml b/config/secrets.yml index 28814f4d..53d90c23 100755 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -12,7 +12,7 @@ development: secret_key_base: b61d85f8ed2a1a9e0eeece3443b3e8f838d002cc1d9f32115d8e93db920e2957adfedc57501d44741211538f3108b742cdeada87d5bfae796c53da1f90a3cd61 - sx_provision_url: 192.168.1.94:3002 #provision.zsai.ws/api + sx_provision_url: 192.168.1.94:3002/api #provision.zsai.ws/api server_mode: cloud cipher_type: AES-256-CBC aes_key: <%= ENV['AES_KEY'] %>