lorj 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/lorj_account_import.rb +128 -0
- data/build/build_with_proxy.sh +1 -1
- data/lib/core/core.rb +36 -1
- data/lib/core/core_import_export.rb +168 -0
- data/lib/core/core_setup_encrypt.rb +52 -25
- data/lib/core/lorj_basecontroller.rb +154 -0
- data/lib/core/lorj_baseprocess.rb +16 -0
- data/lib/lorj/version.rb +2 -2
- data/lib/lorj.rb +1 -0
- data/lib/lorj_account.rb +17 -1
- data/lorj-spec/cache/.key +2 -0
- data/lorj-spec/data/accounts/test.yaml +21 -0
- data/lorj-spec/data/config.yaml +15 -0
- data/lorj-spec/process/mock/data.yaml +39 -0
- data/lorj-spec/process/mock/defaults.yaml +0 -20
- data/lorj.gemspec +1 -1
- data/spec/05_lorj_keypath_spec.rb +19 -5
- data/spec/06_lorj_object_data_spec.rb +19 -5
- data/spec/09_prc_spec.rb +31 -6
- data/spec/10_lorj_log_spec.rb +21 -7
- data/spec/11_lorj_defaults_spec.rb +18 -6
- data/spec/12_lorj_config_spec.rb +18 -6
- data/spec/13_lorj_account_spec.rb +25 -6
- data/spec/20_lorj_meta_spec.rb +24 -8
- data/spec/21_lorj_processes_spec.rb +18 -6
- data/spec/22_lorj_core_spec.rb +19 -6
- data/spec/30_lorj_basedefinition_spec.rb +125 -0
- data/spec/31_lorj_importexport_spec.rb +182 -0
- data/spec/spec_helper.rb +54 -0
- metadata +43 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b033bdd31601389a91bf2772c3960307fdb7e2b
|
4
|
+
data.tar.gz: 73451dacdb51d7c898d43f3f4752ed4dd340cfad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0807c1c536cf7bbccb7fc2fe224e7f136e29721bc3fe691339c3aad337f68ea6eeeef8728ce1b070cbe4483bf5e3d99f18361f6114ddd3e8831aa9db2a80c9a
|
7
|
+
data.tar.gz: bd5b1c98d312503272591359658e86b3a9a3fd280a0ca6623bfc86398273351afe50d332a269fb75f55bbd19371cd97772f0c9dd3549122aa881bcc430a1c23b
|
@@ -0,0 +1,128 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
# This script works in ruby 1.8
|
19
|
+
|
20
|
+
require 'lorj'
|
21
|
+
|
22
|
+
# require 'ruby-debug'
|
23
|
+
# Debugger.start
|
24
|
+
|
25
|
+
if ARGV.length <= 3
|
26
|
+
puts "Syntax is 'ruby #{__FILE__}' <LorjRef> <key> <CloudDataFile> "\
|
27
|
+
"[<AccountName[@provider]>]\n"\
|
28
|
+
"where:\n"\
|
29
|
+
"LorjRef : Lorj application struture to use. \n"\
|
30
|
+
" Format: <datapath>=<process>[@<libToLoad]\n"\
|
31
|
+
" datapath : Path where Lorj store data.\n"\
|
32
|
+
" process : Lorj process name to load. It can be a path to a\n"\
|
33
|
+
' process file.'\
|
34
|
+
" libToLoad : Optional. Ruby library containing The Lorj process.\n"\
|
35
|
+
" If missing, it will try to load a lib named \n"\
|
36
|
+
' lorj_<process>'\
|
37
|
+
'key : Base64 encoded key. Used to decrypt the <CloudDataFi'\
|
38
|
+
"le>\n"\
|
39
|
+
"CloudDataFile : File containing the Lorj cloud data to import.\n"\
|
40
|
+
"AccountName : Account name to import. Usually the CloudDataFile\n"\
|
41
|
+
" have the name embedded and may use that one except\n"\
|
42
|
+
' if you force it.'
|
43
|
+
exit
|
44
|
+
end
|
45
|
+
|
46
|
+
ref, key_encoded, data_file, account = ARGV
|
47
|
+
|
48
|
+
ref_found = ref.match(/^(.*)=(.*?)(@(.*))?$/)
|
49
|
+
|
50
|
+
unless ref_found
|
51
|
+
puts 'LorjRef must be formatted as : <datapath>=<process>[@<libToLoad]'
|
52
|
+
exit 1
|
53
|
+
end
|
54
|
+
|
55
|
+
datapath = ref_found[1]
|
56
|
+
process = ref_found[2]
|
57
|
+
|
58
|
+
if ref_found[3].nil?
|
59
|
+
lib_name = "lorj_#{process}"
|
60
|
+
else
|
61
|
+
lib_name = ref_found[4]
|
62
|
+
end
|
63
|
+
|
64
|
+
unless File.exist?(data_file)
|
65
|
+
puts "#{data_file} doesn't exist. Please check and retry."
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
if key_encoded == ''
|
70
|
+
puts 'The key provided is empty. Please check and retry.'
|
71
|
+
exit 1
|
72
|
+
end
|
73
|
+
|
74
|
+
begin
|
75
|
+
require lib_name
|
76
|
+
rescue => e
|
77
|
+
puts "Warning! Unable to load RubyGem '#{lib_name}'.\n#{e}"
|
78
|
+
end
|
79
|
+
|
80
|
+
if key_encoded.length % 4 > 0
|
81
|
+
key_encoded += '=' * (4 - (key_encoded.length % 4))
|
82
|
+
end
|
83
|
+
|
84
|
+
begin
|
85
|
+
key_yaml = Base64.strict_decode64(key_encoded)
|
86
|
+
rescue => e
|
87
|
+
puts "Reading Base64 Key: '#{key_encoded}' is not a valid encoded Base64"\
|
88
|
+
" data.\n#{e}\nPlease check and retry."
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
begin
|
93
|
+
entr = YAML.load(key_yaml)
|
94
|
+
rescue => e
|
95
|
+
puts "Reading Base64 Key: '#{key_yaml}' is not a valid YAML data.\n#{e}\n"\
|
96
|
+
'Please check and retry.'
|
97
|
+
exit 1
|
98
|
+
else
|
99
|
+
unless entr.key?(:iv) && entr.key?(:key) && entr.key?(:salt)
|
100
|
+
puts 'Reading Base64 Key: Invalid key. Missing entropy data.'
|
101
|
+
exit 1
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
name, controller = account.split('@') unless account.nil?
|
106
|
+
|
107
|
+
PrcLib.data_path = datapath
|
108
|
+
|
109
|
+
keypath = Lorj::KeyPath.new(process)
|
110
|
+
|
111
|
+
processes = [{ :process_module => keypath.key_tree }]
|
112
|
+
|
113
|
+
core = Lorj::Core.new(Lorj::Account.new, processes)
|
114
|
+
|
115
|
+
data = File.read(data_file).strip
|
116
|
+
|
117
|
+
# debugger # rubocop: disable Lint/Debugger
|
118
|
+
|
119
|
+
core.account_import(entr, data, name, controller)
|
120
|
+
|
121
|
+
puts 'Import done.'
|
122
|
+
|
123
|
+
if core.config.ac_save
|
124
|
+
puts "Config imported and saved in #{core.config['account#name']}"
|
125
|
+
exit 0
|
126
|
+
end
|
127
|
+
puts 'Issue during configuration saved.'
|
128
|
+
exit 1
|
data/build/build_with_proxy.sh
CHANGED
data/lib/core/core.rb
CHANGED
@@ -310,11 +310,46 @@ module Lorj
|
|
310
310
|
#
|
311
311
|
# * *Raises* :
|
312
312
|
# No exceptions
|
313
|
-
def register(oObject, sObjectType = nil)
|
313
|
+
def register(oObject, sObjectType = nil)
|
314
314
|
return nil if !oObject || !@core_object
|
315
315
|
@core_object.register(oObject, sObjectType)
|
316
316
|
end
|
317
317
|
|
318
|
+
# Function to import an encrypted Hash as a Lorj Account.
|
319
|
+
#
|
320
|
+
# For details about this functions, see #Lorj::BaseDefinition.account_import
|
321
|
+
#
|
322
|
+
# * *Args* :
|
323
|
+
# - +key+ : key to use to decrypt the 'enc_hash'.
|
324
|
+
# - +enc_hash+ : Encrypted Hash.
|
325
|
+
#
|
326
|
+
# * *Raises* :
|
327
|
+
# No exceptions
|
328
|
+
def account_import(key, enc_hash, name = nil, controller = nil)
|
329
|
+
return nil if @core_object.nil?
|
330
|
+
@core_object.account_import(key, enc_hash, name, controller)
|
331
|
+
end
|
332
|
+
|
333
|
+
# Function to export a Lorj Account in an encrypted Hash.
|
334
|
+
#
|
335
|
+
# For details about this functions, see #Lorj::BaseDefinition.account_export
|
336
|
+
#
|
337
|
+
# * *Args* :
|
338
|
+
# - +map+ : Hash map of fields to extract. if map is nil, the
|
339
|
+
# export function will loop in the list of keys in the 'account' layer.
|
340
|
+
# - +with_name+ : True to extract :name and :provider as well.
|
341
|
+
# True by default.
|
342
|
+
#
|
343
|
+
# * *returns* :
|
344
|
+
# - key: String. Key used to encrypt.
|
345
|
+
# - env_hash: String. Base64 encrypted Hash.
|
346
|
+
# OR
|
347
|
+
# - nil if issues.
|
348
|
+
def account_export(map = nil, with_name = true)
|
349
|
+
return nil if @core_object.nil?
|
350
|
+
@core_object.account_export(map, with_name)
|
351
|
+
end
|
352
|
+
|
318
353
|
# Core parameters are:
|
319
354
|
# the_config : Optional. An instance of a configuration system which *HAVE*
|
320
355
|
# to provide get/set/exist?/[]/[]=
|
@@ -0,0 +1,168 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
#
|
19
|
+
module Lorj
|
20
|
+
# Implements account_import and account_export
|
21
|
+
# exposed by core.
|
22
|
+
class BaseDefinition
|
23
|
+
# Function to import an encrypted Hash as a Lorj Account.
|
24
|
+
#
|
25
|
+
# The encrypted Hash will be decrypted by the key provided.
|
26
|
+
# The content of the hash will be stored in the 'account' layer
|
27
|
+
# of config.
|
28
|
+
#
|
29
|
+
# The 'account' layer is not cleaned before. If you need to
|
30
|
+
# clean it up, do:
|
31
|
+
# config.ac_new(account_name, controller_name)
|
32
|
+
#
|
33
|
+
# or if the Hash data contains :name and :provider
|
34
|
+
# config.ac_erase
|
35
|
+
#
|
36
|
+
# To save it in a file, you will need to call
|
37
|
+
# config.ac_save(filename)
|
38
|
+
#
|
39
|
+
# If you pass 'name' and 'controller', ac_update will be used to update the
|
40
|
+
# account data
|
41
|
+
# If the imported data contains name and controller data, by default, it
|
42
|
+
# will call ac_update except if name is an empty string.
|
43
|
+
#
|
44
|
+
# The location used comes from PrcLib.data_path
|
45
|
+
# Passwords will be encrypted by the internal .key file stored in
|
46
|
+
# PrcLib.pdata_path
|
47
|
+
#
|
48
|
+
# The imported Hash will follow the process data model. But it won't
|
49
|
+
# verify if some data are missed for any object action (create/delete/...)
|
50
|
+
#
|
51
|
+
# * *Args* :
|
52
|
+
# - +key+ : key to use to decrypt the 'enc_hash'.
|
53
|
+
# - +enc_hash+ : Encrypted Hash.
|
54
|
+
# - +name+ : Optional. Name of the account.
|
55
|
+
# - +controller+ : Optional. Name of the controller.
|
56
|
+
#
|
57
|
+
# * *Raises* :
|
58
|
+
# No exceptions
|
59
|
+
def account_import(key, enc_hash, name = nil, controller = nil)
|
60
|
+
hash = _get_encrypted_value(enc_hash, key, 'Encrypted account data')
|
61
|
+
|
62
|
+
data = YAML.load(hash)
|
63
|
+
|
64
|
+
_update_account_meta(data, name, controller)
|
65
|
+
|
66
|
+
entr = _get_encrypt_key
|
67
|
+
|
68
|
+
data.each do |s, sh|
|
69
|
+
sh.each do |k, v|
|
70
|
+
key = "#{s}##{k}"
|
71
|
+
data_def = Lorj.data.auto_section_data(key)
|
72
|
+
if data_def && data_def[:encrypted].is_a?(TrueClass)
|
73
|
+
v = _encrypt_value(v, entr)
|
74
|
+
end
|
75
|
+
config.set(key, v, :name => 'account')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Function to export a Lorj Account in an encrypted Hash.
|
81
|
+
#
|
82
|
+
# The encrypted Hash will be encrypted by a new key returned.
|
83
|
+
# The content of the hash will built thanks to a Hash mapping
|
84
|
+
# or the list of data list in the config 'account' layer.
|
85
|
+
#
|
86
|
+
# * *Args* :
|
87
|
+
# - +map+ : Hash map of fields to extract. if map is nil, the
|
88
|
+
# export function will loop in the list of keys in the 'account' layer.
|
89
|
+
# if map is provided, following data are expected:
|
90
|
+
# - <key> : Data key to extract from config.
|
91
|
+
# - :keys: Array. SubHash tree of keys to create. If :keys is missing,
|
92
|
+
# the Data key will define the SubHash tree to use.
|
93
|
+
#
|
94
|
+
# Ex:
|
95
|
+
# map = {
|
96
|
+
# # like :keys => [credentials, auth_uri]
|
97
|
+
# 'credentials#auth_uri' => {},
|
98
|
+
# # extract from maestro but export under :server
|
99
|
+
# 'maestro#image_name' => {:keys => [:server, image_name]}
|
100
|
+
# }
|
101
|
+
# - +with_name+ : True to extract :name and :provider as well.
|
102
|
+
# True by default.
|
103
|
+
# - +account_only+ : True data extracted must come exclusively from the
|
104
|
+
# config 'account' layer.
|
105
|
+
#
|
106
|
+
# * *returns* :
|
107
|
+
# - key: String. Key used to encrypt.
|
108
|
+
# - env_hash: String. Base64 encrypted Hash.
|
109
|
+
# OR
|
110
|
+
# - nil if issues.
|
111
|
+
def account_export(map = nil, with_name = true, account_only = true)
|
112
|
+
map = _account_map if map.nil?
|
113
|
+
|
114
|
+
map.merge!('account#name' => {}, 'account#provider' => {}) if with_name
|
115
|
+
|
116
|
+
entr = _get_encrypt_key
|
117
|
+
rhash = {}
|
118
|
+
map.each do |k, v|
|
119
|
+
data_def = Lorj.data.auto_section_data(k)
|
120
|
+
|
121
|
+
if account_only
|
122
|
+
data = config.get(k, nil, :name => 'account')
|
123
|
+
else
|
124
|
+
data = config[k]
|
125
|
+
end
|
126
|
+
|
127
|
+
rhash_tree = Lorj.data.first_section(k)
|
128
|
+
rhash_tree = v[:keys] if v.key?(:keys)
|
129
|
+
if !data_def.nil? && data_def[:encrypted].is_a?(TrueClass)
|
130
|
+
data = _get_encrypted_value(data, entr, data_def[:desc])
|
131
|
+
end
|
132
|
+
rhash.rh_set(data, *rhash_tree)
|
133
|
+
end
|
134
|
+
|
135
|
+
entr = _new_encrypt_key
|
136
|
+
[entr, _encrypt_value(rhash.to_yaml, entr)]
|
137
|
+
end
|
138
|
+
|
139
|
+
private
|
140
|
+
|
141
|
+
def _update_account_meta(data, name, controller)
|
142
|
+
if name.nil? && data.rh_exist?(:account, :name)
|
143
|
+
name = data.rh_get(:account, :name)
|
144
|
+
end
|
145
|
+
if controller.nil? && data.rh_exist?(:account, :provider)
|
146
|
+
controller = data.rh_get(:account, :provider)
|
147
|
+
end
|
148
|
+
|
149
|
+
name = nil if name == ''
|
150
|
+
|
151
|
+
config.ac_update(name, controller) unless name.nil? || controller.nil?
|
152
|
+
end
|
153
|
+
|
154
|
+
def _account_map
|
155
|
+
map = {}
|
156
|
+
|
157
|
+
config.each(:name => 'account') do |s, v|
|
158
|
+
next unless v.is_a?(Hash)
|
159
|
+
v.keys.each do |k|
|
160
|
+
unless s == :account && [:name, :provider].include?(k)
|
161
|
+
map["#{s}##{k}"] = {}
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
map
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -14,6 +14,7 @@
|
|
14
14
|
|
15
15
|
require 'highline/import'
|
16
16
|
require 'encryptor'
|
17
|
+
require 'base64'
|
17
18
|
|
18
19
|
# Module Lorj which contains several classes.
|
19
20
|
#
|
@@ -27,16 +28,37 @@ require 'encryptor'
|
|
27
28
|
module Lorj
|
28
29
|
# Adding encrypt core functions.
|
29
30
|
class BaseDefinition
|
31
|
+
private
|
32
|
+
|
33
|
+
# internal runtime function to create a new key
|
34
|
+
# *parameters*:
|
35
|
+
# - +new+ : true to create a new key.
|
36
|
+
#
|
37
|
+
# *return*:
|
38
|
+
# - entropy: Hash. Entropy data used as key to encrypt values.
|
39
|
+
# Details from encryptor's gem.
|
40
|
+
# - :key: password
|
41
|
+
# - :salt : String current time number
|
42
|
+
# - :iv: Base64 random iv
|
43
|
+
def _new_encrypt_key(key = rand(36**10).to_s(36))
|
44
|
+
random_iv = OpenSSL::Cipher::Cipher.new('aes-256-cbc').random_iv
|
45
|
+
{
|
46
|
+
:key => key,
|
47
|
+
:salt => Time.now.to_i.to_s,
|
48
|
+
:iv => Base64.strict_encode64(random_iv)
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
30
52
|
# internal runtime function for process call
|
31
53
|
# Get encrypted value hidden by *
|
32
54
|
#
|
55
|
+
# Use PrcLib.pdata_path to store/read a '.key' file
|
56
|
+
#
|
33
57
|
# *parameters*:
|
34
|
-
# - +
|
35
|
-
# - +default+ : encrypted default value
|
36
|
-
# - +entropy+ : Entropy Hash
|
58
|
+
# - +new+ : true to create a new key.
|
37
59
|
#
|
38
60
|
# *return*:
|
39
|
-
# - value : encrypted value.
|
61
|
+
# - value : encrypted key value.
|
40
62
|
#
|
41
63
|
# *raise*:
|
42
64
|
#
|
@@ -45,17 +67,12 @@ module Lorj
|
|
45
67
|
key_file = File.join(PrcLib.pdata_path, '.key')
|
46
68
|
if !File.exist?(key_file)
|
47
69
|
# Need to create a random key.
|
48
|
-
|
49
|
-
entr = {
|
50
|
-
:key => rand(36**10).to_s(36),
|
51
|
-
:salt => Time.now.to_i.to_s,
|
52
|
-
:iv => Base64.strict_encode64(random_iv)
|
53
|
-
}
|
70
|
+
entr = _new_encrypt_key
|
54
71
|
|
55
72
|
Lorj.debug(2, "Writing '%s' key file", key_file)
|
56
|
-
PrcLib.
|
57
|
-
PrcLib.pdata_path
|
58
|
-
|
73
|
+
unless PrcLib.dir_exists?(PrcLib.pdata_path)
|
74
|
+
PrcLib.ensure_dir_exists(PrcLib.pdata_path)
|
75
|
+
end
|
59
76
|
File.open(key_file, 'w+') do |out|
|
60
77
|
out.write(Base64.encode64(entr.to_yaml))
|
61
78
|
end
|
@@ -118,12 +135,29 @@ module Lorj
|
|
118
135
|
:iv => Base64.strict_decode64(entr[:iv]),
|
119
136
|
:salt => entr[:salt]
|
120
137
|
)
|
121
|
-
rescue
|
122
|
-
PrcLib.error(
|
123
|
-
|
138
|
+
rescue => e
|
139
|
+
PrcLib.error("Unable to decrypt your %s.\n"\
|
140
|
+
"%s\n"\
|
141
|
+
' You will need to re-enter it.',
|
142
|
+
sDesc, e)
|
124
143
|
end
|
125
144
|
end
|
126
145
|
|
146
|
+
# Function to encrypt a data with a entr key.
|
147
|
+
#
|
148
|
+
# *return*:
|
149
|
+
# - value : encrypted value in Base64 encoded data.
|
150
|
+
def _encrypt_value(value, entr)
|
151
|
+
Base64.strict_encode64(
|
152
|
+
Encryptor.encrypt(
|
153
|
+
:value => value,
|
154
|
+
:key => entr[:key],
|
155
|
+
:iv => Base64.strict_decode64(entr[:iv]),
|
156
|
+
:salt => entr[:salt]
|
157
|
+
)
|
158
|
+
)
|
159
|
+
end
|
160
|
+
|
127
161
|
# internal runtime function for process call
|
128
162
|
# Ask encrypted function executed by _ask
|
129
163
|
#
|
@@ -132,7 +166,7 @@ module Lorj
|
|
132
166
|
# - +default+ : encrypted default value
|
133
167
|
#
|
134
168
|
# *return*:
|
135
|
-
# - value : encrypted value.
|
169
|
+
# - value : encrypted value in Base64.
|
136
170
|
#
|
137
171
|
# *raise*:
|
138
172
|
#
|
@@ -160,14 +194,7 @@ module Lorj
|
|
160
194
|
PrcLib.message('%s cannot be empty.', sDesc) if value_free == ''
|
161
195
|
end
|
162
196
|
end
|
163
|
-
|
164
|
-
Encryptor.encrypt(
|
165
|
-
:value => value_free,
|
166
|
-
:key => entr[:key],
|
167
|
-
:iv => Base64.strict_decode64(entr[:iv]),
|
168
|
-
:salt => entr[:salt]
|
169
|
-
)
|
170
|
-
)
|
197
|
+
_encrypt_value(value_free, entr)
|
171
198
|
end
|
172
199
|
end
|
173
200
|
end
|
@@ -104,5 +104,159 @@ module Lorj
|
|
104
104
|
end
|
105
105
|
controller_error '%s is not set.', key
|
106
106
|
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
# controller helper function:
|
111
|
+
# This helper controller function helps to query and object list
|
112
|
+
# from a Lorj query Hash. See query Hash details in #ctrl_query_match.
|
113
|
+
#
|
114
|
+
# * *args*:
|
115
|
+
# - +objects+ : Collection of object which respond to each
|
116
|
+
# - +query+ : Hash. Containing a list of attributes to test
|
117
|
+
# See #ctrl_do_query_match for details
|
118
|
+
# - +&block+ : block to extract the object data from a key.
|
119
|
+
def ctrl_query_each(objects, query) # :doc:
|
120
|
+
results = []
|
121
|
+
Lorj.debug(4, "Filtering with '%s'", query)
|
122
|
+
unless objects.class.method_defined?(:each)
|
123
|
+
controller_error "'%s' do not have 'each' function.", objects.class
|
124
|
+
end
|
125
|
+
objects.each do |o|
|
126
|
+
if block_given?
|
127
|
+
selected = ctrl_do_query_match(o, query) { |d, k| yield d, k }
|
128
|
+
else
|
129
|
+
selected = ctrl_do_query_match(o, query)
|
130
|
+
end
|
131
|
+
results.push o if selected
|
132
|
+
end
|
133
|
+
Lorj.debug(4, '%d records selected', results.length)
|
134
|
+
results
|
135
|
+
end
|
136
|
+
|
137
|
+
# controller helper function:
|
138
|
+
# Function to return match status
|
139
|
+
# from a list of attributes regarding a query attribute list
|
140
|
+
#
|
141
|
+
# * *args*:
|
142
|
+
# - +object+ : Object to query.
|
143
|
+
# - +query+ : Hash containing a list of attributes to test
|
144
|
+
# The query value support several cases:
|
145
|
+
# - Regexp : must Regexp.match
|
146
|
+
# - default equality : must match ==
|
147
|
+
# - +&block+ : block to extract the object data from a key.
|
148
|
+
#
|
149
|
+
# * *returns*:
|
150
|
+
# - true if this object is selected by the query.
|
151
|
+
# OR
|
152
|
+
# - false otherwise
|
153
|
+
#
|
154
|
+
# * *exception*:
|
155
|
+
# - No exception
|
156
|
+
#
|
157
|
+
# by default, this function will extract data from the object
|
158
|
+
# with followinf functions: If one fails, it will try the next one.
|
159
|
+
# :[], or :key or &block.
|
160
|
+
# The optional &block is a third way defined by the controller to extract
|
161
|
+
# data.
|
162
|
+
# The &block is defined as followed:
|
163
|
+
# * *args*:
|
164
|
+
# - +object+ : The object to get data from
|
165
|
+
# - +key+ : The key used to extract data
|
166
|
+
# * *returns*:
|
167
|
+
# - value extracted.
|
168
|
+
# * *exception*:
|
169
|
+
# - Any object exception during data extraction.
|
170
|
+
#
|
171
|
+
def ctrl_do_query_match(object, query)
|
172
|
+
selected = true
|
173
|
+
query.each do |key, match_value|
|
174
|
+
if block_given?
|
175
|
+
found, v = _get_from(object, key) { |d, k| yield d, k }
|
176
|
+
else
|
177
|
+
found, v = _get_from(object, key)
|
178
|
+
end
|
179
|
+
|
180
|
+
Lorj.debug(4, "'%s.%s' = '%s'", object.class, key, v) if found
|
181
|
+
|
182
|
+
selected = lorj_filter_regexp(v, match_value)
|
183
|
+
selected |= lorj_filter_default(v, match_value)
|
184
|
+
break unless selected
|
185
|
+
end
|
186
|
+
Lorj.debug(4, 'object selected.') if selected
|
187
|
+
selected
|
188
|
+
end
|
189
|
+
|
190
|
+
def ctrl_query_select(query, *limit)
|
191
|
+
return {} if limit.length == 0
|
192
|
+
query.select { |_k, v| limit.include?(v.class) }
|
193
|
+
end
|
194
|
+
|
195
|
+
def _get_from(data, key)
|
196
|
+
ret = nil
|
197
|
+
found = nil
|
198
|
+
|
199
|
+
[:[], key].each do |f|
|
200
|
+
found, ret = _get_from_func(data, key, f)
|
201
|
+
break if found
|
202
|
+
end
|
203
|
+
return [found, ret] if found || !block_given?
|
204
|
+
|
205
|
+
begin
|
206
|
+
Lorj.debug(4, "yield extract '%s' from '%s'", key, object.class)
|
207
|
+
return [true, yield(data, key)]
|
208
|
+
rescue
|
209
|
+
PrcLib.error("yield extract '%s' from '%s' error \n%s",
|
210
|
+
key, object.class, e)
|
211
|
+
end
|
212
|
+
[false, nil]
|
213
|
+
end
|
214
|
+
|
215
|
+
def _get_from_func(data, key, func = nil)
|
216
|
+
func = key if func.nil?
|
217
|
+
v = nil
|
218
|
+
if data.class.method_defined?(func)
|
219
|
+
begin
|
220
|
+
found = true
|
221
|
+
if key == func
|
222
|
+
Lorj.debug(5, "extract try with '%s.%s'", data.class, func)
|
223
|
+
v = data.send(func)
|
224
|
+
else
|
225
|
+
Lorj.debug(5, "extract try with '%s.%s(%s)'",
|
226
|
+
data.class, func, key)
|
227
|
+
v = data.send(func, key)
|
228
|
+
end
|
229
|
+
rescue => e
|
230
|
+
Lorj.debug(5, "'%s': error reported by '%s.%s(%s)'\n%s",
|
231
|
+
__method__, data.class, func, key, e)
|
232
|
+
found = false
|
233
|
+
end
|
234
|
+
end
|
235
|
+
[found, v]
|
236
|
+
end
|
237
|
+
# Function to check if a value match a regexp
|
238
|
+
#
|
239
|
+
# * *returns*:
|
240
|
+
# - true if the match is not a regexp, or if regexp match
|
241
|
+
# OR
|
242
|
+
# - false otherwise
|
243
|
+
#
|
244
|
+
def lorj_filter_regexp(value, match_value)
|
245
|
+
return false unless match_value.is_a?(Regexp)
|
246
|
+
|
247
|
+
return true if match_value.match(value)
|
248
|
+
false
|
249
|
+
end
|
250
|
+
|
251
|
+
# Function to check if a value match a filter value.
|
252
|
+
#
|
253
|
+
# * *returns*:
|
254
|
+
# - true if match
|
255
|
+
# OR
|
256
|
+
# - false otherwise
|
257
|
+
#
|
258
|
+
def lorj_filter_default(value, match_value)
|
259
|
+
(value == match_value)
|
260
|
+
end
|
107
261
|
end
|
108
262
|
end
|
@@ -111,6 +111,16 @@ module Lorj
|
|
111
111
|
class BaseProcess
|
112
112
|
private
|
113
113
|
|
114
|
+
def account_export(map = nil, with_name = true, account_only = false) #:doc:
|
115
|
+
fail Lorj::PrcError.new, 'No Base object loaded.' unless @base_object
|
116
|
+
@base_object.account_export(map, with_name, account_only)
|
117
|
+
end
|
118
|
+
|
119
|
+
def account_import(key, enc_hash, name = nil, controller = nil) #:doc:
|
120
|
+
fail Lorj::PrcError.new, 'No Base object loaded.' unless @base_object
|
121
|
+
@base_object.account_export(key, enc_hash, name, controller)
|
122
|
+
end
|
123
|
+
|
114
124
|
def query_cache_cleanup(sObjectType) #:doc:
|
115
125
|
fail Lorj::PrcError.new, 'No Base object loaded.' unless @base_object
|
116
126
|
@base_object.query_cleanup(sObjectType)
|
@@ -340,6 +350,8 @@ module Lorj
|
|
340
350
|
end
|
341
351
|
end
|
342
352
|
|
353
|
+
# rubocop: disable Metrics/CyclomaticComplexity
|
354
|
+
# rubocop: disable Metrics/PerceivedComplexity
|
343
355
|
def _qs_check_query_valid?(elem, key, value)
|
344
356
|
if value.is_a?(Array)
|
345
357
|
path = value.clone
|
@@ -348,11 +360,15 @@ module Lorj
|
|
348
360
|
where = elem[key].rh_get(path)
|
349
361
|
return true if where.is_a?(Array) && where.flatten.include?(v)
|
350
362
|
return true if where == v
|
363
|
+
elsif value.is_a?(Regexp)
|
364
|
+
return true if value.match(elem[key])
|
351
365
|
else
|
352
366
|
return true if elem[key] == value
|
353
367
|
end
|
354
368
|
false
|
355
369
|
end
|
370
|
+
# rubocop : enable Metrics/CyclomaticComplexity
|
371
|
+
# rubocop : enable Metrics/PerceivedComplexity
|
356
372
|
|
357
373
|
def _qs_info_init(sInfoMsg)
|
358
374
|
info = {
|