Refactor hawkbit core and security (#2833)
* Refactor hawkbit core and security * improve access to the base core features - static * thus easiear access * and less boilerplate passing of instances Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com> * Refactor context classes * make JSON context serialization default * AccessContext * Split hawkbit-security-core to other modules and remove it --------- Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -11,7 +11,6 @@ package org.eclipse.hawkbit.repository.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import jakarta.persistence.Query;
|
||||
|
||||
@@ -15,7 +15,6 @@ import java.util.Map;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import lombok.Data;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
|
||||
@@ -45,7 +44,6 @@ public class JpaConfiguration extends JpaBaseConfiguration {
|
||||
private final Map<String, String> eclipselink = new HashMap<>();
|
||||
}
|
||||
|
||||
private final TenantAware.TenantResolver tenantResolver;
|
||||
// only for testing purposes ddl generation may be enabled
|
||||
private final Map<String, String> eclipselinkProperties;
|
||||
|
||||
@@ -53,10 +51,8 @@ public class JpaConfiguration extends JpaBaseConfiguration {
|
||||
protected JpaConfiguration(
|
||||
final DataSource dataSource, final JpaProperties properties,
|
||||
final ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
|
||||
final TenantAware.TenantResolver tenantResolver,
|
||||
final Properties eclipselinkProperties) {
|
||||
super(dataSource, properties, jtaTransactionManagerProvider);
|
||||
this.tenantResolver = tenantResolver;
|
||||
this.eclipselinkProperties = eclipselinkProperties.getEclipselink();
|
||||
}
|
||||
|
||||
@@ -67,7 +63,7 @@ public class JpaConfiguration extends JpaBaseConfiguration {
|
||||
@Override
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(final ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
|
||||
return new TransactionManager(tenantResolver);
|
||||
return new TransactionManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,9 +15,9 @@ import java.util.Objects;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.transaction.Transaction;
|
||||
|
||||
import org.eclipse.hawkbit.context.AccessContext;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.EntityPropertyChangeListener;
|
||||
import org.eclipse.hawkbit.repository.jpa.utils.ExceptionMapper;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
||||
import org.eclipse.persistence.descriptors.ClassDescriptor;
|
||||
import org.eclipse.persistence.descriptors.DescriptorEventManager;
|
||||
@@ -29,7 +29,7 @@ import org.springframework.transaction.support.DefaultTransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.orm.jpa.JpaTransactionManager} that sets the {@link TenantAware#getCurrentTenant()} in the eclipselink session. This has
|
||||
* {@link org.springframework.orm.jpa.JpaTransactionManager} that sets the {@link AccessContext#tenant()} in the eclipselink session. This has
|
||||
* to be done in eclipselink after a {@link Transaction} has been started.
|
||||
* <p/>
|
||||
* The class also handles setting:
|
||||
@@ -45,8 +45,6 @@ class TransactionManager extends JpaTransactionManager {
|
||||
|
||||
private static final Class<?> JPA_TARGET;
|
||||
|
||||
private transient TenantAware.TenantResolver tenantResolver;
|
||||
|
||||
static {
|
||||
try {
|
||||
JPA_TARGET = Class.forName("org.eclipse.hawkbit.repository.jpa.model.JpaTarget");
|
||||
@@ -57,10 +55,6 @@ class TransactionManager extends JpaTransactionManager {
|
||||
}
|
||||
}
|
||||
|
||||
TransactionManager(final TenantAware.TenantResolver tenantResolver) {
|
||||
this.tenantResolver = tenantResolver;
|
||||
}
|
||||
|
||||
private static final EntityPropertyChangeListener ENTITY_PROPERTY_CHANGE_LISTENER = new EntityPropertyChangeListener();
|
||||
|
||||
@Override
|
||||
@@ -83,7 +77,7 @@ class TransactionManager extends JpaTransactionManager {
|
||||
}
|
||||
}
|
||||
|
||||
final String currentTenant = tenantResolver.resolveTenant();
|
||||
final String currentTenant = AccessContext.tenant();
|
||||
if (currentTenant == null) {
|
||||
cleanupTenant(em);
|
||||
} else {
|
||||
|
||||
@@ -22,8 +22,8 @@ import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.eclipse.hawkbit.context.AccessContext;
|
||||
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantAwareHolder;
|
||||
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
|
||||
import org.eclipse.persistence.annotations.Multitenant;
|
||||
import org.eclipse.persistence.annotations.MultitenantType;
|
||||
@@ -50,7 +50,7 @@ public abstract class AbstractJpaTenantAwareBaseEntity extends AbstractJpaBaseEn
|
||||
private String tenant;
|
||||
|
||||
/**
|
||||
* Tenant aware entities extend the equals/hashcode strategy with the tenant name. That would allow for instance in a
|
||||
* AccessContext aware entities extend the equals/hashcode strategy with the tenant name. That would allow for instance in a
|
||||
* multi-schema based data separation setup to have the same primary key for different entities of different tenants.
|
||||
*/
|
||||
@Override
|
||||
@@ -59,7 +59,7 @@ public abstract class AbstractJpaTenantAwareBaseEntity extends AbstractJpaBaseEn
|
||||
}
|
||||
|
||||
/**
|
||||
* Tenant aware entities extend the equals/hashcode strategy with the tenant name. That would allow for instance in a
|
||||
* AccessContext aware entities extend the equals/hashcode strategy with the tenant name. That would allow for instance in a
|
||||
* multi-schema based data separation setup to have the same primary key for different entities of
|
||||
* different tenants.
|
||||
*/
|
||||
@@ -86,13 +86,13 @@ public abstract class AbstractJpaTenantAwareBaseEntity extends AbstractJpaBaseEn
|
||||
*/
|
||||
@PrePersist
|
||||
void prePersist() {
|
||||
// before persisting the entity check the current ID of the tenant by using the TenantAware service
|
||||
final String currentTenant = TenantAwareHolder.getInstance().getTenantAware().getCurrentTenant();
|
||||
// before persisting the entity check the current ID of the tenant by using the AccessContext
|
||||
final String currentTenant = AccessContext.tenant();
|
||||
if (currentTenant == null) {
|
||||
throw new TenantNotExistException(
|
||||
String.format(
|
||||
"Tenant %s does not exists, cannot create entity %s with id %d",
|
||||
TenantAwareHolder.getInstance().getTenantAware().getCurrentTenant(), getClass(), getId()));
|
||||
"AccessContext %s does not exists, cannot create entity %s with id %d",
|
||||
AccessContext.tenant(), getClass(), getId()));
|
||||
}
|
||||
setTenant(currentTenant.toUpperCase());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user