From 67c40ac0d9197d84642ab6d1295fda4b6ad64481 Mon Sep 17 00:00:00 2001 From: Ramon Leon Date: Tue, 24 Nov 2020 16:34:16 +0100 Subject: [PATCH 1/2] [JENKINS-60482] Improve alternate ec2 point checks --- .../hudson/plugins/ec2/AmazonEC2Cloud.java | 20 ++++++++++++++++--- .../hudson/plugins/ec2/Messages.properties | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java b/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java index 5d990f2d8..e67ad6826 100644 --- a/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java @@ -38,6 +38,8 @@ import java.net.URL; import java.util.List; import java.util.Locale; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.annotation.Nullable; import javax.servlet.ServletException; @@ -59,6 +61,8 @@ * @author Kohsuke Kawaguchi */ public class AmazonEC2Cloud extends EC2Cloud { + private final static Logger LOGGER = Logger.getLogger(AmazonEC2Cloud.class.getName()); + /** * Represents the region. Can be null for backward compatibility reasons. */ @@ -178,6 +182,15 @@ public FormValidation doCheckCloudName(@QueryParameter String value) { return FormValidation.ok(); } + public FormValidation doCheckAltEC2Endpoint(@QueryParameter String value) { + try { + new URL(value); + } catch (MalformedURLException ignored) { + return FormValidation.error(Messages.AmazonEC2Cloud_MalformedUrl()); + } + return FormValidation.ok(); + } + @RequirePOST public ListBoxModel doFillRegionItems( @QueryParameter String altEC2Endpoint, @@ -209,11 +222,12 @@ public ListBoxModel doFillRegionItems( // value if not specified. @VisibleForTesting URL determineEC2EndpointURL(@Nullable String altEC2Endpoint) throws MalformedURLException { - if (Util.fixEmpty(altEC2Endpoint) == null) { + try { + return new URL(altEC2Endpoint); + } catch (MalformedURLException e) { + LOGGER.log(Level.WARNING, "The alternate EC2 endpoint is malformed ({0}). Using the default endpoint ({1})", new Object[]{altEC2Endpoint, DEFAULT_EC2_ENDPOINT}); return new URL(DEFAULT_EC2_ENDPOINT); } - - return new URL(altEC2Endpoint); } @RequirePOST diff --git a/src/main/resources/hudson/plugins/ec2/Messages.properties b/src/main/resources/hudson/plugins/ec2/Messages.properties index c17b48acb..0152a90c3 100644 --- a/src/main/resources/hudson/plugins/ec2/Messages.properties +++ b/src/main/resources/hudson/plugins/ec2/Messages.properties @@ -9,5 +9,5 @@ EC2SpotSlave.Spot1=Spot $ EC2SpotSlave.Spot2= max bid price AmazonEC2Cloud.NonUniqName=Cloud name must be unique across EC2 clouds - +AmazonEC2Cloud.MalformedUrl=The URL is malformed. The default endpoint will be used General.MissingPermission=You do not have the Overall/Administer right to modify this field From fe4fa4b905ff76136b752834af3ebf0e6789154b Mon Sep 17 00:00:00 2001 From: Ramon Leon Date: Tue, 24 Nov 2020 16:47:40 +0100 Subject: [PATCH 2/2] [JENKINS-60482] Allow empty alternate ec2 endpoint --- .../java/hudson/plugins/ec2/AmazonEC2Cloud.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java b/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java index e67ad6826..cad58d077 100644 --- a/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java @@ -183,10 +183,12 @@ public FormValidation doCheckCloudName(@QueryParameter String value) { } public FormValidation doCheckAltEC2Endpoint(@QueryParameter String value) { - try { - new URL(value); - } catch (MalformedURLException ignored) { - return FormValidation.error(Messages.AmazonEC2Cloud_MalformedUrl()); + if (Util.fixEmpty(value) != null) { + try { + new URL(value); + } catch (MalformedURLException ignored) { + return FormValidation.error(Messages.AmazonEC2Cloud_MalformedUrl()); + } } return FormValidation.ok(); } @@ -222,6 +224,9 @@ public ListBoxModel doFillRegionItems( // value if not specified. @VisibleForTesting URL determineEC2EndpointURL(@Nullable String altEC2Endpoint) throws MalformedURLException { + if (Util.fixEmpty(altEC2Endpoint) == null) { + return new URL(DEFAULT_EC2_ENDPOINT); + } try { return new URL(altEC2Endpoint); } catch (MalformedURLException e) {