View Javadoc
1   /*
2    * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.pgm.debug;
45  
46  import java.io.File;
47  import java.io.IOException;
48  import java.net.InetAddress;
49  import java.net.URI;
50  import java.net.URISyntaxException;
51  import java.net.UnknownHostException;
52  import java.nio.file.Path;
53  import java.nio.file.Paths;
54  import java.text.MessageFormat;
55  
56  import org.eclipse.jetty.server.Connector;
57  import org.eclipse.jetty.server.HttpConfiguration;
58  import org.eclipse.jetty.server.HttpConnectionFactory;
59  import org.eclipse.jetty.server.Server;
60  import org.eclipse.jetty.server.ServerConnector;
61  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
62  import org.eclipse.jetty.servlet.ServletContextHandler;
63  import org.eclipse.jetty.servlet.ServletHolder;
64  import org.eclipse.jgit.errors.ConfigInvalidException;
65  import org.eclipse.jgit.lfs.server.LargeFileRepository;
66  import org.eclipse.jgit.lfs.server.LfsProtocolServlet;
67  import org.eclipse.jgit.lfs.server.fs.FileLfsRepository;
68  import org.eclipse.jgit.lfs.server.fs.FileLfsServlet;
69  import org.eclipse.jgit.lfs.server.s3.S3Config;
70  import org.eclipse.jgit.lfs.server.s3.S3Repository;
71  import org.eclipse.jgit.pgm.Command;
72  import org.eclipse.jgit.pgm.TextBuiltin;
73  import org.eclipse.jgit.pgm.internal.CLIText;
74  import org.eclipse.jgit.storage.file.FileBasedConfig;
75  import org.eclipse.jgit.util.FS;
76  import org.kohsuke.args4j.Argument;
77  import org.kohsuke.args4j.Option;
78  
79  @Command(common = true, usage = "usage_runLfsStore")
80  class LfsStore extends TextBuiltin {
81  
82  	/**
83  	 * Tiny web application server for testing
84  	 */
85  	class AppServer {
86  
87  		private final Server server;
88  
89  		private final ServerConnector connector;
90  
91  		private final ContextHandlerCollection contexts;
92  
93  		private URI uri;
94  
95  		AppServer(int port) {
96  			server = new Server();
97  
98  			HttpConfiguration http_config = new HttpConfiguration();
99  			http_config.setOutputBufferSize(32768);
100 
101 			connector = new ServerConnector(server,
102 					new HttpConnectionFactory(http_config));
103 			connector.setPort(port);
104 			try {
105 				String host = InetAddress.getByName("localhost") //$NON-NLS-1$
106 						.getHostAddress();
107 				connector.setHost(host);
108 				if (host.contains(":") && !host.startsWith("[")) //$NON-NLS-1$ //$NON-NLS-2$
109 					host = "[" + host + "]"; //$NON-NLS-1$//$NON-NLS-2$
110 				uri = new URI("http://" + host + ":" + port); //$NON-NLS-1$ //$NON-NLS-2$
111 			} catch (UnknownHostException e) {
112 				throw new RuntimeException("Cannot find localhost", e); //$NON-NLS-1$
113 			} catch (URISyntaxException e) {
114 				throw new RuntimeException("Unexpected URI error on " + uri, e); //$NON-NLS-1$
115 			}
116 
117 			contexts = new ContextHandlerCollection();
118 			server.setHandler(contexts);
119 			server.setConnectors(new Connector[] { connector });
120 		}
121 
122 		/**
123 		 * Create a new servlet context within the server.
124 		 * <p>
125 		 * This method should be invoked before the server is started, once for
126 		 * each context the caller wants to register.
127 		 *
128 		 * @param path
129 		 *            path of the context; use "/" for the root context if
130 		 *            binding to the root is desired.
131 		 * @return the context to add servlets into.
132 		 */
133 		ServletContextHandler addContext(String path) {
134 			assertNotRunning();
135 			if ("".equals(path)) //$NON-NLS-1$
136 				path = "/"; //$NON-NLS-1$
137 
138 			ServletContextHandler ctx = new ServletContextHandler();
139 			ctx.setContextPath(path);
140 			contexts.addHandler(ctx);
141 
142 			return ctx;
143 		}
144 
145 		void start() throws Exception {
146 			server.start();
147 		}
148 
149 		void stop() throws Exception {
150 			server.stop();
151 		}
152 
153 		URI getURI() {
154 			return uri;
155 		}
156 
157 		private void assertNotRunning() {
158 			if (server.isRunning()) {
159 				throw new IllegalStateException("server is running"); //$NON-NLS-1$
160 			}
161 		}
162 	}
163 
164 	private static enum StoreType {
165 		FS, S3;
166 	}
167 
168 	private static enum StorageClass {
169 		REDUCED_REDUNDANCY, STANDARD
170 	}
171 
172 	private static final String OBJECTS = "objects/"; //$NON-NLS-1$
173 
174 	private static final String STORE_PATH = "/" + OBJECTS + "*"; //$NON-NLS-1$//$NON-NLS-2$
175 
176 	private static final String PROTOCOL_PATH = "/lfs/objects/batch"; //$NON-NLS-1$
177 
178 	@Option(name = "--port", aliases = {"-p" },
179 			metaVar = "metaVar_port", usage = "usage_LFSPort")
180 	int port;
181 
182 	@Option(name = "--store", metaVar = "metaVar_lfsStorage", usage = "usage_LFSRunStore")
183 	StoreType storeType;
184 
185 	@Option(name = "--store-url", aliases = {"-u" }, metaVar = "metaVar_url",
186 			usage = "usage_LFSStoreUrl")
187 	String storeUrl;
188 
189 	@Option(name = "--region", aliases = {"-r" },
190 			metaVar = "metaVar_s3Region", usage = "usage_S3Region")
191 	String region; // $NON-NLS-1$
192 
193 	@Option(name = "--bucket", aliases = {"-b" },
194 			metaVar = "metaVar_s3Bucket", usage = "usage_S3Bucket")
195 	String bucket; // $NON-NLS-1$
196 
197 	@Option(name = "--storage-class", aliases = {"-c" },
198 			metaVar = "metaVar_s3StorageClass", usage = "usage_S3StorageClass")
199 	StorageClass storageClass = StorageClass.REDUCED_REDUNDANCY;
200 
201 	@Option(name = "--expire", aliases = {"-e" },
202 			metaVar = "metaVar_seconds", usage = "usage_S3Expiration")
203 	int expirationSeconds = 600;
204 
205 	@Option(name = "--no-ssl-verify", usage = "usage_S3NoSslVerify")
206 	boolean disableSslVerify = false;
207 
208 	@Argument(required = false, metaVar = "metaVar_directory", usage = "usage_LFSDirectory")
209 	String directory;
210 
211 	String protocolUrl;
212 
213 	String accessKey;
214 
215 	String secretKey;
216 
217 	@Override
218 	protected boolean requiresRepository() {
219 		return false;
220 	}
221 
222 	protected void run() throws Exception {
223 		AppServer server = new AppServer(port);
224 		URI baseURI = server.getURI();
225 		ServletContextHandler app = server.addContext("/"); //$NON-NLS-1$
226 
227 		final LargeFileRepository repository;
228 		switch (storeType) {
229 		case FS:
230 			Path dir = Paths.get(directory);
231 			FileLfsRepository fsRepo = new FileLfsRepository(
232 					getStoreUrl(baseURI), dir);
233 			FileLfsServlet content = new FileLfsServlet(fsRepo, 30000);
234 			app.addServlet(new ServletHolder(content), STORE_PATH);
235 			repository = fsRepo;
236 			break;
237 
238 		case S3:
239 			readAWSKeys();
240 			checkOptions();
241 			S3Config config = new S3Config(region, bucket,
242 					storageClass.toString(), accessKey, secretKey,
243 					expirationSeconds, disableSslVerify);
244 			repository = new S3Repository(config);
245 			break;
246 		default:
247 			throw new IllegalArgumentException(MessageFormat
248 					.format(CLIText.get().lfsUnknownStoreType, storeType));
249 		}
250 
251 		LfsProtocolServlet protocol = new LfsProtocolServlet() {
252 
253 			private static final long serialVersionUID = 1L;
254 
255 			@Override
256 			protected LargeFileRepository getLargeFileRepository(
257 					LfsRequest request, String path) {
258 				return repository;
259 			}
260 		};
261 		app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH);
262 
263 		server.start();
264 
265 		outw.println(MessageFormat.format(CLIText.get().lfsProtocolUrl,
266 				getProtocolUrl(baseURI)));
267 		if (storeType == StoreType.FS) {
268 			outw.println(MessageFormat.format(CLIText.get().lfsStoreDirectory,
269 					directory));
270 			outw.println(MessageFormat.format(CLIText.get().lfsStoreUrl,
271 					getStoreUrl(baseURI)));
272 		}
273 	}
274 
275 	private void checkOptions() {
276 		if (bucket == null || bucket.length() == 0) {
277 			throw die(MessageFormat.format(CLIText.get().s3InvalidBucket,
278 					bucket));
279 		}
280 	}
281 
282 	private void readAWSKeys() throws IOException, ConfigInvalidException {
283 		String credentialsPath = System.getProperty("user.home") //$NON-NLS-1$
284 				+ "/.aws/credentials"; //$NON-NLS-1$
285 		FileBasedConfig c = new FileBasedConfig(new File(credentialsPath),
286 				FS.DETECTED);
287 		c.load();
288 		accessKey = c.getString("default", null, "accessKey"); //$NON-NLS-1$//$NON-NLS-2$
289 		secretKey = c.getString("default", null, "secretKey"); //$NON-NLS-1$ //$NON-NLS-2$
290 		if (accessKey == null || accessKey.isEmpty()) {
291 			throw die(MessageFormat.format(CLIText.get().lfsNoAccessKey,
292 					credentialsPath));
293 		}
294 		if (secretKey == null || secretKey.isEmpty()) {
295 			throw die(MessageFormat.format(CLIText.get().lfsNoSecretKey,
296 					credentialsPath));
297 		}
298 	}
299 
300 	private String getStoreUrl(URI baseURI) {
301 		if (storeUrl == null) {
302 			if (storeType == StoreType.FS) {
303 				storeUrl = baseURI + "/" + OBJECTS; //$NON-NLS-1$
304 			} else {
305 				die("Local store not running and no --store-url specified"); //$NON-NLS-1$
306 			}
307 		}
308 		return storeUrl;
309 	}
310 
311 	private String getProtocolUrl(URI baseURI) {
312 		if (protocolUrl == null) {
313 			protocolUrl = baseURI + PROTOCOL_PATH;
314 		}
315 		return protocolUrl;
316 	}
317 }