Rename UserTenantAware to TenantAwareUser (#1668)

in order to be compatible with other TenantAware entities

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-02-27 08:43:40 +02:00
committed by GitHub
parent 24d70827b7
commit a0db5ff70e
5 changed files with 25 additions and 26 deletions

View File

@@ -21,7 +21,7 @@ import java.util.regex.Pattern;
import org.eclipse.hawkbit.im.authentication.MultitenancyIndicator; import org.eclipse.hawkbit.im.authentication.MultitenancyIndicator;
import org.eclipse.hawkbit.im.authentication.PermissionUtils; import org.eclipse.hawkbit.im.authentication.PermissionUtils;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails; import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserTenantAware; import org.eclipse.hawkbit.im.authentication.TenantAwareUser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -57,10 +57,10 @@ public class InMemoryUserManagementAutoConfiguration extends GlobalAuthenticatio
InMemoryUserManagementAutoConfiguration( InMemoryUserManagementAutoConfiguration(
final SecurityProperties securityProperties, final SecurityProperties securityProperties,
final TenantAwareUserProperties userTenantAwareProperties, final TenantAwareUserProperties tenantAwareUserProperties,
final Optional<PasswordEncoder> passwordEncoder) { final Optional<PasswordEncoder> passwordEncoder) {
userDetailsService = userDetailsService( userDetailsService = userDetailsService(
securityProperties, userTenantAwareProperties, passwordEncoder.orElse(null)); securityProperties, tenantAwareUserProperties, passwordEncoder.orElse(null));
} }
@Override @Override
@@ -72,11 +72,11 @@ public class InMemoryUserManagementAutoConfiguration extends GlobalAuthenticatio
private static UserDetailsService userDetailsService( private static UserDetailsService userDetailsService(
final SecurityProperties securityProperties, final SecurityProperties securityProperties,
final TenantAwareUserProperties userTenantAwareProperties, final TenantAwareUserProperties tenantAwareUserProperties,
final PasswordEncoder passwordEncoder) { final PasswordEncoder passwordEncoder) {
final List<User> userPrincipals = new ArrayList<>(); final List<User> userPrincipals = new ArrayList<>();
userTenantAwareProperties.getUsers().forEach((username, user) -> { tenantAwareUserProperties.getUsers().forEach((username, user) -> {
final UserTenantAware userPrincipal = new UserTenantAware( final TenantAwareUser userPrincipal = new TenantAwareUser(
username, password(user.getPassword(), passwordEncoder), username, password(user.getPassword(), passwordEncoder),
createAuthorities(user.getRoles(), Collections::emptyList), createAuthorities(user.getRoles(), Collections::emptyList),
ObjectUtils.isEmpty(user.getTenant()) ? DEFAULT_TENANT : user.getTenant()); ObjectUtils.isEmpty(user.getTenant()) ? DEFAULT_TENANT : user.getTenant());
@@ -87,7 +87,7 @@ public class InMemoryUserManagementAutoConfiguration extends GlobalAuthenticatio
// the default user from spring security properties as super DEFAULT tenant user // the default user from spring security properties as super DEFAULT tenant user
if (userPrincipals.isEmpty()) { if (userPrincipals.isEmpty()) {
userPrincipals userPrincipals
.add(new UserTenantAware( .add(new TenantAwareUser(
securityProperties.getUser().getName(), securityProperties.getUser().getName(),
password(securityProperties.getUser().getPassword(), passwordEncoder), password(securityProperties.getUser().getPassword(), passwordEncoder),
createAuthorities( createAuthorities(
@@ -153,9 +153,9 @@ public class InMemoryUserManagementAutoConfiguration extends GlobalAuthenticatio
} }
private static User clone(final User user) { private static User clone(final User user) {
if (user instanceof UserTenantAware) { if (user instanceof TenantAwareUser) {
return new UserTenantAware(user.getUsername(), user.getPassword(), user.getAuthorities(), return new TenantAwareUser(user.getUsername(), user.getPassword(), user.getAuthorities(),
((UserTenantAware)user).getTenant()); ((TenantAwareUser)user).getTenant());
} else { } else {
return new User(user.getUsername(), user.getPassword(), user.getAuthorities()); return new User(user.getUsername(), user.getPassword(), user.getAuthorities());
} }

View File

@@ -74,17 +74,17 @@ public class SecurityAutoConfiguration {
* resolving user authorities/roles. * resolving user authorities/roles.
* *
* @param securityProperties The Spring {@link SecurityProperties} for the security user * @param securityProperties The Spring {@link SecurityProperties} for the security user
* @param userTenantAwareProperties The {@link TenantAwareUserProperties} for the managed users * @param tenantAwareUserProperties The {@link TenantAwareUserProperties} for the managed users
* @return an {@link InMemoryUserAuthoritiesResolver} bean * @return an {@link InMemoryUserAuthoritiesResolver} bean
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public UserAuthoritiesResolver inMemoryAuthoritiesResolver(final SecurityProperties securityProperties, public UserAuthoritiesResolver inMemoryAuthoritiesResolver(final SecurityProperties securityProperties,
final TenantAwareUserProperties userTenantAwareProperties) { final TenantAwareUserProperties tenantAwareUserProperties) {
final Map<String, User> userTenantAwares = userTenantAwareProperties.getUsers(); final Map<String, User> tenantAwareUsers = tenantAwareUserProperties.getUsers();
final Map<String, List<String>> usersToPermissions; final Map<String, List<String>> usersToPermissions;
if (!CollectionUtils.isEmpty(userTenantAwares)) { if (!CollectionUtils.isEmpty(tenantAwareUsers)) {
usersToPermissions = userTenantAwares.entrySet().stream().collect( usersToPermissions = tenantAwareUsers.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getRoles())); Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getRoles()));
} else { } else {
usersToPermissions = Collections.singletonMap(securityProperties.getUser().getName(), usersToPermissions = Collections.singletonMap(securityProperties.getUser().getName(),

View File

@@ -18,7 +18,7 @@ import java.util.concurrent.Callable;
import org.eclipse.hawkbit.im.authentication.SpPermission; import org.eclipse.hawkbit.im.authentication.SpPermission;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails; import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserTenantAware; import org.eclipse.hawkbit.im.authentication.TenantAwareUser;
import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder; import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder;
import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@@ -163,7 +163,7 @@ public class SecurityContextSwitch {
authorities = annotation.authorities(); authorities = annotation.authorities();
} }
final TestingAuthenticationToken testingAuthenticationToken = new TestingAuthenticationToken( final TestingAuthenticationToken testingAuthenticationToken = new TestingAuthenticationToken(
new UserTenantAware(annotation.principal(), annotation.tenantId()), new TenantAwareUser(annotation.principal(), annotation.tenantId()),
annotation.credentials(), authorities); annotation.credentials(), authorities);
testingAuthenticationToken.setDetails( testingAuthenticationToken.setDetails(
new TenantAwareAuthenticationDetails(annotation.tenantId(), annotation.controller())); new TenantAwareAuthenticationDetails(annotation.tenantId(), annotation.controller()));

View File

@@ -27,7 +27,7 @@ import org.springframework.security.core.userdetails.User;
@Getter @Getter
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class UserTenantAware extends User { public class TenantAwareUser extends User {
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -40,7 +40,7 @@ public class UserTenantAware extends User {
* @param authorities the authorities which the user has * @param authorities the authorities which the user has
* @param tenant the tenant of the user * @param tenant the tenant of the user
*/ */
public UserTenantAware(final String username, final String password, public TenantAwareUser(final String username, final String password,
final Collection<? extends GrantedAuthority> authorities, final String tenant) { final Collection<? extends GrantedAuthority> authorities, final String tenant) {
super(username, password, authorities == null ? Collections.emptyList() : authorities); super(username, password, authorities == null ? Collections.emptyList() : authorities);
this.tenant = tenant; this.tenant = tenant;
@@ -52,7 +52,7 @@ public class UserTenantAware extends User {
* @param username the username of the user * @param username the username of the user
* @param tenant the tenant of the user * @param tenant the tenant of the user
*/ */
public UserTenantAware(final String username, String tenant) { public TenantAwareUser(final String username, String tenant) {
this(username, "***", null, tenant); this(username, "***", null, tenant);
} }

View File

@@ -21,7 +21,7 @@ import java.util.stream.Collectors;
import org.eclipse.hawkbit.ContextAware; import org.eclipse.hawkbit.ContextAware;
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions; import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails; import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserTenantAware; import org.eclipse.hawkbit.im.authentication.TenantAwareUser;
import org.eclipse.hawkbit.tenancy.TenantAware; import org.eclipse.hawkbit.tenancy.TenantAware;
import org.eclipse.hawkbit.tenancy.UserAuthoritiesResolver; import org.eclipse.hawkbit.tenancy.UserAuthoritiesResolver;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@@ -39,7 +39,6 @@ import org.springframework.security.oauth2.core.oidc.user.OidcUser;
* from the {@link SecurityContext#getAuthentication()} * from the {@link SecurityContext#getAuthentication()}
* {@link Authentication#getDetails()} which holds the * {@link Authentication#getDetails()} which holds the
* {@link TenantAwareAuthenticationDetails} object. * {@link TenantAwareAuthenticationDetails} object.
*
*/ */
public class SecurityContextTenantAware implements ContextAware { public class SecurityContextTenantAware implements ContextAware {
@@ -85,8 +84,8 @@ public class SecurityContextTenantAware implements ContextAware {
final Object principal = context.getAuthentication().getPrincipal(); final Object principal = context.getAuthentication().getPrincipal();
if (context.getAuthentication().getDetails() instanceof TenantAwareAuthenticationDetails) { if (context.getAuthentication().getDetails() instanceof TenantAwareAuthenticationDetails) {
return ((TenantAwareAuthenticationDetails) context.getAuthentication().getDetails()).getTenant(); return ((TenantAwareAuthenticationDetails) context.getAuthentication().getDetails()).getTenant();
} else if (principal instanceof UserTenantAware) { } else if (principal instanceof TenantAwareUser) {
return ((UserTenantAware) principal).getTenant(); return ((TenantAwareUser) principal).getTenant();
} }
} }
return null; return null;
@@ -191,14 +190,14 @@ public class SecurityContextTenantAware implements ContextAware {
private final Authentication delegate; private final Authentication delegate;
private final UserTenantAware principal; private final TenantAwareUser principal;
private final TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails; private final TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails;
private AuthenticationDelegate(final Authentication delegate, final String tenant, final String username, private AuthenticationDelegate(final Authentication delegate, final String tenant, final String username,
final Collection<? extends GrantedAuthority> authorities) { final Collection<? extends GrantedAuthority> authorities) {
this.delegate = delegate; this.delegate = delegate;
this.principal = new UserTenantAware(username, username, authorities, tenant); this.principal = new TenantAwareUser(username, username, authorities, tenant);
tenantAwareAuthenticationDetails = new TenantAwareAuthenticationDetails(tenant, false); tenantAwareAuthenticationDetails = new TenantAwareAuthenticationDetails(tenant, false);
} }