Here is how I was able to do the encryption and decryption in Service Fabric without the Azure Key Vault. Since the data we were encrypting was not changing the decision was to encrypt the values with the certificate and place the values into the correct xml files.
- Generate or obtain a X509 certificate. Make sure that the KeyUsage has "DataEncipherment" indicated this is critical to encryption of data
- Get the thumbprint for the certificate. You can get this in your MMC certificates plugin.
- Using powershell, encrypt the text using the Invoke-ServiceFabricEncryptedText function in powershell. Use the thumbprint from the cert to encrypt the text. This will create an encryption of the text that contains a base 64-bit string that contains the secret ciphertext as well as the information about the certificate used to encrypt it. This is critical! Also, it is a good idea to run the Inoke-ServiceFabricDecryptText method on the encrypted string using the certificate to ensure it decrypts fine.
- Now comes the fun part, putting this into all the XML files in the correct way to get this to work. (This is where it gets messy).
- First you need to modify the settings.xml file. The parameter that you want to be encrypted needs to be set to IsEncrypted="true" Value="" and MustOverride="true".
- Next you need to declare the override parameter in the ApplicationManifest.xml file. Make sure the parameter name in the application manifest is the same as in the settinss. Set the Name of the parameter in the Parameters section in the applicationmanifest.xml file and set the value = "".
- In the ConfigurationOverride section where you have the Parameter name referenced, set the value to the Parameter in the parameters section. Currently this will be blank, also set the IsEncrypted="true".
- This is where I got stuck. All this other information was readily available, this next section wasn't. Next in the ApplicationParameters folder under your project, select the XML file used when you publish to your service fabric. This is were you will place the encrypted value. Create a Parameter in this section, set the name to the name you gave in your ApplicationManifest and set the value to the encrypted value generated from the certificate.
When you deploy this parameter will be passed in as an encrypted value. To use it in code you need to make sure you refernece the DecryptValue. For example:
var decryptedPassword= configurationPackage.Settings.Sections["sectionname"].Parameters["ConnectionString"].DecryptValue();
This will generate a SecureString that you can use throughout your code. I converted the value to a string using one of many references on how to convert SecureString to string available online.
That's it. I hope this helps someone else that is not using the Azure Key Vault to secure secrets.