|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Autofac; |
| 6 | +using Autofac.Builder; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | +using NuGetGallery; |
| 11 | + |
| 12 | +namespace NuGet.Jobs |
| 13 | +{ |
| 14 | + public static class StorageAccountHelper |
| 15 | + { |
| 16 | + private const string StorageUseManagedIdentityPropertyName = "Storage_UseManagedIdentity"; |
| 17 | + private const string StorageManagedIdentityClientIdPropertyName = "Storage_ManagedIdentityClientId"; |
| 18 | + |
| 19 | + public static IServiceCollection ConfigureStorageMsi( |
| 20 | + this IServiceCollection serviceCollection, |
| 21 | + IConfiguration configuration, |
| 22 | + string useManageIdentityPropertyName = null, |
| 23 | + string managedIdentityClientIdPropertyName = null) |
| 24 | + { |
| 25 | + if (serviceCollection == null) |
| 26 | + { |
| 27 | + throw new ArgumentNullException(nameof(serviceCollection)); |
| 28 | + } |
| 29 | + if (configuration == null) |
| 30 | + { |
| 31 | + throw new ArgumentNullException(nameof(configuration)); |
| 32 | + } |
| 33 | + |
| 34 | + useManageIdentityPropertyName ??= StorageUseManagedIdentityPropertyName; |
| 35 | + managedIdentityClientIdPropertyName ??= StorageManagedIdentityClientIdPropertyName; |
| 36 | + |
| 37 | + string useManagedIdentityStr = configuration[useManageIdentityPropertyName]; |
| 38 | + string managedIdentityClientId = configuration[managedIdentityClientIdPropertyName]; |
| 39 | + bool useManagedIdentity = false; |
| 40 | + if (!string.IsNullOrWhiteSpace(useManagedIdentityStr)) |
| 41 | + { |
| 42 | + useManagedIdentity = bool.Parse(useManagedIdentityStr); |
| 43 | + } |
| 44 | + return serviceCollection.Configure<StorageMsiConfiguration>(storageConfiguration => |
| 45 | + { |
| 46 | + storageConfiguration.UseManagedIdentity = useManagedIdentity; |
| 47 | + storageConfiguration.ManagedIdentityClientId = managedIdentityClientId; |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + public static CloudBlobClientWrapper CreateCloudBlobClient( |
| 52 | + this IServiceProvider serviceProvider, |
| 53 | + string storageConnectionString, |
| 54 | + bool readAccessGeoRedundant = false, |
| 55 | + TimeSpan? requestTimeout = null) |
| 56 | + { |
| 57 | + if (serviceProvider == null) |
| 58 | + { |
| 59 | + throw new ArgumentNullException(nameof(serviceProvider)); |
| 60 | + } |
| 61 | + if (string.IsNullOrWhiteSpace(storageConnectionString)) |
| 62 | + { |
| 63 | + throw new ArgumentException($"{nameof(storageConnectionString)} cannot be null or empty.", nameof(storageConnectionString)); |
| 64 | + } |
| 65 | + |
| 66 | + var msiConfiguration = serviceProvider.GetRequiredService<IOptions<StorageMsiConfiguration>>().Value; |
| 67 | + return CreateCloudBlobClient( |
| 68 | + msiConfiguration, |
| 69 | + storageConnectionString, |
| 70 | + readAccessGeoRedundant, |
| 71 | + requestTimeout); |
| 72 | + } |
| 73 | + |
| 74 | + public static IRegistrationBuilder<CloudBlobClientWrapper, SimpleActivatorData, SingleRegistrationStyle> RegisterStorageAccount<TConfiguration>( |
| 75 | + this ContainerBuilder builder, |
| 76 | + Func<TConfiguration, string> getConnectionString, |
| 77 | + Func<TConfiguration, bool> getReadAccessGeoRedundant = null, |
| 78 | + TimeSpan? requestTimeout = null) |
| 79 | + where TConfiguration : class, new() |
| 80 | + { |
| 81 | + if (builder == null) |
| 82 | + { |
| 83 | + throw new ArgumentNullException(nameof(builder)); |
| 84 | + } |
| 85 | + if (getConnectionString == null) |
| 86 | + { |
| 87 | + throw new ArgumentNullException(nameof(getConnectionString)); |
| 88 | + } |
| 89 | + |
| 90 | + return builder.Register(c => |
| 91 | + { |
| 92 | + var options = c.Resolve<IOptionsSnapshot<TConfiguration>>(); |
| 93 | + string storageConnectionString = getConnectionString(options.Value); |
| 94 | + bool readAccessGeoRedundant = getReadAccessGeoRedundant?.Invoke(options.Value) ?? false; |
| 95 | + var msiConfiguration = c.Resolve<IOptions<StorageMsiConfiguration>>().Value; |
| 96 | + return CreateCloudBlobClient( |
| 97 | + msiConfiguration, |
| 98 | + storageConnectionString, |
| 99 | + readAccessGeoRedundant, |
| 100 | + requestTimeout); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + private static CloudBlobClientWrapper CreateCloudBlobClient( |
| 105 | + StorageMsiConfiguration msiConfiguration, |
| 106 | + string storageConnectionString, |
| 107 | + bool readAccessGeoRedundant = false, |
| 108 | + TimeSpan? requestTimeout = null) |
| 109 | + { |
| 110 | + if (msiConfiguration.UseManagedIdentity) |
| 111 | + { |
| 112 | + if (string.IsNullOrWhiteSpace(msiConfiguration.ManagedIdentityClientId)) |
| 113 | + { |
| 114 | + return CloudBlobClientWrapper.UsingDefaultAzureCredential( |
| 115 | + storageConnectionString, |
| 116 | + readAccessGeoRedundant: readAccessGeoRedundant, |
| 117 | + requestTimeout: requestTimeout); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + return CloudBlobClientWrapper.UsingMsi( |
| 122 | + storageConnectionString, |
| 123 | + msiConfiguration.ManagedIdentityClientId, |
| 124 | + readAccessGeoRedundant, |
| 125 | + requestTimeout); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return new CloudBlobClientWrapper( |
| 130 | + storageConnectionString, |
| 131 | + readAccessGeoRedundant, |
| 132 | + requestTimeout); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments