-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PopulateCredentialsTask. Also removed Identifier column for now
- Loading branch information
1 parent
d92a97c
commit d048af7
Showing
9 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/NuGetGallery.Operations/Infrastructure/TableValuedParameter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using System.Text; | ||
using Dapper; | ||
using Microsoft.SqlServer.Server; | ||
|
||
namespace NuGetGallery.Operations.Infrastructure | ||
{ | ||
public class TableValuedParameter : SqlMapper.IDynamicParameters | ||
{ | ||
public string Name { get; private set; } | ||
public string TableType { get; private set; } | ||
public DataTable TableValue { get; private set; } | ||
|
||
public TableValuedParameter(string name, string tableType, DataTable tableValue) | ||
{ | ||
Name = name; | ||
TableType = tableType; | ||
TableValue = tableValue; | ||
} | ||
|
||
public void AddParameters(IDbCommand command, SqlMapper.Identity identity) | ||
{ | ||
var sqlCommand = (SqlCommand)command; | ||
var param = new SqlParameter(Name, TableValue) | ||
{ | ||
TypeName = TableType, | ||
SqlDbType = SqlDbType.Structured, | ||
Direction = ParameterDirection.Input | ||
}; | ||
sqlCommand.Parameters.Add(param); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/NuGetGallery.Operations/Tasks/DataManagement/PopulateCredentialsTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using NuGetGallery.Operations.Infrastructure; | ||
|
||
namespace NuGetGallery.Operations.Tasks.DataManagement | ||
{ | ||
[Command("populatecredentials", "Populates the Credentials table for users who are missing data", AltName = "pc")] | ||
public class PopulateCredentialsTask : DatabaseTask | ||
{ | ||
private const string WhatIfQuery = "BEGIN TRAN\r\n" + Query + "\r\nROLLBACK TRAN"; | ||
private const string CommitQuery = "BEGIN TRAN\r\n" + Query + "\r\nCOMMIT TRAN"; | ||
|
||
private const string Query = @" | ||
DECLARE @results TABLE( | ||
Action nchar(10), | ||
UserKey int, | ||
Type nvarchar(64), | ||
Value nvarchar(256) | ||
) | ||
MERGE INTO Credentials dest | ||
USING @creds src | ||
ON src.UserKey = dest.UserKey AND src.Type = dest.Type | ||
WHEN NOT MATCHED THEN | ||
INSERT(UserKey, Type, Value) | ||
VALUES(src.UserKey, src.Type, src.Value) | ||
OUTPUT | ||
$action AS 'Action', | ||
inserted.UserKey, | ||
inserted.Type, | ||
inserted.Value | ||
INTO @results; | ||
SELECT COUNT(*) FROM @results | ||
"; | ||
|
||
private Dictionary<string, string> _hashAlgorithmToCredType = new Dictionary<string, string>() { | ||
{Constants.PBKDF2HashAlgorithmId, CredentialTypes.Password.Pbkdf2}, | ||
{Constants.Sha1HashAlgorithmId, CredentialTypes.Password.Sha1} | ||
}; | ||
|
||
public override void ExecuteCommand() | ||
{ | ||
WithConnection(c => | ||
{ | ||
// Get user credentials | ||
var users = c.Query("SELECT [Key], HashedPassword, PasswordHashAlgorithm, ApiKey FROM Users"); | ||
|
||
// Build a table | ||
var dt = new DataTable(); | ||
dt.Columns.Add("UserKey", typeof(int)); | ||
dt.Columns.Add("Type", typeof(string)); | ||
dt.Columns.Add("Value", typeof(string)); | ||
foreach (var user in users) | ||
{ | ||
var row = dt.NewRow(); | ||
row.SetField("UserKey", (int)user.Key); | ||
row.SetField("Type", CredentialTypes.ApiKeyV1); | ||
row.SetField("Value", ((Guid)user.ApiKey).ToString().ToLowerInvariant()); | ||
dt.Rows.Add(row); | ||
|
||
|
||
string passwordCredType; | ||
if (!_hashAlgorithmToCredType.TryGetValue(user.PasswordHashAlgorithm, out passwordCredType)) | ||
{ | ||
Log.Error("Unknown Hash Algorithm: {0}", user.PasswordHashAlgorithm); | ||
} | ||
else | ||
{ | ||
row = dt.NewRow(); | ||
row.SetField("UserKey", (int)user.Key); | ||
row.SetField("Type", passwordCredType); | ||
row.SetField("Value", (string)user.HashedPassword); | ||
dt.Rows.Add(row); | ||
} | ||
} | ||
|
||
WithTableType(c, "Temp_PopulateCredentialsInputType", "UserKey int, Type nvarchar(64), Value nvarchar(256)", () => | ||
{ | ||
// Update the DB | ||
var updatedRowCount = c.Execute( | ||
WhatIf ? WhatIfQuery : CommitQuery, | ||
new TableValuedParameter("@creds", "Temp_PopulateCredentialsInputType", dt)); | ||
|
||
Log.Info("Inserted {0} credential records", updatedRowCount); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.