summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.server
diff options
context:
space:
mode:
authorSelvasundaram <selvam@gluster.com>2011-07-18 22:11:44 +0530
committerSelvasundaram <selvam@gluster.com>2011-07-18 22:11:44 +0530
commit948ef913fae74a9b67f0901d7bbf9153c52bfffb (patch)
tree921cc444fe33260acafd6b4ab8655644b561a557 /src/com.gluster.storage.management.server
parent691f4b7519a7758de9316766d9963d68cdd50924 (diff)
Import SSH keys feature
Diffstat (limited to 'src/com.gluster.storage.management.server')
-rw-r--r--src/com.gluster.storage.management.server/.classpath1
-rw-r--r--src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/jersey-multipart-1.5.jarbin0 -> 49330 bytes
-rw-r--r--src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/mimepull-1.3.jarbin0 -> 38683 bytes
-rw-r--r--src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java70
-rw-r--r--src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java2
5 files changed, 68 insertions, 5 deletions
diff --git a/src/com.gluster.storage.management.server/.classpath b/src/com.gluster.storage.management.server/.classpath
index 59631d14..107e139d 100644
--- a/src/com.gluster.storage.management.server/.classpath
+++ b/src/com.gluster.storage.management.server/.classpath
@@ -10,5 +10,6 @@
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry combineaccessrules="false" kind="src" path="/com.gluster.storage.management.core"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry exported="true" kind="lib" path="/com.gluster.storage.management.client/lib/jersey-1.5/jersey-multipart-1.5.jar"/>
<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>
</classpath>
diff --git a/src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/jersey-multipart-1.5.jar b/src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/jersey-multipart-1.5.jar
new file mode 100644
index 00000000..1c134f05
--- /dev/null
+++ b/src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/jersey-multipart-1.5.jar
Binary files differ
diff --git a/src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/mimepull-1.3.jar b/src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/mimepull-1.3.jar
new file mode 100644
index 00000000..48cc9295
--- /dev/null
+++ b/src/com.gluster.storage.management.server/WebContent/WEB-INF/lib/mimepull-1.3.jar
Binary files differ
diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java
index a589c2a2..36443a4c 100644
--- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java
+++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/KeysResource.java
@@ -23,10 +23,15 @@ package com.gluster.storage.management.server.resources.v1_0;
import static com.gluster.storage.management.core.constants.RESTConstants.RESOURCE_PATH_KEYS;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Date;
+import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
@@ -34,14 +39,17 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
+import com.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import com.gluster.storage.management.core.utils.FileUtil;
+import com.gluster.storage.management.core.utils.ProcessResult;
import com.gluster.storage.management.core.utils.ProcessUtil;
import com.gluster.storage.management.server.utils.SshUtil;
+import com.sun.jersey.multipart.FormDataParam;
@Path(RESOURCE_PATH_KEYS)
public class KeysResource extends AbstractResource {
+ ProcessUtil processUtil = new ProcessUtil();
-
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportSshkeys() {
@@ -65,25 +73,79 @@ public class KeysResource extends AbstractResource {
}
}
- public String createSskKeyZipFile() {
+ private String createSskKeyZipFile() {
String targetDir = System.getProperty("java.io.tmpdir");
String zipFile = targetDir + "ssh-keys.tar";
String sourcePemFile = SshUtil.PEM_FILE.getAbsolutePath();
String sourcePubKeyFile = SshUtil.PUBLIC_KEY_FILE.getAbsolutePath();
String targetPemFile = targetDir + File.separator + SshUtil.PEM_FILE.getName();
String targetPubKeyFile = targetDir + File.separator + SshUtil.PUBLIC_KEY_FILE.getName();
- ProcessUtil processUtil = new ProcessUtil();
// Copy keys to temp folder
processUtil.executeCommand("cp", sourcePemFile, targetPemFile);
processUtil.executeCommand("cp", sourcePubKeyFile, targetPubKeyFile);
// To zip the key files
- processUtil.executeCommand("tar", "cvf", zipFile, "-C", "/tmp", SshUtil.PEM_FILE.getName(), SshUtil.PUBLIC_KEY_FILE.getName());
+ processUtil.executeCommand("tar", "cvf", zipFile, "-C", "/tmp", SshUtil.PEM_FILE.getName(),
+ SshUtil.PUBLIC_KEY_FILE.getName());
// To remove the copied key files
processUtil.executeCommand("rm", "-f", targetPubKeyFile, targetPubKeyFile);
return zipFile;
}
+
+ @POST
+ @Consumes(MediaType.MULTIPART_FORM_DATA)
+ @SuppressWarnings({"deprecation" })
+ public Response importSshKeys(@FormDataParam("file") InputStream uploadedInputStream) {
+ File uploadedFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "keys.tar");
+ Date today = new Date();
+ try {
+ writeToFile(uploadedInputStream, uploadedFile.getAbsolutePath());
+
+ // To backup existing SSH pem and public keys
+ if (SshUtil.PEM_FILE.isFile()) {
+ if (!SshUtil.PEM_FILE
+ .renameTo(new File(SshUtil.PEM_FILE.getAbsolutePath() + "-" + today.toString()))) {
+ throw new GlusterRuntimeException("Unable to backup pem key!");
+ }
+ }
+
+ if (SshUtil.PUBLIC_KEY_FILE.isFile()) {
+ if (!SshUtil.PUBLIC_KEY_FILE.renameTo(new File(SshUtil.PUBLIC_KEY_FILE.getAbsolutePath() + "-"
+ + today.toString()))) {
+ throw new GlusterRuntimeException("Unable to backup public key!");
+ }
+ }
+ // Extract SSH pem and public key files.
+ ProcessResult output = processUtil.executeCommand("tar", "xvf", uploadedFile.getName(), "-C",
+ SshUtil.SSH_AUTHORIZED_KEYS_DIR);
+ uploadedFile.delete();
+ if (output.isSuccess()) {
+ return createdResponse("SSH Key imported successfully");
+ } else {
+ return errorResponse(output.getOutput());
+ }
+ } catch (Exception e) {
+ return errorResponse(e.getMessage());
+ }
+ }
+
+ // save uploaded file to the file (with path)
+ private void writeToFile(InputStream inputStream, String toFile) {
+ try {
+ int read = 0;
+ byte[] bytes = new byte[1024];
+
+ OutputStream out = new FileOutputStream(new File(toFile));
+ while ((read = inputStream.read(bytes)) != -1) {
+ out.write(bytes, 0, read);
+ }
+ out.flush();
+ out.close();
+ } catch (IOException e) {
+ throw new GlusterRuntimeException(e.getMessage());
+ }
+ }
}
diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java
index 4771a230..746b8832 100644
--- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java
+++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java
@@ -49,7 +49,7 @@ import com.gluster.storage.management.core.utils.ProcessResult;
@Component
public class SshUtil {
private static final String TEMP_DIR = "/tmp/";
- private static final String SSH_AUTHORIZED_KEYS_DIR = "/root/.ssh/";
+ public static final String SSH_AUTHORIZED_KEYS_DIR = "/root/.ssh/";
private static final String SSH_AUTHORIZED_KEYS_FILE = "authorized_keys";
private static final String SSH_AUTHORIZED_KEYS_PATH = SSH_AUTHORIZED_KEYS_DIR + SSH_AUTHORIZED_KEYS_FILE;
private LRUCache<String, Connection> sshConnCache = new LRUCache<String, Connection>(10);