package org.sagebionetworks.gcp;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import com.amazonaws.util.StringUtils;
import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;

public abstract class AbstractSynapseGoogleCloudCredentialsProvider implements CredentialsProvider {

	public static final String GOOGLE_CLOUD_CREDENTIALS_WERE_NOT_FOUND = "Google Cloud credentials were not found.";
	public static final String ORG_SAGEBIONETWORKS_STACK_GCP_SVC_ACCOUNT_KEY = "org.sagebionetworks.gcp.key";

	final Properties properties;

	AbstractSynapseGoogleCloudCredentialsProvider(Properties properties) {
		this.properties = properties;
	}

	/**
	 * Search the provided Properties for the credentials.
	 */
	final public Credentials getCredentials() {
		try {
			Properties properties = getProperties();
			if (properties != null) {
				String accessKey = StringUtils.trim(properties.getProperty(ORG_SAGEBIONETWORKS_STACK_GCP_SVC_ACCOUNT_KEY));
				if (accessKey != null) {
					return ServiceAccountCredentials.fromStream(new ByteArrayInputStream(accessKey.getBytes(StandardCharsets.UTF_8)));
				}
			}
			throw new IllegalStateException(GOOGLE_CLOUD_CREDENTIALS_WERE_NOT_FOUND);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Extending classes provide a Properties object that could contain sage credentials.
	 * @return
	 */
	 Properties getProperties() {
	 	return this.properties;
	 }


}

@@ -31,87 +31,8 @@
 p;%0A%0A
-import java.io.ByteArrayInputStream;%0Aimport java.nio.charset.StandardCharsets;%0A
 impo
@@ -510,33 +510,408 @@
 RKS_
-STACK_GCP_SVC_ACCOUNT_KEY
+GOOGLE_CLOUD_CLIENT_ID = %22org.sagebionetworks.google.cloud.client.id%22;%0A%09public static final String ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_EMAIL = %22org.sagebionetworks.google.cloud.client.email%22;%0A%09public static final String ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_PRIVATE_KEY = %22org.sagebionetworks.google.cloud.key%22;%0A%09public static final String ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_PRIVATE_KEY_ID
  = %22
@@ -935,14 +935,26 @@
 ks.g
-cp
+oogle.cloud
 .key
+.id
 %22;%0A%0A
@@ -1299,108 +1299,467 @@
 ing 
-accessKey = StringUtils.trim(properties.getProperty(ORG_SAGEBIONETWORKS_STACK_GCP_SVC_ACCOUNT_KEY
+clientId = StringUtils.trim(properties.getProperty(ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_ID));%0A%09%09%09%09String clientEmail = StringUtils.trim(properties.getProperty(ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_EMAIL));%0A%09%09%09%09String privateKeyPkcs8 = StringUtils.trim(properties.getProperty(ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_PRIVATE_KEY));%0A%09%09%09%09String privateKeyId = StringUtils.trim(properties.getProperty(ORG_SAGEBIONETWORKS_GOOGLE_CLOUD_CLIENT_PRIVATE_KEY_ID
 ));
+;
 %0A%09%09%09
@@ -1763,25 +1763,98 @@
 %09%09%09%09if (
-access
+clientId != null && clientEmail != null && privateKeyPkcs8 != null && private
 Key
+Id
  != null
@@ -1903,83 +1903,72 @@
 from
-Stream(new ByteArrayInputStream(accessKey.getBytes(StandardCharsets.UTF_8))
+Pkcs8(clientId, clientEmail, privateKeyPkcs8, privateKeyId, null
 );%0A%09

use four keys instead of one