Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit f72ea77

Browse files
author
Christy Henriksson
authored
Revert "Update jobs to latest SQL AAD (#485)" (#492) (#493)
This reverts commit d4e769d.
1 parent 5b766ba commit f72ea77

53 files changed

Lines changed: 348 additions & 452 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ArchivePackages/ArchivePackages.Job.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Design;
7-
using System.Data.SqlClient;
87
using System.Diagnostics.Tracing;
98
using System.Linq;
109
using System.Threading.Tasks;
@@ -13,6 +12,8 @@
1312
using Microsoft.WindowsAzure.Storage.Blob;
1413
using Newtonsoft.Json.Linq;
1514
using NuGet.Jobs;
15+
using NuGet.Services.KeyVault;
16+
using NuGet.Services.Sql;
1617

1718
namespace ArchivePackages
1819
{
@@ -52,20 +53,22 @@ public class Job : JobBase
5253
/// Blob containing the cursor data. Cursor data comprises of cursorDateTime
5354
/// </summary>
5455
public string CursorBlobName { get; set; }
56+
57+
private ISqlConnectionFactory _packageDbConnectionFactory;
5558

5659
protected CloudBlobContainer SourceContainer { get; private set; }
5760

5861
protected CloudBlobContainer PrimaryDestinationContainer { get; private set; }
5962

6063
protected CloudBlobContainer SecondaryDestinationContainer { get; private set; }
6164

62-
private SqlConnectionStringBuilder PackageDatabase { get; set; }
63-
6465
public Job() : base(JobEventSource.Log) { }
6566

6667
public override void Init(IServiceContainer serviceContainer, IDictionary<string, string> jobArgsDictionary)
6768
{
68-
PackageDatabase = RegisterDatabase(serviceContainer, jobArgsDictionary, JobArgumentNames.PackageDatabase);
69+
var secretInjector = (ISecretInjector)serviceContainer.GetService(typeof(ISecretInjector));
70+
var packageDbConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.PackageDatabase);
71+
_packageDbConnectionFactory = new AzureSqlConnectionFactory(packageDbConnectionString, secretInjector);
6972

7073
Source = CloudStorageAccount.Parse(
7174
JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.Source));
@@ -89,18 +92,13 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
8992

9093
public override async Task Run()
9194
{
92-
JobEventSourceLog.PreparingToArchive(
93-
Source.Credentials.AccountName, SourceContainer.Name,
94-
PrimaryDestination.Credentials.AccountName, PrimaryDestinationContainer.Name,
95-
PackageDatabase.DataSource, PackageDatabase.InitialCatalog);
96-
95+
JobEventSourceLog.PreparingToArchive(Source.Credentials.AccountName, SourceContainer.Name, PrimaryDestination.Credentials.AccountName, PrimaryDestinationContainer.Name, _packageDbConnectionFactory.DataSource, _packageDbConnectionFactory.InitialCatalog);
9796
await Archive(PrimaryDestinationContainer);
9897

9998
// todo: consider reusing package query for primary and secondary archives
10099
if (SecondaryDestinationContainer != null)
101100
{
102101
JobEventSourceLog.PreparingToArchive2(SecondaryDestination.Credentials.AccountName, SecondaryDestinationContainer.Name);
103-
104102
await Archive(SecondaryDestinationContainer);
105103
}
106104
}
@@ -126,10 +124,9 @@ private async Task Archive(CloudBlobContainer destinationContainer)
126124

127125
JobEventSourceLog.CursorData(cursorDateTime.ToString(DateTimeFormatSpecifier));
128126

129-
JobEventSourceLog.GatheringPackagesToArchiveFromDb(PackageDatabase.DataSource, PackageDatabase.InitialCatalog);
130-
127+
JobEventSourceLog.GatheringPackagesToArchiveFromDb(_packageDbConnectionFactory.DataSource, _packageDbConnectionFactory.InitialCatalog);
131128
List<PackageRef> packages;
132-
using (var connection = await OpenSqlConnectionAsync(JobArgumentNames.PackageDatabase))
129+
using (var connection = await _packageDbConnectionFactory.CreateAsync())
133130
{
134131
packages = (await connection.QueryAsync<PackageRef>(@"
135132
SELECT pr.Id, p.NormalizedVersion AS Version, p.Hash, p.LastEdited, p.Published
@@ -138,8 +135,7 @@ FROM Packages p
138135
WHERE Published > @cursorDateTime OR LastEdited > @cursorDateTime", new { cursorDateTime = cursorDateTime }))
139136
.ToList();
140137
}
141-
142-
JobEventSourceLog.GatheredPackagesToArchiveFromDb(packages.Count, PackageDatabase.DataSource, PackageDatabase.InitialCatalog);
138+
JobEventSourceLog.GatheredPackagesToArchiveFromDb(packages.Count, _packageDbConnectionFactory.DataSource, _packageDbConnectionFactory.InitialCatalog);
143139

144140
var archiveSet = packages
145141
.AsParallel()

src/ArchivePackages/ArchivePackages.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<PackageReference Include="Newtonsoft.Json">
7676
<Version>9.0.1</Version>
7777
</PackageReference>
78+
<PackageReference Include="NuGet.Services.Sql">
79+
<Version>2.25.0-master-30453</Version>
80+
</PackageReference>
7881
<PackageReference Include="System.Net.Http">
7982
<Version>4.3.3</Version>
8083
</PackageReference>

src/Gallery.CredentialExpiration/Gallery.CredentialExpiration.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
<PackageReference Include="Newtonsoft.Json">
9393
<Version>9.0.1</Version>
9494
</PackageReference>
95+
<PackageReference Include="NuGet.Services.Sql">
96+
<Version>2.25.0-master-30263</Version>
97+
</PackageReference>
9598
<PackageReference Include="NuGet.Services.Storage">
9699
<Version>2.1.3</Version>
97100
</PackageReference>

src/Gallery.CredentialExpiration/GalleryCredentialExpiration.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@
66
using System.Data.SqlClient;
77
using System.Linq;
88
using System.Threading.Tasks;
9+
using NuGet.Services.Sql;
910
using Gallery.CredentialExpiration.Models;
1011

1112
namespace Gallery.CredentialExpiration
1213
{
1314
public class GalleryCredentialExpiration : ICredentialExpirationExporter
1415
{
1516
private readonly CredentialExpirationJobMetadata _jobMetadata;
17+
private readonly ISqlConnectionFactory _galleryDatabase;
1618

17-
private Func<Task<SqlConnection>> OpenGallerySqlConnectionAsync { get; }
18-
19-
public GalleryCredentialExpiration(
20-
CredentialExpirationJobMetadata jobMetadata,
21-
Func<Task<SqlConnection>> openGallerySqlConnectionAsync)
19+
public GalleryCredentialExpiration(CredentialExpirationJobMetadata jobMetadata, ISqlConnectionFactory galleryDatabase)
2220
{
2321
_jobMetadata = jobMetadata;
24-
OpenGallerySqlConnectionAsync = openGallerySqlConnectionAsync;
22+
_galleryDatabase = galleryDatabase;
2523
}
2624

2725
/// <summary>
@@ -53,7 +51,7 @@ public async Task<List<ExpiredCredentialData>> GetCredentialsAsync(TimeSpan time
5351
var minNotificationDate = ConvertToString(GetMinNotificationDate());
5452

5553
// Connect to database
56-
using (var galleryConnection = await OpenGallerySqlConnectionAsync())
54+
using (var galleryConnection = await _galleryDatabase.CreateAsync())
5755
{
5856
// Fetch credentials that expire in _warnDaysBeforeExpiration days
5957
// + the user's e-mail address

src/Gallery.CredentialExpiration/Job.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
67
using System.ComponentModel.Design;
78
using System.Data.SqlClient;
@@ -14,7 +15,10 @@
1415
using Microsoft.Extensions.Logging;
1516
using Microsoft.WindowsAzure.Storage;
1617
using Newtonsoft.Json;
18+
using Newtonsoft.Json.Linq;
1719
using NuGet.Jobs;
20+
using NuGet.Services.KeyVault;
21+
using NuGet.Services.Sql;
1822
using NuGet.Services.Storage;
1923

2024
namespace Gallery.CredentialExpiration
@@ -30,6 +34,8 @@ public class Job : JobBase
3034
private string _galleryBrand;
3135
private string _galleryAccountUrl;
3236

37+
private ISqlConnectionFactory _galleryDatabase;
38+
3339
private string _mailFrom;
3440
private SmtpClient _smtpClient;
3541

@@ -41,7 +47,9 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
4147
{
4248
_whatIf = JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.WhatIf);
4349

44-
RegisterDatabase(serviceContainer, jobArgsDictionary, JobArgumentNames.GalleryDatabase);
50+
var secretInjector = (ISecretInjector)serviceContainer.GetService(typeof(ISecretInjector));
51+
var databaseConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.GalleryDatabase);
52+
_galleryDatabase = new AzureSqlConnectionFactory(databaseConnectionString, secretInjector);
4553

4654
_galleryBrand = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryBrand);
4755
_galleryAccountUrl = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryAccountUrl);
@@ -63,17 +71,12 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
6371
_storage = storageFactory.Create();
6472
}
6573

66-
public Task<SqlConnection> OpenGallerySqlConnectionAsync()
67-
{
68-
return OpenSqlConnectionAsync(JobArgumentNames.GalleryDatabase);
69-
}
70-
7174
public override async Task Run()
7275
{
7376
var jobRunTime = DateTimeOffset.UtcNow;
7477
// Default values
7578
var jobCursor = new JobRunTimeCursor( jobCursorTime: jobRunTime, maxProcessedCredentialsTime: jobRunTime );
76-
var galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), OpenGallerySqlConnectionAsync);
79+
var galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), _galleryDatabase);
7780

