summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/resources/v1_0/KeysResource.java
blob: 674e49e77fc7e4f60549c00638d8e90ad5432724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * KeysResource.java
 *
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gluster Management Console is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.gluster.storage.management.gateway.resources.v1_0;

import static org.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.text.SimpleDateFormat;
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.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;
import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import org.gluster.storage.management.core.utils.FileUtil;
import org.gluster.storage.management.core.utils.ProcessResult;
import org.gluster.storage.management.core.utils.ProcessUtil;
import org.gluster.storage.management.gateway.utils.SshUtil;

import com.sun.jersey.multipart.FormDataParam;

@Path(RESOURCE_PATH_KEYS)
public class KeysResource extends AbstractResource {
	private static final Logger logger = Logger.getLogger(KeysResource.class);

	@GET
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	public Response exportSshkeys() {
		File archiveFile = new File(createSskKeyZipFile());
		byte[] data = FileUtil.readFileAsByteArray(archiveFile);
		archiveFile.delete();
		return streamingOutputResponse(createStreamingOutput(data));
	}
	
	private String createSskKeyZipFile() {
		String targetDir = FileUtil.getTempDirName();
		String zipFile = targetDir + File.separator + "ssh-keys.tar";
		String sourcePrivateKeyFile = SshUtil.PRIVATE_KEY_FILE.getAbsolutePath();
		String sourcePublicKeyFile = SshUtil.PUBLIC_KEY_FILE.getAbsolutePath();
		String targetPrivateKeyFile = targetDir + File.separator + SshUtil.PRIVATE_KEY_FILE.getName();
		String targetPubKeyFile = targetDir + File.separator + SshUtil.PUBLIC_KEY_FILE.getName();

		if (!SshUtil.PRIVATE_KEY_FILE.isFile()) {
			throw new GlusterRuntimeException("No private key file [" + SshUtil.PRIVATE_KEY_FILE.getName() + "] found!");
		}
		
		if (!SshUtil.PUBLIC_KEY_FILE.isFile()) {
			throw new GlusterRuntimeException("No public key file [" + SshUtil.PUBLIC_KEY_FILE.getName() + "] found!");
		}
		
		// Copy keys to temp folder
		ProcessResult result = ProcessUtil.executeCommand("cp", sourcePrivateKeyFile, targetPrivateKeyFile);
		if (!result.isSuccess()) {
			throw new GlusterRuntimeException("Failed to copy key files! [" + result.getOutput() + "]");
		}
		result = ProcessUtil.executeCommand("cp", sourcePublicKeyFile, targetPubKeyFile);
		if (!result.isSuccess()) {
			throw new GlusterRuntimeException("Failed to copy key files! [" + result.getOutput() + "]");
		}
		
		// To compress the key files
		result = ProcessUtil.executeCommand("tar", "cvf", zipFile, "-C", targetDir, SshUtil.PRIVATE_KEY_FILE.getName(),
				SshUtil.PUBLIC_KEY_FILE.getName());
		if (!result.isSuccess()) {
			throw new GlusterRuntimeException("Failed to compress key files! [" + result.getOutput() + "]");
		}

		// To remove the copied key files
		try {
			ProcessUtil.executeCommand("rm", "-f", targetPrivateKeyFile, targetPubKeyFile); // Ignore the errors if any
		} catch (Exception e) {
			logger.warn(e.toString());
		}
		return zipFile;
	}

	@POST
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public Response importSshKeys(@FormDataParam("file") InputStream uploadedInputStream) {
		File uploadedFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "keys.tar");
		String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

		writeToFile(uploadedInputStream, uploadedFile.getAbsolutePath());

		// To backup existing SSH private and public keys, if exist.
		if (SshUtil.PRIVATE_KEY_FILE.isFile()) {
			if (!SshUtil.PRIVATE_KEY_FILE.renameTo(new File(SshUtil.PRIVATE_KEY_FILE.getAbsolutePath() + "-" + timestamp))) {
				throw new GlusterRuntimeException("Unable to backup private key!");
			}
		}

		if (SshUtil.PUBLIC_KEY_FILE.isFile()) {
			if (!SshUtil.PUBLIC_KEY_FILE
					.renameTo(new File(SshUtil.PUBLIC_KEY_FILE.getAbsolutePath() + "-" + timestamp))) {
				throw new GlusterRuntimeException("Unable to backup public key!");
			}
		}
		// Extract SSH private and public key files.
		ProcessResult output = ProcessUtil.executeCommand("tar", "xvf", uploadedFile.getAbsolutePath(), "-C",
				SshUtil.SSH_AUTHORIZED_KEYS_DIR_LOCAL);
		uploadedFile.delete();
		if (!output.isSuccess()) {
			String errMsg = "Error in importing SSH keys: [" + output.toString() + "]";
			logger.error(errMsg);
			throw new GlusterRuntimeException(errMsg);
		}
		return createdResponse("SSH Key imported successfully");
	}

	// 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());
		}
	}
}