Class: Itamae::Secrets::Store
- Inherits:
-
Object
- Object
- Itamae::Secrets::Store
- Defined in:
- lib/itamae/secrets/store.rb
Instance Attribute Summary collapse
-
#base_dir ⇒ Object
readonly
Returns the value of attribute base_dir.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(*args) ⇒ Object
- #fetch(*args) ⇒ Object
-
#initialize(base_dir) ⇒ Store
constructor
A new instance of Store.
- #keychain ⇒ Object
- #keychain_path ⇒ Object
- #store(name, value, key = 'default') ⇒ Object
- #values_path ⇒ Object
Constructor Details
#initialize(base_dir) ⇒ Store
Returns a new instance of Store.
10 11 12 13 |
# File 'lib/itamae/secrets/store.rb', line 10 def initialize(base_dir) @base_dir = Pathname.new(base_dir) ensure_base_dir! end |
Instance Attribute Details
#base_dir ⇒ Object (readonly)
Returns the value of attribute base_dir.
15 16 17 |
# File 'lib/itamae/secrets/store.rb', line 15 def base_dir @base_dir end |
Instance Method Details
#[](name) ⇒ Object
29 30 31 |
# File 'lib/itamae/secrets/store.rb', line 29 def [](name) fetch(name, nil) end |
#[]=(*args) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/itamae/secrets/store.rb', line 56 def []=(*args) case args.size when 2 store(*args) when 3 store(args[0], args[2], args[1]) else raise ArgumentError, "wrong number of arguments (#{args.size} for 2..3)" end end |
#fetch(*args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/itamae/secrets/store.rb', line 33 def fetch(*args) if args.size > 2 raise ArgumentError, "wrong number of arguments (#{args.size} for 1..2)" end name = args[0].to_s validate_name!(name) value_path = values_path.join(name) if value_path.exist? encrypted_data = Decryptor.load_json(value_path.read) encrypted_data.key = keychain.load(encrypted_data.key_name) JSON.parse(encrypted_data.plaintext)['value'] else if args.size == 1 raise KeyError, "key not found: #{name}" else args[1] end end end |
#keychain ⇒ Object
25 26 27 |
# File 'lib/itamae/secrets/store.rb', line 25 def keychain @keychain ||= Keychain.new(keychain_path) end |
#keychain_path ⇒ Object
17 18 19 |
# File 'lib/itamae/secrets/store.rb', line 17 def keychain_path base_dir.join('keys') end |
#store(name, value, key = 'default') ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/itamae/secrets/store.rb', line 67 def store(name, value, key = 'default') name = name.to_s validate_name!(name) value_path = values_path.join(name) encrypted_data = Encryptor.new({value: value}.to_json, keychain.load(key)) open(value_path, 'w', 0600) do |io| io.puts encrypted_data.to_s end end |
#values_path ⇒ Object
21 22 23 |
# File 'lib/itamae/secrets/store.rb', line 21 def values_path base_dir.join('values') end |