Class: Sekrat::Warehouse::FileSystem
- Inherits:
-
Object
- Object
- Sekrat::Warehouse::FileSystem
- 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
-
#basedir ⇒ Object
readonly
Returns the value of attribute basedir.
Instance Method Summary collapse
-
#ids ⇒ Array<String>
Get the list of secret IDs known to the warehouse.
-
#initialize(basedir: File.expand_path('.')) ⇒ FileSystem
constructor
Instantiate a new FileSystem warehouse with a base directory for secret storage.
-
#retrieve(id) ⇒ String
Given a secret ID, attempt to retrieve and return the secret data.
-
#store(id, data) ⇒ String
Given a secret ID and secret data, save the data to the filesystem, indexed by the ID.
Constructor Details
#initialize(basedir: File.expand_path('.')) ⇒ FileSystem
Instantiate a new FileSystem warehouse with a base directory for secret storage.
19 20 21 |
# File 'lib/sekrat/warehouse/file_system.rb', line 19 def initialize(basedir: File.('.')) @basedir = basedir end |
Instance Attribute Details
#basedir ⇒ Object (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
#ids ⇒ Array<String>
Get the list of secret IDs known to the warehouse
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.
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.
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 |