Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Comment on lines +505 to 513
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This implementation has a couple of issues: it leaks the InputStream and reads from it incorrectly.

  1. Resource Leak: The InputStream is not closed. Using a try-with-resources statement is the standard way to prevent this.
  2. Incorrect Stream Reading: Reading into a fixed-size buffer is unreliable. 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 the catch block.

You will need to add import java.io.ByteArrayOutputStream;.

    try (InputStream stream =
        BigQueryJdbcOAuthUtilityTest.class.getResourceAsStream("/fake.p12")) {
      assertNotNull("Resource stream for /fake.p12 should not be null", stream);
      // In Java 9+ you can use stream.readAllBytes(). For Java 8, this is a common pattern.
      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(), "notasecret");
      assertNotNull(pk);
    } catch (Exception e) {
      fail("Failed to get private key from p12 bytes: " + e.getMessage());
    }

Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This method has the same issues as testPrivateKeyFromP12Bytes regarding resource handling and stream reading. The InputStream is not closed, and reading into a fixed-size buffer is unreliable.

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());
    }

Expand Down
Loading