Skip to content

Commit

Permalink
💥 breaking KapuaQueries down to basic bits
Browse files Browse the repository at this point in the history
Signed-off-by: dseurotech <[email protected]>
  • Loading branch information
dseurotech committed Jan 15, 2025
1 parent e13bc0d commit 52a7160
Show file tree
Hide file tree
Showing 389 changed files with 4,945 additions and 7,402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,35 @@
*******************************************************************************/
package org.eclipse.kapua.commons.rest.filters;

import com.google.common.base.Strings;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.net.HttpHeaders;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.util.WebUtils;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.rest.filters.settings.KapuaRestFiltersSetting;
import org.eclipse.kapua.commons.rest.filters.settings.KapuaRestFiltersSettingKeys;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.service.account.AccountFactory;
import org.eclipse.kapua.service.account.AccountListResult;
import org.eclipse.kapua.service.account.AccountQuery;
import org.eclipse.kapua.service.account.AccountService;
import org.eclipse.kapua.service.endpoint.EndpointInfo;
import org.eclipse.kapua.service.endpoint.EndpointInfoFactory;
Expand All @@ -35,23 +50,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Strings;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.net.HttpHeaders;

/**
* CORS {@link Filter} implementation.
Expand Down Expand Up @@ -107,7 +109,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
String errorMessage = null;

if (Strings.isNullOrEmpty(fetchSite)) {
logger.warn("Sec-Fetch-Site' header not present in request: {} {}. CORSResponseFilter may produce false positives for this request. User-Agent is: {}", httpRequest.getMethod(), httpRequest.getPathInfo(), httpRequest.getHeader(HttpHeaders.USER_AGENT));
logger.warn("Sec-Fetch-Site' header not present in request: {} {}. CORSResponseFilter may produce false positives for this request. User-Agent is: {}", httpRequest.getMethod(),
httpRequest.getPathInfo(), httpRequest.getHeader(HttpHeaders.USER_AGENT));

Check warning on line 113 in commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java#L112-L113

Added lines #L112 - L113 were not covered by tests
}
if (Strings.isNullOrEmpty(origin)) {
logger.warn("'Origin' header not present in request: {} {}. User-Agent is: {}", httpRequest.getMethod(), httpRequest.getPathInfo(), httpRequest.getHeader(HttpHeaders.USER_AGENT));
Expand All @@ -130,7 +133,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
logger.error(errorMessage);
}
} else {
logger.debug("HTTP sec-fetch-site same-origin detected and allowed. Request: {} {}. User-Agent is: {}", httpRequest.getMethod(), httpRequest.getPathInfo(), httpRequest.getHeader(HttpHeaders.USER_AGENT));
logger.debug("HTTP sec-fetch-site same-origin detected and allowed. Request: {} {}. User-Agent is: {}", httpRequest.getMethod(), httpRequest.getPathInfo(),
httpRequest.getHeader(HttpHeaders.USER_AGENT));

Check warning on line 137 in commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java#L136-L137

Added lines #L136 - L137 were not covered by tests
}
}
int errorCode = httpResponse.getStatus();
Expand All @@ -152,12 +156,12 @@ private String getExplicitOrigin(String origin) throws MalformedURLException {
}

switch (originUrl.getProtocol()) {
case "http":
return origin + ":80";
case "https":
return origin + ":443";
default:
return origin;
case "http":
return origin + ":80";

Check warning on line 160 in commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java#L160

Added line #L160 was not covered by tests
case "https":
return origin + ":443";

Check warning on line 162 in commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java#L162

Added line #L162 was not covered by tests
default:
return origin;

Check warning on line 164 in commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java#L164

Added line #L164 was not covered by tests
}
}

Expand Down Expand Up @@ -191,7 +195,7 @@ private synchronized void refreshOrigins() {
logger.info("Refreshing list of origins...");

Multimap<String, KapuaId> newAllowedOrigins = HashMultimap.create();
AccountQuery accounts = accountFactory.newQuery(null);
final KapuaQuery accounts = new KapuaQuery((KapuaId) null);

Check warning on line 198 in commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/filters/src/main/java/org/eclipse/kapua/commons/rest/filters/CORSResponseFilter.java#L198

Added line #L198 was not covered by tests
AccountListResult accountListResult = KapuaSecurityUtils.doPrivileged(() -> accountService.query(accounts));
accountListResult.getItems().forEach(account -> {
EndpointInfoQuery endpointInfoQuery = endpointInfoFactory.newQuery(account.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eclipse.kapua.commons.model.domains.Domains;
import org.eclipse.kapua.commons.model.mappers.KapuaBaseMapper;
import org.eclipse.kapua.commons.model.mappers.KapuaBaseMapperImpl;
import org.eclipse.kapua.commons.model.query.QueryFactoryImpl;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordCreator;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordRepository;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreService;
Expand All @@ -48,7 +47,6 @@
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.domain.Domain;
import org.eclipse.kapua.model.domain.DomainEntry;
import org.eclipse.kapua.model.query.QueryFactory;

import com.google.inject.Provides;
import com.google.inject.multibindings.Multibinder;
Expand All @@ -63,7 +61,6 @@ public class CommonsModule extends AbstractKapuaModule {

@Override
protected void configureModule() {
bind(QueryFactory.class).to(QueryFactoryImpl.class).in(Singleton.class);
bind(CryptoSettings.class).toInstance(new CryptoSettings());
bind(CryptoUtil.class).to(CryptoUtilImpl.class).in(Singleton.class);
bind(QRCodeBuilder.class).to(QRCodeBuilderImpl.class).in(Singleton.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
* The {@link KapuaEntityCreator} type.
* @param <S>
* The {@link KapuaEntityService} type.
* @param <Q>
* The {@link KapuaQuery} type.
* @param <F>
* The {@link KapuaEntityFactory} type.
* @since 1.0.0
Expand All @@ -66,8 +64,7 @@ public abstract class AbstractKapuaConfigurableResourceLimitedService<
E extends KapuaEntity,
C extends KapuaEntityCreator<E>,
S extends KapuaEntityService<E, C>,
Q extends KapuaQuery,
F extends KapuaEntityFactory<E, C, Q>
F extends KapuaEntityFactory<E, C>
>
extends AbstractKapuaConfigurableService
implements KapuaEntityService<E, C> {
Expand All @@ -82,7 +79,7 @@ public abstract class AbstractKapuaConfigurableResourceLimitedService<
* Constructor.
*
* @param pid
* The {@link KapuaConfigurableService} id.
* The {@link KapuaConfigurableService} id.KapuaEntit
* @param domain
* The {@link Domain} on which check access.
* @param entityManagerFactory
Expand Down Expand Up @@ -267,7 +264,7 @@ private long allowedChildEntities(KapuaId scopeId, KapuaId targetScopeId, Map<St
return Integer.MAX_VALUE;
}
return KapuaSecurityUtils.doPrivileged(() -> {
Q countQuery = getFactory().newQuery(scopeId);
KapuaQuery countQuery = new KapuaQuery(scopeId);

Check warning on line 267 in commons/src/main/java/org/eclipse/kapua/commons/configuration/AbstractKapuaConfigurableResourceLimitedService.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/AbstractKapuaConfigurableResourceLimitedService.java#L267

Added line #L267 was not covered by tests

// Current used entities
long currentUsedEntities = this.count(countQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.domain.Domain;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
Expand Down Expand Up @@ -527,7 +528,7 @@ protected Map<String, Object> getConfigValues(KapuaId scopeId, boolean excludeDi
// Check access
getAuthorizationService().checkPermission(getPermissionFactory().newPermission(Optional.ofNullable(domain).map(d -> d.getName()).orElse(null), Actions.read, scopeId));
// Get configuration values
ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
KapuaQuery query = new KapuaQuery(scopeId);

Check warning on line 531 in commons/src/main/java/org/eclipse/kapua/commons/configuration/AbstractKapuaConfigurableService.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/AbstractKapuaConfigurableService.java#L531

Added line #L531 was not covered by tests

query.setPredicate(
query.andPredicate(
Expand Down Expand Up @@ -583,7 +584,7 @@ public void setConfigValues(KapuaId scopeId, KapuaId parentId, Map<String, Objec

validateConfigurations(ocd, values, scopeId, parentId);

ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
KapuaQuery query = new KapuaQuery(scopeId);

Check warning on line 587 in commons/src/main/java/org/eclipse/kapua/commons/configuration/AbstractKapuaConfigurableService.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/AbstractKapuaConfigurableService.java#L587

Added line #L587 was not covered by tests
query.setPredicate(
query.andPredicate(
query.attributePredicate(ServiceConfigAttributes.SERVICE_ID, pid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.kapua.commons.jpa.KapuaJpaRepositoryConfiguration;
import org.eclipse.kapua.commons.jpa.KapuaUpdatableEntityJpaRepository;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.storage.TxContext;

public class ServiceConfigImplJpaRepository
Expand All @@ -27,7 +28,7 @@ public ServiceConfigImplJpaRepository(KapuaJpaRepositoryConfiguration jpaRepoCon

@Override
public ServiceConfigListResult findByScopeAndPid(TxContext txContext, KapuaId scopeId, String pid) throws KapuaException {
final ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
final KapuaQuery query = new KapuaQuery(scopeId);

Check warning on line 31 in commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigImplJpaRepository.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigImplJpaRepository.java#L31

Added line #L31 was not covered by tests

query.setPredicate(
query.andPredicate(
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.kapua.model.config.metatype.KapuaTmetadata;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.service.config.KapuaConfigurableService;
import org.eclipse.kapua.storage.TxContext;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -144,7 +145,7 @@ public void setConfigValues(TxContext txContext, KapuaId scopeId, Optional<Kapua

validateConfigurations(txContext, ocd, values, scopeId, parentId);

ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
KapuaQuery query = new KapuaQuery(scopeId);

Check warning on line 148 in commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerImpl.java#L148

Added line #L148 was not covered by tests
query.setPredicate(
query.andPredicate(
query.attributePredicate(ServiceConfigAttributes.SERVICE_ID, pid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class UsedEntitiesCounterImpl<
C extends KapuaEntityCreator<E>,
L extends KapuaListResult<E>,
Q extends KapuaQuery,
F extends KapuaEntityFactory<E, C, Q>
F extends KapuaEntityFactory<E, C>
> implements UsedEntitiesCounter {

private final F factory;
Expand All @@ -42,7 +42,7 @@ public UsedEntitiesCounterImpl(F factory,

@Override
public long countEntitiesInScope(TxContext tx, KapuaId scopeId) throws KapuaException {
final Q query = factory.newQuery(scopeId);
final KapuaQuery query = new KapuaQuery(scopeId);

Check warning on line 45 in commons/src/main/java/org/eclipse/kapua/commons/configuration/UsedEntitiesCounterImpl.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/UsedEntitiesCounterImpl.java#L45

Added line #L45 was not covered by tests
// Do count
return entityRepository.count(tx, query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*******************************************************************************/
package org.eclipse.kapua.commons.event;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.jpa.EntityManager;
import org.eclipse.kapua.commons.jpa.EntityManagerFactory;
Expand All @@ -22,7 +27,6 @@
import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordQuery;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreService;
import org.eclipse.kapua.commons.service.event.store.api.ServiceEventUtil;
import org.eclipse.kapua.commons.service.event.store.internal.EventStoreFactoryImpl;
import org.eclipse.kapua.commons.setting.system.SystemSetting;
import org.eclipse.kapua.commons.setting.system.SystemSettingKey;
import org.eclipse.kapua.commons.util.KapuaDateUtils;
Expand All @@ -35,11 +39,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
* Event bus housekeeper. It is responsible to send unsent messages or send again messages gone in error.
*
Expand Down Expand Up @@ -78,7 +77,8 @@ private enum EventsProcessType {
* @param servicesEntryList
* @throws KapuaException
*/
public ServiceEventHousekeeper(EventStoreService eventStoreService, EntityManagerFactory entityManagerFactory, ServiceEventBus eventbus, List<ServiceEntry> servicesEntryList) throws KapuaException {
public ServiceEventHousekeeper(EventStoreService eventStoreService, EntityManagerFactory entityManagerFactory, ServiceEventBus eventbus, List<ServiceEntry> servicesEntryList)
throws KapuaException {

Check warning on line 81 in commons/src/main/java/org/eclipse/kapua/commons/event/ServiceEventHousekeeper.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/event/ServiceEventHousekeeper.java#L81

Added line #L81 was not covered by tests
this.eventbus = eventbus;
this.servicesEntryList = servicesEntryList;
manager = entityManagerFactory.createEntityManager();
Expand Down Expand Up @@ -161,7 +161,7 @@ private void findAndSendUnsentEvents(String serviceName, EventsProcessType event
}

private EventStoreRecordListResult getUnsentEvents(String serviceName, EventsProcessType eventsProcessType) throws KapuaException {
EventStoreRecordQuery query = new EventStoreFactoryImpl().newQuery(null);
EventStoreRecordQuery query = new EventStoreRecordQuery(null);

Check warning on line 164 in commons/src/main/java/org/eclipse/kapua/commons/event/ServiceEventHousekeeper.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/event/ServiceEventHousekeeper.java#L164

Added line #L164 was not covered by tests

AndPredicate andPredicate = query.andPredicate();
andPredicate.and(query.attributePredicate(EventStoreRecordAttributes.SERVICE_NAME, serviceName));
Expand Down
Loading

0 comments on commit 52a7160

Please sign in to comment.