Skip to content

Commit

Permalink
SNOW-532834 Using default role when snowflake.role.name is not specif…
Browse files Browse the repository at this point in the history
…ied (#507)
  • Loading branch information
sfc-gh-alhuang committed Jul 14, 2023
1 parent 2af18ec commit 5547ea4
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class SnowflakeStreamingIngestExample {
// Please follow the example in profile_streaming.json.example to see the required properties, or
// if you have already set up profile.json with Snowpipe before, all you need is to add the "role"
// property.
// property. If the "role" is not specified, the default user role will be applied.
private static String PROFILE_PATH = "profile.json";
private static final ObjectMapper mapper = new ObjectMapper();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/snowflake/ingest/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static Properties createProperties(Properties inputProp) {
}

if (!properties.containsKey(Constants.ROLE)) {
throw new SFException(ErrorCode.MISSING_CONFIG, "role");
logger.logInfo("Snowflake role is not provided, the default user role will be applied.");
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/net/snowflake/ingest/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public static String getSchema() throws Exception {
return schema;
}

public static Properties getProperties(Constants.BdecVersion bdecVersion) throws Exception {
public static Properties getProperties(Constants.BdecVersion bdecVersion, boolean useDefaultRole)
throws Exception {
if (profile == null) {
init();
}
Expand All @@ -236,7 +237,9 @@ public static Properties getProperties(Constants.BdecVersion bdecVersion) throws
props.put(SCHEMA, schema);
props.put(WAREHOUSE, warehouse);
props.put(PRIVATE_KEY, privateKeyPem);
props.put(ROLE, role);
if (!useDefaultRole) {
props.put(ROLE, role);
}
props.put(ACCOUNT_URL, getAccountURL());
props.put(BLOB_FORMAT_VERSION, bdecVersion.toByte());
return props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class OAuthBasicTest {
/** Create client with invalid authorization type, this should fail. */
@Test
public void invalidAuthType() throws Exception {
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE);
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE, false);
props.put(Constants.AUTHORIZATION_TYPE, "INVALID_AUTH_TYPE");
SFException e =
Assert.assertThrows(
Expand All @@ -38,7 +38,7 @@ public void invalidAuthType() throws Exception {
/** Create client with missing config, this should fail. */
@Test
public void missingOAuthParam() throws Exception {
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE);
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE, false);
props.put(Constants.AUTHORIZATION_TYPE, Constants.OAUTH);

// Missing oauth_client_id
Expand Down Expand Up @@ -83,7 +83,7 @@ public void missingOAuthParam() throws Exception {
/** Create client with mock credential, should fail when refreshing token */
@Test(expected = SecurityException.class)
public void testCreateOAuthClient() throws Exception {
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE);
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE, false);
props.remove(Constants.PRIVATE_KEY);
props.put(Constants.AUTHORIZATION_TYPE, Constants.OAUTH);
props.put(Constants.OAUTH_CLIENT_ID, "MOCK_CLIENT_ID");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void setUp() throws Exception {
String.format(
"create or replace table %s.%s.%s (col int)",
databaseName, SCHEMA_NAME, TABLE_NAME));
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE);
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE, false);
if (props.getProperty(ROLE).equals("DEFAULT_ROLE")) {
props.setProperty(ROLE, "ACCOUNTADMIN");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void beforeAll() throws Exception {
.createStatement()
.execute(String.format("use warehouse %s", TestUtils.getWarehouse()));

prop = TestUtils.getProperties(Constants.BdecVersion.THREE);
prop = TestUtils.getProperties(Constants.BdecVersion.THREE, false);
if (prop.getProperty(ROLE).equals("DEFAULT_ROLE")) {
prop.setProperty(ROLE, "ACCOUNTADMIN");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static net.snowflake.ingest.utils.Constants.BLOB_NO_HEADER;
import static net.snowflake.ingest.utils.Constants.COMPRESS_BLOB_TWICE;
import static net.snowflake.ingest.utils.Constants.REGISTER_BLOB_ENDPOINT;
import static net.snowflake.ingest.utils.Constants.ROLE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

Expand Down Expand Up @@ -89,10 +88,9 @@ public void beforeAll() throws Exception {
.createStatement()
.execute(String.format("use warehouse %s", TestUtils.getWarehouse()));

prop = TestUtils.getProperties(Constants.BdecVersion.THREE);
if (prop.getProperty(ROLE).equals("DEFAULT_ROLE")) {
prop.setProperty(ROLE, "ACCOUNTADMIN");
}
// Test without role param
prop = TestUtils.getProperties(Constants.BdecVersion.THREE, true);

client =
(SnowflakeStreamingIngestClientInternal<?>)
SnowflakeStreamingIngestClientFactory.builder("client1").setProperties(prop).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void before() throws Exception {

conn.createStatement().execute(String.format("use warehouse %s;", TestUtils.getWarehouse()));

Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE);
Properties props = TestUtils.getProperties(Constants.BdecVersion.THREE, false);
if (props.getProperty(ROLE).equals("DEFAULT_ROLE")) {
props.setProperty(ROLE, "ACCOUNTADMIN");
}
Expand Down

0 comments on commit 5547ea4

Please sign in to comment.