7881
try
7982
{
@@ -86,7 +89,7 @@ public override async Task Run()
8689
// Load from cursor
8790
// Throw if the schema is not correct to ensure that not-intended emails are sent.
8891
jobCursor = JsonConvert.DeserializeObject<JobRunTimeCursor>(content, new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error });
89-
galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), OpenGallerySqlConnectionAsync);
92+
galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), _galleryDatabase);
9093
}
9194

9295
// Connect to database

src/Gallery.Maintenance/DeleteExpiredVerificationKeysTask.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Threading.Tasks;
1010
using Gallery.Maintenance.Models;
1111
using Microsoft.Extensions.Logging;
12-
using NuGet.Jobs;
1312

1413
namespace Gallery.Maintenance
1514
{
@@ -38,7 +37,7 @@ public override async Task RunAsync(Job job)
3837
{
3938
IEnumerable<PackageVerificationKey> expiredKeys;
4039

41-
using (var connection = await job.OpenSqlConnectionAsync(JobArgumentNames.GalleryDatabase))
40+
using (var connection = await job.GalleryDatabase.CreateAsync())
4241
{
4342
expiredKeys = await connection.QueryWithRetryAsync<PackageVerificationKey>(
4443
SelectQuery,
@@ -60,7 +59,7 @@ public override async Task RunAsync(Job job)
6059

6160
if (expectedRowCount > 0)
6261
{
63-
using (var connection = await job.OpenSqlConnectionAsync(JobArgumentNames.GalleryDatabase))
62+
using (var connection = await job.GalleryDatabase.CreateAsync())
6463
{
6564
using (var transaction = connection.BeginTransaction())
6665
{

src/Gallery.Maintenance/Gallery.Maintenance.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<PackageReference Include="Newtonsoft.Json">
6868
<Version>9.0.1</Version>
6969
</PackageReference>
70+
<PackageReference Include="NuGet.Services.Sql">
71+
<Version>2.25.0-master-30263</Version>
72+
</PackageReference>
7073
<PackageReference Include="System.Net.Http">
7174
<Version>4.3.3</Version>
7275
</PackageReference>

src/Gallery.Maintenance/Job.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Design;
7+
using System.Data;
78
using System.Linq;
89
using System.Threading.Tasks;
910
using Microsoft.Extensions.Logging;
1011
using NuGet.Jobs;
1112
using NuGet.Services.KeyVault;
13+
using NuGet.Services.Sql;
1214

1315
namespace Gallery.Maintenance
1416
{
@@ -17,9 +19,15 @@ namespace Gallery.Maintenance
1719
/// </summary>
1820
public class Job : JobBase
1921
{
22+
23+
public ISqlConnectionFactory GalleryDatabase { get; private set; }
24+
2025
public override void Init(IServiceContainer serviceContainer, IDictionary<string, string> jobArgsDictionary)
2126
{
22-
RegisterDatabase(serviceContainer, jobArgsDictionary, JobArgumentNames.GalleryDatabase);
27+
var secretInjector = (ISecretInjector)serviceContainer.GetService(typeof(ISecretInjector));
28+
var databaseConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.GalleryDatabase);
29+
30+
GalleryDatabase = new AzureSqlConnectionFactory(databaseConnectionString, secretInjector);
2331
}
2432

2533
public override async Task Run()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.Threading.Tasks;
5+
6+
// ReSharper disable once CheckNamespace
7+
namespace System.Data.SqlClient
8+
{
9+
public static class SqlConnectionStringBuilderExtensions
10+
{
11+
public static Task<SqlConnection> ConnectTo(this SqlConnectionStringBuilder self)
12+
{
13+
return ConnectTo(self.ConnectionString);
14+
}
15+
16+
private static async Task<SqlConnection> ConnectTo(string connection)
17+
{
18+
var c = new SqlConnection(connection);
19+
await c.OpenAsync().ConfigureAwait(continueOnCapturedContext: false);
20+
return c;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)