Skip to content

Commit

Permalink
Fixing regression in DACPAC creation when multiple managed identities…
Browse files Browse the repository at this point in the history
… are available

Updated AKS API version in cluster creation bicep
  • Loading branch information
mmckechney committed Sep 18, 2023
1 parent 9218f13 commit 1342ada
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 34 deletions.
4 changes: 2 additions & 2 deletions scripts/templates/Modules/aks.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ resource aksAcrPull 'Microsoft.Authorization/roleAssignments@2020-04-01-preview'
}
}

resource aks 'Microsoft.ContainerService/managedClusters@2022-05-02-preview' = {
resource aks 'Microsoft.ContainerService/managedClusters@2023-05-01' = {
name: aksClusterName
location: location
sku:{
name: 'Basic'
name: 'Base'
tier: 'Free'
}
identity: {
Expand Down
2 changes: 1 addition & 1 deletion src/SqlBuildManager.Console/Batch/BatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public BatchManager(CommandLineArgs cmdLine, string queryFile, string outputFile
log.LogInformation($"Extracting Platinum Dacpac from {cmdLine.DacPacArgs.PlatinumServerSource} : {cmdLine.DacPacArgs.PlatinumDbSource}");
string dacpacName = Path.Combine(cmdLine.RootLoggingPath, cmdLine.DacPacArgs.PlatinumDbSource + ".dacpac");

if (!DacPacHelper.ExtractDacPac(cmdLine.DacPacArgs.PlatinumDbSource, cmdLine.DacPacArgs.PlatinumServerSource, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, dacpacName, cmdLine.TimeoutRetryCount))
if (!DacPacHelper.ExtractDacPac(cmdLine.DacPacArgs.PlatinumDbSource, cmdLine.DacPacArgs.PlatinumServerSource, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, dacpacName, cmdLine.TimeoutRetryCount, cmdLine.IdentityArgs.ClientId))
{
log.LogError($"Error creating the Platinum dacpac from {cmdLine.DacPacArgs.PlatinumServerSource} : {cmdLine.DacPacArgs.PlatinumDbSource}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ private static Command OverrideFromSqlUtilityCommand
}
}

private static Command DecryptSettingsFile
{
get
{
var cmd = new Command("decrypt", "Decrypt a settings file")
{
settingsfileExistingRequiredOption,
settingsfileKeyRequiredOption
};
cmd.Handler = CommandHandler.Create<CommandLineArgs, string>(Worker.DecryptSettingsFile);
cmd.IsHidden = true;
return cmd;

}
}

/// <summary>
/// Utility commands for generating override file from SQL statement and interrogating Service Bus and EventHubs
/// </summary>
Expand All @@ -254,8 +270,9 @@ private static Command UtilityCommand
{
OverrideFromSqlUtilityCommand,
QueueUtilityCommand,
EventHubUtilityCommand
};
EventHubUtilityCommand,
DecryptSettingsFile
};
return cmd;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SqlBuildManager.Console/Threaded/ThreadedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private async Task<int> ExecuteFromQueue(CommandLineArgs cmdLine, string buildRe
log.LogInformation($"Extracting Platinum Dacpac from {cmdLine.DacPacArgs.PlatinumServerSource} : {cmdLine.DacPacArgs.PlatinumDbSource}");
string dacpacName = Path.Combine(ThreadedManager.rootLoggingPath, cmdLine.DacPacArgs.PlatinumDbSource + ".dacpac");

if (!DacPacHelper.ExtractDacPac(cmdLine.DacPacArgs.PlatinumDbSource, cmdLine.DacPacArgs.PlatinumServerSource, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, dacpacName, cmdLine.DefaultScriptTimeout))
if (!DacPacHelper.ExtractDacPac(cmdLine.DacPacArgs.PlatinumDbSource, cmdLine.DacPacArgs.PlatinumServerSource, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, dacpacName, cmdLine.DefaultScriptTimeout, cmdLine.IdentityArgs.ClientId))
{
var m = new LogMsg()
{
Expand Down
2 changes: 1 addition & 1 deletion src/SqlBuildManager.Console/Threaded/ThreadedRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ internal async Task<int> RunDatabaseBuild(ThreadedLogging threadedLog)
{
runData.ForceCustomDacpac = true;
//This will set the BuildData and BuildFileName and ProjectFileName properties on runData
var status = DacPacHelper.UpdateBuildRunDataForDacPacSync(ref runData, server, targetDatabase, authType, username, password, loggingDirectory, cmdArgs.BuildRevision, cmdArgs.DefaultScriptTimeout, cmdArgs.AllowObjectDelete);
var status = DacPacHelper.UpdateBuildRunDataForDacPacSync(ref runData, server, targetDatabase, authType, username, password, loggingDirectory, cmdArgs.BuildRevision, cmdArgs.DefaultScriptTimeout, cmdArgs.AllowObjectDelete, cmdArgs.IdentityArgs.ClientId);
switch (status)
{
case DacpacDeltasStatus.Success:
Expand Down
25 changes: 15 additions & 10 deletions src/SqlBuildManager.Console/Worker/Worker.Utility.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using Microsoft.Extensions.Logging;
using Spectre.Console;
using SqlBuildManager.Console.CommandLine;
using SqlBuildManager.Console.KeyVault;
using SqlBuildManager.Console.Kubernetes;
using SqlBuildManager.Console.Queue;
using SqlBuildManager.Enterprise.Policy;
using SqlBuildManager.Interfaces.Console;
using SqlSync.SqlBuild.MultiDb;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using sb = SqlSync.SqlBuild;

namespace SqlBuildManager.Console
Expand All @@ -41,7 +36,7 @@ internal static int CreateDacpac(CommandLineArgs cmdLine)
Directory.CreateDirectory(path);
}

if (!sb.DacPacHelper.ExtractDacPac(cmdLine.Database, cmdLine.Server, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, fullName, cmdLine.DefaultScriptTimeout))
if (!sb.DacPacHelper.ExtractDacPac(cmdLine.Database, cmdLine.Server, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, fullName, cmdLine.DefaultScriptTimeout, cmdLine.IdentityArgs.ClientId))
{
log.LogError($"Error creating the dacpac from {cmdLine.Server} : {cmdLine.Database}");
return (int)ExecutionReturn.BuildFileExtractionError;
Expand Down Expand Up @@ -270,7 +265,7 @@ internal static int CreatePackageFromDiff(CommandLineArgs cmdLine)
string id = Guid.NewGuid().ToString();
string goldTmp = Path.Combine(path, $"gold-{id}.dacpac");
string targetTmp = Path.Combine(path, $"target-{id}.dacpac");
if (!sb.DacPacHelper.ExtractDacPac(cmdLine.SynchronizeArgs.GoldDatabase, cmdLine.SynchronizeArgs.GoldServer, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, goldTmp, cmdLine.DefaultScriptTimeout))
if (!sb.DacPacHelper.ExtractDacPac(cmdLine.SynchronizeArgs.GoldDatabase, cmdLine.SynchronizeArgs.GoldServer, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, goldTmp, cmdLine.DefaultScriptTimeout, cmdLine.IdentityArgs.ClientId))
{
log.LogError($"Error creating the tempprary dacpac from {cmdLine.SynchronizeArgs.GoldServer} : {cmdLine.SynchronizeArgs.GoldDatabase}");
return (int)ExecutionReturn.BuildFileExtractionError;
Expand All @@ -280,7 +275,7 @@ internal static int CreatePackageFromDiff(CommandLineArgs cmdLine)
log.LogInformation($"Temporary DACPAC created from {cmdLine.SynchronizeArgs.GoldServer} : {cmdLine.SynchronizeArgs.GoldDatabase} saved to -- {goldTmp}");
}

if (!sb.DacPacHelper.ExtractDacPac(cmdLine.Database, cmdLine.Server, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, targetTmp, cmdLine.DefaultScriptTimeout))
if (!sb.DacPacHelper.ExtractDacPac(cmdLine.Database, cmdLine.Server, cmdLine.AuthenticationArgs.AuthenticationType, cmdLine.AuthenticationArgs.UserName, cmdLine.AuthenticationArgs.Password, targetTmp, cmdLine.DefaultScriptTimeout, cmdLine.IdentityArgs.ClientId))
{
log.LogError($"Error creating the tempprary dacpac from {cmdLine.Server} : {cmdLine.Database}");
return (int)ExecutionReturn.BuildFileExtractionError;
Expand Down Expand Up @@ -575,7 +570,7 @@ internal static sb.DacpacDeltasStatus GetSbmFromDacPac(CommandLineArgs cmd, Mult
cmd.AuthenticationArgs.Password,
cmd.BuildRevision,
cmd.DefaultScriptTimeout,
multiDb, out sbmName, batchScripts, cmd.AllowObjectDelete);
multiDb, out sbmName, batchScripts, cmd.AllowObjectDelete, cmd.IdentityArgs.ClientId);
}
else
{
Expand All @@ -589,7 +584,7 @@ internal static sb.DacpacDeltasStatus GetSbmFromDacPac(CommandLineArgs cmd, Mult
cmd.AuthenticationArgs.Password,
cmd.BuildRevision,
cmd.DefaultScriptTimeout,
multiDb, out sbmName, batchScripts, cmd.AllowObjectDelete);
multiDb, out sbmName, batchScripts, cmd.AllowObjectDelete, cmd.IdentityArgs.ClientId);
}
}

Expand Down Expand Up @@ -750,6 +745,16 @@ internal static string ConsoleTableBuilder(List<string[]> splits, List<int> sect
return sb.ToString().Trim();
}

internal static void DecryptSettingsFile(CommandLineArgs cmdLine, string settingsfilekey)
{
(var success, cmdLine) = Init(cmdLine);
var serialized = JsonSerializer.Serialize<CommandLineArgs>(cmdLine, new JsonSerializerOptions() { WriteIndented = true });
System.Console.WriteLine();
System.Console.WriteLine(serialized);
System.Console.WriteLine();

}

internal static int ShowCommands(bool markdown)
{
var filledCmdDocs = CommandLineBuilder.ListCommands_ForDocs();
Expand Down
10 changes: 5 additions & 5 deletions src/SqlBuildManager.Console/sbm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
<ProjectReference Include="..\SqlSync.SqlBuild\SqlSync.SqlBuild.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.2" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.9.2" />
<PackageReference Include="Azure.Identity" Version="1.10.1" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.3" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.9.3" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.16.1" />
<PackageReference Include="Azure.ResourceManager" Version="1.7.0" />
<PackageReference Include="Azure.ResourceManager.AppContainers" Version="1.1.0" />
<PackageReference Include="Azure.ResourceManager.Batch" Version="1.2.0" />
<PackageReference Include="Azure.ResourceManager.ContainerInstance" Version="1.1.0" />
<PackageReference Include="Azure.ResourceManager.EventHubs" Version="1.0.0" />
<PackageReference Include="Azure.ResourceManager.Network" Version="1.4.0" />
<PackageReference Include="Azure.ResourceManager.Network" Version="1.5.0" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.6.0" />
<!--<PackageReference Include="Microsoft.Azure.Management.Batch" Version="15.0.0" />-->
Expand Down Expand Up @@ -61,7 +61,7 @@
<PackageReference Include="Microsoft.Azure.Batch" Version="16.0.0" />
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="3.0.3" />
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="4.0.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.17.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" />
<PackageReference Include="YamlDotNet" Version="13.3.1" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/SqlBuildManager.Logging/SqlBuildManager.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.2" />
<PackageReference Include="Azure.Identity" Version="1.10.1" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.3" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Expressions" Version="3.4.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
Expand Down
1 change: 1 addition & 0 deletions src/SqlSync.Connection/ConnectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public static bool TestDatabaseConnection(ConnectionData connData)
DbConnection conn = null;
try
{
connData.ScriptTimeout = 60;
conn = GetConnection(connData);
conn.Open();
conn.Close();
Expand Down
18 changes: 9 additions & 9 deletions src/SqlSync.SqlBuild/DacPacHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DacPacHelper
{
private static ILogger log = SqlBuildManager.Logging.ApplicationLogging.CreateLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public static bool ExtractDacPac(string sourceDatabase, string sourceServer, AuthenticationType authType, string userName, string password, string dacPacFileName, int timeouts)
public static bool ExtractDacPac(string sourceDatabase, string sourceServer, AuthenticationType authType, string userName, string password, string dacPacFileName, int timeouts, string managedIdentityClientId)
{

try
Expand All @@ -35,7 +35,7 @@ public static bool ExtractDacPac(string sourceDatabase, string sourceServer, Aut
if (!string.IsNullOrWhiteSpace(password)) connData.Password = password;

//Pre-test the connection. the DacServices can hang for a long time if the connection is bad
if (!ConnectionHelper.TestDatabaseConnection(connData))
if (!ConnectionHelper.TestDatabaseConnection(sourceDatabase, sourceServer, userName, password,authType, timeouts, managedIdentityClientId))
{
log.LogError($"Unable to create Dacpac for {sourceServer}.{sourceDatabase}. Database connection test failed.");
return false;
Expand Down Expand Up @@ -279,10 +279,10 @@ internal static DacpacDeltasStatus CleanDacPacScript(string dacPacGeneratedScrip
}


public static DacpacDeltasStatus UpdateBuildRunDataForDacPacSync(ref SqlBuildRunData runData, string targetServerName, string targetDatabase, AuthenticationType authType, string userName, string password, string workingDirectory, string buildRevision, int defaultScriptTimeout, bool allowObjectDelete)
public static DacpacDeltasStatus UpdateBuildRunDataForDacPacSync(ref SqlBuildRunData runData, string targetServerName, string targetDatabase, AuthenticationType authType, string userName, string password, string workingDirectory, string buildRevision, int defaultScriptTimeout, bool allowObjectDelete, string managedIdentityClientId)
{
string tmpDacPacName = Path.Combine(workingDirectory, targetDatabase + ".dacpac");
if (!ExtractDacPac(targetDatabase, targetServerName, authType, userName, password, tmpDacPacName, runData.DefaultScriptTimeout))
if (!ExtractDacPac(targetDatabase, targetServerName, authType, userName, password, tmpDacPacName, runData.DefaultScriptTimeout, managedIdentityClientId))
{
return DacpacDeltasStatus.ExtractionFailure;
}
Expand Down Expand Up @@ -320,7 +320,7 @@ public static DacpacDeltasStatus UpdateBuildRunDataForDacPacSync(ref SqlBuildRun
return DacpacDeltasStatus.Success;
}

public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string platinumDacPac, string targetDacpac, string database, string server, AuthenticationType authType, string username, string password, string buildRevision, int defaultScriptTimeout, MultiDbData multiDb, out string sbmName, bool batchScripts, bool allowObjectDelete)
public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string platinumDacPac, string targetDacpac, string database, string server, AuthenticationType authType, string username, string password, string buildRevision, int defaultScriptTimeout, MultiDbData multiDb, out string sbmName, bool batchScripts, bool allowObjectDelete, string managedIdentityClientId)
{
string workingFolder = (!string.IsNullOrEmpty(rootLoggingPath) ? rootLoggingPath : Path.GetTempPath());

Expand All @@ -341,7 +341,7 @@ public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string
else if (!string.IsNullOrEmpty(database) && !string.IsNullOrEmpty(server))
{
string targetDacPac = Path.Combine(workingFolder, database + ".dacpac");
if (!DacPacHelper.ExtractDacPac(database, server, authType, username, password, targetDacPac, defaultScriptTimeout))
if (!DacPacHelper.ExtractDacPac(database, server, authType, username, password, targetDacPac, defaultScriptTimeout, managedIdentityClientId))
{
log.LogError($"Error extracting dacpac from {database} : {server}");
return DacpacDeltasStatus.ExtractionFailure;
Expand All @@ -359,7 +359,7 @@ public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string
database = serv.Overrides.ElementAt(i).OverrideDbTarget;

string targetDacPac = Path.Combine(workingFolder, database + ".dacpac");
if (!DacPacHelper.ExtractDacPac(database, server, authType, username, password, targetDacPac, defaultScriptTimeout))
if (!DacPacHelper.ExtractDacPac(database, server, authType, username, password, targetDacPac, defaultScriptTimeout, managedIdentityClientId))
{
log.LogError($"Error extracting dacpac from {server} : {database}");
return DacpacDeltasStatus.ExtractionFailure;
Expand Down Expand Up @@ -407,9 +407,9 @@ public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string
}
return stat;
}
public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string platinumDacPac, string database, AuthenticationType authType, string server, string username, string password, string buildRevision, int defaultScriptTimeout, MultiDbData multiDb, out string sbmName, bool batchScripts, bool allowObjectDelete)
public static DacpacDeltasStatus GetSbmFromDacPac(string rootLoggingPath, string platinumDacPac, string database, AuthenticationType authType, string server, string username, string password, string buildRevision, int defaultScriptTimeout, MultiDbData multiDb, out string sbmName, bool batchScripts, bool allowObjectDelete, string managedIdentityClientId)
{
return GetSbmFromDacPac(rootLoggingPath, platinumDacPac, string.Empty, database, server, authType, username, password, buildRevision, defaultScriptTimeout, multiDb, out sbmName, batchScripts, allowObjectDelete);
return GetSbmFromDacPac(rootLoggingPath, platinumDacPac, string.Empty, database, server, authType, username, password, buildRevision, defaultScriptTimeout, multiDb, out sbmName, batchScripts, allowObjectDelete, managedIdentityClientId);
}


Expand Down
2 changes: 1 addition & 1 deletion src/SqlSync.SqlBuild/SqlBuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ internal SqlSyncBuildData.BuildRow ProcessBuild(SqlBuildRunData runData, Backgro
var database = ((SqlSyncBuildData.ScriptRow)filteredScripts[0].Row).Database;
string targetDatabase = GetTargetDatabase(database);
log.LogWarning($"Custom dacpac required for {serverName} : {targetDatabase}. Generating file.");
var stat = DacPacHelper.UpdateBuildRunDataForDacPacSync(ref runData, serverName, targetDatabase, connData.AuthenticationType, connData.UserId, connData.Password, projectFilePath, runData.BuildRevision, runData.DefaultScriptTimeout, runData.AllowObjectDelete);
var stat = DacPacHelper.UpdateBuildRunDataForDacPacSync(ref runData, serverName, targetDatabase, connData.AuthenticationType, connData.UserId, connData.Password, projectFilePath, runData.BuildRevision, runData.DefaultScriptTimeout, runData.AllowObjectDelete, connData.ManagedIdentityClientId);

if (stat == DacpacDeltasStatus.Success)
{
Expand Down

0 comments on commit 1342ada

Please sign in to comment.