Module: ConfigurationHelper
- Defined in:
- lib/sapis/configuration_helper.rb
Overview
<configuration_helper.rb> - Part of Sav’s APIs. Copyright © 2011 Saverio Miroddi
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Constant Summary collapse
- CONFIGURATION_FILE =
File.('.sav_scripts', '~')
- PASSWORD_KEY_FILE =
File.('.sav_scripts_key', '~')
- PASSWORD_CIPHER =
The encryption is intentionally weak.
'des'- PASSWORD_KEY =
IO.read(PASSWORD_KEY_FILE).chomp
Class Method Summary collapse
- .decrypt(ciphertext, key, algo, options = {}) ⇒ Object
- .encrypt(plaintext, key, algo, options = {}) ⇒ Object
-
.load_configuration(options = {}) ⇒ Object
Loads the configuration from a file, for a given group.
-
.load_configuration_values(*keys) ⇒ Object
Shortcut for the previous method.
Class Method Details
.decrypt(ciphertext, key, algo, options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sapis/configuration_helper.rb', line 109 def self.decrypt(ciphertext, key, algo, ={}) puts ">>> ConfigurationHelper.decrypt should have key and algo as options" base_64_decoding = !![:base_64_decoding] ciphertext = Base64.decode64(ciphertext) if base_64_decoding cipher = OpenSSL::Cipher::Cipher.new(algo) cipher.decrypt cipher.key = key cipher.iv = ciphertext.slice!(0, cipher.iv_len) plaintext = cipher.update(ciphertext) + cipher.final plaintext end |
.encrypt(plaintext, key, algo, options = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sapis/configuration_helper.rb', line 91 def self.encrypt(plaintext, key, algo, ={}) base_64_encoding = !![:base_64_encoding] cipher = OpenSSL::Cipher::Cipher.new(algo) cipher.encrypt iv = cipher.random_iv cipher.key = key cipher.iv = iv ciphertext = iv + cipher.update(plaintext) + cipher.final ciphertext = Base64.encode64(ciphertext) if base_64_encoding ciphertext end |
.load_configuration(options = {}) ⇒ Object
Loads the configuration from a file, for a given group.
Returns a Hash with an extra method: :path(key), which interprets the value as an absolute path if it starts with a slash (/), and as a relative to home if it doesn’t.
options:
:group manually specify the group; defaults to the calling script.
:sym_keys keys as symbols
:file configuration filename
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sapis/configuration_helper.rb', line 45 def self.load_configuration(={}) $stderr.puts ">>> MIGRATE TO SIMPLE_SCRIPTING::CONFIG!" raise "Change argument to :group if needed (when the filename is different from the group)" if .is_a?(String) group = [:group] || File.basename($0).chomp('.rb') sym_keys = [:sym_keys] configuration_file = [:file] || CONFIGURATION_FILE configuration = ParseConfig.new(configuration_file)[group] raise "Group not found in configuration: #{group}" if configuration.nil? if configuration['password'] configuration['password'] = decrypt(configuration['password'], PASSWORD_KEY, PASSWORD_CIPHER, :base_64_decoding => true) end configuration = Hash[configuration.map{ |key, value| [key.to_sym, value] }] if sym_keys def configuration.path(key) raw_value = self[key] raw_value.start_with?('/') ? raw_value : File.(raw_value, '~') end configuration end |
.load_configuration_values(*keys) ⇒ Object
Shortcut for the previous method.
Returns a String when querying only one key, otherwise an Array.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sapis/configuration_helper.rb', line 77 def self.load_configuration_values(*keys) configuration = load_configuration values = keys.inject([]) do |current_values, key| current_values << configuration[key] end if values.size == 1 values.first else values end end |