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 6104c80 CAMEL-14986 camel-undertow-spring-security: don't allow
access to resource if no token is provided and add logging
6104c80 is described below
commit 6104c8018ad8c92c13bd6c989268c479d83b20d2
Author: JiriOndrusek <[email protected]>
AuthorDate: Thu Apr 30 09:39:49 2020 +0200
CAMEL-14986 camel-undertow-spring-security: don't allow access to resource
if no token is provided and add logging
---
.../spring/security/SpringSecurityProvider.java | 15 +++++-
.../component/spring/security/MockFilter.java | 14 +++++-
.../component/spring/security/NoTokenTest.java | 54 ++++++++++++++++++++++
3 files changed, 80 insertions(+), 3 deletions(-)
diff --git
a/components/camel-undertow-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityProvider.java
b/components/camel-undertow-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityProvider.java
index 09f46f9..012ae82 100644
---
a/components/camel-undertow-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityProvider.java
+++
b/components/camel-undertow-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityProvider.java
@@ -30,14 +30,17 @@ import io.undertow.servlet.handlers.ServletRequestContext;
import io.undertow.util.AttachmentKey;
import io.undertow.util.StatusCodes;
import org.apache.camel.component.undertow.spi.UndertowSecurityProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import
org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
public class SpringSecurityProvider implements UndertowSecurityProvider {
-
public static final String PRINCIPAL_NAME_HEADER =
SpringSecurityProvider.class.getName() + "_principal";
+
+ private static final Logger LOG =
LoggerFactory.getLogger(SpringSecurityProvider.class);
private static final AttachmentKey<String> PRINCIPAL_NAME_KEY =
AttachmentKey.create(String.class);
private Filter securityFilter;
@@ -58,10 +61,12 @@ public class SpringSecurityProvider implements
UndertowSecurityProvider {
FilterChain fc = (servletRequest, servletResponse) -> {
Authentication a =
SecurityContextHolder.getContext().getAuthentication();
if (a instanceof JwtAuthenticationToken) {
+ LOG.debug("Authentication token is present.");
boolean allowed = false;
Collection<GrantedAuthority> grantedAuthorities =
((JwtAuthenticationToken) a).getAuthorities();
for (GrantedAuthority grantedAuthority : grantedAuthorities) {
if
(allowedRoles.contains(grantedAuthority.getAuthority())) {
+ LOG.debug("Authenticated principal {} has authority to
access resource.", ((JwtAuthenticationToken) a).getName());
allowed = true;
break;
}
@@ -71,10 +76,16 @@ public class SpringSecurityProvider implements
UndertowSecurityProvider {
httpExchange.putAttachment(PRINCIPAL_NAME_KEY,
((JwtAuthenticationToken) a).getName());
httpExchange.setStatusCode(StatusCodes.OK);
return;
+ } else {
+ LOG.debug("Authenticated principal {} doesn't have
authority to access resource.", ((JwtAuthenticationToken) a).getName());
}
- httpExchange.setStatusCode(StatusCodes.FORBIDDEN);
+ } else {
+ //this is logged as warn, because it shows an error in
configuration
+ //spring-security shouldn't allow to access this code if
configuration is correct
+ LOG.warn("Authentication token is not present. Access is
FORBIDDEN.");
}
+ httpExchange.setStatusCode(StatusCodes.FORBIDDEN);
};
securityFilter.doFilter(request, response, fc);
diff --git
a/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/MockFilter.java
b/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/MockFilter.java
index c7ed518..f16295d 100644
---
a/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/MockFilter.java
+++
b/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/MockFilter.java
@@ -37,6 +37,8 @@ public class MockFilter implements Filter {
private Jwt jwt;
+ private boolean putJwtIntoContext = true;
+
@Override
public void init(FilterConfig filterConfig) throws ServletException {
@@ -50,7 +52,9 @@ public class MockFilter implements Filter {
Collection<? extends GrantedAuthority> grantedAuthorities = new
KeycloakRealmRoleConverter().convert(jwt);
- SecurityContextHolder.getContext().setAuthentication(new
JwtAuthenticationToken(jwt, grantedAuthorities));
+ if (putJwtIntoContext) {
+ SecurityContextHolder.getContext().setAuthentication(new
JwtAuthenticationToken(jwt, grantedAuthorities));
+ }
if (chain != null) {
chain.doFilter(request, response);
@@ -65,4 +69,12 @@ public class MockFilter implements Filter {
public void setJwt(Jwt jwt) {
this.jwt = jwt;
}
+
+ public boolean isPutJwtIntoContext() {
+ return putJwtIntoContext;
+ }
+
+ public void setPutJwtIntoContext(boolean putJwtIntoContext) {
+ this.putJwtIntoContext = putJwtIntoContext;
+ }
}
diff --git
a/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/NoTokenTest.java
b/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/NoTokenTest.java
new file mode 100644
index 0000000..208f75d
--- /dev/null
+++
b/components/camel-undertow-spring-security/src/test/java/org/apache/camel/component/spring/security/NoTokenTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.spring.security;
+
+import io.undertow.util.StatusCodes;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.http.base.HttpOperationFailedException;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NoTokenTest extends AbstractSpringSecurityBearerTokenTest {
+
+ @Test
+ public void testBearerTokenAccess() throws Exception {
+ //configure token in mockFilter
+ getMockFilter().setJwt(createToken("Alice", "user"));
+ getMockFilter().setPutJwtIntoContext(false);
+
+ try {
+ template.requestBody("undertow:http://localhost:{{port}}/myapp",
+ "empty body",
+ String.class);
+ Assert.fail("Access has to be denied");
+ } catch (CamelExecutionException e) {
+ HttpOperationFailedException he =
assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
+ assertEquals(StatusCodes.FORBIDDEN, he.getStatusCode());
+ }
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() {
+
from("undertow:http://localhost:{{port}}/myapp?allowedRoles=user")
+ .transform(simple("Hello ${in.header." +
SpringSecurityProvider.PRINCIPAL_NAME_HEADER + "}!"));
+ }
+ };
+ }
+}