src/main/java/org/embulk/input/gcs/GcsAuthentication.java in embulk-input-gcs-0.1.6 vs src/main/java/org/embulk/input/gcs/GcsAuthentication.java in embulk-input-gcs-0.1.7

- old
+ new

@@ -1,13 +1,15 @@ package org.embulk.input.gcs; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import java.security.GeneralSecurityException; +import java.util.Collections; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.compute.ComputeCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpTransport; @@ -24,27 +26,32 @@ public class GcsAuthentication { private final Logger log = Exec.getLogger(GcsAuthentication.class); private final Optional<String> serviceAccountEmail; private final Optional<String> p12KeyFilePath; + private final Optional<String> jsonKeyFilePath; private final String applicationName; private final HttpTransport httpTransport; private final JsonFactory jsonFactory; private final HttpRequestInitializer credentials; - public GcsAuthentication(String authMethod, Optional<String> serviceAccountEmail, Optional<String> p12KeyFilePath, String applicationName) + public GcsAuthentication(String authMethod, Optional<String> serviceAccountEmail, + Optional<String> p12KeyFilePath, Optional<String> jsonKeyFilePath, String applicationName) throws IOException, GeneralSecurityException { this.serviceAccountEmail = serviceAccountEmail; this.p12KeyFilePath = p12KeyFilePath; + this.jsonKeyFilePath = jsonKeyFilePath; this.applicationName = applicationName; this.httpTransport = GoogleNetHttpTransport.newTrustedTransport(); this.jsonFactory = new JacksonFactory(); if (authMethod.equals("compute_engine")) { this.credentials = getComputeCredential(); + } else if(authMethod.toLowerCase().equals("json_key")) { + this.credentials = getServiceAccountCredentialFromJsonFile(); } else { this.credentials = getServiceAccountCredential(); } } @@ -65,9 +72,17 @@ StorageScopes.DEVSTORAGE_READ_ONLY ) ) .setServiceAccountPrivateKeyFromP12File(new File(p12KeyFilePath.orNull())) .build(); + } + + private GoogleCredential getServiceAccountCredentialFromJsonFile() throws IOException + { + FileInputStream stream = new FileInputStream(jsonKeyFilePath.orNull()); + + return GoogleCredential.fromStream(stream, httpTransport, jsonFactory) + .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_ONLY)); } /** * @see http://developers.guge.io/accounts/docs/OAuth2ServiceAccount#creatinganaccount * @see https://developers.google.com/accounts/docs/OAuth2 \ No newline at end of file