I currently have my data protection keys stored in Redis, but I would like to migrate them into a database running MySQL.
How can I migrate my existing data protection keys from Redis to MySQL?
Info about data protection keys: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-6.0
I have just done this, so here's a write-up of how I did it. Hopefully someone will find it useful.
Pre-requisites:
redis-cli
via a Linux instance (I'm using Amazon Linux in AWS)PersistKeysToStackExchangeRedis(redis, key: [KEY])
in yourStartup.cs
Migrating Keys
Change your Startup.cs to use MySQL for the data source and comment out/remove Redis configuration, replacing
MyKeysContext
with your DbContext name. In this example I'm using MyAppDataProtectionKeys.Ensure you have added the DataProtection table definitions to your DbContext and run the migrations to add the tables to MySQL, more info here: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-8.0&tabs=visual-studio
Query your Redis instance for your existing keys and run insert scripts to insert/migrate the keys from Redis to your newly created MySQL tables.
I have encapsulated the generation of the insert scripts using the following bash script. It echos the required scripts to enter data into the table, copy them from your console and execute them against your database:
After inserting you should have your data protection keys in MySQL. The default layout of the table is as shown below (Columns: Id, FriendlyName, Xml):
and the following INFO messages that may be one indication that your migration was not successful:
The following log setup is useful for surfacing these INFO messages:
Once happy with the migration, you can delete your keys from Redis using the command (modifying with your data protection key):
Other notes: