Skip to content

Commit

Permalink
feat: proxies can now be set using HTTPS_PROXY (#261)
Browse files Browse the repository at this point in the history
Fixes MWTELE-42
  • Loading branch information
quintesse authored Sep 25, 2024
1 parent c742557 commit 0266b14
Show file tree
Hide file tree
Showing 2 changed files with 37 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;
import org.jspecify.annotations.NullMarked;
Expand All @@ -26,6 +27,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 @@ -118,7 +120,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
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 0266b14

Please sign in to comment.