Skip to content

Commit 6321218

Browse files
committed
Use AzureCliCredential instead of DefaultAzureCredential
.. and use ManagedIdentityCredential when clientId is provided. Prompted by dotnet/dnceng#5450
1 parent dd4d490 commit 6321218

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/Microsoft.SourceIndexer.Tasks/DownloadStage1Index.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using Azure;
6+
using Azure.Core;
67
using Azure.Core.Diagnostics;
78
using Azure.Identity;
89
using Azure.Storage.Blobs;
@@ -57,8 +58,7 @@ private void ExecuteCore()
5758

5859
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
5960

60-
DefaultAzureCredential credential;
61-
DefaultAzureCredentialOptions credentialoptions;
61+
TokenCredential credential;
6262

6363
if (string.IsNullOrEmpty(ClientId) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ARM_CLIENT_ID")))
6464
{
@@ -68,17 +68,14 @@ private void ExecuteCore()
6868

6969
if (string.IsNullOrEmpty(ClientId))
7070
{
71-
credentialoptions = new DefaultAzureCredentialOptions {};
71+
credential = new AzureCliCredential();
7272
Log.LogMessage($"Trying to use managed identity without default identity");
7373
}
7474
else
7575
{
76-
credentialoptions = new DefaultAzureCredentialOptions { ManagedIdentityClientId = ClientId };
77-
Log.LogMessage($"Trying to use managed identity with client id: {ClientId}");
76+
credential = new ManagedIdentityCredential(ClientId);
7877
}
7978

80-
credential = new DefaultAzureCredential(credentialoptions);
81-
8279
BlobServiceClient blobServiceClient = new(
8380
new Uri(StorageAccount),
8481
credential);
@@ -104,7 +101,7 @@ private void ExecuteCore()
104101
BlobClient blobClient = containerClient.GetBlobClient(newest.Name);
105102
var loggableUrl = new UriBuilder(blobClient.Uri) {Fragment = "", Query = ""};
106103
Log.LogMessage($"Extracting {loggableUrl} to {OutputDirectory}");
107-
try
104+
try
108105
{
109106
using Stream fileStream = blobClient.OpenRead();
110107
using var input = new GZipInputStream(fileStream);

src/SourceBrowser/src/SourceIndexServer/Models/AzureBlobFileSystem.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Azure.Identity;
1+
using Azure.Core;
2+
using Azure.Identity;
23
using Azure.Storage.Blobs;
34
using Azure.Storage.Blobs.Models;
45
using System;
@@ -11,7 +12,7 @@ namespace Microsoft.SourceBrowser.SourceIndexServer.Models
1112
public class AzureBlobFileSystem : IFileSystem
1213
{
1314
private readonly BlobContainerClient container;
14-
private DefaultAzureCredential credential;
15+
private TokenCredential credential;
1516
private string clientId;
1617

1718
public AzureBlobFileSystem(string uri)
@@ -20,9 +21,9 @@ public AzureBlobFileSystem(string uri)
2021
clientId = Environment.GetEnvironmentVariable("ARM_CLIENT_ID");
2122

2223
if (string.IsNullOrEmpty(clientId))
23-
credential = new DefaultAzureCredential();
24+
credential = new AzureCliCredential();
2425
else
25-
credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = clientId });
26+
credential = new ManagedIdentityCredential(clientId);
2627

2728
container = new BlobContainerClient(new Uri(uri),
2829
credential);
@@ -86,4 +87,4 @@ public IEnumerable<string> ReadLines(string name)
8687
}
8788
}
8889
}
89-
}
90+
}

src/UploadIndexStage1/Program.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ static async Task Main(string[] args)
6868

6969
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
7070

71-
DefaultAzureCredential credential;
72-
DefaultAzureCredentialOptions credentialoptions;
71+
TokenCredential credential;
7372

7473
if (string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ARM_CLIENT_ID")))
7574
{
@@ -79,17 +78,15 @@ static async Task Main(string[] args)
7978

8079
if (string.IsNullOrEmpty(clientId))
8180
{
82-
credentialoptions = new DefaultAzureCredentialOptions {};
81+
credential = new AzureCliCredential();
8382
System.Console.WriteLine("Trying to use managed identity without default identity");
8483
}
8584
else
8685
{
87-
credentialoptions = new DefaultAzureCredentialOptions { ManagedIdentityClientId = clientId };
88-
System.Console.WriteLine("Trying to use managed identity with client id: " + clientId);
86+
System.Console.WriteLine("Trying to use ManagedIdentityCredential with ClientID");
87+
credential = new ManagedIdentityCredential(clientId);
8988
}
9089

91-
credential = new DefaultAzureCredential(credentialoptions);
92-
9390
BlobServiceClient blobServiceClient = new(
9491
new Uri(storageAccount),
9592
credential);

0 commit comments

Comments
 (0)