Class: Sym::MagicFile
Overview
This class provides a very simple API for loading/reading encrypted files into memory buffers, while supporting all of the convenience features of the sym CLI.
You initialize this class with just two things: a pathname to a file (encrypted or not), and the key identifier. The identifier can either be a filename, or OS-X Keychain entry, or environment variable name, etc — basically it is resolved like any other ‘-k <value>` CLI flag.
Example
In this example, we assume that the environment variable $PRIVATE_KEY contain the key to be used in decryption. Note that methods decrypt
and read
are synomymous
require 'sym/magic_file'
magic = Sym::MagicFile.new('/usr/local/etc/secrets.yml.enc', 'PRIVATE_KEY')
YAML.load(magic.read)
Or, lets say you are using the config
gem. Then you would do something like this:
require 'config'
Settings.add_source!(YAML.load(magic.decrypt))
Instance Attribute Summary collapse
-
#action ⇒ Object
writeonly
Sets the attribute action.
-
#key_value ⇒ Object
Returns the value of attribute key_value.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#pathname ⇒ Object
Returns the value of attribute pathname.
Instance Method Summary collapse
-
#decrypt ⇒ Object
Returns decrypted string.
-
#decrypt_to(filename) ⇒ Object
Decrypts
pathname
to afilename
. -
#encrypt ⇒ Object
Returns encrypted string.
-
#encrypt_to(filename) ⇒ Object
Encrypts
pathname
to afilename
. -
#initialize(pathname, key_value, **opts) ⇒ MagicFile
constructor
A new instance of MagicFile.
-
#read ⇒ Object
Returns decrypted string.
Constructor Details
#initialize(pathname, key_value, **opts) ⇒ MagicFile
Returns a new instance of MagicFile.
30 31 32 |
# File 'lib/sym/magic_file.rb', line 30 def initialize(pathname, key_value, **opts) init(key_value, opts, pathname) end |
Instance Attribute Details
#action=(value) ⇒ Object
Sets the attribute action
28 29 30 |
# File 'lib/sym/magic_file.rb', line 28 def action=(value) @action = value end |
#key_value ⇒ Object
Returns the value of attribute key_value.
28 29 30 |
# File 'lib/sym/magic_file.rb', line 28 def key_value @key_value end |
#opts ⇒ Object
Returns the value of attribute opts.
28 29 30 |
# File 'lib/sym/magic_file.rb', line 28 def opts @opts end |
#pathname ⇒ Object
Returns the value of attribute pathname.
28 29 30 |
# File 'lib/sym/magic_file.rb', line 28 def pathname @pathname end |
Instance Method Details
#decrypt ⇒ Object
Returns decrypted string
58 59 60 61 |
# File 'lib/sym/magic_file.rb', line 58 def decrypt self.opts.merge!({ decrypt: true }) action end |
#decrypt_to(filename) ⇒ Object
Decrypts pathname
to a filename
46 47 48 49 |
# File 'lib/sym/magic_file.rb', line 46 def decrypt_to(filename) self.opts.merge!({output: filename}) decrypt end |
#encrypt ⇒ Object
Returns encrypted string
52 53 54 55 |
# File 'lib/sym/magic_file.rb', line 52 def encrypt self.opts.merge!({ encrypt: true }) action end |
#encrypt_to(filename) ⇒ Object
Encrypts pathname
to a filename
40 41 42 43 |
# File 'lib/sym/magic_file.rb', line 40 def encrypt_to(filename) self.opts.merge!({output: filename}) encrypt end |
#read ⇒ Object
Returns decrypted string
35 36 37 |
# File 'lib/sym/magic_file.rb', line 35 def read decrypt end |