-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from hashicorp/VLTE-single_decrypt
vault_single_decrypt flag to allow for the decryption of single attributes
- Loading branch information
Showing
4 changed files
with
189 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,7 +180,7 @@ vault_attribute :credit_card, | |
- **Note** Changing this value for an existing application will make existing values no longer decryptable! | ||
|
||
#### Lazy attribute decryption | ||
By default, `vault-rails` will decrypt a record’s encrypted attributes on that record’s initializarion. You can configure an encrypted model to decrypt attributes lazily, which will prevent communication with Vault until an encrypted attribute’s getter method is called, at which point all of the record’s encrypted attributes will be decrypted. This is useful if you do not always need access to encrypted attributes. For example: | ||
By default, `vault-rails` will decrypt a record’s encrypted attributes on that record’s initialization. You can configure an encrypted model to decrypt attributes lazily, which will prevent communication with Vault until an encrypted attribute’s getter method is called, at which point all of the record’s encrypted attributes will be decrypted. This is useful if you do not always need access to encrypted attributes. For example: | ||
|
||
|
||
```ruby | ||
|
@@ -202,6 +202,33 @@ person.ssn # Vault communication happens here | |
# => "123-45-6789" | ||
``` | ||
|
||
#### Single, lazy attribute decryption | ||
By default, `vault-rails` will decrypt all encrypted attributes on that record’s initialization on a class by class basis. You can configure an encrypted model to decrypt attributes lazily and and individually. This will prevent vault from loading all vault_attributes defined on a class the moment one attribute is requested. | ||
|
||
|
||
```ruby | ||
class Person < ActiveRecord::Base | ||
include Vault::EncryptedModel | ||
vault_lazy_decrypt! | ||
vault_single_decrypt! | ||
vault_attribute :ssn | ||
vault_attribute :email | ||
end | ||
# Without vault_single_decrypt: | ||
person = Person.find(id) # Vault communication happens here | ||
person.ssn # Vault communication happens here, fetches both ssn and email | ||
# => "123-45-6789" | ||
# With vault_single_decrypt: | ||
person = Person.find(id) | ||
person.ssn # Vault communication happens here, fetches only ssn | ||
# => "123-45-6789" | ||
person.email # Vault communication happens here, fetches only email | ||
# => "[email protected]" | ||
``` | ||
|
||
#### Serialization | ||
|
||
By default, all values are assumed to be "text" fields in the database. Sometimes it is beneficial for your application to work with a more flexible data structure (such as a Hash or Array). Vault-rails can automatically serialize and deserialize these structures for you: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
class LazySinglePerson < ActiveRecord::Base | ||
include Vault::EncryptedModel | ||
|
||
self.table_name = "people" | ||
|
||
vault_lazy_decrypt! | ||
vault_single_decrypt! | ||
|
||
vault_attribute :ssn | ||
|
||
vault_attribute :credit_card, | ||
encrypted_column: :cc_encrypted | ||
|
||
def encryption_context | ||
"user_#{id}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters