Skip to content

Commit

Permalink
♻️ breaking the dependency between job-engine and rest-api by moving …
Browse files Browse the repository at this point in the history
…common code in commons-rest

Signed-off-by: dseurotech <davide.salvador@eurotech.com>
  • Loading branch information
dseurotech committed Jun 10, 2024
1 parent 6d26658 commit aee38d0
Show file tree
Hide file tree
Showing 104 changed files with 2,779 additions and 1,849 deletions.
13 changes: 9 additions & 4 deletions commons-rest/filters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-endpoint-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-rest-api-core</artifactId>
</dependency>
<!-- re-declare as provided as our web container will provide this -->
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -52,5 +48,14 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-commons</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-security-shiro</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@
*******************************************************************************/
package org.eclipse.kapua.commons.rest.filters;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.eclipse.kapua.commons.rest.filters.auth.KapuaTokenAuthenticationFilter;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;

/**
* This {@link Filter} cleans up the {@link Subject#getSession()} state and the {@link KapuaSecurityUtils#getSession()} after a request.
* <p>
* The processing of the request can leave some information on the Shiro {@link Subject} or the {@link org.eclipse.kapua.commons.security.KapuaSession} and we must clean it to avoid that
* a subsequent request uses a {@link Thread} with dirty data inside.
* The processing of the request can leave some information on the Shiro {@link Subject} or the {@link org.eclipse.kapua.commons.security.KapuaSession} and we must clean it to avoid that a subsequent
* request uses a {@link Thread} with dirty data inside.
* <p>
* Apache Shiro it is possible to define {@code noSessionCreation} on the urls mappings in the shiro.ini.
* Unfortunately using the {@code noSessionCreation} does not have any effect because our {@link org.eclipse.kapua.app.api.core.auth.KapuaTokenAuthenticationFilter} is invoked before the {@link org.apache.shiro.web.filter.session.NoSessionCreationFilter}
* so it has no effect (see {@link org.apache.shiro.web.filter.session.NoSessionCreationFilter javadoc}.
* Apache Shiro it is possible to define {@code noSessionCreation} on the urls mappings in the shiro.ini. Unfortunately using the {@code noSessionCreation} does not have any effect because our
* {@link KapuaTokenAuthenticationFilter} is invoked before the {@link org.apache.shiro.web.filter.session.NoSessionCreationFilter} so it has no effect (see
* {@link org.apache.shiro.web.filter.session.NoSessionCreationFilter javadoc}.
*
* @since 1.1.0
*/
Expand All @@ -50,16 +51,21 @@ public void destroy() {
}

