|
| 1 | +--- |
| 2 | +title: "Add secret store with Azure Key Vault integration" |
| 3 | +layout: default |
| 4 | +--- |
| 5 | + |
| 6 | +# Add secret store with Azure Key Vault integration |
| 7 | +The Arcus secret store is a alternative on the general usage of storing secrets in the application configuration (`IConfiguration`). It is important in application development to differentiate between configuration data and sensitive information like secrets. The Arcus secret store is extremely flexible and can be extended to support several secret providers to retrieve its secrets - both built-in as well as custom. |
| 8 | + |
| 9 | +The secret store can be added during any part of de application lifetime. Once added, you can benefit from the safe and convenient way of secret retrieval that is the Arcus secret store. |
| 10 | + |
| 11 | +This user guide will cover how the Arcus secret store can be added to an existing API application in order to retrieve secrets from Azure Key Vault. |
| 12 | + |
| 13 | +## Terminology |
| 14 | +To fully understand the power of the secret store, some terminology has to be understood: |
| 15 | +- **Secret**: piece of sensitive information like access keys or connection strings; anything that is private and cannot be made public |
| 16 | +- **Secret store**: central place where the application retrieve its secrets. |
| 17 | +- **Secret provider**: implementation that provides secrets to the secret store (ex.: Azure Key Vault secret provider), many secret providers can be configured within a secret store. |
| 18 | + |
| 19 | +## Sample application |
| 20 | +In this user guide, a fictive API application will be used to add the secret store to. We will be working with two major parts. |
| 21 | + |
| 22 | +The initial place where the application will be started: |
| 23 | +```csharp |
| 24 | +public class Program |
| 25 | +{ |
| 26 | + public static void Main(string[] args) |
| 27 | + { |
| 28 | + WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 29 | + builder.Configuration.AddJsonFile("appsettings.json"); |
| 30 | + |
| 31 | + WebApplication app = builder.Build(); |
| 32 | + app.UseRouting(); |
| 33 | + app.Run(); |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +And the API controller where a secret is being used: |
| 39 | +```csharp |
| 40 | +[ApiController] |
| 41 | +public class OrderController : ControllerBase |
| 42 | +{ |
| 43 | + private readonly IConfiguration _configuration; |
| 44 | + |
| 45 | + public OrderController(IConfiguration configuration) |
| 46 | + { |
| 47 | + _configuration = configuration; |
| 48 | + } |
| 49 | + |
| 50 | + [HttpPost] |
| 51 | + public async Task Post([FromBody] Order order) |
| 52 | + { |
| 53 | + string connectionString = _configuration["Azure:ServiceBus:ConnectionString"]; |
| 54 | + |
| 55 | + // Post Order to Azure Service Bus... |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +> Note that in this example, we use the application configuration to retrieve the Azure Service Bus connection string to post the incoming order to Azure Service Bus and not the Arcus secret store. |
| 61 | +
|
| 62 | +## Use Arcus secret store |
| 63 | +For us to move away from the application configuration, we need to make use of the Arcus secret store. The following step instructions will guide you in this process: |
| 64 | + |
| 65 | +### 1. Install Arcus security |
| 66 | +For this example, we will be using Azure Key Vault as our single secret provider in the secret store, so we can install this directly: |
| 67 | +```shell |
| 68 | +PM > Install-Package Arcus.Security.Providers.AzureKeyVault |
| 69 | +``` |
| 70 | + |
| 71 | +> Note that this package depends on the `Arcus.Security.Core` package, where the secret store exists. |
| 72 | +
|
| 73 | +### 2. Add Arcus secret store to application |
| 74 | +Once the package is installed, add the secret store via extensions to the API application: |
| 75 | +* 2.1 Use the `.ConfigureSecretStore` to setup the secret store with necessary secret providers |
| 76 | +* 2.2 Use the the `.AddAzureKeyVaultWithManagedIdentity` to add the Azure Key Vault secret provider to the secret store |
| 77 | + |
| 78 | +```csharp |
| 79 | +using Arcus.Security.Core.Caching.Configuration; |
| 80 | + |
| 81 | +public class Program |
| 82 | +{ |
| 83 | + public static void Main(string[] args) |
| 84 | + { |
| 85 | + WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 86 | + builder.Configuration.AddJsonFile("appsettings.json"); |
| 87 | + |
| 88 | + builder.Host.ConfigureSecretStore((configuration, stores) => |
| 89 | + { |
| 90 | + string vaultUri = configuration["Azure:KeyVault:VaultUri"]; |
| 91 | + stores.AddAzureKeyVaultWithManagedIdentity(vaultUri, CacheConfiguration.Default); |
| 92 | + }); |
| 93 | + |
| 94 | + WebApplication app = builder.Build(); |
| 95 | + app.UseRouting(); |
| 96 | + app.Run(); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +> Note that during the configuration of the secret store, you will be able to access the application configuration; in this case it is used to retrieve the URI where the Azure Key Vault is located. |
| 102 | +
|
| 103 | +### 3. Inject the Arcus secret store in application |
| 104 | +Now that the Arcus secret store is added and configured to the application, the application can use it to retrieve it secrets. The Arcus secret store is accessible throughout the application via the `ISecretProvider` interface - combining all the configured secret providers. |
| 105 | + |
| 106 | +In the `OrderController`, inject the `ISecretProvider` interface via the constructor. The `ISecretProvider` will allow you to retrieve the Azure Service Bus connection string from the secret store. |
| 107 | +```csharp |
| 108 | +using Arcus.Security.Core; |
| 109 | + |
| 110 | +[ApiController] |
| 111 | +public class OrderController : ControllerBase |
| 112 | +{ |
| 113 | + private readonly ISecretProvider _secretProvider; |
| 114 | + |
| 115 | + public OrderController(ISecretProvider secretProvider) |
| 116 | + { |
| 117 | + _secretProvider = secretProvider; |
| 118 | + } |
| 119 | + |
| 120 | + [HttpPost] |
| 121 | + public async Task Post([FromBody] Order order) |
| 122 | + { |
| 123 | + string connectionString = await _secretProvider.GetRawSecretAsync("Azure_ServiceBus_ConnectionString"); |
| 124 | + |
| 125 | + // Post Order to Azure Service Bus... |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Conclusion |
| 131 | +In this user guide, you've seen how the Arcus secret store can be added to an existing application to retrieve secrets. The Arcus secret store is a very wide topic and can be configured with many different options. See [this documentation page](../03-Features/secret-store/index.md) to learn more about the Arcus secret store. |
| 132 | + |
| 133 | +## Further reading |
| 134 | +- [Arcus secret store documentation](../03-Features/secret-store/index.md) |
| 135 | + - [Azure Key Vault secret provider](../03-Features/secret-store/provider/key-vault.md) |
| 136 | + - [Create your own secret provider](../03-Features/secret-store/create-new-secret-provider.md) |
| 137 | +- [Introducing Arcus secret store](https://www.codit.eu/blog/introducing-secret-store-net-core/) |
| 138 | +- [The power of the Arcus secret store](https://www.codit.eu/blog/the-power-of-the-arcus-secret-store/) |
| 139 | +- [Role-based authorization by low-level customization of the Arcus secret store](https://www.codit.eu/blog/role-based-authorization-low-level-customization-arcus-secret-store/) |
0 commit comments