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
|
/*
* SonarQube
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.bootstrap;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Optional;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import okio.Buffer;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.scanner.bootstrap.ScannerPluginInstaller.InstalledPlugin;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClientFactories;
import static org.apache.commons.io.FileUtils.moveFile;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.ThrowableAssert.ThrowingCallable;
public class PluginFilesTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public MockWebServer server = new MockWebServer();
private File userHome;
private PluginFiles underTest;
@Before
public void setUp() throws Exception {
HttpConnector connector = HttpConnector.newBuilder().url(server.url("/").toString()).build();
GlobalAnalysisMode analysisMode = new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()));
DefaultScannerWsClient wsClient = new DefaultScannerWsClient(WsClientFactories.getDefault().newClient(connector), false, analysisMode);
userHome = temp.newFolder();
MapSettings settings = new MapSettings();
settings.setProperty("sonar.userHome", userHome.getAbsolutePath());
underTest = new PluginFiles(wsClient, settings.asConfig());
}
@Test
public void get_jar_from_cache_if_present() throws Exception {
FileAndMd5 jar = createFileInCache("foo");
File result = underTest.get(newInstalledPlugin("foo", jar.md5)).get();
verifySameContent(result, jar);
// no requests to server
assertThat(server.getRequestCount()).isZero();
}
@Test
public void download_and_add_jar_to_cache_if_missing() throws Exception {
FileAndMd5 tempJar = new FileAndMd5();
enqueueDownload(tempJar);
InstalledPlugin plugin = newInstalledPlugin("foo", tempJar.md5);
File result = underTest.get(plugin).get();
verifySameContent(result, tempJar);
HttpUrl requestedUrl = server.takeRequest().getRequestUrl();
assertThat(requestedUrl.encodedPath()).isEqualTo("/api/plugins/download");
assertThat(requestedUrl.encodedQuery()).isEqualTo("plugin=foo&acceptCompressions=pack200");
// get from cache on second call
result = underTest.get(plugin).get();
verifySameContent(result, tempJar);
assertThat(server.getRequestCount()).isOne();
}
@Test
public void download_compressed_and_add_uncompressed_to_cache_if_missing() throws Exception {
FileAndMd5 jar = new FileAndMd5();
enqueueCompressedDownload(jar, true);
InstalledPlugin plugin = newInstalledPlugin("foo", jar.md5);
File result = underTest.get(plugin).get();
verifySameContentAfterCompression(jar.file, result);
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getRequestUrl().queryParameter("acceptCompressions")).isEqualTo("pack200");
// get from cache on second call
result = underTest.get(plugin).get();
verifySameContentAfterCompression(jar.file, result);
assertThat(server.getRequestCount()).isOne();
}
@Test
public void return_empty_if_plugin_not_found_on_server() {
server.enqueue(new MockResponse().setResponseCode(404));
InstalledPlugin plugin = newInstalledPlugin("foo", "abc");
Optional<File> result = underTest.get(plugin);
assertThat(result).isEmpty();
}
@Test
public void fail_if_integrity_of_download_is_not_valid() throws IOException {
FileAndMd5 tempJar = new FileAndMd5();
enqueueDownload(tempJar.file, "invalid_hash");
InstalledPlugin plugin = newInstalledPlugin("foo", "abc");
expectISE("foo", "was expected to have checksum invalid_hash but had " + tempJar.md5,
() -> underTest.get(plugin));
}
@Test
public void fail_if_integrity_of_compressed_download_is_not_valid() throws Exception {
FileAndMd5 jar = new FileAndMd5();
enqueueCompressedDownload(jar, false);
InstalledPlugin plugin = newInstalledPlugin("foo", jar.md5);
expectISE("foo", "was expected to have checksum invalid_hash but had ", () -> underTest.get(plugin).get());
}
@Test
public void fail_if_md5_header_is_missing_from_response() throws IOException {
File tempJar = temp.newFile();
enqueueDownload(tempJar, null);
InstalledPlugin plugin = newInstalledPlugin("foo", "abc");
expectISE("foo", "did not return header Sonar-MD5", () -> underTest.get(plugin));
}
@Test
public void fail_if_compressed_download_cannot_be_uncompressed() {
MockResponse response = new MockResponse().setBody("not binary");
response.setHeader("Sonar-MD5", DigestUtils.md5Hex("not binary"));
response.setHeader("Sonar-UncompressedMD5", "abc");
response.setHeader("Sonar-Compression", "pack200");
server.enqueue(response);
InstalledPlugin plugin = newInstalledPlugin("foo", "abc");
expectISE("foo", "Pack200 error", () -> underTest.get(plugin).get());
}
@Test
public void fail_if_server_returns_error() {
server.enqueue(new MockResponse().setResponseCode(500));
InstalledPlugin plugin = newInstalledPlugin("foo", "abc");
expectISE("foo", "returned code 500", () -> underTest.get(plugin));
}
@Test
public void download_a_new_version_of_plugin_during_blue_green_switch() throws IOException {
FileAndMd5 tempJar = new FileAndMd5();
enqueueDownload(tempJar);
// expecting to download plugin foo with checksum "abc"
InstalledPlugin pluginV1 = newInstalledPlugin("foo", "abc");
File result = underTest.get(pluginV1).get();
verifySameContent(result, tempJar);
// new version of downloaded jar is put in cache with the new md5
InstalledPlugin pluginV2 = newInstalledPlugin("foo", tempJar.md5);
result = underTest.get(pluginV2).get();
verifySameContent(result, tempJar);
assertThat(server.getRequestCount()).isOne();
// v1 still requests server and downloads v2
enqueueDownload(tempJar);
result = underTest.get(pluginV1).get();
verifySameContent(result, tempJar);
assertThat(server.getRequestCount()).isEqualTo(2);
}
@Test
public void fail_if_cached_file_is_outside_cache_dir() throws IOException {
FileAndMd5 tempJar = new FileAndMd5();
enqueueDownload(tempJar);
InstalledPlugin plugin = newInstalledPlugin("foo/bar", "abc");
assertThatThrownBy(() -> underTest.get(plugin))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Fail to download plugin [foo/bar]. Key is not valid.");
}
private FileAndMd5 createFileInCache(String pluginKey) throws IOException {
FileAndMd5 tempFile = new FileAndMd5();
return moveToCache(pluginKey, tempFile);
}
private FileAndMd5 moveToCache(String pluginKey, FileAndMd5 jar) throws IOException {
File jarInCache = new File(userHome, "cache/" + jar.md5 + "/sonar-" + pluginKey + "-plugin.jar");
moveFile(jar.file, jarInCache);
return new FileAndMd5(jarInCache, jar.md5);
}
/**
* Enqueue download of file with valid MD5
*/
private void enqueueDownload(FileAndMd5 file) throws IOException {
enqueueDownload(file.file, file.md5);
}
/**
* Enqueue download of file with a MD5 that may not be returned (null) or not valid
*/
private void enqueueDownload(File file, @Nullable String md5) throws IOException {
Buffer body = new Buffer();
body.write(FileUtils.readFileToByteArray(file));
MockResponse response = new MockResponse().setBody(body);
if (md5 != null) {
response.setHeader("Sonar-MD5", md5);
}
server.enqueue(response);
}
/**
* Enqueue download of file with a MD5 that may not be returned (null) or not valid
*/
private void enqueueCompressedDownload(FileAndMd5 jar, boolean validMd5) throws IOException {
Buffer body = new Buffer();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(jar.file.toPath())));
OutputStream output = new GZIPOutputStream(new BufferedOutputStream(bytes))) {
Pack200.newPacker().pack(in, output);
}
body.write(bytes.toByteArray());
MockResponse response = new MockResponse().setBody(body);
response.setHeader("Sonar-MD5", validMd5 ? DigestUtils.md5Hex(bytes.toByteArray()) : "invalid_hash");
response.setHeader("Sonar-UncompressedMD5", jar.md5);
response.setHeader("Sonar-Compression", "pack200");
server.enqueue(response);
}
private static InstalledPlugin newInstalledPlugin(String pluginKey, String fileChecksum) {
InstalledPlugin plugin = new InstalledPlugin();
plugin.key = pluginKey;
plugin.hash = fileChecksum;
return plugin;
}
private static void verifySameContent(File file1, FileAndMd5 file2) {
assertThat(file1).isFile().exists();
assertThat(file2.file).isFile().exists();
assertThat(file1).hasSameContentAs(file2.file);
}
/**
* Packing and unpacking a JAR generates a different file.
*/
private void verifySameContentAfterCompression(File file1, File file2) throws IOException {
assertThat(file1).isFile().exists();
assertThat(file2).isFile().exists();
assertThat(packAndUnpackJar(file1)).hasSameContentAs(packAndUnpackJar(file2));
}
private File packAndUnpackJar(File source) throws IOException {
File packed = temp.newFile();
try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(source.toPath())));
OutputStream out = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(packed.toPath())))) {
Pack200.newPacker().pack(in, out);
}
File to = temp.newFile();
try (InputStream input = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(packed.toPath())));
JarOutputStream output = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(to.toPath())))) {
Pack200.newUnpacker().unpack(input, output);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return to;
}
private void expectISE(String pluginKey, String message, ThrowingCallable shouldRaiseThrowable) {
assertThatThrownBy(shouldRaiseThrowable)
.isInstanceOf(IllegalStateException.class)
.hasMessageStartingWith("Fail to download plugin [" + pluginKey + "]")
.hasMessageContaining(message);
}
private class FileAndMd5 {
private final File file;
private final String md5;
FileAndMd5(File file, String md5) {
this.file = file;
this.md5 = md5;
}
FileAndMd5() throws IOException {
this.file = temp.newFile();
FileUtils.write(this.file, RandomStringUtils.random(3));
try (InputStream fis = FileUtils.openInputStream(this.file)) {
this.md5 = DigestUtils.md5Hex(fis);
} catch (IOException e) {
throw new IllegalStateException("Fail to compute md5 of " + this.file, e);
}
}
}
}
|