[java-metadata-aggregator] 03/03: MDA-299 - Add HTTPSProtocolURLValidator

Ian Young ian at iay.org.uk
Wed Jan 31 14:09:42 UTC 2024


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch dev/MDA-299
in repository java-metadata-aggregator.

View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=2ddd756d5de23ed2fcf9538dd49181fb51a8eddd

commit 2ddd756d5de23ed2fcf9538dd49181fb51a8eddd
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Wed Jan 31 14:06:53 2024 +0000

    MDA-299 - Add HTTPSProtocolURLValidator
    
    https://shibboleth.atlassian.net/browse/MDA-299
---
 .../validate/url/HTTPSProtocolURLValidator.java    | 43 +++++++++++++++++
 .../resources/net/shibboleth/metadata/beans.xml    |  3 ++
 .../url/HTTPSProtocolURLValidatorTest.java         | 54 ++++++++++++++++++++++
 3 files changed, 100 insertions(+)

diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/url/HTTPSProtocolURLValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/url/HTTPSProtocolURLValidator.java
new file mode 100644
index 0000000..8394535
--- /dev/null
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/url/HTTPSProtocolURLValidator.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.metadata.validate.url;
+
+import java.net.URL;
+
+import javax.annotation.Nonnull;
+
+import net.shibboleth.metadata.Item;
+import net.shibboleth.metadata.pipeline.StageProcessingException;
+import net.shibboleth.metadata.validate.BaseValidator;
+import net.shibboleth.metadata.validate.Validator;
+
+
+/**
+ * Validates that a {@link URL} has the <code>https</code> protocol.
+ */
+public class HTTPSProtocolURLValidator extends BaseValidator implements Validator<URL> {
+
+    @Override
+    public @Nonnull Action validate(final @Nonnull URL url, final @Nonnull Item<?> item,
+            final @Nonnull String stageId) throws StageProcessingException {
+        final var protocol = url.getProtocol();
+        if (!"https".equals(protocol)) {
+            addError("protocol '" + protocol + "' must be https", item, stageId);
+            return Action.DONE;
+        }
+        return Action.CONTINUE;
+    }
+
+}
diff --git a/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml b/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
index 0a455df..b8c70aa 100644
--- a/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
+++ b/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
@@ -354,6 +354,9 @@
     <bean id="mda.EmptyPortURLValidator" abstract="true" parent="mda.validator_parent"
         class="net.shibboleth.metadata.validate.url.EmptyPortURLValidator"/>
     
+    <bean id="mda.HTTPSProtocolURLValidator" abstract="true" parent="mda.validator_parent"
+        class="net.shibboleth.metadata.validate.url.HTTPSProtocolURLValidator"/>
+    
     <bean id="mda.MissingHostURLValidator" abstract="true" parent="mda.validator_parent"
         class="net.shibboleth.metadata.validate.url.MissingHostURLValidator"/>
 
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/url/HTTPSProtocolURLValidatorTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/url/HTTPSProtocolURLValidatorTest.java
new file mode 100644
index 0000000..d96be44
--- /dev/null
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/url/HTTPSProtocolURLValidatorTest.java
@@ -0,0 +1,54 @@
+package net.shibboleth.metadata.validate.url;
+
+import java.net.URL;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.testing.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+
+public class HTTPSProtocolURLValidatorTest {
+
+    @Test
+    public void testHttps() throws Exception {
+        final var val = new HTTPSProtocolURLValidator();
+        val.setId("test");
+        val.initialize();
+        final var item = new MockItem("data");
+        final var res = val.validate(new URL("https://example.com/"), item, "stage");
+        Assert.assertEquals(res, Action.CONTINUE);
+        final var errors = item.getItemMetadata().get(ErrorStatus.class);
+        Assert.assertEquals(errors.size(), 0);
+    }
+
+    @Test
+    public void testHTTPS() throws Exception {
+        final var val = new HTTPSProtocolURLValidator();
+        val.setId("test");
+        val.initialize();
+        final var item = new MockItem("data");
+        // Check that the protocol is accepted in upper case too.
+        // The URL class normalises it to lower case.
+        final var res = val.validate(new URL("HTTPS://example.com/"), item, "stage");
+        Assert.assertEquals(res, Action.CONTINUE);
+        final var errors = item.getItemMetadata().get(ErrorStatus.class);
+        Assert.assertEquals(errors.size(), 0);
+    }
+
+    @Test
+    public void testHttp() throws Exception {
+        final var val = new HTTPSProtocolURLValidator();
+        val.setId("test");
+        val.initialize();
+        final var item = new MockItem("data");
+        final var res = val.validate(new URL("http://example.com/"), item, "stage");
+        Assert.assertEquals(res, Action.DONE);
+        final var errors = item.getItemMetadata().get(ErrorStatus.class);
+        Assert.assertEquals(errors.size(), 1);
+        final var msg = errors.get(0).getStatusMessage();
+        Assert.assertEquals(msg, "protocol 'http' must be https");
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list