-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerRestFileUpload.java
More file actions
282 lines (246 loc) · 7.85 KB
/
DockerRestFileUpload.java
File metadata and controls
282 lines (246 loc) · 7.85 KB
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
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* This class allows to upload files into running Docker container via docker rest API (v.18 + required)
*
* Upload is implemented by emulating
*
* docker exec -i ubuntu /bin/bash -c 'cat > file' < file
*
* behavior.
*
* Author: Boris Treukhov
*
* boris@treukhov.com
*
* Based on the HttpHijack class by Nick (missedone) Tan: https://gist.github.com/missedone/76517e618486db746056
* and answer on StackOverlow http://stackoverflow.com/a/24167546/241986
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Scanner;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DockerRestFileUpload {
private String filename;
private String host;
private String path;
private String execId;
private String containerId;
private DockerRestFileUpload(String serverAddress, String containerId,
String filename) {
uri = URI.create(serverAddress);
host = uri.getHost();
path = uri.getPath();
if (path.equals("")) {
path = "/";
}
String query = uri.getQuery();
if (query != null) {
path = path + "?" + query;
}
try {
socket = createSocket(uri);
} catch (IOException e) {
throw new IllegalStateException("Failed to create socket", e);
}
this.containerId = containerId;
this.filename = filename;
}
/**
* Creates an output stream, which redirects its input to the file in the
* docker container.
*
* @param serverAddress
* Address of the server, for example http://localhost:4243
* @param containerId
* Container Id of its leading part, for example 604abc
* @param fileName
* The name of the file, for example /var/log/test
* @return
* @throws IOException
*/
public static OutputStream uploadFile(String serverAddress,
String containerId, String fileName) throws IOException {
final DockerRestFileUpload uploadFile = new DockerRestFileUpload(
serverAddress, containerId, fileName);
boolean needToClose = true;
try {
uploadFile.execCreate();
uploadFile.execStartAndUpgrade();
final OutputStream os = uploadFile.socket.getOutputStream();
try{
return new OutputStream() {
@Override
public void write(int b) throws IOException {
os.write(b);
}
@Override
public void write(byte[] b) throws IOException {
os.write(b);
}
@Override
public void write(byte[] b, int off, int len)
throws IOException {
os.write(b, off, len);
}
@Override
public void close() throws IOException {
try {
os.flush();
}
finally {
uploadFile.close();
}
}
};
}
finally{
needToClose = false;
}
} finally {
if (needToClose)
try {
uploadFile.close();
} catch (IOException e) {
}
}
}
private static Socket createSocket(URI uri) throws java.io.IOException {
String scheme = uri.getScheme();
String host = uri.getHost();
int port = uri.getPort();
if (port == -1) {
if (scheme.equals("https")) {
port = 443;
} else if (scheme.equals("http")) {
port = 80;
} else {
throw new IllegalArgumentException("Unsupported scheme");
}
}
if (scheme.equals("https")) {
SocketFactory factory = SSLSocketFactory.getDefault();
return factory.createSocket(host, port);
} else {
return new Socket(host, port);
}
}
private void execCreate() throws IOException {
///Note to docker-java users - currently it's not able to encode ">" as escape sequence in JSON
///its either ">" or "\\u003e" instead of \u003e which causes inconvenience
String payload = "{\"Detach\": false,\"Tty\": false,\"AttachStdin\" :true, "
+ "\"AttachStdout\" :true,\"AttachStderr\":true,"
+ "\"Cmd\":[\"/bin/bash\", \"-c\","
+ " \"touch '"
+ filename
+ "' && echo 'ok' && false || cat \\" + "u003e '" + filename + "' \"]}";
StringBuffer request = new StringBuffer();
request.append("POST " + path + "containers/" + containerId
+ "/exec" + " HTTP/1.1\r\n");
request.append("Host: " + host + "\r\n");
request.append("Content-Type: application/json\r\n");
request.append("Content-Length: " + payload.length() + "\r\n");
request.append("\r\n");
request.append(payload);
socket.getOutputStream().write(
request.toString().getBytes(Charset.forName("UTF-8")));
socket.getOutputStream().flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String header = reader.readLine();
if (header.contains("404"))
throw new IllegalStateException("Container with ID " + containerId
+ " not found");
String line;
do {
line = reader.readLine();
if (line != null && line.contains("\"Id\":")) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(line).useDelimiter(":");
String s = scanner.next();
if (s.contains("Id")) {
String sid = scanner.next();
this.execId = sid.replaceAll("[\",\\}]*", "");
}
break;
}
} while (line != null);
if (this.execId == null)
throw new IllegalStateException("Failed to start exec");
}
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
DockerRestFileUpload.uploadFile("http://localhost:4243", "c0111a111e",
"/var/log/example.txt")));
try {
bw.write("Mabel and Dipper!\n");
} finally {
bw.close();
}
}
private void execStartAndUpgrade() throws IOException {
String payload = "{\"Detach\": false,\"Tty\": false,\"AttachStdin\" :true, \"AttachStdout\" :true,\"AttachStderr\":true}";
StringBuffer request = new StringBuffer();
request.append("POST " + path + "exec/" + execId + "/start"
+ " HTTP/1.1\r\n");
request.append("Upgrade: tcp\r\n");
request.append("Connection: Upgrade\r\n");
request.append("Host: " + host + "\r\n");
request.append("Content-Type: application/json\r\n");
request.append("Content-Length: " + payload.length() + "\r\n");
request.append("\r\n");
request.append(payload);
socket.getOutputStream().write(
request.toString().getBytes(Charset.forName("UTF-8")));
socket.getOutputStream().flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String header = reader.readLine();
if (!header.equals("HTTP/1.1 101 UPGRADED")) {
throw new IOException("Invalid handshake response: " + header);
}
do {
header = reader.readLine();
log.info("header: {}", header);
} while (!header.equals(""));
String ok = reader.readLine();
if (!ok.trim().equals("ok")) {
throw new IllegalStateException("Unexpected result of touch. " + ok);
}
}
private static final Logger log = LoggerFactory.getLogger(DockerRestFileUpload.class);
private URI uri;
public Socket socket;
private DockerRestFileUpload(URI url) {
uri = url;
}
public void close() throws java.io.IOException {
socket.close();
}
}