How can I write to the SecuredSettings of an existing database from code

StackOverflow https://stackoverflow.com/questions/23127524

  •  05-07-2023
  •  | 
  •  

Pergunta

I'm trying to setup PeriodicBackups for an existing database from code. The PeriodicBackups bundle has been enabled in the web.config.

I'm currently using this code:

            using (var session = systemStore.OpenSession())
            {
                var databaseDocument = session.Load<DatabaseDocument>("Raven/Databases/MyDatabase");
                if (databaseDocument != null)
                {
                    databaseDocument.Settings.Add("Raven/AzureStorageAccount", "MyStorageAccount");                        
                    databaseDocument.SecuredSettings.Add("Raven/AzureStorageKey", "abc123");
                    session.SaveChanges();
                }
            }

This results in the following settings for the database "MyDatabase":

{
  "Id": "MyDatabase",
  "Settings": {
    "Raven/DataDir": "~\\Databases\\MyDatabase",
    "Raven/AzureStorageAccount": "MyStorageAccount"
  },
  "SecuredSettings": {
    "Raven/AzureStorageKey": "<data could not be decrypted>"
  },
  "Disabled": false
}

And PeriodicBackups naturally don't work. I guess it's probably because of the AzureStorageKey that can't be decrypted. Am I supposed to encrypt the values I write to SecuredSettings myself? and if so, then how do I do that?

Foi útil?

Solução

You cannot directly write to the sys docs in this manner when you have secure settings. You need to use the store.DatabaseCommands.Admin.CreateDatabase() method, which knows how to handle this.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top