Commit Graph

20614 Commits

Author SHA1 Message Date
Jorge Manrubia
1fff866dd1 Exclude encryption performance tests from isolated runs
Encryption performance tests were meant to be excluded from the builds,
however they were still being executed when running `isolated:` tests
for active record adapters.

See https://buildkite.com/rails/rails/builds/76249#e7605d50-87be-445e-a9ae-a5761c996597/1512-1531
2021-04-03 23:11:30 +02:00
George Claghorn
432bc68a2c Fix more Book references.
Follow-up to 434fb39.
2021-04-03 08:31:59 -04:00
George Claghorn
b4120d0a30 Fix lingering references to Book
Follow-up to 434fb39.
2021-04-03 08:20:42 -04:00
Ricardo Díaz
434fb39d47
Add new "encrypted_books" table to the schema
Reusing the "books" one could cause interferences when fixtures are
loaded in a very specific order such as:

https://buildkite.com/rails/rails/builds/76217#ee4ce591-e6c1-4a0d-a7db-1f83647d141e

Reproduction script:

```
activerecord $ bin/test -v --seed 23607 -n "/^(?:EagerAssociationTest#(?:test_preloading_a_regular_association_with_a_typo_through_a_polymorphic_association_still_raises)|ActiveRecord::Encryption::EncryptableFixtureTest#(?:test_fixtures_get_encrypted_automatically)|ViewWithoutPrimaryKeyTest#(?:test_attributes|test_reading))$/"
```
2021-04-03 08:00:01 -04:00
Jorge Manrubia
224c9b34cb
Add Active Record encryption to CHANGELOG 2021-04-02 07:40:00 -04:00
Ryuta Kamizono
7e1820d71b
Merge pull request #41818 from ricardotk002/fix-missing-require
Fix seemingly wrong require "lib"
2021-04-02 19:56:11 +09:00
Ricardo Díaz
27b0180232 Remove print statement / uncomment relevant assertion 2021-04-02 01:03:43 -05:00
Ricardo Díaz
b11645eb2b Fix wrong require
This library is used here

https://github.com/rails/rails/blob/1251703/activerecord/lib/active_record/encryption/encryptor.rb#L121

and here

https://github.com/rails/rails/blob/1251703/activerecord/lib/active_record/encryption/encryptor.rb#L135

