Class: Arcanus::Chest
- Inherits:
-
Object
- Object
- Arcanus::Chest
- Defined in:
- lib/arcanus/chest.rb
Overview
Encapsulates the collection of encrypted secrets managed by Arcanus.
Defined Under Namespace
Classes: Item
Constant Summary collapse
- SIGNATURE_SIZE_BITS =
rubocop:disable Metrics/ClassLength
256
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the chest as if it were a hash.
-
#contents ⇒ Object
Returns the contents of the chest as a hash.
-
#fetch(*args) ⇒ Object
Fetch key from the chest as if it were a hash.
-
#get(key_path) ⇒ Object
Get value at the specified key path.
-
#initialize(key:, chest_file_path:) ⇒ Chest
constructor
A new instance of Chest.
-
#method_missing(method_sym) ⇒ Object
Provides access to chest items using regular method calls instead of hash accesses.
-
#save ⇒ Object
For each key in the chest, encrypt the new value if it has changed.
-
#set(key_path, value) ⇒ Object
Set value for the specified key path.
- #update(new_hash) ⇒ Object
Constructor Details
#initialize(key:, chest_file_path:) ⇒ Chest
Returns a new instance of Chest.
11 12 13 14 15 16 17 |
# File 'lib/arcanus/chest.rb', line 11 def initialize(key:, chest_file_path:) @key = key @chest_file_path = chest_file_path @original_encrypted_hash = YAML.load_file(chest_file_path).to_hash @original_decrypted_hash = decrypt_hash(@original_encrypted_hash) @hash = Utils.deep_dup(@original_decrypted_hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym) ⇒ Object
Provides access to chest items using regular method calls instead of hash accesses.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/arcanus/chest.rb', line 39 def method_missing(method_sym, *) method_name = method_sym.to_s if @hash.key?(method_name) value = @hash[method_name] if value.is_a?(Hash) Item.new(value, [method_name]) else value end else raise KeyError, "Key '#{method_name}' does not exist in this Arcanus chest", caller end end |
Instance Method Details
#[](key) ⇒ Object
Access the chest as if it were a hash.
23 24 25 |
# File 'lib/arcanus/chest.rb', line 23 def [](key) @hash[key] end |
#contents ⇒ Object
Returns the contents of the chest as a hash.
33 34 35 |
# File 'lib/arcanus/chest.rb', line 33 def contents @hash end |
#fetch(*args) ⇒ Object
Fetch key from the chest as if it were a hash.
28 29 30 |
# File 'lib/arcanus/chest.rb', line 28 def fetch(*args) @hash.fetch(*args) end |
#get(key_path) ⇒ Object
Get value at the specified key path.
72 73 74 75 76 77 78 |
# File 'lib/arcanus/chest.rb', line 72 def get(key_path) keys = key_path.split('.') keys.inject(@hash) { |hash, key| hash[key] } rescue NoMethodError raise Arcanus::Errors::InvalidKeyPathError, "Key path '#{key_path}' does not correspond to an actual key" end |
#save ⇒ Object
For each key in the chest, encrypt the new value if it has changed.
The goal is to create a file where the only lines that differ are the keys that changed.
88 89 90 91 92 93 94 95 96 |
# File 'lib/arcanus/chest.rb', line 88 def save modified_hash = process_hash_changes(@original_encrypted_hash, @original_decrypted_hash, @hash) File.open(@chest_file_path, 'w') do |f| f.puts('# Do not edit this file directly! Run `arcanus edit`') f.write(modified_hash.to_yaml) end end |
#set(key_path, value) ⇒ Object
Set value for the specified key path.
59 60 61 62 63 64 65 66 |
# File 'lib/arcanus/chest.rb', line 59 def set(key_path, value) keys = key_path.split('.') nested_hash = keys[0..-2].inject(@hash) { |hash, key| hash[key] } nested_hash[keys[-1]] = value rescue NoMethodError raise Arcanus::Errors::InvalidKeyPathError, "Key path '#{key_path}' does not correspond to an actual key" end |
#update(new_hash) ⇒ Object
80 81 82 |
# File 'lib/arcanus/chest.rb', line 80 def update(new_hash) @hash = new_hash end |