Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDKMigration] Update Gallery.CredentialExpiration to use ManagedIdentity #10297

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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 NuGet.Jobs.Configuration;
Expand All @@ -11,6 +11,8 @@ public class InitializationConfiguration : MessageServiceConfiguration

public string DataStorageAccount { get; set; }

public string DataStorageAccountUrl { get; set; }

public string EmailPublisherConnectionString { get; set; }

public string EmailPublisherTopicName { get; set; }
Expand Down
8 changes: 6 additions & 2 deletions src/Gallery.CredentialExpiration/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Azure.Core;
using Azure.Identity;
using Azure.Storage.Blobs;
using Gallery.CredentialExpiration.Models;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -51,8 +53,10 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
InitializationConfiguration);

FromAddress = new MailAddress(InitializationConfiguration.MailFrom);

var storageAccount = new BlobServiceClientFactory(AzureStorageFactory.PrepareConnectionString(InitializationConfiguration.DataStorageAccount));

var tokenCredential = _serviceProvider.GetRequiredService<TokenCredential>();
var storageAccount = new BlobServiceClientFactory(new Uri(InitializationConfiguration.DataStorageAccountUrl), tokenCredential);

var storageFactory = new AzureStorageFactory(
storageAccount,
InitializationConfiguration.ContainerName,
Expand Down
22 changes: 21 additions & 1 deletion src/NuGet.Jobs.Common/StorageAccountExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Autofac;
using Autofac.Builder;
using Azure.Core;
using Azure.Data.Tables;
using Azure.Identity;
using Azure.Storage.Blobs;
Expand All @@ -22,7 +23,8 @@ public static IServiceCollection ConfigureStorageMsi(
this IServiceCollection serviceCollection,
IConfiguration configuration,
string storageUseManagedIdentityPropertyName = null,
string storageManagedIdentityClientIdPropertyName = null)
string storageManagedIdentityClientIdPropertyName = null,
string localDevelopmentPropertyName = null)
{
if (serviceCollection == null)
{
Expand All @@ -35,9 +37,12 @@ public static IServiceCollection ConfigureStorageMsi(

storageUseManagedIdentityPropertyName ??= Constants.StorageUseManagedIdentityPropertyName;
storageManagedIdentityClientIdPropertyName ??= Constants.StorageManagedIdentityClientIdPropertyName;
localDevelopmentPropertyName ??= Constants.ConfigureForLocalDevelopment;

string useManagedIdentityStr = configuration[storageUseManagedIdentityPropertyName];
string localDevelopmentStr = configuration[localDevelopmentPropertyName];
bool useManagedIdentity = false;
bool setupLocalDevelopment = false;

string managedIdentityClientId = string.IsNullOrWhiteSpace(configuration[storageManagedIdentityClientIdPropertyName])
? configuration[Constants.ManagedIdentityClientIdKey]
Expand All @@ -47,6 +52,21 @@ public static IServiceCollection ConfigureStorageMsi(
{
useManagedIdentity = bool.Parse(useManagedIdentityStr);
}

if (!string.IsNullOrWhiteSpace(localDevelopmentStr))
{
setupLocalDevelopment = bool.Parse(localDevelopmentStr);
}

if (setupLocalDevelopment)
{
serviceCollection.AddSingleton<TokenCredential>(new DefaultAzureCredential());
}
else
{
serviceCollection.AddSingleton<TokenCredential>(new ManagedIdentityCredential(managedIdentityClientId));
}

return serviceCollection.Configure<StorageMsiConfiguration>(storageConfiguration =>
{
storageConfiguration.UseManagedIdentity = useManagedIdentity;
Expand Down
1 change: 1 addition & 0 deletions src/NuGet.Services.Configuration/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public static class Constants
public static string KeyVaultSendX5c = "KeyVault_SendX5c";
public static string StorageUseManagedIdentityPropertyName = "Storage_UseManagedIdentity";
public static string StorageManagedIdentityClientIdPropertyName = "Storage_ManagedIdentityClientId";
public static string ConfigureForLocalDevelopment = "Local_Development";
}
}