#!/usr/bin/env ruby # encoding: UTF-8 # (c) Copyright 2014 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # This script works in ruby 1.8 require 'lorj' # require 'ruby-debug' # Debugger.start # Class to test encryted data. class Test < Lorj::BaseDefinition def initialize(core) @core = core end def self.def_internal(name) spec_name = 's' + name # To call the function identified internally with 'spec' prefix define_method(spec_name) do |*p| send(name, *p) end end # Internal function to test. def_internal '_get_encrypt_key' def run puts 'Checking imported account...' tests = [:account_key_test] tests.each { |t| send(t) if self.class.private_method_defined?(t) } end private # Test if the encryted data (if encrypted) is readable def account_key_test key = 'credentials#account_key' opts = Lorj.data.auto_section_data(key) data = @core.config[key] if opts[:encrypted].is_a?(TrueClass) entr = _get_encrypt_key res = Lorj::SSLCrypt.get_encrypted_value(data, entr, key) status = !res.nil? else status = true end test_state(status, key, data) end def test_state(res, test, value) test_str = "#{test}. (#{value})" if res puts "OK : #{test_str}" else puts "FAIL : #{test_str}" end end end # TODO: Implement Thor instead of ARGV use. # TODO: Support to load local process. if ARGV.length <= 3 puts "Syntax is 'ruby #{__FILE__}' "\ "[]\n"\ "where:\n"\ "LorjRef : Lorj application struture to use. \n"\ " Format: ]>\n"\ " datapath : Path where Lorj store data.\n"\ " pdatapath : Path where Lorj store private data.\n"\ 'key : Base64 encoded key. Used to decrypt the \n"\ "CloudDataFile : File containing the Lorj cloud data to import.\n"\ "AccountName : Account name to import. Usually the CloudDataFile\n"\ " have the name embedded and may use that one except\n"\ ' if you force it.' exit end ref, key_encoded, data_file, account = ARGV ref_found = ref.match(/^(.*(\|(.*))?)$/) unless ref_found puts 'LorjRef must be formatted as : ]>='\ '[@ 0 key_encoded += '=' * (4 - (key_encoded.length % 4)) end begin key_yaml = Base64.strict_decode64(key_encoded) rescue => e puts "Reading Base64 Key: '#{key_encoded}' is not a valid encoded Base64"\ " data.\n#{e}\nPlease check and retry." exit 1 end begin entr = YAML.load(key_yaml) rescue => e puts "Reading Base64 Key: '#{key_yaml}' is not a valid YAML data.\n#{e}\n"\ 'Please check and retry.' exit 1 else unless entr.key?(:iv) && entr.key?(:key) && entr.key?(:salt) puts 'Reading Base64 Key: Invalid key. Missing entropy data.' exit 1 end end PrcLib.data_path = datapath PrcLib.pdata_path = pdatapath data = File.read(data_file).strip core = Lorj.account_import(entr, data, account) puts 'Import done.' unless core.config.ac_save puts 'Issue during configuration saved.' exit 1 end puts "Config imported and saved in #{core.config['account#name']}" Test.new(core).run puts 'Import process done.'