Skip to content

Commit

Permalink
fix(csharp/src/Drivers/Interop/Snowflake): add test to demonstrate DE…
Browse files Browse the repository at this point in the history
…FAULT_ROLE behavior (#2151)

- Introduces a test to demonstrate the behavior of using DEFAULT_ROLE
for ADBC, and then changes the role using the `adbc.snowflake.sql.role`
parameter and validates the user is executing in the new role context.

- Closes #2080

Co-authored-by: David Coe <[email protected]>
  • Loading branch information
davidhcoe and David Coe authored Sep 10, 2024
1 parent 27f57b3 commit 2765af0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
38 changes: 38 additions & 0 deletions csharp/test/Drivers/Interop/Snowflake/DriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ public DriverTests()
_connection = _database.Connect(options);
}

/// <summary>
/// Validates if the DEFAULT_ROLE works correctly for ADBC.
/// </summary>
[SkippableFact, Order(1)]
public void ValidateUserRole()
{
Skip.If(_testConfiguration.RoleInfo == null);

// first test with the DEFAULT_ROLE value
Assert.True(CurrentRoleIsExpectedRole(_connection, _testConfiguration.RoleInfo.DefaultRole)); ;

// now connect with the new role and ensure we get the new role successfully
Dictionary<string, string> parameters = new Dictionary<string, string>();
Dictionary<string, string> options = new Dictionary<string, string>();

using AdbcDriver localSnowflakeDriver = SnowflakeTestingUtils.GetSnowflakeAdbcDriver(_testConfiguration, out parameters);
parameters.Add(SnowflakeParameters.ROLE, _testConfiguration.RoleInfo.NewRole);

using AdbcDatabase localDatabase = localSnowflakeDriver.Open(parameters);
using AdbcConnection localConnection = localDatabase.Connect(options);
Assert.True(CurrentRoleIsExpectedRole(localConnection, _testConfiguration.RoleInfo.NewRole));
}

private bool CurrentRoleIsExpectedRole(AdbcConnection cn, string expectedRole)
{
using AdbcStatement statement = cn.CreateStatement();
statement.SqlQuery = "SELECT CURRENT_ROLE() as CURRENT_ROLE;";

QueryResult queryResult = statement.ExecuteQuery();
using RecordBatch? recordBatch = queryResult.Stream?.ReadNextRecordBatchAsync().Result;
Assert.True(recordBatch != null);

StringArray stringArray = (StringArray)recordBatch.Column("CURRENT_ROLE");
Assert.True(stringArray.Length > 0);

return expectedRole == stringArray.GetString(0);
}

/// <summary>
/// Validates if the driver can connect to a live server and
/// parse the results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"password": ""
}
},
"roleInfo": {
"defaultRole": "",
"newRole": ""
},
"query": "",
"expectedResults": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ internal class SnowflakeTestConfiguration : TestConfiguration
[JsonPropertyName("authentication")]
public SnowflakeAuthentication Authentication { get; set; } = new SnowflakeAuthentication();

/// <summary>
/// The snowflake Authentication
/// </summary>
[JsonPropertyName("roleInfo")]
public RoleInfo? RoleInfo { get; set; }
}

public class SnowflakeAuthentication
Expand Down Expand Up @@ -134,4 +139,13 @@ public class DefaultAuthentication
[JsonPropertyName("password")]
public string Password { get; set; } = string.Empty;
}

public class RoleInfo
{
[JsonPropertyName("defaultRole")]
public string DefaultRole { get; set; } = string.Empty;

[JsonPropertyName("newRole")]
public string NewRole { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal class SnowflakeParameters
public const string ACCOUNT = "adbc.snowflake.sql.account";
public const string USERNAME = "username";
public const string PASSWORD = "password";
public const string ROLE = "adbc.snowflake.sql.role";
public const string WAREHOUSE = "adbc.snowflake.sql.warehouse";
public const string AUTH_TYPE = "adbc.snowflake.sql.auth_type";
public const string AUTH_TOKEN = "adbc.snowflake.sql.client_option.auth_token";
Expand Down

0 comments on commit 2765af0

Please sign in to comment.