Module: Vault::Rails

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

Defined Under Namespace

Modules: Configurable, JSONSerializer Classes: InvalidCiphertext, 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
DEV_PREFIX =
"vault:dev:".freeze
VERSION =
"0.7.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)


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

def client
  @client
end

Class Method Details

.decrypt(path, key, ciphertext, client: self.client, context: nil) ⇒ 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



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

def decrypt(path, key, ciphertext, client: self.client, context: nil)
  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: client, context: context)
    else
      result = self.memory_decrypt(path, key, ciphertext, client: client, context: context)
    end

    return self.force_encoding(result)
  end
end

.encrypt(path, key, plaintext, client: self.client, context: nil) ⇒ 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



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

def encrypt(path, key, plaintext, client: self.client, context: nil)
  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: client, context: context)
    else
      result = self.memory_encrypt(path, key, plaintext, client: client, context: context)
    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.



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

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)


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

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)


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

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



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

def setup!
  Vault.setup!

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

  self
end

.transform_decode(ciphertext, opts = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vault/rails.rb', line 157

def transform_decode(ciphertext, opts={})
  return ciphertext if ciphertext&.empty?
  request_opts = {}
  request_opts[:value] = ciphertext

  if opts[:transformation]
    request_opts[:transformation] = opts[:transformation]
  end

  role_name = transform_role_name(opts)
  puts request_opts
  client.transform.decode(role_name: role_name, **request_opts)
end

.transform_encode(plaintext, opts = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vault/rails.rb', line 144

def transform_encode(plaintext, opts={})
  return plaintext if plaintext&.empty?
  request_opts = {}
  request_opts[:value] = plaintext

  if opts[:transformation]
    request_opts[:transformation] = opts[:transformation]
  end

  role_name = transform_role_name(opts)
  client.transform.encode(role_name: role_name, **request_opts)
end