Module: Vault::Rails

Defined in:
lib/vault/rails.rb,
lib/vault/rails/errors.rb,
lib/vault/rails/version.rb,
lib/vault/rails/serializer.rb,
lib/vault/rails/configurable.rb

Defined Under Namespace

Modules: Configurable, JSONSerializer Classes: UnknownSerializerError, ValidationFailedError, VaultRailsError

Constant Summary collapse

SERIALIZERS =

The list of serializers.

Returns:

  • (Hash<Symbol, Module>)
{
  json: Vault::Rails::JSONSerializer,
}.freeze
DEFAULT_ENCODING =

The default encoding.

Returns:

  • (String)
"utf-8".freeze
DEV_WARNING =

The warning string to print when running in development mode.

"[vault-rails] Using in-memory cipher - this is not secure " \
"and should never be used in production-like environments!".freeze
VERSION =
"0.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientVault::Client (readonly)

API client object based off the configured options in Configurable.

Returns:

  • (Vault::Client)


34
35
36
# File 'lib/vault/rails.rb', line 34

def client
  @client
end

Class Method Details

.decrypt(path, key, ciphertext, client = self.client) ⇒ String

Decrypt the given ciphertext data using the provided mount and key.

Parameters:

  • path (String)

    the mount point

  • key (String)

    the key to decrypt at

  • ciphertext (String)

    the ciphertext to decrypt

  • client (Vault::Client) (defaults to: self.client)

    the Vault client to use

Returns:

  • (String)

    the decrypted plaintext text



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/vault/rails.rb', line 107

def decrypt(path, key, ciphertext, client = self.client)
  if ciphertext.blank?
    return ciphertext
  end

  path = path.to_s if !path.is_a?(String)
  key  = key.to_s if !key.is_a?(String)

  with_retries do
    if self.enabled?
      result = self.vault_decrypt(path, key, ciphertext, client)
    else
      result = self.memory_decrypt(path, key, ciphertext, client)
    end

    return self.force_encoding(result)
  end
end

.encrypt(path, key, plaintext, client = self.client) ⇒ String

Encrypt the given plaintext data using the provided mount and key.

Parameters:

  • path (String)

    the mount point

  • key (String)

    the key to encrypt at

  • plaintext (String)

    the plaintext to encrypt

  • client (Vault::Client) (defaults to: self.client)

    the Vault client to use

Returns:

  • (String)

    the encrypted cipher text



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vault/rails.rb', line 75

def encrypt(path, key, plaintext, client = self.client)
  if plaintext.blank?
    return plaintext
  end

  path = path.to_s if !path.is_a?(String)
  key  = key.to_s if !key.is_a?(String)

  with_retries do
    if self.enabled?
      result = self.vault_encrypt(path, key, plaintext, client)
    else
      result = self.memory_encrypt(path, key, plaintext, client)
    end

    return self.force_encoding(result)
  end
end

.method_missing(m, *args, &block) ⇒ Object

Delegate all methods to the client object, essentially making the module object behave like a Client.



49
50
51
52
53
54
55
# File 'lib/vault/rails.rb', line 49

def method_missing(m, *args, &block)
  if client.respond_to?(m)
    client.public_send(m, *args, &block)
  else
    super
  end
end

.respond_to_missing?(m, include_private = false) ⇒ Boolean

Delegating ‘respond_to` to the Client.

Returns:

  • (Boolean)


58
59
60
# File 'lib/vault/rails.rb', line 58

def respond_to_missing?(m, include_private = false)
  client.respond_to?(m, include_private) || super
end

.serializer_for(key) ⇒ ~Serializer

Get the serializer that corresponds to the given key. If the key does not correspond to a known serializer, an exception will be raised.

Parameters:

  • key (#to_sym)

    the name of the serializer

Returns:

  • (~Serializer)


133
134
135
136
137
138
139
140
141
# File 'lib/vault/rails.rb', line 133

def serializer_for(key)
  key = key.to_sym if !key.is_a?(Symbol)

  if serializer = SERIALIZERS[key]
    return serializer
  else
    raise Vault::Rails::UnknownSerializerError.new(key)
  end
end

.setup!Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/vault/rails.rb', line 36

def setup!
  Vault.setup!

  @client = Vault.client
  @client.class.instance_eval do
    include Vault::Rails::Configurable
  end

  self
end