Skip to content

Commit

Permalink
feat: proxies can now be set using HTTPS_PROXY
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse committed Sep 13, 2024
1 parent f314074 commit 08d09a5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.redhat.insights.InsightsErrorCode.ERROR_IDENTIFICATION_NOT_DEFINED;

import com.redhat.insights.InsightsException;
import java.net.URI;
import java.time.Duration;
import java.util.Optional;

Expand All @@ -24,6 +25,7 @@ public class EnvAndSysPropsInsightsConfiguration extends DefaultInsightsConfigur
public static final String ENV_ARCHIVE_UPLOAD_DIR = "RHT_INSIGHTS_JAVA_ARCHIVE_UPLOAD_DIR";
public static final String ENV_PROXY_HOST = "RHT_INSIGHTS_JAVA_PROXY_HOST";
public static final String ENV_PROXY_PORT = "RHT_INSIGHTS_JAVA_PROXY_PORT";
public static final String ENV_HTTPS_PROXY = "HTTPS_PROXY";
public static final String ENV_OPT_OUT = "RHT_INSIGHTS_JAVA_OPT_OUT";
public static final String ENV_CONNECT_PERIOD = "RHT_INSIGHTS_JAVA_CONNECT_PERIOD";
public static final String ENV_UPDATE_PERIOD = "RHT_INSIGHTS_JAVA_UPDATE_PERIOD";
Expand Down Expand Up @@ -116,7 +118,13 @@ public Optional<ProxyConfiguration> getProxyConfiguration() {
String host = lookup(ENV_PROXY_HOST);
String port = lookup(ENV_PROXY_PORT);
if (host == null || port == null) {
return Optional.empty();
String httpsProxy = lookup(ENV_HTTPS_PROXY);
if (httpsProxy == null) {
return Optional.empty();
}
URI proxyUri = URI.create(httpsProxy);
host = proxyUri.getHost();
port = Integer.toString(proxyUri.getPort());
}
return Optional.of(new ProxyConfiguration(host, Integer.parseUnsignedInt(port)));
}
Expand Down
13 changes: 13 additions & 0 deletions api/src/main/java/com/redhat/insights/http/BackoffWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public interface Action {
void run() throws Throwable;
}

public static class FatalBackoffException extends RuntimeException {
public FatalBackoffException(String message, Throwable cause) {
super(message, cause);
}
}

private final InsightsLogger logger;
private final long initialDelay;
private final double factor;
Expand Down Expand Up @@ -53,6 +59,13 @@ public int run() {
action.run();
return count;
} catch (Throwable err) {
if (err instanceof FatalBackoffException) {
InsightsException fatalErr =
new InsightsException(
ERROR_CLIENT_BACKOFF_RETRIES_FAILED, "Exponential backoff fatal error");
fatalErr.addSuppressed(err);
throw fatalErr;
}
if (retryFailure == null) {
retryFailure =
new InsightsException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.configuration;

import static com.redhat.insights.config.EnvAndSysPropsInsightsConfiguration.*;
Expand Down Expand Up @@ -98,6 +98,33 @@ void testGetProxyConfiguration() {
"Configuration does not contain value passed through environment variable.");
}

@Test
void testGetProxyConfigurationWithUnusedEnv() {
environmentVariables.set(ENV_HTTPS_PROXY, "https://env-https-proxy-host:54321");
assertEquals(
"env-proxy-host",
config.getProxyConfiguration().get().getHost(),
"Configuration does not contain value passed through environment variable.");
assertEquals(
12345,
config.getProxyConfiguration().get().getPort(),
"Configuration does not contain value passed through environment variable.");
}

@Test
void testGetProxyConfigurationAlternative() {
environmentVariables.remove(ENV_PROXY_HOST).remove(ENV_PROXY_PORT);
environmentVariables.set(ENV_HTTPS_PROXY, "https://env-https-proxy-host:54321");
assertEquals(
"env-https-proxy-host",
config.getProxyConfiguration().get().getHost(),
"Configuration does not contain value passed through environment variable.");
assertEquals(
54321,
config.getProxyConfiguration().get().getPort(),
"Configuration does not contain value passed through environment variable.");
}

@Test
void testIsOptingOut() {
assertEquals(
Expand Down

0 comments on commit 08d09a5

Please sign in to comment.