Skip to content

Commit dad2982

Browse files
committed
[Cache] Document cache encryption using SodiumMarshaller
1 parent 84fb7fa commit dad2982

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

cache.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,119 @@ Clear all caches everywhere:
714714
.. code-block:: terminal
715715
716716
$ php bin/console cache:pool:clear cache.global_clearer
717+
718+
Encrypting the Cache
719+
--------------------
720+
721+
To encrypt the cache using ``libsodium``, you can use the
722+
:class:`Symfony\\Component\\Cache\\Marshaller\\SodiumMarshaller`.
723+
724+
Generate a key:
725+
726+
.. code-block:: terminal
727+
728+
$ php -r 'echo base64_encode(sodium_crypto_box_keypair());'
729+
730+
And add it to your :doc:`secret store </configuration/secrets>` as
731+
``CACHE_DECRYPTION_KEY`` and enable the ``SodiumMarshaller``:
732+
733+
.. configuration-block::
734+
735+
.. code-block:: yaml
736+
737+
# config/packages/cache.yaml
738+
services:
739+
Symfony\Component\Cache\Marshaller\SodiumMarshaller:
740+
decorates: cache.default_marshaller
741+
arguments:
742+
- ['%env(base64:CACHE_DECRYPTION_KEY)%']
743+
- '@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'
744+
745+
.. code-block:: xml
746+
747+
<!-- config/packages/cache.xml -->
748+
<?xml version="1.0" encoding="UTF-8" ?>
749+
<container xmlns="http://symfony.com/schema/dic/services"
750+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
751+
xmlns:framework="http://symfony.com/schema/dic/symfony"
752+
xsi:schemaLocation="http://symfony.com/schema/dic/services
753+
https://symfony.com/schema/dic/services/services-1.0.xsd
754+
http://symfony.com/schema/dic/symfony
755+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
756+
757+
<services>
758+
<service id="Symfony\Component\Cache\Marshaller\SodiumMarshaller">
759+
<factory class="Symfony\Component\Cache\Adapter\RedisAdapter" method="createConnection"/>
760+
<argument>redis://localhost</argument>
761+
<argument type="collection">
762+
<argument>env(base64:CACHE_DECRYPTION_KEY)</argument>
763+
</argument>
764+
<argument type="service" id="@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner"/>
765+
</service>
766+
</services>
767+
</container>
768+
769+
.. code-block:: php
770+
771+
// config/packages/cache.php
772+
use Symfony\Component\Cache\Marshaller\SodiumMarshaller;
773+
774+
$container->register(SodiumMarshaller::class)
775+
->addArgument(['env(base64:CACHE_DECRYPTION_KEY)'])
776+
->addArgument(service('@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'));
777+
778+
Rotating the encryption key
779+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
780+
781+
To rotate your encryption keys but still be able to read existing cache entries,
782+
add the old encryption key to the service arguments. The first key will be used
783+
for reading and writing, and the additional key(s) will only be used for reading.
784+
785+
.. configuration-block::
786+
787+
.. code-block:: yaml
788+
789+
# config/packages/cache.yaml
790+
services:
791+
Symfony\Component\Cache\Marshaller\SodiumMarshaller:
792+
decorates: cache.default_marshaller
793+
arguments:
794+
- ['%env(base64:CACHE_DECRYPTION_KEY)%', '%env(base64:OLD_CACHE_DECRYPTION_KEY)%']
795+
- '@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'
796+
797+
.. code-block:: xml
798+
799+
<!-- config/packages/cache.xml -->
800+
<?xml version="1.0" encoding="UTF-8" ?>
801+
<container xmlns="http://symfony.com/schema/dic/services"
802+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
803+
xmlns:framework="http://symfony.com/schema/dic/symfony"
804+
xsi:schemaLocation="http://symfony.com/schema/dic/services
805+
https://symfony.com/schema/dic/services/services-1.0.xsd
806+
http://symfony.com/schema/dic/symfony
807+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
808+
809+
<services>
810+
<service id="Symfony\Component\Cache\Marshaller\SodiumMarshaller">
811+
<factory class="Symfony\Component\Cache\Adapter\RedisAdapter" method="createConnection"/>
812+
<argument>redis://localhost</argument>
813+
<argument type="collection">
814+
<argument>env(base64:CACHE_DECRYPTION_KEY)</argument>
815+
<argument>env(base64:OLD_CACHE_DECRYPTION_KEY)</argument>
816+
</argument>
817+
<argument type="service" id="@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner"/>
818+
</service>
819+
</services>
820+
</container>
821+
822+
.. code-block:: php
823+
824+
// config/packages/cache.php
825+
use Symfony\Component\Cache\Marshaller\SodiumMarshaller;
826+
827+
$container->register(SodiumMarshaller::class)
828+
->addArgument(['env(base64:CACHE_DECRYPTION_KEY)', 'env(base64:OLD_CACHE_DECRYPTION_KEY)'])
829+
->addArgument(service('@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'));
830+
831+
Once all cache entries encrypted with the old key have expired, you can remove
832+
`OLD_CACHE_DECRYPTION_KEY` completely.

0 commit comments

Comments
 (0)