|
| 1 | +--- |
| 2 | +title: Azure Service Fabric - Using Service Fabric application KeyVault references | Microsoft Docs |
| 3 | +description: This article explains how to use service-fabric KeyVaultReference support for application secrets. |
| 4 | +services: service-fabric |
| 5 | +author: athinanthny |
| 6 | + |
| 7 | +ms.service: service-fabric |
| 8 | +ms.devlang: dotnet |
| 9 | +ms.topic: article |
| 10 | +ms.date: 09/20/2019 |
| 11 | +ms.author: atsenthi |
| 12 | +--- |
| 13 | + |
| 14 | +# KeyVaultReference support for Service Fabric applications (preview) |
| 15 | + |
| 16 | +A common challenge when building cloud applications is how to securely store secrets required by your application. For example, you might want to store the container repository credentials in keyvault and reference it in application manifest. Service Fabric KeyVaultReference uses Service Fabric Managed Identity and makes it easy to reference keyvault secrets. The remainder of this article details how to use Service Fabric KeyVaultReference and includes some typical usage. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- Managed Identity for Application (MIT) |
| 21 | + |
| 22 | + Service Fabric KeyVaultReference support uses application's Managed Identity and therefore applications planing to use KeyVaultReferences should use Managed Identity. Follow this [document](concepts-managed-identity.md) to enable managed identity for your application. |
| 23 | + |
| 24 | +- Central Secrets Store (CSS). |
| 25 | + |
| 26 | + Central Secrets Store(CSS) is service-fabric's encrypted local secrets cache, KeyVaultReference once fetched are cached in CSS. |
| 27 | + |
| 28 | + Add the below to your cluster configuration under `fabricSettings` to enable all the required features for KeyVaultReference support. |
| 29 | + |
| 30 | + ```json |
| 31 | + "fabricSettings": |
| 32 | + [ |
| 33 | + ... |
| 34 | + { |
| 35 | + "parameters": [ |
| 36 | + "name": "CentralSecretService" |
| 37 | + { |
| 38 | + "name": "IsEnabled", |
| 39 | + "value": "true" |
| 40 | + }, |
| 41 | + { |
| 42 | + "name": "MinReplicaSetSize", |
| 43 | + "value": "3" |
| 44 | + }, |
| 45 | + { |
| 46 | + "name": "TargetReplicaSetSize", |
| 47 | + "value": "3" |
| 48 | + } |
| 49 | + ], |
| 50 | + }, |
| 51 | + { |
| 52 | + "name": "ManagedIdentityTokenService", |
| 53 | + "parameters": [ |
| 54 | + { |
| 55 | + "name": "IsEnabled", |
| 56 | + "value": "true" |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | + ] |
| 61 | + ``` |
| 62 | + |
| 63 | + > [!NOTE] |
| 64 | + > It's recommended to use a separate encryption certificate for CSS. You can add it under the "CentralSecretService" section. |
| 65 | + |
| 66 | + ```json |
| 67 | + { |
| 68 | + "name": "EncryptionCertificateThumbprint", |
| 69 | + "value": "<EncryptionCertificateThumbprint for CSS>" |
| 70 | + } |
| 71 | + ``` |
| 72 | + |
| 73 | +- Grant application's managed identity access permission to the keyvault |
| 74 | + |
| 75 | + Reference this [document](how-to-grant-access-other-resources.md) to see how to grant managed identity access to keyvault. Also note if you are using System Assigned Managed Identity, the managed identity is created only after application deployment. |
| 76 | + |
| 77 | +## Keyvault secret as application parameter |
| 78 | +Let's say the application needs to read the backend database password stored in keyvault, Service Fabric KeyVaultReference support makes it easy. Below example reads `DBPassword` secret from keyvault using Service Fabric KeyVaultReference support. |
| 79 | + |
| 80 | +- Add a section to settings.xml |
| 81 | + |
| 82 | + Define `DBPassword` parameter with Type `KeyVaultReference` and Value `<KeyVaultURL>` |
| 83 | + |
| 84 | + ```xml |
| 85 | + <Section Name="dbsecrets"> |
| 86 | + <Parameter Name="DBPassword" Type="KeyVaultReference" Value="https://vault200.vault.azure.net/secrets/dbpassword/8ec042bbe0ea4356b9b171588a8a1f32"/> |
| 87 | + </Section> |
| 88 | + ``` |
| 89 | +- Reference the new section in ApplicationManifest.xml in `<ConfigPackagePolicies>` |
| 90 | + |
| 91 | + ```xml |
| 92 | + <ServiceManifestImport> |
| 93 | + <Policies> |
| 94 | + <IdentityBindingPolicy ServiceIdentityRef="WebAdmin" ApplicationIdentityRef="ttkappuser" /> |
| 95 | + <ConfigPackagePolicies CodePackageRef="Code"> |
| 96 | + <!--Linux container example--> |
| 97 | + <ConfigPackage Name="Config" SectionName="dbsecrets" EnvironmentVariableName="SecretPath" MountPoint="/var/secrets"/> |
| 98 | + <!--Windows container example--> |
| 99 | + <!-- <ConfigPackage Name="Config" SectionName="dbsecrets" EnvironmentVariableName="SecretPath" MountPoint="C:\secrets"/> --> |
| 100 | + </ConfigPackagePolicies> |
| 101 | + </Policies> |
| 102 | + </ServiceManifestImport> |
| 103 | + ``` |
| 104 | + |
| 105 | +- Using KeyVaultReference in your application |
| 106 | + |
| 107 | + Service Fabric on service instantiation will resolve the KeyVaultReference Parameter using application's managed identity. Each parameter listed under `<Section Name=dbsecrets>` will be a file under the folder pointed to by EnvironmentVariable SecretPath. Below C# code snippet show how to read DBPassword in your application. |
| 108 | + |
| 109 | + ```C# |
| 110 | + string secretPath = Environment.GetEnvironmentVariable("SecretPath"); |
| 111 | + using (StreamReader sr = new StreamReader(Path.Combine(secretPath, "DBPassword"))) |
| 112 | + { |
| 113 | + string dbPassword = sr.ReadToEnd(); |
| 114 | + // dbPassword to connect to DB |
| 115 | + } |
| 116 | + ``` |
| 117 | + > [!NOTE] |
| 118 | + > For the container scenario, you can use the MountPoint to control where the `secrets` will be mounted. |
| 119 | + |
| 120 | +## Keyvault secret as environment variable |
| 121 | + |
| 122 | +Service Fabric environment variables now support KeyVaultReference type, below example shows how to bind an environment variable to a secret stored in KeyVault. |
| 123 | + |
| 124 | +```xml |
| 125 | +<EnvironmentVariables> |
| 126 | + <EnvironmentVariable Name="EventStorePassword" Type="KeyVaultReference" Value="https://ttkvault.vault.azure.net/secrets/clustercert/e225bd97e203430d809740b47736b9b8"/> |
| 127 | +</EnvironmentVariables> |
| 128 | +``` |
| 129 | + |
| 130 | +```C# |
| 131 | +string eventStorePassword = Environment.GetEnvironmentVariable("EventStorePassword"); |
| 132 | +``` |
| 133 | +## Keyvault secret as container repository password |
| 134 | +KeyVaultReference is a supported type for container RepositoryCredentials, below example shows how to use a keyvault |
| 135 | +reference as container repository password. |
| 136 | +```xml |
| 137 | + <Policies> |
| 138 | + <ContainerHostPolicies CodePackageRef="Code"> |
| 139 | + <RepositoryCredentials AccountName="user1" Type="KeyVaultReference" Password="https://ttkvault.vault.azure.net/secrets/containerpwd/e225bd97e203430d809740b47736b9b8"/> |
| 140 | + </ContainerHostPolicies> |
| 141 | +``` |
| 142 | +## FAQ |
| 143 | +- Managed identity needs to be enabled for KeyVaultReference support, your application activation will fail if KeyVaultReference is used without enabling Managed Identity. |
| 144 | + |
| 145 | +- If you are using system assigned identity, it's created only after the application is deployed and this creates a circular dependency. Once your application is deployed, you can grant the system assigned identity access permission to keyvault. You can find the system assigned identity by name {cluster}/{application name}/{servicename} |
| 146 | + |
| 147 | +- The keyvault needs to be in the same subscription as your service fabric cluster. |
| 148 | + |
| 149 | +## Next steps |
| 150 | + |
| 151 | +* [Azure KeyVault Documentation](https://docs.microsoft.com/azure/key-vault/) |
0 commit comments