Class: Sekrat::Warehouse::FileSystem

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/sekrat/warehouse/file_system.rb,
lib/sekrat/warehouse/file_system/version.rb

Overview

A Sekrat::Warehouse implementation

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir: File.expand_path('.')) ⇒ FileSystem

Instantiate a new FileSystem warehouse with a base directory for secret storage.

Parameters:

  • basedir: (String) (defaults to: File.expand_path('.'))

    the filesystem directory to use as the base for secret storage



19
20
21
# File 'lib/sekrat/warehouse/file_system.rb', line 19

def initialize(basedir: File.expand_path('.'))
  @basedir = basedir
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



13
14
15
# File 'lib/sekrat/warehouse/file_system.rb', line 13

def basedir
  @basedir
end

Instance Method Details

#idsArray<String>

Get the list of secret IDs known to the warehouse

Returns:

  • (Array<String>)

    the secret IDs



25
26
27
28
29
# File 'lib/sekrat/warehouse/file_system.rb', line 25

def ids
  Dir["#{basedir}/**/*"].
    select {|path| File.file?(path)}.
    map {|path| path.gsub(/^#{Regexp.escape(basedir)}\//, '')}
end

#retrieve(id) ⇒ String

Given a secret ID, attempt to retrieve and return the secret data.

Parameters:

  • id (String)

    the secret ID

Returns:

  • (String)

    the secret data

Raises:

  • (Sekrat::NotFound)

    if the warehouse does not contain the requested secret

  • (Sekrat::Error)

    if there any unspecific retrieval issues



58
59
60
61
62
63
64
65
66
67
# File 'lib/sekrat/warehouse/file_system.rb', line 58

def retrieve(id)
  file = filename(id)
  raise Sekrat::NotFound.new("'#{id}'") unless File.exist?(file)

  begin
    Base64.decode64(File.read(file))
  rescue
    raise Sekrat::Error.new("could not read secret '#{id}'")
  end
end

#store(id, data) ⇒ String

Given a secret ID and secret data, save the data to the filesystem, indexed by the ID.

New entries are saved outright, and reusing an ID will overwrite the old data.

Parameters:

  • id (String)

    the secret ID

  • data (String)

    the secret data

Returns:

  • (String)

    the original data passed in

Raises:

  • (Sekrat::StorageFailure)

    if there any any problems along the way



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sekrat/warehouse/file_system.rb', line 40

def store(id, data)
  begin
    file = filename(id)
    FileUtils.mkdir_p(File.dirname(file))
    out = File.open(file, 'w')
    out.write(Base64.encode64(data))
    out.close
  rescue
    raise Sekrat::StorageFailure.new("couldn't save")
  end
end