| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using Azure; |
| | | 6 | | using Azure.Core; |
| | | 7 | | using Azure.Storage.Blobs; |
| | | 8 | | using Azure.Extensions.AspNetCore.DataProtection.Blobs; |
| | | 9 | | using Azure.Storage; |
| | | 10 | | using Microsoft.AspNetCore.DataProtection.KeyManagement; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | |
| | | 13 | | #pragma warning disable AZC0001 // Extension methods have to be in the correct namespace to appear in intellisense. |
| | | 14 | | namespace Microsoft.AspNetCore.DataProtection |
| | | 15 | | #pragma warning disable |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Contains Azure-specific extension methods for modifying a |
| | | 19 | | /// <see cref="IDataProtectionBuilder"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class AzureStorageBlobDataProtectionBuilderExtensions |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Configures the data protection system to persist keys to the specified path |
| | | 25 | | /// in Azure Blob Storage. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="builder">The builder instance to modify.</param> |
| | | 28 | | /// <param name="sasUri">The full URI where the key file should be stored. |
| | | 29 | | /// The URI must contain the SAS token as a query string parameter.</param> |
| | | 30 | | /// <returns>The value <paramref name="builder"/>.</returns> |
| | | 31 | | /// <remarks> |
| | | 32 | | /// The container referenced by <paramref name="blobSasUri"/> must already exist. |
| | | 33 | | /// </remarks> |
| | | 34 | | public static IDataProtectionBuilder PersistKeysToAzureBlobStorage(this IDataProtectionBuilder builder, Uri blob |
| | | 35 | | { |
| | 0 | 36 | | if (builder == null) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentNullException(nameof(builder)); |
| | | 39 | | } |
| | 0 | 40 | | if (blobSasUri == null) |
| | | 41 | | { |
| | 0 | 42 | | throw new ArgumentNullException(nameof(blobSasUri)); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | var uriBuilder = new BlobUriBuilder(blobSasUri); |
| | | 46 | | BlobClient client; |
| | | 47 | | |
| | | 48 | | // The SAS token is present in the query string. |
| | 0 | 49 | | if (uriBuilder.Sas == null) |
| | | 50 | | { |
| | 0 | 51 | | throw new ArgumentException($"{nameof(blobSasUri)} is expected to be a SAS URL.", nameof(blobSasUri)); |
| | | 52 | | } |
| | | 53 | | else |
| | | 54 | | { |
| | 0 | 55 | | client = new BlobClient(blobSasUri); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return PersistKeysToAzureBlobStorage(builder, client); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Configures the data protection system to persist keys to the specified path |
| | | 63 | | /// in Azure Blob Storage. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="builder">The builder instance to modify.</param> |
| | | 66 | | /// <param name="sasUri">The full URI where the key file should be stored. |
| | | 67 | | /// The URI must contain the SAS token as a query string parameter.</param> |
| | | 68 | | /// <param name="tokenCredential">The credentials to connect to the blob.</param> |
| | | 69 | | /// <returns>The value <paramref name="builder"/>.</returns> |
| | | 70 | | /// <remarks> |
| | | 71 | | /// The container referenced by <paramref name="blobUri"/> must already exist. |
| | | 72 | | /// </remarks> |
| | | 73 | | public static IDataProtectionBuilder PersistKeysToAzureBlobStorage(this IDataProtectionBuilder builder, Uri blob |
| | | 74 | | { |
| | 0 | 75 | | if (builder == null) |
| | | 76 | | { |
| | 0 | 77 | | throw new ArgumentNullException(nameof(builder)); |
| | | 78 | | } |
| | 0 | 79 | | if (blobUri == null) |
| | | 80 | | { |
| | 0 | 81 | | throw new ArgumentNullException(nameof(blobUri)); |
| | | 82 | | } |
| | 0 | 83 | | if (tokenCredential == null) |
| | | 84 | | { |
| | 0 | 85 | | throw new ArgumentNullException(nameof(tokenCredential)); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | var client = new BlobClient(blobUri, tokenCredential); |
| | | 89 | | |
| | 0 | 90 | | return PersistKeysToAzureBlobStorage(builder, client); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Configures the data protection system to persist keys to the specified path |
| | | 95 | | /// in Azure Blob Storage. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="builder">The builder instance to modify.</param> |
| | | 98 | | /// <param name="sasUri">The full URI where the key file should be stored. |
| | | 99 | | /// The URI must contain the SAS token as a query string parameter.</param> |
| | | 100 | | /// <param name="sharedKeyCredential">The credentials to connect to the blob.</param> |
| | | 101 | | /// <returns>The value <paramref name="builder"/>.</returns> |
| | | 102 | | /// <remarks> |
| | | 103 | | /// The container referenced by <paramref name="blobUri"/> must already exist. |
| | | 104 | | /// </remarks> |
| | | 105 | | public static IDataProtectionBuilder PersistKeysToAzureBlobStorage(this IDataProtectionBuilder builder, Uri blob |
| | | 106 | | { |
| | 0 | 107 | | if (builder == null) |
| | | 108 | | { |
| | 0 | 109 | | throw new ArgumentNullException(nameof(builder)); |
| | | 110 | | } |
| | 0 | 111 | | if (blobUri == null) |
| | | 112 | | { |
| | 0 | 113 | | throw new ArgumentNullException(nameof(blobUri)); |
| | | 114 | | } |
| | 0 | 115 | | if (sharedKeyCredential == null) |
| | | 116 | | { |
| | 0 | 117 | | throw new ArgumentNullException(nameof(sharedKeyCredential)); |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | var client = new BlobClient(blobUri, sharedKeyCredential); |
| | | 121 | | |
| | 0 | 122 | | return PersistKeysToAzureBlobStorage(builder, client); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Configures the data protection system to persist keys to the specified path |
| | | 127 | | /// in Azure Blob Storage. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="builder">The builder instance to modify.</param> |
| | | 130 | | /// <param name="connectionString">A connection string includes the authentication information |
| | | 131 | | /// required for your application to access data in an Azure Storage |
| | | 132 | | /// account at runtime. |
| | | 133 | | /// </param> |
| | | 134 | | /// <param name="containerName">The container name to use.</param> |
| | | 135 | | /// <param name="blobName">The blob name to use.</param> |
| | | 136 | | /// <returns>The value <paramref name="builder"/>.</returns> |
| | | 137 | | /// <remarks> |
| | | 138 | | /// The container referenced by <paramref name="containerName"/><paramref name="blobName"/> must already exist. |
| | | 139 | | /// </remarks> |
| | | 140 | | public static IDataProtectionBuilder PersistKeysToAzureBlobStorage(this IDataProtectionBuilder builder, string c |
| | | 141 | | { |
| | 0 | 142 | | if (builder == null) |
| | | 143 | | { |
| | 0 | 144 | | throw new ArgumentNullException(nameof(builder)); |
| | | 145 | | } |
| | 0 | 146 | | if (connectionString == null) |
| | | 147 | | { |
| | 0 | 148 | | throw new ArgumentNullException(nameof(connectionString)); |
| | | 149 | | } |
| | 0 | 150 | | if (containerName == null) |
| | | 151 | | { |
| | 0 | 152 | | throw new ArgumentNullException(nameof(containerName)); |
| | | 153 | | } |
| | 0 | 154 | | if (blobName == null) |
| | | 155 | | { |
| | 0 | 156 | | throw new ArgumentNullException(nameof(blobName)); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | var client = new BlobServiceClient(connectionString).GetBlobContainerClient(containerName).GetBlobClient(blo |
| | | 160 | | |
| | 0 | 161 | | return PersistKeysToAzureBlobStorage(builder, client); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Configures the data protection system to persist keys to the specified path |
| | | 166 | | /// in Azure Blob Storage. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="builder">The builder instance to modify.</param> |
| | | 169 | | /// <param name="blobClient">The <see cref="BlobClient"/> in which the |
| | | 170 | | /// key file should be stored.</param> |
| | | 171 | | /// <returns>The value <paramref name="builder"/>.</returns> |
| | | 172 | | /// <remarks> |
| | | 173 | | /// The blob referenced by <paramref name="blobClient"/> must already exist. |
| | | 174 | | /// </remarks> |
| | | 175 | | public static IDataProtectionBuilder PersistKeysToAzureBlobStorage(this IDataProtectionBuilder builder, BlobClie |
| | | 176 | | { |
| | 1 | 177 | | if (builder == null) |
| | | 178 | | { |
| | 0 | 179 | | throw new ArgumentNullException(nameof(builder)); |
| | | 180 | | } |
| | 1 | 181 | | if (blobClient == null) |
| | | 182 | | { |
| | 0 | 183 | | throw new ArgumentNullException(nameof(blobClient)); |
| | | 184 | | } |
| | | 185 | | |
| | 1 | 186 | | builder.Services.Configure<KeyManagementOptions>(options => |
| | 1 | 187 | | { |
| | 2 | 188 | | options.XmlRepository = new AzureBlobXmlRepository(blobClient); |
| | 2 | 189 | | }); |
| | 1 | 190 | | return builder; |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | } |