Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions dsl/camel-jbang/camel-launcher/src/install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ manifest_base_url="${CAMEL_INSTALL_MANIFEST_BASE_URL:-https://camel.apache.org/c
maven_base_url="${CAMEL_INSTALL_MAVEN_BASE_URL:-https://repo1.maven.org/maven2/org/apache/camel/camel-launcher}"
snapshot_base_url="${CAMEL_INSTALL_SNAPSHOT_BASE_URL:-https://repository.apache.org/content/repositories/snapshots/org/apache/camel/camel-launcher}"
ca_cert="${CAMEL_INSTALL_CA_CERT:-}"
stall_timeout="${CAMEL_INSTALL_STALL_TIMEOUT:-60}"

# A download gives up when it cannot connect within 30 seconds, or when it gets almost nothing (under 1 KB/s) for
# $stall_timeout seconds; a slow but working download of the archive (over 100 MB) is not cut off by a total time.
connect_timeout=30

data_root="${XDG_DATA_HOME:-$HOME/.local/share}/camel-cli/versions"
camel_cli_root="${XDG_DATA_HOME:-$HOME/.local/share}/camel-cli"
Expand Down Expand Up @@ -138,15 +143,20 @@ fetch() {
outfile="$2"
if command -v curl >/dev/null 2>&1; then
if [ -n "$ca_cert" ]; then
curl -fsSL --cacert "$ca_cert" -o "$outfile" "$url"
curl -fsSL --connect-timeout "$connect_timeout" --speed-limit 1024 --speed-time "$stall_timeout" \
--cacert "$ca_cert" -o "$outfile" "$url"
else
curl -fsSL -o "$outfile" "$url"
curl -fsSL --connect-timeout "$connect_timeout" --speed-limit 1024 --speed-time "$stall_timeout" \
-o "$outfile" "$url"
fi
elif command -v wget >/dev/null 2>&1; then
# wget tries 20 times by default, each waiting 900 seconds for data
if [ -n "$ca_cert" ]; then
wget -nv --ca-certificate="$ca_cert" -O "$outfile" "$url"
wget -nv --connect-timeout="$connect_timeout" --read-timeout="$stall_timeout" --tries=2 \
--ca-certificate="$ca_cert" -O "$outfile" "$url"
else
wget -nv -O "$outfile" "$url"
wget -nv --connect-timeout="$connect_timeout" --read-timeout="$stall_timeout" --tries=2 \
-O "$outfile" "$url"
fi
else
fail "curl or wget is required to install the Camel CLI"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -36,6 +37,8 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -177,6 +180,21 @@ static void serveTruncatedThenClose(ServerSocket rawServer, byte[] fullBody) {
}
}

// the headers and half of the body, and then nothing more, while the connection stays open
static void serveHalfThenStall(ServerSocket rawServer, byte[] fullBody, CountDownLatch release) {
try (Socket client = rawServer.accept()) {
String headers = "HTTP/1.1 200 OK\r\nContent-Length: " + fullBody.length + "\r\nConnection: close\r\n\r\n";
OutputStream out = client.getOutputStream();
out.write(headers.getBytes(StandardCharsets.US_ASCII));
out.write(fullBody, 0, fullBody.length / 2);
out.flush();
release.await(60, TimeUnit.SECONDS);
} catch (IOException ignored) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

static Stream<Arguments> invalidManifests() {
String hash = "a".repeat(64);
return Stream.of(
Expand Down Expand Up @@ -1103,6 +1121,39 @@ void rejectsInterruptedDownload(@TempDir Path temp) throws Exception {
}
}

@Test
void givesUpOnAStalledDownload(@TempDir Path temp) throws Exception {
CountDownLatch release = new CountDownLatch(1);
try (WebsiteInstallerFixture fixture = WebsiteInstallerFixture.start(temp.resolve("fixture"));
ServerSocket rawServer = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
Path home = Files.createDirectory(temp.resolve("home"));
Path tar = fixture.safeTar("1.0.0");
Path zip = fixture.safeZip("1.0.0");
fixture.publishManifest("/camel-cli/releases/latest.properties", "1.0.0", tar, zip);

byte[] tarBytes = Files.readAllBytes(tar);
Thread server = new Thread(() -> serveHalfThenStall(rawServer, tarBytes, release));
server.setDaemon(true);
server.start();

Map<String, String> env = new HashMap<>(fixture.environment(home));
env.put("CAMEL_INSTALL_MAVEN_BASE_URL", "http://127.0.0.1:" + rawServer.getLocalPort());
// the download gives up after 2 seconds without data, instead of 60
env.put("CAMEL_INSTALL_STALL_TIMEOUT", "2");

long start = System.nanoTime();
WebsiteInstallerFixture.Result r = fixture.run(List.of("/bin/sh", SCRIPT.toString()), env);

// CAMEL-27004: curl waited for the rest of the body for as long as the server kept the connection open
assertNotEquals(0, r.exit());
assertTrue(Duration.ofNanos(System.nanoTime() - start).compareTo(Duration.ofSeconds(30)) < 0,
"the installer gave up on the stalled download");
assertFalse(Files.exists(home.resolve(".local/bin/camel")));
} finally {
release.countDown();
}
}

@Test
void rejectsWhenChecksumToolMissing(@TempDir Path temp) throws Exception {
try (WebsiteInstallerFixture fixture = WebsiteInstallerFixture.start(temp.resolve("fixture"))) {
Expand Down
Loading