-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore(bq jdbc): fix p12 unittest resource access #12294
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,11 +29,10 @@ | |
| import com.google.auth.oauth2.UserCredentials; | ||
| import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.security.PrivateKey; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
|
|
@@ -503,11 +502,12 @@ public void testPrivateKeyFromPkcs8_wrong() { | |
| // -keypass notasecret -storetype pkcs12 -keystore ./fake.p12 | ||
| @Test | ||
| public void testPrivateKeyFromP12Bytes() { | ||
| URL resource = BigQueryJdbcOAuthUtilityTest.class.getResource("/fake.p12"); | ||
| InputStream stream = BigQueryJdbcOAuthUtilityTest.class.getResourceAsStream("/fake.p12"); | ||
| try { | ||
| PrivateKey pk = | ||
| BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes( | ||
| Files.readAllBytes(Paths.get(resource.toURI())), "notasecret"); | ||
| int size = 16 * 1024; | ||
| byte[] data = new byte[size]; | ||
| stream.read(data, 0, data.length); | ||
| PrivateKey pk = BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes(data, "notasecret"); | ||
| assertNotNull(pk); | ||
| } catch (Exception e) { | ||
| assertTrue(false); | ||
|
|
@@ -516,11 +516,12 @@ public void testPrivateKeyFromP12Bytes() { | |
|
|
||
| @Test | ||
| public void testPrivateKeyFromP12Bytes_wrong_password() { | ||
| URL resource = BigQueryJdbcOAuthUtilityTest.class.getResource("/fake.p12"); | ||
| InputStream stream = BigQueryJdbcOAuthUtilityTest.class.getResourceAsStream("/fake.p12"); | ||
| try { | ||
| PrivateKey pk = | ||
| BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes( | ||
| Files.readAllBytes(Paths.get(resource.toURI())), "fake"); | ||
| int size = 16 * 1024; | ||
| byte[] data = new byte[size]; | ||
| stream.read(data, 0, data.length); | ||
| PrivateKey pk = BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes(data, "fake"); | ||
| assertNull(pk); | ||
| } catch (Exception e) { | ||
| assertTrue(false); | ||
|
Comment on lines
+519
to
527
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method has the same issues as Please apply a similar fix here using a try-with-resources statement and reading the full stream content. This ensures the test is robust and doesn't leak resources. try (InputStream stream =
BigQueryJdbcOAuthUtilityTest.class.getResourceAsStream("/fake.p12")) {
assertNotNull("Resource stream for /fake.p12 should not be null", stream);
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int nRead;
while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, nRead);
}
PrivateKey pk = BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes(baos.toByteArray(), "fake");
assertNull(pk);
} catch (Exception e) {
fail("Test should not have thrown an exception for wrong password: " + e.getMessage());
} |
||
|
|
||
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.
This implementation has a couple of issues: it leaks the
InputStreamand reads from it incorrectly.InputStreamis not closed. Using a try-with-resources statement is the standard way to prevent this.read()doesn't guarantee to fill the buffer, and if the file size doesn't match the buffer size, you'll either process incomplete data or data with extra null bytes.The suggestion below fixes these issues by using a try-with-resources block and reading the entire stream into a
ByteArrayOutputStream. It also improves the error handling in thecatchblock.You will need to add
import java.io.ByteArrayOutputStream;.