Skip to content

Commit

Permalink
Move to latest ServerCommon version (#10106)
Browse files Browse the repository at this point in the history
* Move to latest ServerCommon version

This bring Azure.Data.Tables and Azure.Storage.Blobs into several new places.
NuGet.Services.Storage has a new constructor affecting a lot of DI setup.

* Don't exclude test helper projects
* Handle question mark SAS in more places
* Switch to explicit AzureStorageFactory construction

This improves compile time validation.
  • Loading branch information
joelverhagen authored Aug 2, 2024
1 parent d25d34d commit a782405
Show file tree
Hide file tree
Showing 38 changed files with 172 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled>
<EnablePackageVersionOverride>true</EnablePackageVersionOverride>
<NuGetClientPackageVersion>6.9.1</NuGetClientPackageVersion>
<ServerCommonPackageVersion>2.120.0</ServerCommonPackageVersion>
<ServerCommonPackageVersion>2.122.0</ServerCommonPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="4.4.0" />
Expand Down
4 changes: 2 additions & 2 deletions src/Gallery.CredentialExpiration/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Azure.Storage.Blobs;
using Gallery.CredentialExpiration.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using Newtonsoft.Json;
using NuGet.Jobs;
using NuGet.Services.Messaging;
Expand Down Expand Up @@ -52,7 +52,7 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string

FromAddress = new MailAddress(InitializationConfiguration.MailFrom);

var storageAccount = CloudStorageAccount.Parse(InitializationConfiguration.DataStorageAccount);
var storageAccount = new BlobServiceClient(AzureStorageFactory.PrepareConnectionString(InitializationConfiguration.DataStorageAccount));
var storageFactory = new AzureStorageFactory(
storageAccount,
InitializationConfiguration.ContainerName,
Expand Down
23 changes: 14 additions & 9 deletions src/GitHubVulnerabilities2Db/Job.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// 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 System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Azure.Storage.Blobs;
using GitHubVulnerabilities2Db.Configuration;
using GitHubVulnerabilities2Db.Fakes;
using GitHubVulnerabilities2Db.Gallery;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using NuGet.Jobs;
using NuGet.Jobs.Configuration;
using NuGet.Services.Cursor;
Expand Down Expand Up @@ -164,15 +165,19 @@ protected void ConfigureCollectorServices(ContainerBuilder containerBuilder)
.Register(ctx =>
{
var config = ctx.Resolve<GitHubVulnerabilities2DbConfiguration>();
return CloudStorageAccount.Parse(config.StorageConnectionString);
var connectionString = AzureStorageFactory.PrepareConnectionString(config.StorageConnectionString);
return new BlobServiceClient(connectionString);
})
.As<CloudStorageAccount>();
.As<BlobServiceClient>();

containerBuilder
.RegisterType<AzureStorageFactory>()
.WithParameter(
(parameter, ctx) => parameter.Name == "containerName",
(parameter, ctx) => ctx.Resolve<GitHubVulnerabilities2DbConfiguration>().CursorContainerName)
.Register(ctx =>
{
return new AzureStorageFactory(
ctx.Resolve<BlobServiceClient>(),
ctx.Resolve<GitHubVulnerabilities2DbConfiguration>().CursorContainerName,
ctx.Resolve<ILogger<AzureStorage>>());
})
.As<StorageFactory>()
.As<IStorageFactory>();

Expand All @@ -193,4 +198,4 @@ private DurableCursor CreateCursor(IComponentContext ctx, Func<GitHubVulnerabili
return new DurableCursor(storage.ResolveUri(getBlobName(config)), storage, DateTimeOffset.MinValue);
}
}
}
}
23 changes: 14 additions & 9 deletions src/GitHubVulnerabilities2v3/Job.cs
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 System;
Expand All @@ -7,13 +7,14 @@
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Azure.Storage.Blobs;
using GitHubVulnerabilities2v3.Configuration;
using GitHubVulnerabilities2v3.Extensions;
using GitHubVulnerabilities2v3.Telemetry;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using NuGet.Jobs;
using NuGet.Services.Cursor;
using NuGet.Services.GitHub.Collector;
Expand Down Expand Up @@ -116,15 +117,19 @@ protected void ConfigureCollectorServices(ContainerBuilder containerBuilder)
.Register(ctx =>
{
var config = ctx.Resolve<GitHubVulnerabilities2v3Configuration>();
return CloudStorageAccount.Parse(config.StorageConnectionString);
var connectionString = AzureStorageFactory.PrepareConnectionString(config.StorageConnectionString);
return new BlobServiceClient(connectionString);
})
.As<CloudStorageAccount>();
.As<BlobServiceClient>();

