Skip to content
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

Add service account key file auth in BqStreamer #207

Open
wants to merge 1 commit into
base: gcp-master
Choose a base branch
from
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
42 changes: 41 additions & 1 deletion twitter-eventlistener-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>1.55.0</version>
<version>1.84.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -145,6 +145,46 @@
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.16.2</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class TwitterEventListenerConfig
private String knowledgeBaseFile;
private String scribeCategory;
private String bqTableFullName;
private String serviceAccountKeyFile;

public String getSlackConfigFile()
{
Expand Down Expand Up @@ -137,4 +138,16 @@ public TwitterEventListenerConfig setBqTableFullName(String bqTableFullName)
this.bqTableFullName = bqTableFullName;
return this;
}

public String getServiceAccountKeyFile()
{
return serviceAccountKeyFile;
}

@Config("event-listener.bq-json-key-file")
public TwitterEventListenerConfig setServiceAccountKeyFile(String serviceAccountKeyFile)
{
this.serviceAccountKeyFile = serviceAccountKeyFile;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.facebook.presto.spi.eventlistener.QueryIOMetadata;
import com.facebook.presto.spi.eventlistener.QueryMetadata;
import com.facebook.presto.spi.eventlistener.QueryStatistics;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.InsertAllRequest;
Expand All @@ -30,6 +32,9 @@

import javax.inject.Inject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,7 +62,7 @@ public BqStreamer(TwitterEventListenerConfig config)
String datasetName = parts.get(1);
String tableName = parts.get(2);

this.bigquery = BigQueryOptions.getDefaultInstance().getService();
this.bigquery = createInstance(config.getServiceAccountKeyFile());
this.tableId = TableId.of(projectId, datasetName, tableName);
}

Expand All @@ -78,6 +83,24 @@ public void handleQueryCompleted(QueryCompletedEvent queryCompletedEvent)
}
}

private static BigQuery createInstance(String jsonKeyPath)
{
if (jsonKeyPath == null) {
// fall back to default Instance by looking up the VM metadata
return BigQueryOptions.getDefaultInstance().getService();
}

GoogleCredentials credentials;
try (FileInputStream serviceAccountStream = new FileInputStream(new File(jsonKeyPath))) {
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
}
catch (IOException e) {
throw new IllegalArgumentException();
}

return BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
}

private static Map<String, Object> prepareRowContent(QueryCompletedEvent queryCompletedEvent)
{
Map<String, Object> rowContent = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public void testDefaults()
.setSlackNotificationTemplateFile(null)
.setKnowledgeBaseFile(null)
.setSlackUri(null)
.setSlackUsers(null));
.setSlackUsers(null)
.setBqTableFullName(null)
.setServiceAccountKeyFile(null));
}

@Test
Expand All @@ -49,6 +51,8 @@ public void testExplicitPropertyMappings()
.put("event-listener.slack-notification-template-file", "/etc/config/notification.json")
.put("event-listener.slack-uri", "https://slack.com")
.put("event-listener.slack-users", "user1|user2")
.put("event-listener.bq-table-full-name", "test-bq")
.put("event-listener.bq-json-key-file", "key.json")
.build();

TwitterEventListenerConfig expected = new TwitterEventListenerConfig()
Expand All @@ -59,7 +63,9 @@ public void testExplicitPropertyMappings()
.setSlackHttpProxy(HostAndPort.fromString("localhost:1008"))
.setSlackNotificationTemplateFile("/etc/config/notification.json")
.setSlackUri(URI.create("https://slack.com"))
.setSlackUsers("user1|user2");
.setSlackUsers("user1|user2")
.setBqTableFullName("test-bq")
.setServiceAccountKeyFile("key.json");

ConfigAssertions.assertFullMapping(properties, expected);
}
Expand Down