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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.transport;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import org.eclipse.jgit.errors.NotSupportedException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdRef;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Ref.Storage;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.SymbolicRef;
/**
* Transport over the non-Git aware Amazon S3 protocol.
* <p>
* This transport communicates with the Amazon S3 servers (a non-free commercial
* hosting service that users must subscribe to). Some users may find transport
* to and from S3 to be a useful backup service.
* <p>
* The transport does not require any specialized Git support on the remote
* (server side) repository, as Amazon does not provide any such support.
* Repository files are retrieved directly through the S3 API, which uses
* extended HTTP/1.1 semantics. This make it possible to read or write Git data
* from a remote repository that is stored on S3.
* <p>
* Unlike the HTTP variant (see
* {@link org.eclipse.jgit.transport.TransportHttp}) we rely upon being able to
* list objects in a bucket, as the S3 API supports this function. By listing
* the bucket contents we can avoid relying on <code>objects/info/packs</code>
* or <code>info/refs</code> in the remote repository.
* <p>
* Concurrent pushing over this transport is not supported. Multiple concurrent
* push operations may cause confusion in the repository state.
*
* @see WalkFetchConnection
* @see WalkPushConnection
*/
public class TransportAmazonS3 extends HttpTransport implements WalkTransport {
static final String S3_SCHEME = "amazon-s3"; //$NON-NLS-1$
static final TransportProtocol PROTO_S3 = new TransportProtocol() {
@Override
public String getName() {
return "Amazon S3"; //$NON-NLS-1$
}
@Override
public Set<String> getSchemes() {
return Collections.singleton(S3_SCHEME);
}
@Override
public Set<URIishField> getRequiredFields() {
return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
URIishField.HOST, URIishField.PATH));
}
@Override
public Set<URIishField> getOptionalFields() {
return Collections.unmodifiableSet(EnumSet.of(URIishField.PASS));
}
@Override
public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException {
return new TransportAmazonS3(local, uri);
}
};
/** User information necessary to connect to S3. */
final AmazonS3 s3;
/** Bucket the remote repository is stored in. */
final String bucket;
/**
* Key prefix which all objects related to the repository start with.
* <p>
* The prefix does not start with "/".
* <p>
* The prefix does not end with "/". The trailing slash is stripped during
* the constructor if a trailing slash was supplied in the URIish.
* <p>
* All files within the remote repository start with
* <code>keyPrefix + "/"</code>.
*/
private final String keyPrefix;
TransportAmazonS3(final Repository local, final URIish uri)
throws NotSupportedException {
super(local, uri);
Properties props = loadProperties();
File directory = local.getDirectory();
if (!props.containsKey("tmpdir") && directory != null) //$NON-NLS-1$
props.put("tmpdir", directory.getPath()); //$NON-NLS-1$
s3 = new AmazonS3(props);
bucket = uri.getHost();
String p = uri.getPath();
if (p.startsWith("/")) //$NON-NLS-1$
p = p.substring(1);
if (p.endsWith("/")) //$NON-NLS-1$
p = p.substring(0, p.length() - 1);
keyPrefix = p;
}
private Properties loadProperties() throws NotSupportedException {
if (local.getDirectory() != null) {
File propsFile = new File(local.getDirectory(), uri.getUser());
if (propsFile.isFile())
return loadPropertiesFile(propsFile);
}
File propsFile = new File(local.getFS().userHome(), uri.getUser());
if (propsFile.isFile())
return loadPropertiesFile(propsFile);
Properties props = new Properties();
String user = uri.getUser();
String pass = uri.getPass();
if (user != null && pass != null) {
props.setProperty("accesskey", user); //$NON-NLS-1$
props.setProperty("secretkey", pass); //$NON-NLS-1$
} else
throw new NotSupportedException(MessageFormat.format(
JGitText.get().cannotReadFile, propsFile));
return props;
}
private static Properties loadPropertiesFile(File propsFile)
throws NotSupportedException {
try {
return AmazonS3.properties(propsFile);
} catch (IOException e) {
throw new NotSupportedException(MessageFormat.format(
JGitText.get().cannotReadFile, propsFile), e);
}
}
@Override
public FetchConnection openFetch() throws TransportException {
final DatabaseS3 c = new DatabaseS3(bucket, keyPrefix + "/objects"); //$NON-NLS-1$
final WalkFetchConnection r = new WalkFetchConnection(this, c);
r.available(c.readAdvertisedRefs());
return r;
}
@Override
public PushConnection openPush() throws TransportException {
final DatabaseS3 c = new DatabaseS3(bucket, keyPrefix + "/objects"); //$NON-NLS-1$
final WalkPushConnection r = new WalkPushConnection(this, c);
r.available(c.readAdvertisedRefs());
return r;
}
@Override
public void close() {
// No explicit connections are maintained.
}
class DatabaseS3 extends WalkRemoteObjectDatabase {
private final String bucketName;
private final String objectsKey;
DatabaseS3(final String b, final String o) {
bucketName = b;
objectsKey = o;
}
private String resolveKey(String subpath) {
if (subpath.endsWith("/")) //$NON-NLS-1$
subpath = subpath.substring(0, subpath.length() - 1);
String k = objectsKey;
while (subpath.startsWith(ROOT_DIR)) {
k = k.substring(0, k.lastIndexOf('/'));
subpath = subpath.substring(3);
}
return k + "/" + subpath; //$NON-NLS-1$
}
@Override
URIish getURI() {
URIish u = new URIish();
u = u.setScheme(S3_SCHEME);
u = u.setHost(bucketName);
u = u.setPath("/" + objectsKey); //$NON-NLS-1$
return u;
}
@Override
Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
try {
return readAlternates(Constants.INFO_ALTERNATES);
} catch (FileNotFoundException err) {
// Fall through.
}
return null;
}
@Override
WalkRemoteObjectDatabase openAlternate(String location)
throws IOException {
return new DatabaseS3(bucketName, resolveKey(location));
}
@Override
Collection<String> getPackNames() throws IOException {
// s3.list returns most recently modified packs first.
// These are the packs most likely to contain missing refs.
final List<String> packList = s3.list(bucket, resolveKey("pack")); //$NON-NLS-1$
final HashSet<String> have = new HashSet<>();
have.addAll(packList);
final Collection<String> packs = new ArrayList<>();
for (String n : packList) {
if (!n.startsWith("pack-") || !n.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
continue;
final String in = n.substring(0, n.length() - 5) + ".idx"; //$NON-NLS-1$
if (have.contains(in))
packs.add(n);
}
return packs;
}
@Override
FileStream open(String path) throws IOException {
final URLConnection c = s3.get(bucket, resolveKey(path));
final InputStream raw = c.getInputStream();
final InputStream in = s3.decrypt(c);
final int len = c.getContentLength();
return new FileStream(in, raw == in ? len : -1);
}
@Override
void deleteFile(String path) throws IOException {
s3.delete(bucket, resolveKey(path));
}
@Override
OutputStream writeFile(final String path,
final ProgressMonitor monitor, final String monitorTask)
throws IOException {
return s3.beginPut(bucket, resolveKey(path), monitor, monitorTask);
}
@Override
void writeFile(String path, byte[] data) throws IOException {
s3.put(bucket, resolveKey(path), data);
}
Map<String, Ref> readAdvertisedRefs() throws TransportException {
final TreeMap<String, Ref> avail = new TreeMap<>();
readPackedRefs(avail);
readLooseRefs(avail);
readRef(avail, Constants.HEAD);
return avail;
}
private void readLooseRefs(TreeMap<String, Ref> avail)
throws TransportException {
try {
for (final String n : s3.list(bucket, resolveKey(ROOT_DIR
+ "refs"))) //$NON-NLS-1$
readRef(avail, "refs/" + n); //$NON-NLS-1$
} catch (IOException e) {
throw new TransportException(getURI(), JGitText.get().cannotListRefs, e);
}
}
private Ref readRef(TreeMap<String, Ref> avail, String rn)
throws TransportException {
final String s;
String ref = ROOT_DIR + rn;
try {
try (BufferedReader br = openReader(ref)) {
s = br.readLine();
}
} catch (FileNotFoundException noRef) {
return null;
} catch (IOException err) {
throw new TransportException(getURI(), MessageFormat.format(
JGitText.get().transportExceptionReadRef, ref), err);
}
if (s == null)
throw new TransportException(getURI(), MessageFormat.format(JGitText.get().transportExceptionEmptyRef, rn));
if (s.startsWith("ref: ")) { //$NON-NLS-1$
final String target = s.substring("ref: ".length()); //$NON-NLS-1$
Ref r = avail.get(target);
if (r == null)
r = readRef(avail, target);
if (r == null)
r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
r = new SymbolicRef(rn, r);
avail.put(r.getName(), r);
return r;
}
if (ObjectId.isId(s)) {
final Ref r = new ObjectIdRef.Unpeeled(loose(avail.get(rn)),
rn, ObjectId.fromString(s));
avail.put(r.getName(), r);
return r;
}
throw new TransportException(getURI(), MessageFormat.format(JGitText.get().transportExceptionBadRef, rn, s));
}
private Storage loose(Ref r) {
if (r != null && r.getStorage() == Storage.PACKED)
return Storage.LOOSE_PACKED;
return Storage.LOOSE;
}
@Override
void close() {
// We do not maintain persistent connections.
}
}
}
|