containerBuilder
.RegisterType<AzureStorageFactory>()
.WithParameter(
(parameter, ctx) => parameter.Name == "containerName",
(parameter, ctx) => ctx.Resolve<GitHubVulnerabilities2v3Configuration>().V3VulnerabilityContainerName)
.Register(ctx =>
{
return new AzureStorageFactory(
ctx.Resolve<BlobServiceClient>(),
ctx.Resolve<GitHubVulnerabilities2v3Configuration>().V3VulnerabilityContainerName,
ctx.Resolve<ILogger<AzureStorage>>());
})
.As<StorageFactory>()
.As<IStorageFactory>();

Expand All @@ -145,4 +150,4 @@ private DurableCursor CreateCursor(IComponentContext ctx, Func<GitHubVulnerabili
return new DurableCursor(storage.ResolveUri(getBlobName(config)), storage, DateTimeOffset.MinValue);
}
}
}
}
10 changes: 8 additions & 2 deletions src/Ng/CommandHelpers.cs
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 System;
Expand Down Expand Up @@ -376,6 +376,12 @@ private static BlobServiceClient GetBlobServiceClient(string storageAccountName,
if (string.IsNullOrEmpty(storageKeyValue))
{
var storageSasValue = arguments.GetOrThrow<string>(argumentNameMap[Arguments.StorageSasValue]);
if (storageSasValue.StartsWith("?"))
{
// workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373
storageSasValue = storageSasValue.Substring(1);
}

connectionString = $"BlobEndpoint=https://{storageAccountName}.blob.core.windows.net/;SharedAccessSignature={storageSasValue}";
}
else
Expand Down Expand Up @@ -406,4 +412,4 @@ private static QueueServiceClient GetQueueServiceClient(string storageAccountNam
}

}
}
}
2 changes: 1 addition & 1 deletion src/Ng/Ng.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NuGet.Services.Storage" VersionOverride="2.121.0-ryuyu-newStorageSdk-9662298" />
<PackageReference Include="NuGet.Services.Storage" />
<PackageReference Include="Serilog.Sinks.File" />
</ItemGroup>
</Project>
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 System;
Expand Down Expand Up @@ -56,7 +56,11 @@ private static void RegisterCursorStorage(ContainerBuilder containerBuilder)
.Register(c =>
{
var options = c.Resolve<IOptionsSnapshot<Catalog2RegistrationConfiguration>>();
return new BlobServiceClient(options.Value.StorageConnectionString);

// workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373
var connectionString = options.Value.StorageConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");

return new BlobServiceClient(connectionString);
})
.Keyed<BlobServiceClient>(CursorBindingKey);

Expand Down
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 System;
Expand Down Expand Up @@ -72,10 +72,13 @@ private async Task ExecuteAsync(CancellationToken token)
var frontCursorUri = frontCursorStorage.ResolveUri(CursorRelativeUri);
var frontCursor = new DurableCursor(frontCursorUri, frontCursorStorage, DateTime.MinValue);

// workaround for https://github.com/Azure/azure-sdk-for-net/issues/44373
var connectionString = _options.Value.StorageConnectionString.Replace("SharedAccessSignature=?", "SharedAccessSignature=");

