Skip to content

Commit 27bdba1

Browse files
committed
clean up connection string parsing
1 parent 680149a commit 27bdba1

3 files changed

Lines changed: 40 additions & 19 deletions

File tree

src/NuGet.Services.Validation.Orchestrator/Job.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Threading.Tasks;
1111
using Autofac;
1212
using Autofac.Core;
13-
using Azure.Core;
1413
using Azure.Identity;
1514
using Azure.Storage.Blobs;
1615
using Microsoft.ApplicationInsights;
@@ -621,27 +620,29 @@ private static BlobServiceClient CreateBlobServiceClient(
621620
{
622621
if (string.IsNullOrWhiteSpace(msiConfiguration.ManagedIdentityClientId))
623622
{
624-
// Using MSI with DefaultAzureCredential (local debugging)
623+
// 1. Using MSI with DefaultAzureCredential (local debugging)
625624
var defaultAzureCredentialOptions = new DefaultAzureCredentialOptions
626625
{
627-
ManagedIdentityClientId = msiConfiguration.ManagedIdentityClientId,
626+
ManagedIdentityClientId = null,
628627
};
629-
var tokenCredential = new DefaultAzureCredential(defaultAzureCredentialOptions);
630628

631-
return new BlobServiceClient(new Uri(storageConnectionString), tokenCredential, blobClientOptions);
629+
return new BlobServiceClient(
630+
ConnectionStringExtensions.GetBlobEndpointFromConnectionString(storageConnectionString),
631+
new DefaultAzureCredential(defaultAzureCredentialOptions),
632+
blobClientOptions);
632633
}
633634
else
634635
{
635-
// Using MSI with ClientId
636-
var tokenCredential = new ManagedIdentityCredential(msiConfiguration.ManagedIdentityClientId);
637-
638-
return new BlobServiceClient(new Uri(storageConnectionString), tokenCredential, blobClientOptions);
636+
// 2. Using MSI with ClientId
637+
return new BlobServiceClient(
638+
ConnectionStringExtensions.GetBlobEndpointFromConnectionString(storageConnectionString),
639+
new ManagedIdentityCredential(msiConfiguration.ManagedIdentityClientId),
640+
blobClientOptions);
639641
}
640642
}
641643
else
642644
{
643-
// Using SAS token
644-
645+
// 3. Using SAS token
645646
// workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373
646647
var connectionString = storageConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");
647648

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Azure.Storage.Blobs;
10+
11+
namespace NuGetGallery
12+
{
13+
public static class ConnectionStringExtensions
14+
{
15+
public static Uri GetBlobEndpointFromConnectionString(string connectionString)
16+
{
17+
var tempClient = new BlobServiceClient(connectionString);
18+
// if _storageConnectionString has SAS token, Uri will contain SAS signature, we need to strip it
19+
var uriBuilder = new UriBuilder(tempClient.Uri);
20+
uriBuilder.Query = "";
21+
uriBuilder.Fragment = "";
22+
return uriBuilder.Uri;
23+
}
24+
}
25+
}

src/NuGetGallery.Core/Services/CloudBlobClientWrapper.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -177,12 +177,7 @@ internal BlobContainerClient CreateBlobContainerClient(CloudBlobLocationMode loc
177177

178178
private Uri GetPrimaryServiceUri()
179179
{
180-
var tempClient = new BlobServiceClient(_storageConnectionString);
181-
// if _storageConnectionString has SAS token, Uri will contain SAS signature, we need to strip it
182-
var uriBuilder = new UriBuilder(tempClient.Uri);
183-
uriBuilder.Query = "";
184-
uriBuilder.Fragment = "";
185-
return uriBuilder.Uri;
180+
return ConnectionStringExtensions.GetBlobEndpointFromConnectionString(_storageConnectionString);
186181
}
187182

188183
private Uri GetSecondaryServiceUri()
@@ -278,4 +273,4 @@ private static ClientCertificateCredential GetCredentialUsingServicePrincipal(st
278273
return new ClientCertificateCredential(tenantId, appID, clientCert, new ClientCertificateCredentialOptions { AuthorityHost = new Uri(authorityHost), SendCertificateChain = true });
279274
}
280275
}
281-
}
276+
}

0 commit comments

Comments
 (0)