Simplify DB to six tables and load credentials via Always Encrypted SonicCredential.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -33,40 +33,36 @@ public sealed class SonicSetupRepository
|
||||
|
||||
const string sql = """
|
||||
SELECT
|
||||
connection.SonicConnectionId,
|
||||
connection.ConnectionName,
|
||||
environment.EnvironmentCode,
|
||||
connection.DomainName,
|
||||
connection.ManagementHost,
|
||||
connection.ManagementPort,
|
||||
connection.ConnectionProtocol,
|
||||
MIN(container.ContainerName)
|
||||
AS ValidationContainerName
|
||||
FROM dbo.SonicConnection AS connection
|
||||
INNER JOIN dbo.SonicContainer AS container
|
||||
ON container.SonicConnectionId =
|
||||
connection.SonicConnectionId
|
||||
INNER JOIN dbo.EsbService AS service
|
||||
ON service.EsbServiceId =
|
||||
container.EsbServiceId
|
||||
INNER JOIN dbo.Environment AS environment
|
||||
ON environment.EnvironmentId =
|
||||
service.EnvironmentId
|
||||
WHERE connection.IsActive = 1
|
||||
AND container.IsActive = 1
|
||||
AND service.IsActive = 1
|
||||
AND environment.IsActive = 1
|
||||
AND environment.EnvironmentCode =
|
||||
connection.[SonicConnectionId],
|
||||
connection.[ConnectionName],
|
||||
environment.[EnvironmentCode],
|
||||
connection.[DomainName],
|
||||
connection.[ManagementHost],
|
||||
connection.[ManagementPort],
|
||||
connection.[ConnectionProtocol],
|
||||
MIN(container.[ContainerName])
|
||||
AS [ValidationContainerName]
|
||||
FROM [dbo].[SonicConnection] AS connection
|
||||
INNER JOIN [dbo].[Environment] AS environment
|
||||
ON environment.[EnvironmentId] =
|
||||
connection.[EnvironmentId]
|
||||
INNER JOIN [dbo].[SonicContainer] AS container
|
||||
ON container.[SonicConnectionId] =
|
||||
connection.[SonicConnectionId]
|
||||
WHERE connection.[IsActive] = 1
|
||||
AND container.[IsActive] = 1
|
||||
AND environment.[IsActive] = 1
|
||||
AND environment.[EnvironmentCode] =
|
||||
@EnvironmentCode
|
||||
GROUP BY
|
||||
connection.SonicConnectionId,
|
||||
connection.ConnectionName,
|
||||
environment.EnvironmentCode,
|
||||
connection.DomainName,
|
||||
connection.ManagementHost,
|
||||
connection.ManagementPort,
|
||||
connection.ConnectionProtocol
|
||||
ORDER BY connection.ConnectionName;
|
||||
connection.[SonicConnectionId],
|
||||
connection.[ConnectionName],
|
||||
environment.[EnvironmentCode],
|
||||
connection.[DomainName],
|
||||
connection.[ManagementHost],
|
||||
connection.[ManagementPort],
|
||||
connection.[ConnectionProtocol]
|
||||
ORDER BY connection.[ConnectionName];
|
||||
""";
|
||||
|
||||
List<SonicSystemOption> systems = [];
|
||||
@@ -119,11 +115,6 @@ public sealed class SonicSetupRepository
|
||||
|
||||
while (await reader.ReadAsync(cancellationToken))
|
||||
{
|
||||
if (reader.IsDBNull(managementPortOrdinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
systems.Add(
|
||||
new SonicSystemOption
|
||||
{
|
||||
@@ -248,13 +239,14 @@ public sealed class SonicSetupRepository
|
||||
if (isDefault)
|
||||
{
|
||||
const string clearDefaultSql = """
|
||||
UPDATE dbo.SonicCredential
|
||||
UPDATE [dbo].[SonicCredential]
|
||||
SET
|
||||
IsDefault = 0,
|
||||
UpdatedAtUtc = SYSUTCDATETIME()
|
||||
WHERE SonicConnectionId =
|
||||
[IsDefault] = 0,
|
||||
[ModifiedDateTime] = SYSUTCDATETIME(),
|
||||
[ModifiedBy] = SUSER_SNAME()
|
||||
WHERE [SonicConnectionId] =
|
||||
@SonicConnectionId
|
||||
AND IsDefault = 1;
|
||||
AND [IsDefault] = 1;
|
||||
""";
|
||||
|
||||
await using SqlCommand clearDefaultCommand =
|
||||
@@ -276,18 +268,20 @@ public sealed class SonicSetupRepository
|
||||
}
|
||||
|
||||
const string insertSql = """
|
||||
INSERT INTO dbo.SonicCredential
|
||||
INSERT INTO [dbo].[SonicCredential]
|
||||
(
|
||||
SonicConnectionId,
|
||||
CredentialName,
|
||||
CredentialUserName,
|
||||
CredentialSecret,
|
||||
IsDefault,
|
||||
IsActive,
|
||||
CreatedAtUtc,
|
||||
UpdatedAtUtc
|
||||
[SonicConnectionId],
|
||||
[CredentialName],
|
||||
[CredentialUserName],
|
||||
[CredentialSecret],
|
||||
[IsDefault],
|
||||
[IsActive],
|
||||
[CreationDateTime],
|
||||
[CreatedBy],
|
||||
[ModifiedDateTime],
|
||||
[ModifiedBy]
|
||||
)
|
||||
OUTPUT INSERTED.SonicCredentialId
|
||||
OUTPUT INSERTED.[SonicCredentialId]
|
||||
VALUES
|
||||
(
|
||||
@SonicConnectionId,
|
||||
@@ -297,6 +291,8 @@ public sealed class SonicSetupRepository
|
||||
@IsDefault,
|
||||
1,
|
||||
SYSUTCDATETIME(),
|
||||
SUSER_SNAME(),
|
||||
NULL,
|
||||
NULL
|
||||
);
|
||||
""";
|
||||
@@ -397,19 +393,19 @@ public sealed class SonicSetupRepository
|
||||
|
||||
const string sql = """
|
||||
SELECT
|
||||
SonicCredentialId,
|
||||
SonicConnectionId,
|
||||
CredentialName,
|
||||
CredentialUserName,
|
||||
CredentialSecret,
|
||||
IsDefault,
|
||||
IsActive
|
||||
FROM dbo.SonicCredential
|
||||
WHERE SonicConnectionId = @SonicConnectionId
|
||||
AND IsActive = 1
|
||||
[SonicCredentialId],
|
||||
[SonicConnectionId],
|
||||
[CredentialName],
|
||||
[CredentialUserName],
|
||||
[CredentialSecret],
|
||||
[IsDefault],
|
||||
[IsActive]
|
||||
FROM [dbo].[SonicCredential]
|
||||
WHERE [SonicConnectionId] = @SonicConnectionId
|
||||
AND [IsActive] = 1
|
||||
ORDER BY
|
||||
IsDefault DESC,
|
||||
CredentialName;
|
||||
[IsDefault] DESC,
|
||||
[CredentialName];
|
||||
""";
|
||||
|
||||
List<SonicCredentialProfile> profiles = [];
|
||||
@@ -502,4 +498,101 @@ public sealed class SonicSetupRepository
|
||||
|
||||
return profiles;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SonicCredentialProfile?> GetCredentialByIdAsync(
|
||||
int sonicCredentialId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (sonicCredentialId <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(sonicCredentialId));
|
||||
}
|
||||
|
||||
const string sql = """
|
||||
SELECT
|
||||
[SonicCredentialId],
|
||||
[SonicConnectionId],
|
||||
[CredentialName],
|
||||
[CredentialUserName],
|
||||
[CredentialSecret],
|
||||
[IsDefault],
|
||||
[IsActive]
|
||||
FROM [dbo].[SonicCredential]
|
||||
WHERE [SonicCredentialId] = @SonicCredentialId
|
||||
AND [IsActive] = 1;
|
||||
""";
|
||||
|
||||
await using SqlConnection connection =
|
||||
new(_connectionString);
|
||||
|
||||
await connection.OpenAsync(cancellationToken);
|
||||
|
||||
await using SqlCommand command =
|
||||
new(sql, connection);
|
||||
|
||||
command.Parameters.Add(
|
||||
new SqlParameter(
|
||||
"@SonicCredentialId",
|
||||
SqlDbType.Int)
|
||||
{
|
||||
Value = sonicCredentialId
|
||||
});
|
||||
|
||||
await using SqlDataReader reader =
|
||||
await command.ExecuteReaderAsync(cancellationToken);
|
||||
|
||||
if (!await reader.ReadAsync(cancellationToken))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int userNameOrdinal =
|
||||
reader.GetOrdinal("CredentialUserName");
|
||||
|
||||
int secretOrdinal =
|
||||
reader.GetOrdinal("CredentialSecret");
|
||||
|
||||
string? userName =
|
||||
reader.IsDBNull(userNameOrdinal)
|
||||
? null
|
||||
: reader.GetString(userNameOrdinal);
|
||||
|
||||
string? secret =
|
||||
reader.IsDBNull(secretOrdinal)
|
||||
? null
|
||||
: reader.GetString(secretOrdinal);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(userName)
|
||||
|| string.IsNullOrEmpty(secret))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new SonicCredentialProfile
|
||||
{
|
||||
SonicCredentialId =
|
||||
reader.GetInt32(
|
||||
reader.GetOrdinal("SonicCredentialId")),
|
||||
|
||||
SonicConnectionId =
|
||||
reader.GetInt32(
|
||||
reader.GetOrdinal("SonicConnectionId")),
|
||||
|
||||
CredentialName =
|
||||
reader.GetString(
|
||||
reader.GetOrdinal("CredentialName")),
|
||||
|
||||
UserName = userName,
|
||||
Secret = secret,
|
||||
|
||||
IsDefault =
|
||||
reader.GetBoolean(
|
||||
reader.GetOrdinal("IsDefault")),
|
||||
|
||||
IsActive =
|
||||
reader.GetBoolean(
|
||||
reader.GetOrdinal("IsActive"))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user