This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new dd59a2e  CAMEL-14987 camel-undertow: if securityProvider is not 
present, but allowedRoles are not empty, access has to be denied
dd59a2e is described below

commit dd59a2e579c622d87a45a10ecce5b1d71c06017d
Author: JiriOndrusek <[email protected]>
AuthorDate: Thu Apr 30 10:27:56 2020 +0200

    CAMEL-14987 camel-undertow: if securityProvider is not present, but 
allowedRoles are not empty, access has to be denied
---
 .../camel/component/undertow/UndertowConsumer.java |  7 +++
 .../spi/SecurityProviderNoAcceptanceTest.java      | 17 +++++--
 .../spi/SecurityProviderWithoutProviderTest.java   | 59 ++++++++++++++++++++++
 3 files changed, 78 insertions(+), 5 deletions(-)

diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
index ff12135..508cdc6 100644
--- 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
@@ -211,6 +211,13 @@ public class UndertowConsumer extends DefaultConsumer 
implements HttpHandler, Su
                 httpExchange.endExchange();
                 return;
             }
+        } else if (computeAllowedRoles() != null && 
!computeAllowedRoles().isEmpty()) {
+            //this case could happen due to bad configuration
+            //if allowedRoles are present but securityProvider is not, access 
has to be denied in this case
+            LOG.warn("Illegal state caused by missing securitProvider but 
existing allowed roles!");
+            httpExchange.setStatusCode(StatusCodes.FORBIDDEN);
+            httpExchange.endExchange();
+            return;
         }
 
         //create new Exchange
diff --git 
a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderNoAcceptanceTest.java
 
b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderNoAcceptanceTest.java
index 2fe18a9..dbb0763 100644
--- 
a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderNoAcceptanceTest.java
+++ 
b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderNoAcceptanceTest.java
@@ -16,8 +16,11 @@
  */
 package org.apache.camel.component.undertow.spi;
 
+import io.undertow.util.StatusCodes;
 import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
+import org.apache.camel.http.base.HttpOperationFailedException;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -34,15 +37,19 @@ public class SecurityProviderNoAcceptanceTest extends 
AbstractSecurityProviderTe
     }
 
     @Test
-    public void testSecuredNotAllowedButNotApplied() throws Exception {
-        securityConfiguration.setRoleToAssign("admin");
+    public void testSecuredNotAcceptingProvider() throws Exception {
+        securityConfiguration.setRoleToAssign("user");
 
         
getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, 
"GET");
 
-        String out = 
template.requestBody("undertow:http://localhost:{{port}}/foo";, null, 
String.class);
+        try {
+            template.requestBody("undertow:http://localhost:{{port}}/foo";, 
null, String.class);
 
-        Assert.assertEquals("", out);
+            fail("Should throw exception");
 
-        assertMockEndpointsSatisfied();
+        } catch (CamelExecutionException e) {
+            HttpOperationFailedException he = 
assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
+            assertEquals(StatusCodes.FORBIDDEN, he.getStatusCode());
+        }
     }
 }
diff --git 
a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderWithoutProviderTest.java
 
b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderWithoutProviderTest.java
new file mode 100644
index 0000000..849806a
--- /dev/null
+++ 
b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/spi/SecurityProviderWithoutProviderTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.undertow.spi;
+
+import java.io.File;
+import java.net.URL;
+
+import io.undertow.util.StatusCodes;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.camel.http.base.HttpOperationFailedException;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Test of basic securityProvider scenario, when provider does not accepts 
security configuration.
+ */
+public class SecurityProviderWithoutProviderTest extends 
AbstractSecurityProviderTest {
+
+    @BeforeClass
+    public static void createSecurtyProviderConfigurationFile() throws 
Exception {
+        URL location = 
MockSecurityProvider.class.getProtectionDomain().getCodeSource().getLocation();
+        File file = new File(location.getPath() + "META-INF/services/" + 
UndertowSecurityProvider.class.getName());
+        if (file.exists()) {
+            file.delete();
+        }
+    }
+
+    @Test
+    public void testSecuredNoProviderd() throws Exception {
+        securityConfiguration.setRoleToAssign("user");
+
+        
getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, 
"GET");
+
+        try {
+            template.requestBody("undertow:http://localhost:{{port}}/foo";, 
null, String.class);
+
+            fail("Should throw exception");
+
+        } catch (CamelExecutionException e) {
+            HttpOperationFailedException he = 
assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
+            assertEquals(StatusCodes.FORBIDDEN, he.getStatusCode());
+        }
+    }
+}

Reply via email to