// Log information about where state will be kept.
_logger.LogInformation(
"Using storage URL: {ContainerUrl}/{StoragePath}",
new BlobServiceClient(_options.Value.StorageConnectionString)
new BlobServiceClient(connectionString)
.GetBlobContainerClient(_options.Value.StorageContainer)
.Uri
.AbsoluteUri,
Expand Down
4 changes: 2 additions & 2 deletions src/Stats.PostProcessReports/DetailedReportPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ private async Task CopySourceBlobsAsync(List<StorageListItem> jsonBlobs, Cancell

private async Task<List<StorageListItem>> EnumerateSourceBlobsAsync()
{
var blobs = await _sourceStorage.List(getMetadata: true, cancellationToken: CancellationToken.None);
var blobs = await _sourceStorage.ListAsync(getMetadata: true, cancellationToken: CancellationToken.None);

return blobs.ToList();
}

private async Task<List<StorageListItem>> EnumerateWorkStorageBlobsAsync()
{
var blobs = await _workStorage.List(getMetadata: true, cancellationToken: CancellationToken.None);
var blobs = await _workStorage.ListAsync(getMetadata: true, cancellationToken: CancellationToken.None);

return blobs.ToList();
}
Expand Down
13 changes: 5 additions & 8 deletions src/Stats.PostProcessReports/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using System.Threading.Tasks;
using Autofac;
using Autofac.Core;
using Azure.Storage.Blobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using NuGet.Jobs;
using NuGet.Services.Storage;

Expand Down Expand Up @@ -39,7 +39,7 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild
.Register(c =>
{
var cfg = c.Resolve<IOptionsSnapshot<PostProcessReportsConfiguration>>().Value;
return CloudStorageAccount.Parse(cfg.StorageAccount);
return new BlobServiceClient(AzureStorageFactory.PrepareConnectionString(cfg.StorageAccount));
})
.AsSelf();

Expand All @@ -48,11 +48,10 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild
{
var cfg = c.Resolve<IOptionsSnapshot<PostProcessReportsConfiguration>>().Value;
var factory = new AzureStorageFactory(
c.Resolve<CloudStorageAccount>(),
c.Resolve<BlobServiceClient>(),
cfg.SourceContainerName,
c.Resolve<ILogger<AzureStorage>>(),
cfg.SourcePath + cfg.DetailedReportDirectoryName,
useServerSideCopy: true,
initializeContainer: false);
var storage = factory.Create();
storage.Verbose = false;
Expand All @@ -65,11 +64,10 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild
{
var cfg = c.Resolve<IOptionsSnapshot<PostProcessReportsConfiguration>>().Value;
var factory = new AzureStorageFactory(
c.Resolve<CloudStorageAccount>(),
c.Resolve<BlobServiceClient>(),
cfg.WorkContainerName,
c.Resolve<ILogger<AzureStorage>>(),
cfg.WorkPath,
useServerSideCopy: true,
initializeContainer: false);
var storage = factory.Create();
storage.Verbose = false;
Expand All @@ -82,11 +80,10 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild
{
var cfg = c.Resolve<IOptionsSnapshot<PostProcessReportsConfiguration>>().Value;
var factory = new AzureStorageFactory(
c.Resolve<CloudStorageAccount>(),
c.Resolve<BlobServiceClient>(),
cfg.DestinationContainerName,
c.Resolve<ILogger<AzureStorage>>(),
cfg.DestinationPath,
useServerSideCopy: true,
initializeContainer: false);
var storage = factory.Create();
storage.Verbose = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ManualStatusChangeCollectorProcessor(
await _handler.Handle(_table, manualChange);
}

return manualChanges.Any() ? manualChanges.Max(c => c.Timestamp.UtcDateTime) : (DateTime?)null;
return manualChanges.Any() ? manualChanges.Max(c => c.Timestamp.Value.UtcDateTime) : null;
}
}
}
10 changes: 5 additions & 5 deletions src/StatusAggregator/Container/ContainerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Blob;
using Azure.Storage.Blobs;

namespace StatusAggregator.Container
{
public class ContainerWrapper : IContainerWrapper
{
private readonly CloudBlobContainer _container;
private readonly BlobContainerClient _container;

public ContainerWrapper(CloudBlobContainer container)
public ContainerWrapper(BlobContainerClient container)
{
_container = container;
}
Expand All @@ -22,8 +22,8 @@ public Task CreateIfNotExistsAsync()

public Task SaveBlobAsync(string name, string contents)
{
var blob = _container.GetBlockBlobReference(name);
return blob.UploadTextAsync(contents);
var blob = _container.GetBlobClient(name);
return blob.UploadAsync(contents);
}
}
}
3 changes: 1 addition & 2 deletions src/StatusAggregator/Container/IContainerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Blob;

namespace StatusAggregator.Container
{
/// <summary>
/// Simple wrapper for <see cref="CloudBlobContainer"/> that exists for unit-testing.
/// Simple wrapper for <see cref="Azure.Storage.Blobs.BlobContainerClient"/> that exists for unit-testing.
/// </summary>
public interface IContainerWrapper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;
using Azure.Data.Tables;
using StatusAggregator.Parse;

namespace StatusAggregator.Factory
Expand All @@ -11,7 +11,7 @@ namespace StatusAggregator.Factory
/// Creates a <typeparamref name="TEntity"/> given a <see cref="ParsedIncident"/>.
/// </summary>
public interface IComponentAffectingEntityFactory<TEntity>
where TEntity : TableEntity
where TEntity : ITableEntity
{
Task<TEntity> CreateAsync(ParsedIncident input);
}
Expand Down
Loading

0 comments on commit a782405

Please sign in to comment.