sqlinsight-pro/tests/SQLInsight.IntegrationTests/AgentSession.cs
Søren Eggert Lundsteen Olsen b2fa78411f Collectors: monitor only chosen databases; database discovery and permission check
CollectorContext carries the instance's DatabaseSelection; the database,
index, Query Store and community-rule collectors skip databases that are not
chosen (system databases always run). DatabaseDiscovery lists every database
with state, size, recovery model, compatibility level and access, and
recognises Dynamics AX with the same signature as the AX rules, through
three-part names only (2000 and 2005+ variants). PermissionCheck reports which
rights from grant-least-privilege.sql the login has (HAS_PERMS_BY_NAME,
version-aware; not possible on 2000).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 13:22:41 +02:00

47 lines
1.7 KiB
C#

using SQLInsight.Collectors;
using SQLInsight.Connectivity;
using SQLInsight.Core.Instances;
namespace SQLInsight.IntegrationTests;
/// <summary>
/// A monitoring connection as the least-privilege agent login, opened exactly like the agent does.
/// </summary>
internal sealed class AgentSession : IAsyncDisposable
{
private readonly OpenedConnection _opened;
private AgentSession(OpenedConnection opened, InstanceInfo instance)
{
_opened = opened;
Instance = instance;
Capabilities = InstanceCapabilities.From(instance);
}
public InstanceInfo Instance { get; }
public InstanceCapabilities Capabilities { get; }
public static async Task<AgentSession> OpenAsync(SqlServerFixture sql)
{
var opened = await new SqlConnectionFactory().OpenAsync(new ConnectionTarget
{
Server = sql.Server,
Authentication = AuthenticationMode.SqlLogin,
UserName = SqlServerFixture.AgentLogin,
Password = SqlServerFixture.AgentPassword,
Provider = ConnectionProvider.SqlClient,
TrustServerCertificate = true,
});
var instance = await InstanceProbe.DetectAsync(opened.Connection);
return new AgentSession(opened, instance);
}
public async Task<T> CollectAsync<T>(ICollector collector, T? previous = null, Core.Configuration.DatabaseSelection? selection = null)
where T : class
{
var context = new CollectorContext(_opened.Connection, Instance, Capabilities, DateTimeOffset.UtcNow, previous, CancellationToken.None, selection);
return (T)await collector.CollectAsync(context);
}
public ValueTask DisposeAsync() => _opened.DisposeAsync();
}