Reference https://buildkite.com/rails/rails/builds/76198#de7db4af-b8ca-46de-986f-0db34b1d4ddc
2021-04-02 01:00:37 -05:00
Sampat Badhe
2032daa415
Fix typos encryption doc (#41817)
* fix typo

[ci skip]
2021-04-02 14:24:43 +09:00
Jorge Manrubia
5a6352c072 Fix deterministic queries that were broken after #41068
This is adding yet another patch to make them work. This system needs to
be reworked as it's currently very brittle.
2021-04-01 22:10:59 +02:00
Jorge Manrubia
e24fb5524a Validate that proper keys are configured when declaring attributes
This enables to disable deterministic encryption by just not setting
deterministic_key.
2021-04-01 18:20:54 +02:00
Jorge Manrubia
2035138115 Use proper hook for loading fixtures extension 2021-04-01 15:02:15 +02:00
Jorge Manrubia
a61692cf41 Add support for uniqueness validations 2021-04-01 15:02:15 +02:00
Jorge Manrubia
f78a480818 Encourage deterministic encryption to remain unchanged
This implements several changes to encourage deterministic encryption to
remain unchanged. The main motivation is letting you define unique
indexes on deterministically-encrypted columns:

- By default, deterministic encryption will always use the oldest
encryption scheme to encrypt new data, when there are many.
- You can skip this default behavior and make it always use the current
encryption scheme with:

```ruby
deterministic: { fixed: false } # using this should be a rare need
```

- Deterministic encryption still supports previous encryption schemes
normally. So they will be used to add additional values to queries, for
example.
- You can't rotate deterministic encryption keys anymore. We can add
support for that in the future.

This makes for reasonable defaults:

- People using "deterministic: true" will get unique indexes working out
of the box.
- The system will encourage keeping deterministic encryption stable:
  - By always using oldest encryption schemes
  - By forbidding configuring multiple keys

But you can still opt-out of the default if you need to.
2021-04-01 15:02:15 +02:00
Jorge Manrubia
7a1fb99302 Add support to declare previous encryption schemes globally 2021-04-01 15:02:15 +02:00
Jorge Manrubia
d35905ccae Add Scheme, a container for encrypted attribute configs
This adds a new class `Scheme` that encapsulates the encryption
properties for a given attribute. This adds a proper code representation
for a domain concept we keep referring to: encryption schemes.

It's in charge of processing the user-passed config properties when
declaring the attribute, as well as of validating those.

This removes the concern of processing properties from
`EncryptableRecord` and `EncryptableAttributeType`. It's also nice to
have a place to group attribute encryption options, versus passing
lists of hashes around.

This is in preparation to upcoming changes that will add new config
options and support for previous encryption schemes.
2021-04-01 15:02:15 +02:00
Jorge Manrubia
28145c3cee Rename master_key => primary_key 2021-04-01 15:02:15 +02:00
Jorge Manrubia
c275b10a0e Show performance tests results now that they are excluded from the build 2021-04-01 15:02:15 +02:00
Jorge Manrubia
bcf6a5fafb Fix test
The .encrypts declaration in `ActionText::Encryption` was making this test
fail. It was failing to load a MySQL error that was being raised when trying
to determine the column limit.
2021-04-01 15:02:15 +02:00
Jorge Manrubia
e4dae63013 Remove before_initialize, I think it's not needed 2021-04-01 15:02:15 +02:00
Jorge Manrubia
a786e34ab2 Move to on_load hook to see if it helps with railty test failing 2021-04-01 15:02:15 +02:00
Jorge Manrubia
bf75702b1c Inline prepend 2021-04-01 15:02:15 +02:00
Jorge Manrubia
9556720527 Wait for active record to be loaded to extend it 2021-04-01 15:02:15 +02:00
Jorge Manrubia
588c972d21 Removes the Action Text dependency from Active Record
Move the Action Text stuff to its own concern in `actiontext`.
2021-04-01 15:02:15 +02:00
Jorge Manrubia
c41b354bf0 Remove mass encryption
I don't think this belong to the library yet. It's more like an util class we used
for building some mass-encryption tasks in HEY.
2021-04-01 15:02:15 +02:00
Jorge Manrubia
26ceb126b9 Override accessors with module
This way users can override them and call #super

https://github.com/rails/rails/pull/41659#discussion_r592624914
2021-04-01 15:02:15 +02:00
Jorge Manrubia
dfa875fec5 Tweak class description 2021-04-01 15:02:15 +02:00
Jorge Manrubia
5cec2eced6 Remove preset env vars for encryption keys
These can be configured as +ActiveRecord::Encryption.config+ keys if you want,
so you can just use your own ENV vars in a config file if you prefer not to use
credentials.
2021-04-01 15:02:15 +02:00
Jorge Manrubia
f9514727af Update activerecord/RUNNING_UNIT_TESTS.rdoc
Co-authored-by: Mark Oleson <fusion2004@hey.com>
2021-04-01 15:02:15 +02:00
Jorge Manrubia
d5625c97df Review class comments
Co-authored-by: Rafael Mendonça França <rafaelmfranca@gmail.com>
2021-04-01 15:02:15 +02:00
Jorge Manrubia
3c379393c3 Split concern inclusion in two lines for clarity
Order is inverted when inlined, which can make things difficult to read.

https://github.com/rails/rails/pull/41659#discussion_r592604925

In this case it doesn't really matter and I think it's better for `Configurable`
to go first, so the change is inverting the previous order deliberately.

[skip ci]
2021-04-01 15:02:15 +02:00
Jorge Manrubia
eada873e4c Make ActiveRecord::Encryption load eagerly along with ActiveRecord
https://github.com/rails/rails/pull/41659#discussion_r592598084
https://github.com/rails/rails/pull/41659#discussion_r592601747

[skip ci]
2021-04-01 15:02:14 +02:00
Jorge Manrubia
c0fa9428e5 Fix test 2021-04-01 15:02:14 +02:00
Jorge Manrubia
7945d4fde3 Make silent by default to prevent output during tests 2021-04-01 15:02:14 +02:00
Jorge Manrubia
9bfca80641 Only run limit-validation tests when the limit exists
SQLite and PostgreSQL won't add a default limit of 255 for string types
2021-04-01 15:02:14 +02:00
Jorge Manrubia
dcbe9cd5e4 Replace class var with class_attribute to prevent child class conflicts with it 2021-04-01 15:02:14 +02:00
Jorge Manrubia
ead2b2bec9 No need to register a type after 75929dfb1c
This was making a test fail, and can make existing code fail too without a good reason.
2021-04-01 15:02:14 +02:00
Jorge Manrubia
826ec6a473 Rename previous_types => previous_encrypted_types
This makes it clear what they are. It was confusing with "cast_type" around.
2021-04-01 15:02:14 +02:00
Jorge Manrubia
0ee7f4a5f1 Use existing support for previous types
This uses the changes from https://github.com/rails/rails/pull/39929. Instead of doing a manual casting of
the previous value as part of the type, it leverages the existing support to do this now.

It also fixes a problem when loading a class with an encrypt declaration that wasn't backed
by a table was failing.

CC @kaspth
2021-04-01 15:02:14 +02:00
Jorge Manrubia
f6e671d423 Fix: memoization that was making tests fail 2021-04-01 15:02:14 +02:00
Jorge Manrubia
36c4787469 Move encryption performance tests out of the main tests pipeline
This adds new rake tasks for these tests. This way, it prevents these
tests from making the build fail.

https://github.com/rails/rails/pull/41659#discussion_r592551649
2021-04-01 15:02:14 +02:00
Jorge Manrubia
997935588d Memoize attributes with a class variable so that child class keep their own list
This was making several tests fail
2021-04-01 15:02:14 +02:00
Jorge Manrubia
4461ba1e27 Test fixes 2021-04-01 15:02:14 +02:00
Jorge Manrubia
81afcabd19 Extract encrypted models to their own files
We are not encrypting attributes when loading models with the table
missing. This way we make sure we only load the encrypted models when
necessary during the encryption tests and prevent the problem of missing
encrypted attributes due to having cached the class without them encrypted.
2021-04-01 15:02:14 +02:00
Jorge Manrubia
406fab468e Don't try to encrypt when the table doesn't exists
It will raise otherwise, which makes loading classes with missing tables
impossible.
2021-04-01 15:02:14 +02:00
Jorge Manrubia
b40285698e Update activerecord/lib/active_record/encryption/config.rb
Fix typo

Co-authored-by: Stefan Vermaas <stefanvermaas1991@gmail.com>
2021-04-01 15:02:14 +02:00
Jorge Manrubia
1163154a0b Make performance helper silent by default 2021-04-01 15:02:14 +02:00
Jorge Manrubia
54b58ef796 Prevent being able to encrypt when in protected mode
You save encrypted ciphertexts in this scenario!
2021-04-01 15:02:14 +02:00
Jorge Manrubia
d28fa6b35c Fix: don't fail with frozen empty strings 2021-04-01 15:02:14 +02:00
Jorge Manrubia
1406ac294f Remove new lines 2021-04-01 15:02:14 +02:00