Skip to content

[Cache] add SodiumMarshaller #35019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2020

Conversation

atailouloute
Copy link
Contributor

@atailouloute atailouloute commented Dec 18, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR

Add SodiumMarshaller to encrypt cache values

To use the SodiumMarshaller we can decorate the cache.default_marshaller:

Symfony\Component\Cache\Marshaller\SodiumMarshaller:
    decorates: cache.default_marshaller
    arguments:
        - ['%env(CACHE_DECRYPTION_KEY)%', '%env(OLD_CACHE_DECRYPTION_KEY)%']
        - '@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'

The first provided key is used to encrypt and decrypt cached values.

In order to allow rotating keys, more keys can be provided - they will be used only to decrypt values.

/cc @nicolas-grekas

@nicolas-grekas nicolas-grekas added this to the next milestone Dec 18, 2019
@nicolas-grekas
Copy link
Member

Cool thanks. Now I'm wondering: how would one use it to encrypt all the cached values?
Can you please share a DI configuration that would work to do so?
I think overriding the cache.default_marshaller service would work.
When we have something that works, we might wonder about whether this is good enough or if we need better configurability.

@atailouloute
Copy link
Contributor Author

@nicolas-grekas I added in the description an example of how to use the SodiumMarshaller by decorating the default cache.default_marshaller

And here is the test I did

// add an item to cache
public function __invoke(CacheItemPoolInterface $cache)
{
    $item = $cache->getItem('framework')->set('symfony');
    $cache->save($item);
}

The value of our item is encrypted in the filesystem cache.
image

And when we retrieve the item

// add an item to cache
public function __invoke(CacheItemPoolInterface $cache)
{
    $item = $cache->getItem('framework');
    dump($item->isHit()); // true
    dump($item->get());   // "symfony"
}

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Dec 18, 2019

Great thanks,

- '%env(file:resolve:SODIUM_DECRYPTION_FILE)%'

next step would be documenting how we generate this content
OR: do we want any integration with the secret vault? there is already a SYMFONY_DECRYPTION_SECRET that is used by default there, can/should we leverage it?

@atailouloute
Copy link
Contributor Author

atailouloute commented Dec 18, 2019

I think we could use the keys generated by SodiumVault, but there is a problem, what will happen if the user generates new wkeys and overrides the existing ones ?

I don't know if this exists already or not, but what about offering to the user (or just internally) the possibility to generate multiple encrypt/decrypt keys, by having this we can generate and use a specific key for the cache (we can also go far and use a different key for each cache pool).

@nicolas-grekas
Copy link
Member

what will happen if the user generates new wkeys and overrides the existing ones ?

Very good point. This means we should handle one (or more) fallback keys that could be used to decrypt values when the main one doesn't work. That would allow rotating keys gracefully.

@atailouloute
Copy link
Contributor Author

So we can have something like that?

- __construct(string $decryptionKey, MarshallerInterface $marshaller = null)
+ __construct(string $decryptionKey, MarshallerInterface $marshaller = null, array $fallbackDecryptionKeys = [])

I'm wondering now where we will store fallback keys and how the user can manage them

@nicolas-grekas
Copy link
Member

What about __construct(array $decryptionKeys, MarshallerInterface $marshaller = null)?

I'm wondering now where we will store fallback keys and how the user can manage them

We'd need to experiment ideas.

@atailouloute
Copy link
Contributor Author

What about __construct(array $decryptionKeys, MarshallerInterface $marshaller = null)?

Yes, better than what I suggested.

We'd need to experiment ideas.

What do you think about a new command that we can use to generate not only one secret key (as we have now with secrets:generate-keys) but as much as we need ?
Instead of generating one secret key per env, we can give a name or a prefix to each one

For example: bin/console keys:generate cache will generate a secret key that we can use only for the cache SodiumMarshaller

@nicolas-grekas nicolas-grekas changed the base branch from 4.4 to master February 6, 2020 10:37
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd need some doc to explain how to generate new key pairs.
We could have a command for that, or we could give a php -r snippet.
Anyway, that's not a blocker to merge this PR.
Good to go on my side. (I did some tweaks + rebased for master)

@nicolas-grekas
Copy link
Member

Thank you @atailouloute.

nicolas-grekas added a commit that referenced this pull request Feb 7, 2020
This PR was merged into the 5.1-dev branch.

Discussion
----------

[Cache] add SodiumMarshaller

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Add `SodiumMarshaller` to encrypt cache values

To use the `SodiumMarshaller` we can decorate the `cache.default_marshaller`:

```yaml
Symfony\Component\Cache\Marshaller\SodiumMarshaller:
    decorates: cache.default_marshaller
    arguments:
        - ['%env(CACHE_DECRYPTION_KEY)%', '%env(OLD_CACHE_DECRYPTION_KEY)%']
        - '@symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'
```

The first provided key is used to encrypt and decrypt cached values.

In order to allow rotating keys, more keys can be provided - they will be used only to decrypt values.

/cc @nicolas-grekas

Commits
-------

540d7eb [Cache] add SodiumMarshaller
@nicolas-grekas nicolas-grekas merged commit 540d7eb into symfony:master Feb 7, 2020
@atailouloute atailouloute deleted the sodium-marshaller branch February 7, 2020 10:45
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants