-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SNOW-926149 Fix issues while using snowflake-jdbc-fips #596
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>net.snowflake.snowflake-ingest-java-e2e-jar-test</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>core</artifactId> | ||
<name>core</name> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- Provided because we let submodules define which way they pull in the SDK (e.g. with/without snowflake-jdbc-fips, etc) --> | ||
<dependency> | ||
<groupId>net.snowflake</groupId> | ||
<artifactId>snowflake-ingest-sdk</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
169 changes: 169 additions & 0 deletions
169
e2e-jar-test/core/src/main/java/net/snowflake/IngestTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package net.snowflake; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import net.snowflake.ingest.streaming.OpenChannelRequest; | ||
import net.snowflake.ingest.streaming.SnowflakeStreamingIngestChannel; | ||
import net.snowflake.ingest.streaming.SnowflakeStreamingIngestClient; | ||
import net.snowflake.ingest.streaming.SnowflakeStreamingIngestClientFactory; | ||
|
||
public class IngestTestUtils { | ||
private static final String PROFILE_PATH = "profile.json"; | ||
|
||
private final Connection connection; | ||
|
||
private final String database; | ||
private final String schema; | ||
private final String table; | ||
|
||
private final String testId; | ||
|
||
private final SnowflakeStreamingIngestClient client; | ||
|
||
private final SnowflakeStreamingIngestChannel channel; | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private final Random random = new Random(); | ||
|
||
private final Base64.Decoder base64Decoder = Base64.getDecoder(); | ||
|
||
public IngestTestUtils(String testName) | ||
throws SQLException, | ||
IOException, | ||
ClassNotFoundException, | ||
NoSuchAlgorithmException, | ||
InvalidKeySpecException { | ||
testId = String.format("%s_%s", testName, UUID.randomUUID().toString().replace("-", "_")); | ||
connection = getConnection(); | ||
database = String.format("database_%s", testId); | ||
schema = String.format("schema_%s", testId); | ||
table = String.format("table_%s", testId); | ||
|
||
connection.createStatement().execute(String.format("create database %s", database)); | ||
connection.createStatement().execute(String.format("create schema %s", schema)); | ||
connection.createStatement().execute(String.format("create table %s (c1 int, c2 varchar, c3 binary)", table)); | ||
|
||
client = | ||
SnowflakeStreamingIngestClientFactory.builder("TestClient01") | ||
.setProperties(loadProperties()) | ||
.build(); | ||
|
||
channel = client.openChannel( | ||
OpenChannelRequest.builder(String.format("channel_%s", this.testId)) | ||
.setDBName(database) | ||
.setSchemaName(schema) | ||
.setTableName(table) | ||
.setOnErrorOption(OpenChannelRequest.OnErrorOption.CONTINUE) | ||
.build()); | ||
} | ||
|
||
private Properties loadProperties() throws IOException { | ||
Properties props = new Properties(); | ||
Iterator<Map.Entry<String, JsonNode>> propIt = | ||
objectMapper.readTree(new String(Files.readAllBytes(Paths.get(PROFILE_PATH)))).fields(); | ||
while (propIt.hasNext()) { | ||
Map.Entry<String, JsonNode> prop = propIt.next(); | ||
props.put(prop.getKey(), prop.getValue().asText()); | ||
} | ||
return props; | ||
} | ||
|
||
private Connection getConnection() | ||
throws IOException, ClassNotFoundException, SQLException, NoSuchAlgorithmException, InvalidKeySpecException { | ||
Class.forName("net.snowflake.client.jdbc.SnowflakeDriver"); | ||
|
||
Properties loadedProps = loadProperties(); | ||
|
||
byte[] decoded = base64Decoder.decode(loadedProps.getProperty("private_key")); | ||
KeyFactory kf = KeyFactory.getInstance("RSA"); | ||
|
||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded); | ||
PrivateKey privateKey = kf.generatePrivate(keySpec); | ||
|
||
Properties props = new Properties(); | ||
props.putAll(loadedProps); | ||
props.put("client_session_keep_alive", "true"); | ||
props.put("privateKey", privateKey); | ||
|
||
return DriverManager.getConnection(loadedProps.getProperty("connect_string"), props); | ||
} | ||
|
||
private Map<String, Object> createRow() { | ||
Map<String, Object> row = new HashMap<>(); | ||
|
||
byte[] bytes = new byte[1024]; | ||
random.nextBytes(bytes); | ||
|
||
row.put("c1", random.nextInt()); | ||
row.put("c2", String.valueOf(random.nextInt())); | ||
row.put("c3", bytes); | ||
|
||
return row; | ||
} | ||
|
||
/** | ||
* Given a channel and expected offset, this method waits up to 60 seconds until the last | ||
* committed offset is equal to the passed offset | ||
*/ | ||
private void waitForOffset(SnowflakeStreamingIngestChannel channel, String expectedOffset) | ||
throws InterruptedException { | ||
int counter = 0; | ||
String lastCommittedOffset = null; | ||
while (counter < 600) { | ||
String currentOffset = channel.getLatestCommittedOffsetToken(); | ||
if (expectedOffset.equals(currentOffset)) { | ||
return; | ||
} | ||
System.out.printf("Waiting for offset expected=%s actual=%s%n", expectedOffset, currentOffset); | ||
lastCommittedOffset = currentOffset; | ||
counter++; | ||
Thread.sleep(100); | ||
} | ||
throw new RuntimeException( | ||
String.format( | ||
"Timeout exceeded while waiting for offset %s. Last committed offset: %s", | ||
expectedOffset, lastCommittedOffset)); | ||
} | ||
|
||
public void test() throws InterruptedException { | ||
// Insert few rows one by one | ||
for (int offset = 2; offset < 1000; offset++) { | ||
offset++; | ||
channel.insertRow(createRow(), String.valueOf(offset)); | ||
} | ||
|
||
// Insert a batch of rows | ||
String offset = "final-offset"; | ||
channel.insertRows( | ||
Arrays.asList(createRow(), createRow(), createRow(), createRow(), createRow()), offset); | ||
|
||
waitForOffset(channel, offset); | ||
} | ||
|
||
public void close() throws Exception { | ||
connection.close(); | ||
channel.close().get(); | ||
client.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>net.snowflake.snowflake-ingest-java-e2e-jar-test</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>fips</artifactId> | ||
<packaging>jar</packaging> | ||
<name>fips</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>net.snowflake.snowflake-ingest-java-e2e-jar-test</groupId> | ||
<artifactId>core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>net.snowflake</groupId> | ||
<artifactId>snowflake-ingest-sdk</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>net.snowflake</groupId> | ||
<artifactId>snowflake-jdbc</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>org.bouncycastle</groupId> | ||
<artifactId>bcpkix-jdk18on</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>org.bouncycastle</groupId> | ||
<artifactId>bcprov-jdk18on</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.snowflake</groupId> | ||
<artifactId>snowflake-jdbc-fips</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
31 changes: 31 additions & 0 deletions
31
e2e-jar-test/fips/src/test/java/net/snowflake/FipsIngestE2ETest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.snowflake; | ||
|
||
import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.security.Security; | ||
|
||
public class FipsIngestE2ETest { | ||
|
||
private IngestTestUtils ingestTestUtils; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
// Add FIPS provider, the SDK does not do this by default | ||
Security.addProvider(new BouncyCastleFipsProvider("C:HYBRID;ENABLE{All};")); | ||
|
||
ingestTestUtils = new IngestTestUtils("fips_ingest"); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
ingestTestUtils.close(); | ||
} | ||
|
||
@Test | ||
public void name() throws InterruptedException { | ||
ingestTestUtils.test(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
jdbc-fips
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed