Class: Sym::MagicFile

Inherits:
Object show all
Defined in:
lib/sym/magic_file.rb

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

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute action to.


28
29
30
# File 'lib/sym/magic_file.rb', line 28

def action=(value)
  @action = value
end

#key_valueObject

Returns the value of attribute key_value


28
29
30
# File 'lib/sym/magic_file.rb', line 28

def key_value
  @key_value
end

#optsObject

Returns the value of attribute opts


28
29
30
# File 'lib/sym/magic_file.rb', line 28

def opts
  @opts
end

#pathnameObject

Returns the value of attribute pathname


28
29
30
# File 'lib/sym/magic_file.rb', line 28

def pathname
  @pathname
end

Instance Method Details

#decryptObject

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

#encryptObject

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

#readObject

Returns decrypted string


35
36
37
# File 'lib/sym/magic_file.rb', line 35

def read
  decrypt
end