ActiveSupport::EncryptedFile (original) (raw)

Namespace

Methods

C

G

K

N

R

W

Constants

Attributes

[R] content_path
[R] env_key
[R] key_path
[R] raise_if_missing_key

Class Public methods

Source: show | on GitHub

def self.generate_key SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(CIPHER)) end

Source: show | on GitHub

def initialize(content_path:, key_path:, env_key:, raise_if_missing_key:) @content_path = Pathname.new(content_path).yield_self { |path| path.symlink? ? path.realpath : path } @key_path = Pathname.new(key_path) @env_key, @raise_if_missing_key = env_key, raise_if_missing_key end

Instance Public methods

Source: show | on GitHub

def change(&block) writing read, &block end

Returns the encryption key, first trying the environment variable specified by env_key, then trying the key file specified by key_path. If raise_if_missing_key is true, raises MissingKeyError if the environment variable is not set and the key file does not exist.

Source: show | on GitHub

def key read_env_key || read_key_file || handle_missing_key end

Source: show | on GitHub

def read if !key.nil? && content_path.exist? decrypt content_path.binread.strip else raise MissingContentError, content_path end end

Source: show | on GitHub

def write(contents) IO.binwrite "#{content_path}.tmp", encrypt(contents) FileUtils.mv "#{content_path}.tmp", content_path end