Skip to content

Commit

Permalink
Add Access Token support to SqlServerDatabase (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
koenekelschot authored Apr 15, 2020
1 parent 5458d5f commit c540293
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions product/roundhouse.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ private static void parse_arguments_and_set_up_configuration(ConfigurationProper
string.Format(
"ConnectionStringAdministration - This is used for connecting to master when you may have a different uid and password than normal."),
option => configuration.ConnectionStringAdmin = option)
.Add("accesstoken=",
"AccessToken - This connection property is used to connect to a SQL Database using an access token (for example Azure AD token).",
option => configuration.AccessToken = option)
.Add("ct=|commandtimeout=",
string.Format(
"CommandTimeout - This is the timeout when commands are run. This is not for admin commands or restore. Defaults to \"{0}\".",
Expand Down Expand Up @@ -398,6 +401,7 @@ private static void parse_arguments_and_set_up_configuration(ConfigurationProper
"/[sql]f[ilesdirectory] VALUE " +
"/s[ervername] VALUE " +
"/c[onnection]s[tring]a[dministration] VALUE " +
"/accesstoken VALUE " +
"/c[ommand]t[imeout] VALUE /c[ommand]t[imeout]a[dmin] VALUE " +
"/r[epositorypath] VALUE /v[ersion] VALUE /v[ersion]f[ile] VALUE /v[ersion]x[path] VALUE " +
"/a[lter]d[atabasefoldername] /r[un]a[fter]c[reate]d[atabasefoldername] VALUE VALUE " +
Expand Down
2 changes: 2 additions & 0 deletions product/roundhouse.core/consoles/DefaultConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public DefaultConfiguration()
public string DatabaseName { get; set; }
public string ConnectionString { get; set; }
public string ConnectionStringAdmin { get; set; }
public string AccessToken { get; set; }
public int CommandTimeout { get; set; }
public int CommandTimeoutAdmin { get; set; }
public string SqlFilesDirectory { get; set; }
Expand Down Expand Up @@ -89,6 +90,7 @@ public string EnvironmentName {
public IDictionary<string, string> to_token_dictionary()
{
var tokens = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
tokens["AccessToken"] = AccessToken.to_string();
tokens["AfterMigrationFolderName"] = AfterMigrationFolderName.to_string();
tokens["AlterDatabaseFolderName"] = AlterDatabaseFolderName.to_string();
tokens["Baseline"] = Baseline.to_string();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface ConfigurationPropertyHolder
string DatabaseName { get; set; }
string ConnectionString { get; set; }
string ConnectionStringAdmin { get; set; }
string AccessToken { get; set; }
int CommandTimeout { get; set; }
int CommandTimeoutAdmin { get; set; }
string SqlFilesDirectory { get; set; }
Expand Down
16 changes: 15 additions & 1 deletion product/roundhouse.databases.sqlserver/ReliableSqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ internal sealed class ReliableSqlConnection : IDbConnection, ICloneable
private readonly RetryPolicy connection_string_failover_policy = get_default_retry_policy();

private string connection_string;

private string access_token;


private static RetryPolicy get_default_retry_policy()
{
Expand Down Expand Up @@ -99,6 +100,19 @@ public string ConnectionString
}
}

/// <summary>
/// Gets or sets the access token for a connection
/// </summary>
public string AccessToken
{
get => access_token;
set
{
access_token = value;
underlying_connection.AccessToken = value;
}
}

/// <summary>
/// Gets the policy that determines whether to retry a connection request, based on how many
/// times the request has been made and the reason for the last failure.
Expand Down
7 changes: 7 additions & 0 deletions product/roundhouse.databases.sqlserver/SqlServerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public SqlServerDatabase()

private string connect_options = "Integrated Security=SSPI;";
private readonly TransientErrorDetectionStrategy error_detection_strategy = new TransientErrorDetectionStrategy();
private string access_token;

public override string sql_statement_separator_regex_pattern
{
Expand All @@ -50,6 +51,8 @@ public override string sql_statement_separator_regex_pattern

public override void initialize_connections(ConfigurationPropertyHolder configuration_property_holder)
{
access_token = configuration_property_holder.AccessToken;

if (!string.IsNullOrEmpty(connection_string))
{
var connection_string_builder = new SqlConnectionStringBuilder(connection_string);
Expand Down Expand Up @@ -102,6 +105,10 @@ protected override AdoNetConnection GetAdoNetConnection(string conn_string)
var command_retry_policy = Policy.Handle<Exception>().Retry(0);

var connection = new ReliableSqlConnection(conn_string, connection_retry_policy, command_retry_policy);
if (!string.IsNullOrEmpty(access_token))
{
connection.AccessToken = access_token;
}

connection_specific_setup(connection);
return new AdoNetConnection(connection);
Expand Down
3 changes: 3 additions & 0 deletions product/roundhouse.tasks/Roundhouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ bool ITask.Execute()

public string ConnectionStringAdmin { get; set; }

public string AccessToken { get; set; }

public int CommandTimeout { get; set; }

public int CommandTimeoutAdmin { get; set; }
Expand Down Expand Up @@ -214,6 +216,7 @@ public IDictionary<string, string> to_token_dictionary()
{
var tokens = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ nameof(AccessToken), AccessToken.to_string() },
{ nameof(AfterMigrationFolderName), AfterMigrationFolderName.to_string() },
{ nameof(AlterDatabaseFolderName), AlterDatabaseFolderName.to_string() },
{ nameof(Baseline), Baseline.to_string() },
Expand Down

0 comments on commit c540293

Please sign in to comment.