Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid proxy URLs #331

Merged
merged 2 commits into from
Jan 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import software.aws.toolkits.eclipse.amazonq.providers.LspManagerProvider;
import software.aws.toolkits.eclipse.amazonq.telemetry.LanguageServerTelemetryProvider;
import software.aws.toolkits.eclipse.amazonq.telemetry.metadata.ExceptionMetadata;
import software.aws.toolkits.eclipse.amazonq.util.ProxyUtil;
import software.aws.toolkits.telemetry.TelemetryDefinitions.Result;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
import software.aws.toolkits.eclipse.amazonq.preferences.AmazonQPreferencePage;
Expand All @@ -43,7 +44,7 @@ public QLspConnectionProvider() throws IOException {

@Override
protected final void addEnvironmentVariables(final Map<String, String> env) {
String httpsProxyPreference = Activator.getDefault().getPreferenceStore().getString(AmazonQPreferencePage.HTTPS_PROXY);
String httpsProxyPreference = ProxyUtil.getHttpsProxyUrl();
String caCertPreference = Activator.getDefault().getPreferenceStore().getString(AmazonQPreferencePage.CA_CERT);
if (!StringUtils.isEmpty(httpsProxyPreference)) {
env.put("HTTPS_PROXY", httpsProxyPreference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private void createHttpsProxyField() {
addField(httpsProxy);
createLabel("""
Sets the address of the proxy to use for all HTTPS connections. \
For example "http://localhost:8888". \
Leave blank if not using a proxy.
Eclipse restart required to take effect.
""", 20, getFieldEditorParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ private Constants() {
public static final String AUTHENTICATE_FAILURE_MESSAGE = "An error occurred while attempting to authenticate. Please try again.";
public static final String IDE_SSL_HANDSHAKE_TITLE = "SSL Handshake Error";
public static final String IDE_SSL_HANDSHAKE_BODY = "The plugin encountered an SSL handshake error. If using a proxy, check the plugin preferences.";
public static final String INVALID_PROXY_CONFIGURATION_TITLE = "Proxy Configuration Error";
public static final String INVALID_PROXY_CONFIGURATION_BODY = "The provided proxy URL is invalid. Please check your plugin configuration.";

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
package software.aws.toolkits.eclipse.amazonq.util;

import java.io.FileInputStream;
import java.net.URI;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.eclipse.mylyn.commons.ui.dialogs.AbstractNotificationPopup;
import org.eclipse.swt.widgets.Display;

import software.amazon.awssdk.utils.StringUtils;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
import software.aws.toolkits.eclipse.amazonq.preferences.AmazonQPreferencePage;

public final class ProxyUtil {

private static boolean hasSeenInvalidProxyNotification;

private ProxyUtil() {
// Prevent initialization
}
Expand All @@ -30,6 +37,22 @@ protected static String getHttpsProxyUrl(final String envVarValue, final String
if (!StringUtils.isEmpty(prefValue)) {
httpsProxy = prefValue;
}
try {
if (!StringUtils.isEmpty(httpsProxy)) {
URI.create(httpsProxy);
}
} catch (IllegalArgumentException e) {
if (!hasSeenInvalidProxyNotification) {
hasSeenInvalidProxyNotification = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user sees this the first time, closes the display. Goes back and changes the proxy url but still does fix the format correctly, will they not see this popup then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point. We don't currently evaluate or attempt to use the proxy URL until the IDE is restarted. This is an overall area for improvement - ideally we can hot swap to the new setting without the need for a restart. The current logic works within our existing constraints. Input validation would also help address this. I'll create a backlog item.

Display.getDefault().asyncExec(() -> {
AbstractNotificationPopup notification = new ToolkitNotification(Display.getCurrent(),
Constants.INVALID_PROXY_CONFIGURATION_TITLE,
Constants.INVALID_PROXY_CONFIGURATION_BODY);
notification.open();
});
}
return null;
}
return httpsProxy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
package software.aws.toolkits.eclipse.amazonq.util;

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

import org.eclipse.swt.widgets.Display;

class ProxyUtilTest {

Expand All @@ -31,4 +37,24 @@ void testPreferenceProxyUrlPrecedence() {
assertEquals(mockUrl, ProxyUtil.getHttpsProxyUrl("http://bar.com:8888", mockUrl));
}

@Test
void testEnvVarInvalidProxyUrl() {
try (MockedStatic<Display> displayMock = mockStatic(Display.class)) {
Display mockDisplay = mock(Display.class);
displayMock.when(Display::getDefault).thenReturn(mockDisplay);
String mockUrl = "127.0.0.1:8000";
assertEquals(null, ProxyUtil.getHttpsProxyUrl(mockUrl, null));
}
}

@Test
void testPreferenceInvalidProxyUrl() {
try (MockedStatic<Display> displayMock = mockStatic(Display.class)) {
Display mockDisplay = mock(Display.class);
displayMock.when(Display::getDefault).thenReturn(mockDisplay);
String mockUrl = "127.0.0.1:8000";
assertEquals(null, ProxyUtil.getHttpsProxyUrl(null, mockUrl));
}
}

}
Loading