-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'agr-stsdk-ft-advay26' into dev-feature-sdkmigration
- Loading branch information
Showing
16 changed files
with
258 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Autofac; | ||
using Autofac.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using NuGet.Services.Configuration; | ||
using NuGetGallery; | ||
|
||
namespace NuGet.Jobs | ||
{ | ||
public static class StorageAccountHelper | ||
{ | ||
public static IServiceCollection ConfigureStorageMsi( | ||
this IServiceCollection serviceCollection, | ||
IConfiguration configuration, | ||
string storageUseManagedIdentityPropertyName = null, | ||
string storageManagedIdentityClientIdPropertyName = null) | ||
{ | ||
if (serviceCollection == null) | ||
{ | ||
throw new ArgumentNullException(nameof(serviceCollection)); | ||
} | ||
if (configuration == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configuration)); | ||
} | ||
|
||
storageUseManagedIdentityPropertyName ??= Constants.StorageUseManagedIdentityPropertyName; | ||
storageManagedIdentityClientIdPropertyName ??= Constants.StorageManagedIdentityClientIdPropertyName; | ||
|
||
string useManagedIdentityStr = configuration[storageUseManagedIdentityPropertyName]; | ||
string managedIdentityClientId = string.IsNullOrEmpty(configuration[storageManagedIdentityClientIdPropertyName]) | ||
? configuration[Constants.ManagedIdentityClientIdKey] | ||
: configuration[storageManagedIdentityClientIdPropertyName]; | ||
bool useManagedIdentity = false; | ||
if (!string.IsNullOrWhiteSpace(useManagedIdentityStr)) | ||
{ | ||
useManagedIdentity = bool.Parse(useManagedIdentityStr); | ||
} | ||
return serviceCollection.Configure<StorageMsiConfiguration>(storageConfiguration => | ||
{ | ||
storageConfiguration.UseManagedIdentity = useManagedIdentity; | ||
storageConfiguration.ManagedIdentityClientId = managedIdentityClientId; | ||
}); | ||
} | ||
|
||
public static CloudBlobClientWrapper CreateCloudBlobClient( | ||
this IServiceProvider serviceProvider, | ||
string storageConnectionString, | ||
bool readAccessGeoRedundant = false, | ||
TimeSpan? requestTimeout = null) | ||
{ | ||
if (serviceProvider == null) | ||
{ | ||
throw new ArgumentNullException(nameof(serviceProvider)); | ||
} | ||
if (string.IsNullOrWhiteSpace(storageConnectionString)) | ||
{ | ||
throw new ArgumentException($"{nameof(storageConnectionString)} cannot be null or empty.", nameof(storageConnectionString)); | ||
} | ||
|
||
var msiConfiguration = serviceProvider.GetRequiredService<IOptions<StorageMsiConfiguration>>().Value; | ||
return CreateCloudBlobClient( | ||
msiConfiguration, | ||
storageConnectionString, | ||
readAccessGeoRedundant, | ||
requestTimeout); | ||
} | ||
|
||
public static IRegistrationBuilder<CloudBlobClientWrapper, SimpleActivatorData, SingleRegistrationStyle> RegisterStorageAccount<TConfiguration>( | ||
this ContainerBuilder builder, | ||
Func<TConfiguration, string> getConnectionString, | ||
Func<TConfiguration, bool> getReadAccessGeoRedundant = null, | ||
TimeSpan? requestTimeout = null) | ||
where TConfiguration : class, new() | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
if (getConnectionString == null) | ||
{ | ||
throw new ArgumentNullException(nameof(getConnectionString)); | ||
} | ||
|
||
return builder.Register(c => | ||
{ | ||
var options = c.Resolve<IOptionsSnapshot<TConfiguration>>(); | ||
string storageConnectionString = getConnectionString(options.Value); | ||
bool readAccessGeoRedundant = getReadAccessGeoRedundant?.Invoke(options.Value) ?? false; | ||
var msiConfiguration = c.Resolve<IOptions<StorageMsiConfiguration>>().Value; | ||
return CreateCloudBlobClient( | ||
msiConfiguration, | ||
storageConnectionString, | ||
readAccessGeoRedundant, | ||
requestTimeout); | ||
}); | ||
} | ||
|
||
private static CloudBlobClientWrapper CreateCloudBlobClient( | ||
StorageMsiConfiguration msiConfiguration, | ||
string storageConnectionString, | ||
bool readAccessGeoRedundant = false, | ||
TimeSpan? requestTimeout = null) | ||
{ | ||
if (msiConfiguration.UseManagedIdentity) | ||
{ | ||
if (string.IsNullOrWhiteSpace(msiConfiguration.ManagedIdentityClientId)) | ||
{ | ||
return CloudBlobClientWrapper.UsingDefaultAzureCredential( | ||
storageConnectionString, | ||
readAccessGeoRedundant: readAccessGeoRedundant, | ||
requestTimeout: requestTimeout); | ||
} | ||
else | ||
{ | ||
return CloudBlobClientWrapper.UsingMsi( | ||
storageConnectionString, | ||
msiConfiguration.ManagedIdentityClientId, | ||
readAccessGeoRedundant, | ||
requestTimeout); | ||
} | ||
} | ||
|
||
return new CloudBlobClientWrapper( | ||
storageConnectionString, | ||
readAccessGeoRedundant, | ||
requestTimeout); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace NuGet.Jobs | ||
{ | ||
public class StorageMsiConfiguration | ||
{ | ||
public bool UseManagedIdentity { get; set; } | ||
public string ManagedIdentityClientId { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.