Class: SecretStore

Inherits:
Object
  • Object
show all
Defined in:
lib/secret_store.rb,
lib/secret_store/version.rb

Defined Under Namespace

Classes: ReadOnly, ReadOnlyYamlBackend, YamlBackend

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password, file_path, options = {}) ⇒ SecretStore

Returns a new instance of SecretStore.



9
10
11
12
13
# File 'lib/secret_store.rb', line 9

def initialize(password, file_path, options = {})
  self.password = password
  backend_class = options.fetch(:backend_class, YamlBackend)
  @data = backend_class.new(file_path)
end

Class Method Details

.new_read_only(password, file_path) ⇒ Object



15
16
17
18
# File 'lib/secret_store.rb', line 15

def self.new_read_only(password, file_path)
  store = new(password, file_path, :backend_class => ReadOnlyYamlBackend)
  store
end

Instance Method Details

#change_password(new_password) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/secret_store.rb', line 42

def change_password(new_password)
  unless @data.permits_writes?
    raise ReadOnly
  end

  decrypted = decrypted_data
  self.password = new_password
  replace_with_decrypted(decrypted)
end

#encrypt(secret) ⇒ Object



38
39
40
# File 'lib/secret_store.rb', line 38

def encrypt(secret)
  cipher.encrypt(secret).chomp
end

#get(key) ⇒ Object



28
29
30
31
32
# File 'lib/secret_store.rb', line 28

def get(key)
  if ciphertext = @data[key]
    cipher.decrypt(ciphertext)
  end
end

#get!(key) ⇒ Object



34
35
36
# File 'lib/secret_store.rb', line 34

def get!(key)
  get(key) or raise IndexError.new(%Q[key not found: "#{key}"])
end

#store(key, secret) ⇒ Object



20
21
22
# File 'lib/secret_store.rb', line 20

def store(key, secret)
  @data.insert(key, encrypt(secret))
end

#store!(key, secret) ⇒ Object



24
25
26
# File 'lib/secret_store.rb', line 24

def store!(key, secret)
  @data.overwrite(key, encrypt(secret))
end