ignite-709: read private key for GCE from the file

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/cc08687a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/cc08687a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/cc08687a

Branch: refs/heads/ignite-789
Commit: cc08687a9d8a9f564c179cd75743f12cca0fde90
Parents: d5fdbf4
Author: Denis Magda <dma...@gridgain.com>
Authored: Thu Apr 23 17:43:13 2015 +0300
Committer: Denis Magda <dma...@gridgain.com>
Committed: Thu Apr 23 17:43:13 2015 +0300

----------------------------------------------------------------------
 .../cloud/TcpDiscoveryCloudNodesIpFinder.java   | 26 ++++++++++++++++++++
 1 file changed, 26 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cc08687a/modules/cloud/src/main/java/org/apache/ignite/spi/discovery/tcp/ipfinder/cloud/TcpDiscoveryCloudNodesIpFinder.java
----------------------------------------------------------------------
diff --git 
a/modules/cloud/src/main/java/org/apache/ignite/spi/discovery/tcp/ipfinder/cloud/TcpDiscoveryCloudNodesIpFinder.java
 
b/modules/cloud/src/main/java/org/apache/ignite/spi/discovery/tcp/ipfinder/cloud/TcpDiscoveryCloudNodesIpFinder.java
index 0680595..d542b6d 100644
--- 
a/modules/cloud/src/main/java/org/apache/ignite/spi/discovery/tcp/ipfinder/cloud/TcpDiscoveryCloudNodesIpFinder.java
+++ 
b/modules/cloud/src/main/java/org/apache/ignite/spi/discovery/tcp/ipfinder/cloud/TcpDiscoveryCloudNodesIpFinder.java
@@ -17,7 +17,9 @@
 
 package org.apache.ignite.spi.discovery.tcp.ipfinder.cloud;
 
+import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.io.Files;
 import com.google.inject.Module;
 import org.apache.ignite.internal.IgniteInterruptedCheckedException;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
@@ -35,6 +37,8 @@ import org.jclouds.compute.domain.NodeMetadata;
 import org.jclouds.sshj.config.SshjSshClientModule;
 import org.jclouds.logging.log4j.config.*;
 
+import java.io.File;
+import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.LinkedList;
@@ -201,6 +205,13 @@ public class TcpDiscoveryCloudNodesIpFinder extends 
TcpDiscoveryIpFinderAdapter
                 if (identity == null)
                     throw new IgniteSpiException("Cloud identity is not set.");
 
+                if (provider.equals("google-compute-engine")) {
+                    if (credential == null)
+                        throw new IgniteSpiException("Cloud credential is not 
set.");
+                    else 
+                        credential = getPrivateKeyFromFile(credential);
+                }
+
                 try {
                     ContextBuilder ctxBuilder = 
ContextBuilder.newBuilder(provider);
                     ctxBuilder.credentials(identity, credential);
@@ -231,4 +242,19 @@ public class TcpDiscoveryCloudNodesIpFinder extends 
TcpDiscoveryIpFinderAdapter
                 throw new IgniteSpiException("Ip finder has not been 
initialized properly.");
         }
     }
+
+    /**
+     * Retrieves a private key from the secrets file.
+     *
+     * @param filePath Full path to the file.
+     * @return Private key
+     */
+    private String getPrivateKeyFromFile(String filePath) throws 
IgniteSpiException {
+        try {
+            return Files.toString(new File(filePath), Charsets.UTF_8);
+        }
+        catch (IOException e) {
+            throw new IgniteSpiException("Failed to retrieve a private key 
from the file: " + filePath, e);
+        }
+    }
 }

Reply via email to