Skip to content

Commit e8dcfef

Browse files
APIView - Connect to cosmos and storage using managed identity (#8345)
1 parent f785c8b commit e8dcfef

11 files changed

Lines changed: 36 additions & 23 deletions

File tree

src/dotnet/APIView/APIViewIntegrationTests/APIViewIntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
15+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
1616
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
1717
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
1818
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />

src/dotnet/APIView/APIViewIntegrationTests/RepositoryTests/CosmosPullRequestRepositoryTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Threading.Tasks;
1010
using System.Linq;
11+
using Azure.Identity;
1112

1213
namespace APIViewIntegrationTests.RepositoryTests
1314
{
@@ -28,7 +29,7 @@ public CosmosPullRequestRepositoryTestsBaseFixture()
2829
_cosmosDBname = "CosmosPullRequestRepositoryTestsDB";
2930
config["CosmosDBName"] = _cosmosDBname;
3031

31-
_cosmosClient = new CosmosClient(config["Cosmos:ConnectionString"]);
32+
_cosmosClient = new CosmosClient(config["CosmosEndpoint"], new DefaultAzureCredential());
3233
var dataBaseResponse = _cosmosClient.CreateDatabaseIfNotExistsAsync(config["CosmosDBName"]).Result;
3334
dataBaseResponse.Database.CreateContainerIfNotExistsAsync("Reviews", "/id").Wait();
3435
dataBaseResponse.Database.CreateContainerIfNotExistsAsync("PullRequests", "/ReviewId").Wait();

src/dotnet/APIView/APIViewIntegrationTests/RepositoryTests/CosmosReviewRepositoryTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Threading.Tasks;
1010
using System.Linq;
11+
using Azure.Identity;
1112

1213
namespace APIViewIntegrationTests.RepositoryTests
1314
{
@@ -27,7 +28,7 @@ public CosmosReviewRepositoryTestsBaseFixture()
2728
_cosmosDBname = "CosmosReviewRepositoryTestsDB";
2829
config["CosmosDBName"] = _cosmosDBname;
2930

30-
_cosmosClient = new CosmosClient(config["Cosmos:ConnectionString"]);
31+
_cosmosClient = new CosmosClient(config["CosmosEndpoint"], new DefaultAzureCredential());
3132
var dataBaseResponse = _cosmosClient.CreateDatabaseIfNotExistsAsync(config["CosmosDBName"]).Result;
3233
dataBaseResponse.Database.CreateContainerIfNotExistsAsync("Reviews", "/id").Wait();
3334

src/dotnet/APIView/APIViewIntegrationTests/TestsBaseFixture.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.AspNetCore.SignalR;
2121
using Microsoft.Extensions.Options;
2222
using Microsoft.ApplicationInsights;
23+
using Azure.Identity;
2324

2425
namespace APIViewIntegrationTests
2526
{
@@ -74,7 +75,7 @@ public TestsBaseFixture()
7475
PackageNameManager = serviceProvider.GetService<PackageNameManager>();
7576
User = TestUser.GetTestuser();
7677

77-
_cosmosClient = new CosmosClient(_config["Cosmos:ConnectionString"]);
78+
_cosmosClient = new CosmosClient(_config["CosmosEndpoint"], new DefaultAzureCredential());
7879
var dataBaseResponse = _cosmosClient.CreateDatabaseIfNotExistsAsync(_config["CosmosDBName"]).Result;
7980
dataBaseResponse.Database.CreateContainerIfNotExistsAsync("Reviews", "/id").Wait();
8081
dataBaseResponse.Database.CreateContainerIfNotExistsAsync("APIRevisions", "/ReviewId").Wait();
@@ -86,8 +87,9 @@ public TestsBaseFixture()
8687
CommentRepository = new CosmosCommentsRepository(_config, _cosmosClient);
8788
var cosmosUserProfileRepository = new CosmosUserProfileRepository(_config, _cosmosClient);
8889

89-
_blobCodeFileContainerClient = new BlobContainerClient(_config["Blob:ConnectionString"], "codefiles");
90-
_blobOriginalContainerClient = new BlobContainerClient(_config["Blob:ConnectionString"], "originals");
90+
var blobServiceClient = new BlobServiceClient(new Uri(_config["StorageAccountUrl"]), new DefaultAzureCredential());
91+
_blobCodeFileContainerClient = blobServiceClient.GetBlobContainerClient("codefiles");
92+
_blobOriginalContainerClient = blobServiceClient.GetBlobContainerClient("originals");
9193
_ = _blobCodeFileContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);
9294
_ = _blobOriginalContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);
9395

src/dotnet/APIView/APIViewUnitTests/APIViewUnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="FluentAssertions" Version="6.10.0" />
21-
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
21+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
2222
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.0" />
2323
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.0" />
2424
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />

src/dotnet/APIView/APIViewWeb/APIViewWeb.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
4444
<PackageReference Include="Microsoft.ApplicationInsights.SnapshotCollector" Version="1.4.5" />
4545
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="7.0.0" />
46-
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
46+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
4747
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.15" />
4848
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.4" />
4949
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="19.225.1" />

src/dotnet/APIView/APIViewWeb/MiddleWare/UITestsMiddleWare.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Azure.Storage.Blobs;
66
using Microsoft.Azure.Cosmos;
77
using Microsoft.Extensions.Configuration;
8+
using Azure.Identity;
9+
using System;
810

911
namespace APIViewWeb.MiddleWare
1012
{
@@ -18,19 +20,20 @@ public UITestsMiddleWare(RequestDelegate next)
1820

1921
public async Task InvokeAsync(HttpContext httpContext, IConfiguration config)
2022
{
21-
var cosmosClient = new CosmosClient(config["Cosmos:ConnectionString"]);
22-
var dataBaseResponse = await cosmosClient.CreateDatabaseIfNotExistsAsync("APIView");
23+
var cosmosClient = new CosmosClient(config["CosmosEndpoint"], new DefaultAzureCredential());
24+
var dataBaseResponse = await cosmosClient.CreateDatabaseIfNotExistsAsync(config["CosmosDBName"]);
2325
_ = await dataBaseResponse.Database.CreateContainerIfNotExistsAsync("Reviews", "/id");
2426
_ = await dataBaseResponse.Database.CreateContainerIfNotExistsAsync("Comments", "/ReviewId");
2527
_ = await dataBaseResponse.Database.CreateContainerIfNotExistsAsync("Profiles", "/id");
2628
_ = await dataBaseResponse.Database.CreateContainerIfNotExistsAsync("PullRequests", "/PullRequestNumber");
2729
_ = await dataBaseResponse.Database.CreateContainerIfNotExistsAsync("UsageSamples", "/ReviewId");
2830
_ = await dataBaseResponse.Database.CreateContainerIfNotExistsAsync("UserPreference", "/ReviewId");
2931

30-
var blobCodeFileContainerClient = new BlobContainerClient(config["Blob:ConnectionString"], "codefiles");
31-
var blobOriginalContainerClient = new BlobContainerClient(config["Blob:ConnectionString"], "originals");
32-
var blobUsageSampleRepository = new BlobContainerClient(config["Blob:ConnectionString"], "usagesamples");
33-
var blobCommentsRepository = new BlobContainerClient(config["Blob:ConnectionString"], "comments");
32+
var blobServiceClient = new BlobServiceClient(new Uri(config["StorageAccountUrl"]), new DefaultAzureCredential());
33+
var blobCodeFileContainerClient = blobServiceClient.GetBlobContainerClient("codefiles");
34+
var blobOriginalContainerClient = blobServiceClient.GetBlobContainerClient("originals");
35+
var blobUsageSampleRepository = blobServiceClient.GetBlobContainerClient("usagesamples");
36+
var blobCommentsRepository = blobServiceClient.GetBlobContainerClient("comments");
3437
_ = await blobCodeFileContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);
3538
_ = await blobOriginalContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);
3639
_ = await blobUsageSampleRepository.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);

src/dotnet/APIView/APIViewWeb/Repositories/BlobCodeFileRepository.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using APIViewWeb.LeanModels;
1010
using APIViewWeb.Models;
1111
using APIViewWeb.Repositories;
12+
using Azure.Identity;
1213
using Azure.Storage.Blobs;
1314
using Microsoft.Extensions.Caching.Memory;
1415
using Microsoft.Extensions.Configuration;
@@ -17,12 +18,12 @@ namespace APIViewWeb
1718
{
1819
public class BlobCodeFileRepository : IBlobCodeFileRepository
1920
{
20-
private BlobContainerClient _container;
2121
private readonly IMemoryCache _cache;
22+
private BlobServiceClient _serviceClient;
2223

2324
public BlobCodeFileRepository(IConfiguration configuration, IMemoryCache cache)
2425
{
25-
_container = new BlobContainerClient(configuration["Blob:ConnectionString"], "codefiles");
26+
_serviceClient = new BlobServiceClient(new Uri(configuration["StorageAccountUrl"]), new DefaultAzureCredential());
2627
_cache = cache;
2728
}
2829

@@ -84,7 +85,8 @@ private BlobClient GetBlobClient(string revisionId, string codeFileId, out strin
8485
{
8586
key = revisionId + "/" + codeFileId;
8687
}
87-
return _container.GetBlobClient(key);
88+
var container = _serviceClient.GetBlobContainerClient("codefiles");
89+
return container.GetBlobClient(key);
8890
}
8991
}
9092
}

src/dotnet/APIView/APIViewWeb/Repositories/BlobOriginalsRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Threading.Tasks;
77
using APIViewWeb.Repositories;
8+
using Azure.Identity;
89
using Azure.Storage.Blobs;
910
using Microsoft.Extensions.Configuration;
1011

@@ -18,7 +19,8 @@ public class BlobOriginalsRepository : IBlobOriginalsRepository
1819

1920
public BlobOriginalsRepository(IConfiguration configuration)
2021
{
21-
_container = new BlobContainerClient(configuration["Blob:ConnectionString"], "originals");
22+
var serviceClient = new BlobServiceClient(new Uri(configuration["StorageAccountUrl"]), new DefaultAzureCredential());
23+
_container = serviceClient.GetBlobContainerClient("originals");
2224
}
2325

2426
public async Task<Stream> GetOriginalAsync(string codeFileId)

src/dotnet/APIView/APIViewWeb/Repositories/BlobUsageSampleRepository.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
using Microsoft.Extensions.Configuration;
88
using System.Text;
99
using System;
10+
using Azure.Identity;
1011

1112
namespace APIViewWeb.Repositories
1213
{
1314
public class BlobUsageSampleRepository : IBlobUsageSampleRepository
1415
{
15-
private BlobContainerClient _container;
16+
private BlobServiceClient _serviceClient;
1617

1718
public BlobUsageSampleRepository(IConfiguration configuration)
1819
{
19-
var connectionString = configuration["Blob:ConnectionString"];
20-
_container = new BlobContainerClient(connectionString, "usagesamples");
20+
_serviceClient = new BlobServiceClient(new Uri(configuration["StorageAccountUrl"]), new DefaultAzureCredential());
2121
}
2222

2323
public async Task<Stream> GetUsageSampleAsync(string sampleFileId)
@@ -53,7 +53,8 @@ public async Task DeleteUsageSampleAsync (string sampleFileId)
5353

5454
private BlobClient GetBlobClient(string sampleFileId)
5555
{
56-
return _container.GetBlobClient(sampleFileId);
56+
var container = _serviceClient.GetBlobContainerClient("usagesamples");
57+
return container.GetBlobClient(sampleFileId);
5758
}
5859
}
5960
}

0 commit comments

Comments
 (0)