/**
* After the invokation of {@link FilterChain#doFilter(ServletRequest, ServletResponse)} the {@link Subject} and the {@link org.eclipse.kapua.commons.security.KapuaSession}
* are checked and cleaned accordingly.
* After the invokation of {@link FilterChain#doFilter(ServletRequest, ServletResponse)} the {@link Subject} and the {@link org.eclipse.kapua.commons.security.KapuaSession} are checked and cleaned
* accordingly.
* <p>
* See also {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
*
* @param request See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @param response See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @param chain See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @throws IOException See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @throws ServletException See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @param request
* See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @param response
* See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @param chain
* See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @throws IOException
* See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @throws ServletException
* See {@link Filter#doFilter(ServletRequest, ServletResponse, FilterChain)} javadoc.
* @since 1.1.0
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.api.core.auth;
package org.eclipse.kapua.commons.rest.filters.auth;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
Expand All @@ -24,11 +29,6 @@
import org.eclipse.kapua.service.authentication.shiro.exceptions.InvalidatedAccessTokenException;
import org.eclipse.kapua.service.authentication.shiro.exceptions.MalformedAccessTokenException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class KapuaTokenAuthenticationFilter extends AuthenticatingFilter {

private static final String OPTIONS = "OPTIONS";
Expand Down Expand Up @@ -73,7 +73,7 @@ protected AuthenticationToken createToken(ServletRequest request, ServletRespons
}

protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e,
ServletRequest request, ServletResponse response) {
ServletRequest request, ServletResponse response) {
HttpServletResponse httpResponse = WebUtils.toHttp(response);
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//now I set a dummy header to propagate the error message to the CORSResponseFilter Class, that eventually will send this error message if CORS filter passes
Expand All @@ -87,7 +87,6 @@ protected boolean onAccessDenied(ServletRequest request, ServletResponse respons
return true;
}


//with this method we choose what exceptions we want to hide in the response and what we want to show as an error message
private String handleAuthException(AuthenticationException ae) {
String errorMessageInResponse = "An error occurred during the authentication process with the provided access token";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.api.core.auth;
package org.eclipse.kapua.commons.rest.filters.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
Expand All @@ -19,10 +22,6 @@
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Category(JUnitTests.class)
public class KapuaTokenAuthenticationFilterTest {

Expand All @@ -37,7 +36,7 @@ public void initialize() {
request = Mockito.mock(HttpServletRequest.class);
response = Mockito.mock(HttpServletResponse.class);
kapuaTokenAuthenticationFilter = new KapuaTokenAuthenticationFilter();
mappedValues = new Object[]{new Object(), 0, 10, 100000, "String", 'c', -10, -1000000000, -100000000000L, 10L, 10.0f, null, 10.10d, true, false};
mappedValues = new Object[] { new Object(), 0, 10, 100000, "String", 'c', -10, -1000000000, -100000000000L, 10L, 10.0f, null, 10.10d, true, false };
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.api.core.auth;
package org.eclipse.kapua.commons.rest.filters.auth;

import java.util.Collections;
import java.util.List;

import com.codahale.metrics.Counter;
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Timer;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.settings.KapuaApiCoreSetting;
import org.eclipse.kapua.commons.metric.CommonsMetric;
import org.eclipse.kapua.commons.metric.MetricsService;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.KapuaObjectFactory;
import org.eclipse.kapua.service.KapuaService;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.List;
import com.codahale.metrics.Counter;
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Timer;

public class MockitoLocator extends KapuaLocator {

Expand All @@ -49,6 +49,7 @@ public List<KapuaService> getServices() {
@Override
public <T> T getComponent(Class<T> componentClass) {
final MetricsService metricsService = new MetricsService() {

@Override
public Counter getCounter(String module, String component, String... names) {
return new Counter();
Expand All @@ -73,9 +74,9 @@ public void registerGauge(Gauge<?> gauge, String module, String component, Strin
if (MetricsService.class.equals(componentClass)) {
return (T) metricsService;
}
if (KapuaApiCoreSetting.class.equals(componentClass)) {
return (T) new KapuaApiCoreSetting();
}
// if (KapuaCommonApiCoreSetting.class.equals(componentClass)) {
// return (T) new KapuaCommonApiCoreSetting();
// }
if (CommonsMetric.class.equals(componentClass)) {
try {
return (T) new CommonsMetric(metricsService, "tests");
Expand Down
85 changes: 85 additions & 0 deletions commons-rest/jersey/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
Eurotech - initial API and implementation
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>kapua-commons-rest</artifactId>
<groupId>org.eclipse.kapua</groupId>
<version>2.1.0-SNAPSHOT</version>
</parent>

<artifactId>kapua-commons-rest-jersey</artifactId>
<dependencies>
<!-- re-declare as provided as our web container will provide this -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- &lt;!&ndash; Apache Shiro security framework &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.shiro</groupId>-->
<!-- <artifactId>shiro-web</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<!-- Moxy for object marshalling unmarshalling -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-api</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-locator-guice</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-qa-markers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-commons</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.api.web;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.util.xml.JAXBContextProvider;
import org.eclipse.kapua.locator.KapuaLocator;
package org.eclipse.kapua.commons.rest.jersey;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.util.xml.JAXBContextProvider;
import org.eclipse.kapua.locator.KapuaLocator;

/**
* Provide a customized JAXBContext that makes the concrete implementations
* known and available for marshalling
* Provide a customized JAXBContext that makes the concrete implementations known and available for marshalling
*
* @since 1.0.0
*/
@Provider
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public class JaxbContextResolver implements ContextResolver<JAXBContext> {

private final JAXBContext jaxbContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.rest.jersey;

import javax.inject.Inject;

import org.eclipse.kapua.commons.setting.AbstractKapuaSetting;

/**
* API setting implementation.
*
* @since 1.0
*/
public class KapuaCommonApiCoreSetting extends AbstractKapuaSetting<KapuaCommonApiCoreSettingKeys> {

private static final String API_SETTING_RESOURCE = "kapua-api-core-settings.properties";

/**
* Construct a new api setting reading settings from {@link KapuaCommonApiCoreSetting#API_SETTING_RESOURCE}
*/
@Inject
public KapuaCommonApiCoreSetting() {
super(API_SETTING_RESOURCE);
}
}
Loading

0 comments on commit aee38d0

Please sign in to comment.