Skip to content
Open
Show file tree
Hide file tree
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 @@ -131,8 +131,8 @@ static Map<String, String> parseOAuthProperties(DataSource ds, String callerClas
switch (authType) {
case GOOGLE_SERVICE_ACCOUNT:
// For using a Google Service Account (OAuth Type 0)
// need: project id, OAuthServiceAcctEmail and OAuthPvtKey or OAuthPvtKeyPath that can be
// .p12 or json.
// need: project id, and OAuthPvtKey or OAuthPvtKeyPath (JSON, PKCS#8, or .p12).
// OAuthServiceAcctEmail is required for non-JSON keys (PKCS#8 or .p12).
// TODO: validation if .p12 or json file can be in getPropertyInfo can be handy for user
String serviceAccountEmail = ds.getOAuthServiceAcctEmail();
String serviceAccountPK = ds.getOAuthPvtKey();
Expand All @@ -142,7 +142,7 @@ static Map<String, String> parseOAuthProperties(DataSource ds, String callerClas
oauthProperties.put(
BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, serviceAccountEmail);
oauthProperties.put(BigQueryJdbcUrlUtility.OAUTH_P12_PASSWORD_PROPERTY_NAME, p12Password);
if (serviceAccountEmail != null && serviceAccountPK != null) {
if (serviceAccountPK != null && !serviceAccountPK.trim().isEmpty()) {
oauthProperties.put(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, serviceAccountPK);
} else {
oauthProperties.put(
Expand Down Expand Up @@ -316,11 +316,14 @@ static boolean isFileExists(String filename) {
}

static boolean isJson(byte[] value) {
if (value == null) {
return false;
}
try {
// This is done this way to ensure strict Json parsing
// https://github.com/google/gson/issues/1208#issuecomment-2120764686
InputStream stream = new ByteArrayInputStream(value);
InputStreamReader reader = new InputStreamReader(stream);
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setStrictness(Strictness.STRICT);
JsonElement json = JsonParser.parseReader(jsonReader);
Expand Down Expand Up @@ -350,7 +353,7 @@ private static GoogleCredentials getGoogleServiceAccountCredentials(

final String keyPath = pvtKeyPath != null ? pvtKeyPath : pvtKey;
PrivateKey key = null;
byte[] keyBytes = pvtKey != null ? pvtKey.getBytes() : null;
byte[] keyBytes = pvtKey != null ? pvtKey.getBytes(StandardCharsets.UTF_8) : null;

if (isFileExists(keyPath)) {
try (InputStream stream = new FileInputStream(keyPath)) {
Expand All @@ -372,7 +375,11 @@ private static GoogleCredentials getGoogleServiceAccountCredentials(

if (stream != null) {
builder = ServiceAccountCredentials.fromStream(stream).toBuilder();
} else if (pvtEmail != null && key != null) {
} else if (key != null) {
if (pvtEmail == null || pvtEmail.trim().isEmpty()) {
throw new BigQueryJdbcRuntimeException(
"OAuthServiceAcctEmail is required when using a PKCS#8 or P12 private key.");
}
builder =
ServiceAccountCredentials.newBuilder().setClientEmail(pvtEmail).setPrivateKey(key);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;

import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.jdbc.telemetry.v1.AuthenticationType;
import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature;
import com.google.cloud.bigquery.jdbc.telemetry.v1.Status;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryTestUtils;
import com.google.cloud.bigquery.jdbc.utils.BigQueryJdbcVersionUtility;
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
import io.opentelemetry.api.OpenTelemetry;
import java.io.IOException;
import java.sql.Connection;
Expand All @@ -47,6 +49,8 @@

public class BigQueryDriverTest extends BigQueryJdbcLoggingBaseTest {

private static final String TEST_SA_EMAIL = "dummytest@dummytest.iam.gserviceaccount.com";

static BigQueryDriver bigQueryDriver;

/** Token-auth URL that connects without network access; shared by the telemetry tests. */
Expand Down Expand Up @@ -292,4 +296,55 @@ public void testConnect_ioExceptionThrowsInsteadOfReturningNull() {
assertThat(thrown.getCause()).isInstanceOf(IOException.class);
}
}

@Test
public void testConnectWithInlineJsonKeyAndBlankEmailInProperties() throws SQLException {
String fakeJson = getFakeServiceAccountJson(TEST_SA_EMAIL);
String baseUrl = TestUtilities.getBaseConnectionUrl();

// Empty email in Properties (dropped by appendPropertiesToURL), omitted email, and whitespace
for (String email : new String[] {"", null, " "}) {
Properties props = new Properties();
props.setProperty(BigQueryJdbcUrlUtility.PROJECT_ID_PROPERTY_NAME, "MyBigQueryProject");
props.setProperty(BigQueryJdbcUrlUtility.OAUTH_TYPE_PROPERTY_NAME, "0");
props.setProperty(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, fakeJson);
if (email != null) {
props.setProperty(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, email);
}

try (Connection connection = bigQueryDriver.connect(baseUrl, props)) {
assertConnectedWithServiceAccount(connection, TEST_SA_EMAIL);
}
}
}

@Test
public void testDataSourceGetConnectionWithInlineJsonKeyWithoutEmail() throws SQLException {
String fakeJson = getFakeServiceAccountJson(TEST_SA_EMAIL);

for (String email : new String[] {null, "", " "}) {
DataSource ds = new DataSource();
ds.setURL(TestUtilities.getBaseConnectionUrl());
ds.setProjectId("MyBigQueryProject");
ds.setOAuthType(0);
ds.setOAuthPvtKey(fakeJson);
if (email != null) {
ds.setOAuthServiceAcctEmail(email);
}

try (Connection connection = ds.getConnection()) {
assertConnectedWithServiceAccount(connection, TEST_SA_EMAIL);
}
}
}

private static void assertConnectedWithServiceAccount(Connection connection, String expectedEmail)
throws SQLException {
assertThat(connection).isNotNull();
assertThat(connection.isClosed()).isFalse();
BigQueryConnection bqConnection = connection.unwrap(BigQueryConnection.class);
assertThat(bqConnection.credentials).isInstanceOf(ServiceAccountCredentials.class);
assertThat(((ServiceAccountCredentials) bqConnection.credentials).getClientEmail())
.isEqualTo(expectedEmail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
import com.google.cloud.bigquery.jdbc.utils.URIBuilder;
import com.google.gson.JsonObject;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -48,6 +49,18 @@ public class BigQueryJdbcBaseTest {
+ //
"-----END PRIVATE KEY-----";

protected static String getFakeServiceAccountJson(String clientEmail) {
JsonObject json = new JsonObject();
json.addProperty("type", "service_account");
json.addProperty("project_id", "MyBigQueryProject");
json.addProperty("private_key_id", "fake-key-id");
json.addProperty("private_key", fake_pkcs8_key);
json.addProperty("client_email", clientEmail);
json.addProperty("client_id", "1234567890");
json.addProperty("token_uri", "https://oauth2.googleapis.com/token");
return json.toString();
}

protected static BigQuery getBigQuery(String connectionUrl) {
try {
Class<?> bqConnClass = Class.forName("com.google.cloud.bigquery.jdbc.BigQueryConnection");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.auth.http.HttpTransportFactory;
Expand All @@ -33,7 +34,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Collections;
Expand All @@ -42,10 +45,13 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class BigQueryJdbcOAuthUtilityTest extends BigQueryJdbcBaseTest {

private static final int USER_AUTH_PORT = 53737;
private static final String TEST_PROJECT_ID = "MyBigQueryProject";
private static final String TEST_SA_EMAIL = "dummytest@dummytest.iam.gserviceaccount.com";
private static final String EXPECTED_USER_AUTH_URL =
"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=client_id&redirect_uri=http://localhost:"
+ USER_AUTH_PORT
Expand Down Expand Up @@ -565,4 +571,167 @@ public void testGetPreGeneratedRefreshTokenCredentialsPropagatesHttpTransportFac
assertThat(((UserCredentials) credentials).toBuilder().getHttpTransportFactory())
.isEqualTo(dummyFactory);
}

@Test
public void testParseOAuthPropsForAuthType0InlineKeyWithoutEmail() {
String fakeJson = getFakeServiceAccountJson(TEST_SA_EMAIL);
String url =
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, fakeJson)
.toString();

Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(url), null);

assertThat(result.get(BigQueryJdbcUrlUtility.OAUTH_TYPE_PROPERTY_NAME))
.isEqualTo("GOOGLE_SERVICE_ACCOUNT");
assertThat(result.get(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME)).isNull();
assertThat(result.get(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME)).isEqualTo(fakeJson);
assertThat(result.get(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME)).isNull();
}

@Test
public void testParseOAuthPropsForAuthType0BlankPvtKeyFallsBackToKeyPath() {
String keyPath = "/tmp/service-account.json";
for (String blankKey : new String[] {"", " "}) {
String url =
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, TEST_SA_EMAIL)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, blankKey)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME, keyPath)
.toString();

Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(url), null);

assertThat(result.get(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME)).isNull();
assertThat(result.get(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME))
.isEqualTo(keyPath);
}
}

@Test
public void testGetCredentialsForServiceAccountInlineJsonWithoutEmail() {
String fakeJson = getFakeServiceAccountJson(TEST_SA_EMAIL);

// Email omitted (null)
assertServiceAccountCredentialsFromUrl(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, fakeJson)
.toString(),
TEST_SA_EMAIL);

// Email empty or whitespace
for (String blankEmail : new String[] {"", " "}) {
assertServiceAccountCredentialsFromUrl(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, blankEmail)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, fakeJson)
.toString(),
TEST_SA_EMAIL);
}
}

@Test
public void testGetCredentialsForServiceAccountJsonFileInPvtKeyWithoutEmail(@TempDir Path tempDir)
throws IOException {
Path keyFile = tempDir.resolve("sa-key.json");
Files.write(keyFile, getFakeServiceAccountJson(TEST_SA_EMAIL).getBytes(StandardCharsets.UTF_8));

assertServiceAccountCredentialsFromUrl(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, keyFile.toString())
.toString(),
TEST_SA_EMAIL);
}

@Test
public void testGetCredentialsForServiceAccountPkcs8WithoutEmailThrows() {
// Email omitted (null)
assertMissingEmailThrows(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, fake_pkcs8_key)
.toString());

// Email empty or whitespace
for (String blankEmail : new String[] {"", " "}) {
assertMissingEmailThrows(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, blankEmail)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PROPERTY_NAME, fake_pkcs8_key)
.toString());
}
}

@Test
public void testGetCredentialsForServiceAccountP12WithAndWithoutEmail()
throws URISyntaxException {
URL resource = BigQueryJdbcOAuthUtilityTest.class.getResource("/fake.p12");
assertNotNull(resource);
String p12Path = Paths.get(resource.toURI()).toString();

assertServiceAccountCredentialsFromUrl(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, TEST_SA_EMAIL)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME, p12Path)
.toString(),
TEST_SA_EMAIL);

// Email omitted (null)
assertMissingEmailThrows(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME, p12Path)
.toString());

// Email empty or whitespace
for (String blankEmail : new String[] {"", " "}) {
assertMissingEmailThrows(
getBaseUri(0, TEST_PROJECT_ID)
.append(BigQueryJdbcUrlUtility.OAUTH_SA_EMAIL_PROPERTY_NAME, blankEmail)
.append(BigQueryJdbcUrlUtility.OAUTH_PVT_KEY_PATH_PROPERTY_NAME, p12Path)
.toString());
}
}

@Test
public void testGetCredentialsForServiceAccountMissingKeyThrows() {
String url = getBaseUri(0, TEST_PROJECT_ID).toString();
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(url), null);

BigQueryJdbcRuntimeException ex =
assertThrows(
BigQueryJdbcRuntimeException.class,
() ->
BigQueryJdbcOAuthUtility.getCredentials(
authProperties, Collections.emptyMap(), false, null, null));
assertThat(ex.getMessage()).contains("No valid credentials provided.");
}

private static void assertServiceAccountCredentialsFromUrl(
String connectionUrl, String expectedEmail) {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(connectionUrl), null);

GoogleCredentials credentials =
BigQueryJdbcOAuthUtility.getCredentials(
authProperties, Collections.emptyMap(), false, null, null);

assertThat(credentials).isInstanceOf(ServiceAccountCredentials.class);
assertThat(((ServiceAccountCredentials) credentials).getClientEmail()).isEqualTo(expectedEmail);
}

private static void assertMissingEmailThrows(String connectionUrl) {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(connectionUrl), null);

BigQueryJdbcRuntimeException ex =
assertThrows(
BigQueryJdbcRuntimeException.class,
() ->
BigQueryJdbcOAuthUtility.getCredentials(
authProperties, Collections.emptyMap(), false, null, null));
assertThat(ex.getMessage())
.contains("OAuthServiceAcctEmail is required when using a PKCS#8 or P12 private key.");
}
}
Loading