mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Agent Proxy and Credential request handlers (#637)
* Agent Proxy account WIP * Fixup Credential create\update\delete handlers * Use current user for test credential * Cleanup and delete code * Convert tabs to spaces
This commit is contained in:
@@ -7,74 +7,89 @@ using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
{
|
||||
public class AgentProxyTests
|
||||
{
|
||||
/// <summary>
|
||||
/// TestHandleCreateAgentProxyRequest
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleCreateAgentProxyRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
// setup
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
var credential = await SecurityTestUtils.SetupCredential(connectionResult);
|
||||
var service = new AgentService();
|
||||
var proxy = AgentTestUtils.GetTestProxyInfo();
|
||||
await AgentTestUtils.DeleteAgentProxy(service, connectionResult, proxy);
|
||||
|
||||
// test
|
||||
await AgentTestUtils.CreateAgentProxy(service, connectionResult, proxy);
|
||||
|
||||
// cleanup
|
||||
await AgentTestUtils.DeleteAgentProxy(service, connectionResult, proxy);
|
||||
await SecurityTestUtils.CleanupCredential(connectionResult, credential);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the default "update agent alert" request handler with valid parameters
|
||||
/// </summary>
|
||||
//[Fact]
|
||||
[Fact]
|
||||
public async Task TestHandleUpdateAgentProxyRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var createContext = new Mock<RequestContext<CreateAgentProxyResult>>();
|
||||
var updateContext = new Mock<RequestContext<UpdateAgentProxyResult>>();
|
||||
var deleteContext = new Mock<RequestContext<ResultStatus>>();
|
||||
|
||||
var service = new AgentService();
|
||||
var proxy = new AgentProxyInfo()
|
||||
{
|
||||
Id = 10,
|
||||
AccountName = "Test Proxy 2",
|
||||
CredentialName = "User",
|
||||
Description = "",
|
||||
IsEnabled = true
|
||||
};
|
||||
|
||||
// cleanup
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
await service.HandleDeleteAgentProxyRequest(new DeleteAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, deleteContext.Object);
|
||||
|
||||
deleteContext.VerifyAll();
|
||||
|
||||
await service.HandleCreateAgentProxyRequest(new CreateAgentProxyParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, createContext.Object);
|
||||
|
||||
createContext.VerifyAll();
|
||||
var credential = await SecurityTestUtils.SetupCredential(connectionResult);
|
||||
var service = new AgentService();
|
||||
var proxy = AgentTestUtils.GetTestProxyInfo();
|
||||
await AgentTestUtils.DeleteAgentProxy(service, connectionResult, proxy);
|
||||
await AgentTestUtils.CreateAgentProxy(service, connectionResult, proxy);
|
||||
|
||||
// test
|
||||
string originalProxyName = proxy.AccountName;
|
||||
proxy.AccountName = proxy.AccountName + " Updated";
|
||||
await service.HandleUpdateAgentProxyRequest(new UpdateAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
OriginalProxyName = originalProxyName,
|
||||
Proxy = proxy
|
||||
}, updateContext.Object);
|
||||
await AgentTestUtils.UpdateAgentProxy(service, connectionResult, originalProxyName, proxy);
|
||||
|
||||
updateContext.VerifyAll();
|
||||
|
||||
await service.HandleDeleteAgentProxyRequest(new DeleteAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, deleteContext.Object);
|
||||
|
||||
deleteContext.VerifyAll();
|
||||
// cleanup
|
||||
await AgentTestUtils.DeleteAgentProxy(service, connectionResult, proxy);
|
||||
await SecurityTestUtils.CleanupCredential(connectionResult, credential);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TestHandleDeleteAgentProxyRequest
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleDeleteAgentProxyRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
// cleanup
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
var credential = await SecurityTestUtils.SetupCredential(connectionResult);
|
||||
var service = new AgentService();
|
||||
var proxy = AgentTestUtils.GetTestProxyInfo();
|
||||
|
||||
// test
|
||||
await AgentTestUtils.DeleteAgentProxy(service, connectionResult, proxy);
|
||||
await SecurityTestUtils.CleanupCredential(connectionResult, credential);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Moq;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
@@ -67,6 +68,17 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
Name = "Joe DBA",
|
||||
EmailAddress = "test@aol.com"
|
||||
};
|
||||
}
|
||||
|
||||
internal static AgentProxyInfo GetTestProxyInfo()
|
||||
{
|
||||
return new AgentProxyInfo()
|
||||
{
|
||||
AccountName = "Test Proxy",
|
||||
CredentialName = SecurityTestUtils.TestCredentialName,
|
||||
Description = "Test proxy description",
|
||||
IsEnabled = true
|
||||
};
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentJob(
|
||||
@@ -185,7 +197,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
Operator = operatorInfo
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentOperator(
|
||||
AgentService service,
|
||||
@@ -199,7 +211,51 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
Operator = operatorInfo
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task CreateAgentProxy(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentProxyInfo proxy)
|
||||
{
|
||||
var context = new Mock<RequestContext<AgentProxyResult>>();
|
||||
await service.HandleCreateAgentProxyRequest(new CreateAgentProxyParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
internal static async Task UpdateAgentProxy(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
string originalProxyName,
|
||||
AgentProxyInfo proxy)
|
||||
{
|
||||
var context = new Mock<RequestContext<AgentProxyResult>>();
|
||||
await service.HandleUpdateAgentProxyRequest(new UpdateAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
OriginalProxyName = originalProxyName,
|
||||
Proxy = proxy
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
internal static async Task DeleteAgentProxy(
|
||||
AgentService service,
|
||||
TestConnectionResult connectionResult,
|
||||
AgentProxyInfo proxy)
|
||||
{
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
await service.HandleDeleteAgentProxyRequest(new DeleteAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
internal static async Task<AgentAlertInfo[]> GetAgentAlerts(string connectionUri)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for the Credential management component
|
||||
/// </summary>
|
||||
public class CredentialTests
|
||||
{
|
||||
/// <summary>
|
||||
/// TestHandleCreateCredentialRequest
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleCreateCredentialRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
// setup
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
var service = new SecurityService();
|
||||
var credential = SecurityTestUtils.GetTestCredentialInfo();
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
|
||||
// test
|
||||
await SecurityTestUtils.CreateCredential(service, connectionResult, credential);
|
||||
|
||||
// cleanup
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TestHandleUpdateCredentialRequest
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleUpdateCredentialRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
// setup
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
var service = new SecurityService();
|
||||
var credential = SecurityTestUtils.GetTestCredentialInfo();
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
await SecurityTestUtils.CreateCredential(service, connectionResult, credential);
|
||||
|
||||
// test
|
||||
await SecurityTestUtils.UpdateCredential(service, connectionResult, credential);
|
||||
|
||||
// cleanup
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TestHandleDeleteCredentialRequest
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleDeleteCredentialRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
// setup
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
var service = new SecurityService();
|
||||
var credential = SecurityTestUtils.GetTestCredentialInfo();
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
await SecurityTestUtils.CreateCredential(service, connectionResult, credential);
|
||||
|
||||
// test
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for the security service component
|
||||
/// </summary>
|
||||
public class SecuritygServiceTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify the script object request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleCreateCredentialRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var createContext = new Mock<RequestContext<CreateCredentialResult>>();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
var service = new SecurityService();
|
||||
var credential = new CredentialInfo()
|
||||
{
|
||||
};
|
||||
|
||||
await service.HandleCreateCredentialRequest(new CreateCredentialParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Credential = credential
|
||||
}, createContext.Object);
|
||||
|
||||
createContext.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Moq;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security
|
||||
{
|
||||
public static class SecurityTestUtils
|
||||
{
|
||||
public static string TestCredentialName = "Current User";
|
||||
|
||||
internal static string GetCurrentUserIdentity()
|
||||
{
|
||||
return string.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName);
|
||||
}
|
||||
|
||||
internal static CredentialInfo GetTestCredentialInfo()
|
||||
{
|
||||
return new CredentialInfo()
|
||||
{
|
||||
Identity = GetCurrentUserIdentity(),
|
||||
Name = TestCredentialName
|
||||
};
|
||||
}
|
||||
|
||||
internal static async Task CreateCredential(
|
||||
SecurityService service,
|
||||
TestConnectionResult connectionResult,
|
||||
CredentialInfo credential)
|
||||
{
|
||||
var context = new Mock<RequestContext<CredentialResult>>();
|
||||
await service.HandleCreateCredentialRequest(new CreateCredentialParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Credential = credential
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
internal static async Task UpdateCredential(
|
||||
SecurityService service,
|
||||
TestConnectionResult connectionResult,
|
||||
CredentialInfo credential)
|
||||
{
|
||||
var context = new Mock<RequestContext<CredentialResult>>();
|
||||
await service.HandleUpdateCredentialRequest(new UpdateCredentialParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Credential = credential
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
internal static async Task DeleteCredential(
|
||||
SecurityService service,
|
||||
TestConnectionResult connectionResult,
|
||||
CredentialInfo credential)
|
||||
{
|
||||
var context = new Mock<RequestContext<ResultStatus>>();
|
||||
await service.HandleDeleteCredentialRequest(new DeleteCredentialParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Credential = credential
|
||||
}, context.Object);
|
||||
context.VerifyAll();
|
||||
}
|
||||
|
||||
public static async Task<CredentialInfo> SetupCredential(TestConnectionResult connectionResult)
|
||||
{
|
||||
var service = new SecurityService();
|
||||
var credential = SecurityTestUtils.GetTestCredentialInfo();
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
await SecurityTestUtils.CreateCredential(service, connectionResult, credential);
|
||||
return credential;
|
||||
}
|
||||
|
||||
public static async Task CleanupCredential(
|
||||
TestConnectionResult connectionResult,
|
||||
CredentialInfo credential)
|
||||
{
|
||||
var service = new SecurityService();
|
||||
await SecurityTestUtils.DeleteCredential(service, connectionResult, credential);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user