5

We have a requirement to store all the secrets on to azure key-vault. This requires us to update the configurations at Runtime.

One challenge we are facing at the moment is to store the password for SOLR basic auth on to azure key-vault.

<indexConfigurations>
        <solrHttpWebRequestFactory type="HttpWebAdapters.HttpWebRequestFactory, SolrNet">
          <patch:delete/>
        </solrHttpWebRequestFactory>
        <solrHttpWebRequestFactory type="HttpWebAdapters.BasicAuthHttpWebRequestFactory, SolrNet">
          <param hint="username">username</param>
          <param hint="password">password</param>
        </solrHttpWebRequestFactory>
      </indexConfigurations>

We managed to read and update the asp.net connection strings and web.config. However not for sitecore configs.

Question:

Is there a way (any sitecore API) which we could use to update the sitecore configurations at runtime?

Thanks.

3

No, you cannot update Sitecore configuration in a runtime (all configs are parsed into one big config on application start).

I would go with different approach then.

Find HttpWebAdapters.BasicAuthHttpWebRequestFactory class in SolrNet.dll and see how it works. Overwrite this class or write your own (must implement IHttpWebRequestFactory interface).

Inside that class, instead of taking password and login from configuration fetch it from different source (that can be changed during a runtime, the simplest case would be a txt file stored somewhere. Remember that the file has to be protected and cannot be accessible publicly).

Once you have your class with custom code, patch solrHttpWebRequestFactory node with your custom type.

3

You can customize configuration by overriding the default ConfigReader defined in web.config. For example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- Insert custom ConfigReader class here -->
    <section name="sitecore" type="MyProject.CustomConfigReader, MyProject" />
    <!-- ... -->
  </configSections>
  <!-- ... -->
</configuration>

Then for your custom config reader, override the default one and add your patching logic after the default patching code is done:

namespace MyProject
{
  public class CustomConfigReader : Sitecore.Configuration.ConfigReader
  {
    protected override void LoadAutoIncludeFiles(System.Xml.XmlNode element)
    {
      base.LoadAutoIncludeFiles(element);
      PatchSolrCredentials(GetConfigPatcher(element));
    }

    protected virtual void PatchSolrCredentials(Sitecore.Configuration.ConfigPatcher patcher)
    {
      // You could also edit the XML nodes directly in patcher.Document.
      var patch = @"<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'>
        <sitecore>
          <contentSearch>
            <indexConfigurations>
              <solrHttpWebRequestFactory>
                  <param hint='username'>username</param>
                  <param hint='password'>password</param>
              </solrHttpWebRequestFactory>
            </indexConfigurations>
          </contentSearch>
        </sitecore>
      </configuration>";
      // (Replace with username and password from key vault)
      var reader = new System.IO.StringReader(patch);
      patcher.ApplyPatch(reader);
    }
  }
}

Here are a couple of other interesting examples of using a custom ConfigReader:

  • Didn't work as congig reader loads begore our services ehivh gets secret from keyvault – Nil Pun Mar 29 '17 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.