Fix a number of cred scan hits (#800)

Bunch of secrets in files, usually fixed by generating random password. The deleted script files didn't seem to be used anywhere.
This commit is contained in:
Charles Gagnon
2019-04-24 13:31:34 -07:00
committed by GitHub
parent e9bf57bc67
commit 85f34b65f1
9 changed files with 119 additions and 665 deletions

View File

@@ -64,7 +64,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
CancellationToken token;
bool ready = false;
mockConnection.Setup(x => x.OpenAsync(It.IsAny<CancellationToken>()))
.Callback<CancellationToken>(t =>
.Callback<CancellationToken>(t =>
{
// Pass the token to the return handler and signal the main thread to cancel
token = t;
@@ -107,7 +107,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
// Wait for the connection task to finish
connectTask.Wait();
// Verify that the connection was cancelled (no connection was created)
Assert.Null(connectTask.Result.ConnectionId);
@@ -125,7 +125,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
CancellationToken token;
bool ready = false;
mockConnection.Setup(x => x.OpenAsync(It.IsAny<CancellationToken>()))
.Callback<CancellationToken>(t =>
.Callback<CancellationToken>(t =>
{
// Pass the token to the return handler and signal the main thread to cancel
token = t;
@@ -139,7 +139,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
}
return Task.FromResult(true);
});
// Given a second connection that succeeds
var mockConnection2 = new Mock<DbConnection> { CallBase = true };
mockConnection2.Setup(x => x.OpenAsync(It.IsAny<CancellationToken>()))
@@ -175,7 +175,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
// Wait for the first connection task to finish
connectTask.Wait();
// Verify that the first connection was cancelled (no connection was created)
Assert.Null(connectTask.Result.ConnectionId);
@@ -193,13 +193,13 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
CancellationToken token;
bool ready = false;
mockConnection.Setup(x => x.OpenAsync(It.IsAny<CancellationToken>()))
.Callback<CancellationToken>(t =>
.Callback<CancellationToken>(t =>
{
// Pass the token to the return handler and signal the main thread to cancel
token = t;
ready = true;
})
.Returns(() =>
.Returns(() =>
{
if (TestUtils.WaitFor(() => token.IsCancellationRequested))
{
@@ -236,7 +236,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
// Wait for the first connection task to finish
connectTask.Wait();
// Verify that the first connection was cancelled (no connection was created)
Assert.Null(connectTask.Result.ConnectionId);
@@ -263,7 +263,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
OwnerUri = "file:///my/test/file.sql",
Connection = connectionDetails
});
// check that a connection was created
Assert.NotEmpty(connectionResult.ConnectionId);
}
@@ -319,10 +319,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
const string otherDbName = "other";
// Given a connection that returns the database name
var dummySqlConnection = new TestSqlConnection(null);
var mockFactory = new Mock<ISqlConnectionFactory>();
mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string connString, string azureAccountToken) =>
.Returns((string connString, string azureAccountToken) =>
{
dummySqlConnection.ConnectionString = connString;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connString);
@@ -388,7 +388,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
ServerName = testConnectionDetails.ServerName,
DatabaseName = testConnectionDetails.DatabaseName,
UserName = "invalidUsername",
Password = "invalidPassword"
Password = Guid.NewGuid().ToString()
};
// triggers exception when opening mock connection
@@ -438,7 +438,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
AuthenticationType = authType
}
});
// check that an error was caught
Assert.NotNull(connectionResult.Messages);
Assert.NotEqual(String.Empty, connectionResult.Messages);
@@ -472,7 +472,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
AuthenticationType = "Integrated"
}
});
// check that the connection was successful
Assert.NotEmpty(connectionResult.ConnectionId);
}
@@ -487,7 +487,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
var connectionResult = await
TestObjects.GetTestConnectionService()
.Connect(null);
// check that an error was caught
Assert.NotNull(connectionResult.Messages);
Assert.NotEqual(String.Empty, connectionResult.Messages);
@@ -595,7 +595,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
[Fact]
public async Task ConnectToDatabaseTest()
{
// connect to a database instance
// connect to a database instance
string ownerUri = "file://my/sample/file.sql";
var connectionResult = await
TestObjects.GetTestConnectionService()
@@ -663,7 +663,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
// register disconnect callback
connectionService.RegisterOnDisconnectTask(
(result, uri) => {
(result, uri) => {
callbackInvoked = true;
Assert.True(uri.Equals(ownerUri));
return Task.FromResult(true);
@@ -779,7 +779,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
.Returns(CreateMockDbConnection(new[] {data}));
var connectionService = new ConnectionService(mockFactory.Object);
// connect to a database instance
// connect to a database instance
string ownerUri = "file://my/sample/file.sql";
var connectionResult = await
connectionService
@@ -816,13 +816,13 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
// setup connection service with callback
var connectionService = TestObjects.GetTestConnectionService();
connectionService.RegisterOnConnectionTask(
(sqlConnection) => {
(sqlConnection) => {
callbackInvoked = true;
return Task.FromResult(true);
return Task.FromResult(true);
}
);
// connect to a database instance
// connect to a database instance
await connectionService.Connect(TestObjects.GetTestConnectionParams());
// verify that a valid connection id was returned
@@ -830,7 +830,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
}
/// <summary>
/// Test ConnectionSummaryComparer
/// Test ConnectionSummaryComparer
/// </summary>
[Fact]
public void TestConnectionSummaryComparer()
@@ -869,14 +869,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
var service = TestObjects.GetTestConnectionService();
var connectParams = TestObjects.GetTestConnectionParams();
// connect to a database instance
// connect to a database instance
var connectionResult = await service.Connect(connectParams);
// verify that a valid connection id was returned
Assert.NotNull(connectionResult.ConnectionId);
Assert.NotEqual(string.Empty, connectionResult.ConnectionId);
Assert.NotNull(new Guid(connectionResult.ConnectionId));
// verify that the (URI -> connection) mapping was created
ConnectionInfo info;
Assert.True(service.TryFindConnection(connectParams.OwnerUri, out info));
@@ -892,8 +892,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
[Fact]
public void TestThatLinuxAndOsxSqlExceptionHasNoErrorCode()
{
RunIfWrapper.RunIfLinuxOrOSX(() =>
{
RunIfWrapper.RunIfLinuxOrOSX(() =>
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder
@@ -931,7 +931,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
public void TestListDatabasesInvalidParams()
{
var service = TestObjects.GetTestConnectionService();
var listParams = new ListDatabasesParams();
var listParams = new ListDatabasesParams();
Assert.Throws<ArgumentException>(() => service.ListDatabases(listParams));
listParams.OwnerUri = "file://notmyfile.sql";
Assert.Throws<Exception>(() => service.ListDatabases(listParams));
@@ -973,7 +973,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
};
}
ConnectionSummaryComparer comparer = new ConnectionSummaryComparer();
// If I compute a hash code
int hashCode = comparer.GetHashCode(summary);
if (summary == null || (serverName == null && databaseName == null && userName == null))
@@ -1086,7 +1086,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
await service.Connect(connectParamsDefault);
await service.Connect(connectParamsQuery);
// We should have one ConnectionInfo and 2 DbConnections
// We should have one ConnectionInfo and 2 DbConnections
ConnectionInfo connectionInfo = service.OwnerToConnectionMap[connectParamsDefault.OwnerUri];
Assert.Equal(2, connectionInfo.CountConnections);
Assert.Equal(1, service.OwnerToConnectionMap.Count);
@@ -1111,7 +1111,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
});
connectionInfo.ConnectionTypeToConnectionMap[ConnectionType.Query] = mockQueryConnection.Object;
// If we disconnect all open connections with the same URI as used above
// If we disconnect all open connections with the same URI as used above
service.Disconnect(disconnectParams);
// Close() should have gotten called for both DbConnections
@@ -1210,7 +1210,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
await Assert.ThrowsAsync<InvalidOperationException>(
() => service.GetOrOpenConnection(TestObjects.ScriptUri, ConnectionType.Query));
}
[Fact]
public async Task GetOrOpenAdminDefaultConnection()
{
@@ -1238,7 +1238,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
// Connect
ConnectionService service = TestObjects.GetTestConnectionService();
var connectionResult = await service.Connect(connectionParameters);
// Verify you can get the connection for default
DbConnection defaultConn = await service.GetOrOpenConnection(connectionParameters.OwnerUri, ConnectionType.Default);
ConnectionInfo connInfo = service.OwnerToConnectionMap[connectionParameters.OwnerUri];
@@ -1322,10 +1322,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
const string otherDbName = "other";
// Given a connection that returns the database name
var connection = new TestSqlConnection(null);
var mockFactory = new Mock<ISqlConnectionFactory>();
mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string connString, string azureAccountToken) =>
.Returns((string connString, string azureAccountToken) =>
{
connection.ConnectionString = connString;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connString);
@@ -1370,12 +1370,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
mockConnection.SetupGet(conn => conn.State).Returns(ConnectionState.Open);
mockConnection.Setup(conn => conn.Close());
mockConnection.Setup(conn => conn.Open());
var connection = mockConnection.Object;
var mockFactory = new Mock<ISqlConnectionFactory>();
mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string connString, string azureAccountToken) =>
.Returns((string connString, string azureAccountToken) =>
{
connection.ConnectionString = connString;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connString);
@@ -1395,7 +1395,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
OwnerUri = ownerUri,
Connection = TestObjects.GetTestConnectionDetails()
});
ConnectionInfo testInfo;
connectionService.TryFindConnection(ownerUri, out testInfo);
@@ -1423,12 +1423,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
mockConnection.SetupGet(conn => conn.State).Returns(ConnectionState.Open);
mockConnection.Setup(conn => conn.Close());
mockConnection.Setup(conn => conn.Open());
var connection = mockConnection.Object;
var mockFactory = new Mock<ISqlConnectionFactory>();
mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string connString, string azureAccountToken) =>
.Returns((string connString, string azureAccountToken) =>
{
connection.ConnectionString = connString;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connString);
@@ -1448,7 +1448,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
OwnerUri = ownerUri,
Connection = TestObjects.GetTestConnectionDetails()
});
ConnectionInfo testInfo;
connectionService.TryFindConnection(ownerUri, out testInfo);
@@ -1467,9 +1467,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
[Fact]
public void ParseConnectionStringTest()
{
// If we make a connection to a live database
// If we make a connection to a live database
ConnectionService service = ConnectionService.Instance;
var connectionString = "Server=tcp:{servername},1433;Initial Catalog={databasename};Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
var details = service.ParseConnectionString(connectionString);

View File

@@ -25,19 +25,19 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
{
private static readonly StoreConfig Config = new StoreConfig
{
CredentialFolder = ".testsecrets",
CredentialFile = "sqltestsecrets.json",
CredentialFolder = ".testsecrets",
CredentialFile = "sqltestsecrets.json",
IsRelativeToUserHomeDir = true
};
private const string CredentialId = "Microsoft_SqlToolsTest_TestId";
private const string Password1 = "P@ssw0rd1";
private const string Password2 = "2Pass2Furious";
private readonly string Password1 = Guid.NewGuid().ToString();
private readonly string Password2 = Guid.NewGuid().ToString();
private const string OtherCredId = CredentialId + "2345";
private const string OtherPassword = CredentialId + "2345";
// Test-owned credential store used to clean up before/after tests to ensure code works as expected
// Test-owned credential store used to clean up before/after tests to ensure code works as expected
// even if previous runs stopped midway through
private readonly ICredentialStore credStore;
private readonly CredentialService service;
@@ -50,7 +50,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
service = new CredentialService(credStore, Config);
DeleteDefaultCreds();
}
public void Dispose()
{
DeleteDefaultCreds();
@@ -89,7 +89,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
{
string errorResponse = null;
var contextMock = RequestContextMocks.Create<bool>(null).AddErrorHandling((msg, code) => errorResponse = msg);
await service.HandleSaveCredentialRequest(new Credential(CredentialId), contextMock.Object);
TestUtils.VerifyErrorSent(contextMock);
Assert.True(errorResponse.Contains("ArgumentException") || errorResponse.Contains("ArgumentNullException"));
@@ -202,7 +202,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
TestUtils.VerifyErrorSent(contextMock);
Assert.Contains("ArgumentNullException", errorResponse);
}
[Fact]
public async Task ReadCredentialThrowsIfIdMissing()
{
@@ -232,7 +232,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials
Assert.Null(actual.Password);
}));
}
[Fact]
public async Task DeleteCredentialThrowsIfIdMissing()
{

View File

@@ -1,6 +1,6 @@
//
// Code originally from http://credentialmanagement.codeplex.com/,
// Licensed under the Apache License 2.0
// Code originally from http://credentialmanagement.codeplex.com/,
// Licensed under the Apache License 2.0
//
using System;
@@ -15,7 +15,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials.Win32
[Fact]
public void CredentialSetCreate()
{
RunIfWrapper.RunIfWindows(() =>
RunIfWrapper.RunIfWindows(() =>
{
Assert.NotNull(new CredentialSet());
});
@@ -24,7 +24,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials.Win32
[Fact]
public void CredentialSetCreateWithTarget()
{
RunIfWrapper.RunIfWindows(() =>
RunIfWrapper.RunIfWindows(() =>
{
Assert.NotNull(new CredentialSet("target"));
});
@@ -33,7 +33,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials.Win32
[Fact]
public void CredentialSetShouldBeIDisposable()
{
RunIfWrapper.RunIfWindows(() =>
RunIfWrapper.RunIfWindows(() =>
{
Assert.True(new CredentialSet() is IDisposable, "CredentialSet needs to implement IDisposable Interface.");
});
@@ -42,7 +42,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials.Win32
[Fact]
public void CredentialSetLoad()
{
RunIfWrapper.RunIfWindows(() =>
RunIfWrapper.RunIfWindows(() =>
{
Win32Credential credential = new Win32Credential
{
@@ -67,7 +67,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials.Win32
[Fact]
public void CredentialSetLoadShouldReturnSelf()
{
RunIfWrapper.RunIfWindows(() =>
RunIfWrapper.RunIfWindows(() =>
{
CredentialSet set = new CredentialSet();
Assert.IsType<CredentialSet>(set.Load());
@@ -79,12 +79,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Credentials.Win32
[Fact]
public void CredentialSetLoadWithTargetFilter()
{
RunIfWrapper.RunIfWindows(() =>
RunIfWrapper.RunIfWindows(() =>
{
Win32Credential credential = new Win32Credential
{
Username = "filteruser",
Password = "filterpassword",
Password = Guid.NewGuid().ToString(),
Target = "filtertarget"
};
credential.Save();

View File

@@ -23,7 +23,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
/// </summary>
public class TestObjects
{
public const string ScriptUri = "file://some/file.sql";
/// <summary>
@@ -48,7 +48,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
public static ConnectParams GetTestConnectionParams(bool useConnectionString = false)
{
return new ConnectParams()
return new ConnectParams()
{
OwnerUri = ScriptUri,
Connection = GetTestConnectionDetails(useConnectionString)
@@ -88,14 +88,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
{
return new ConnectionDetails()
{
ConnectionString = "User ID=user;PWD=password;Database=databaseName;Server=serverName"
ConnectionString = $"User ID=user;PWD={Guid.NewGuid().ToString()};Database=databaseName;Server=serverName"
};
}
return new ConnectionDetails()
{
UserName = "user",
Password = "password",
Password = Guid.NewGuid().ToString(),
DatabaseName = "databaseName",
ServerName = "serverName"
};
@@ -198,7 +198,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
private string _database;
private ConnectionState _state;
public TestSqlConnection()
public TestSqlConnection()
{
}
@@ -207,7 +207,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
{
Data = data;
}
internal TestResultSet[] Data { get; set; }
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
@@ -233,7 +233,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
public override string ConnectionString { get; set; }
public override string Database
{
{
get { return _database; }
}