Fix sonar findings (2) (#3016)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2026-04-15 14:39:28 +03:00
committed by GitHub
parent a00374f455
commit 8015b0e3f1
6 changed files with 73 additions and 81 deletions

View File

@@ -66,10 +66,10 @@ public class AccessContext {
* @return the current tenant * @return the current tenant
*/ */
public static String tenant() { public static String tenant() {
final SecurityContext context = SecurityContextHolder.getContext(); final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (context.getAuthentication() != null) { if (authentication != null) {
final Object principal = context.getAuthentication().getPrincipal(); final Object principal = authentication.getPrincipal();
if (context.getAuthentication().getDetails() instanceof TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails) { if (authentication.getDetails() instanceof TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails) {
return tenantAwareAuthenticationDetails.tenant(); return tenantAwareAuthenticationDetails.tenant();
} else if (principal instanceof TenantAwareUser tenantAwareUser) { } else if (principal instanceof TenantAwareUser tenantAwareUser) {
return tenantAwareUser.getTenant(); return tenantAwareUser.getTenant();
@@ -283,7 +283,7 @@ public class AccessContext {
if (principal instanceof OidcUser oidcUser) { if (principal instanceof OidcUser oidcUser) {
return oidcUser.getPreferredUsername(); return oidcUser.getPreferredUsername();
} }
return principal.toString(); return principal == null ? null : principal.toString();
} }
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@@ -337,7 +337,9 @@ public class AccessContext {
private String[] authorities; private String[] authorities;
private SecCtxInfo(final SecurityContext securityContext) { private SecCtxInfo(final SecurityContext securityContext) {
final Authentication authentication = securityContext.getAuthentication(); final Authentication authentication = Objects.requireNonNull(
securityContext.getAuthentication(),
"Authentication must be non-null to serialize security context");
if (!authentication.isAuthenticated()) { if (!authentication.isAuthenticated()) {
throw new IllegalStateException("Only authenticated context could be serialized"); throw new IllegalStateException("Only authenticated context could be serialized");
} }
@@ -361,8 +363,7 @@ public class AccessContext {
final SecurityContext ctx = SecurityContextHolder.createEmptyContext(); final SecurityContext ctx = SecurityContextHolder.createEmptyContext();
final Object details = tenant == null ? null : new TenantAwareAuthenticationDetails(tenant, false); final Object details = tenant == null ? null : new TenantAwareAuthenticationDetails(tenant, false);
final ActorAware principal = () -> auditor; final ActorAware principal = () -> auditor;
final Collection<? extends GrantedAuthority> grantedAuthorities = final Collection<? extends GrantedAuthority> grantedAuthorities = Stream.of(authorities).map(SimpleGrantedAuthority::new).toList();
Stream.of(authorities).map(SimpleGrantedAuthority::new).toList();
ctx.setAuthentication(new Authentication() { ctx.setAuthentication(new Authentication() {
@Override @Override

View File

@@ -9,8 +9,6 @@
*/ */
package org.eclipse.hawkbit.repository.jpa; package org.eclipse.hawkbit.repository.jpa;
import java.util.Collection;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import lombok.AccessLevel; import lombok.AccessLevel;
@@ -37,9 +35,9 @@ public class DefaultRolloutApprovalStrategy implements RolloutApprovalStrategy {
*/ */
@Override @Override
public boolean isApprovalNeeded(final Rollout rollout) { public boolean isApprovalNeeded(final Rollout rollout) {
return TenantConfigHelper.getAsSystem(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class) return TenantConfigHelper.getAsSystem(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class) // if approval enabled for tenant
&& hasNoApproveRolloutPermission(getCurrentAuthentication().map(Authentication::getAuthorities).orElseGet(List::of).stream() && !getCurrentAuthentication().map(Authentication::getAuthorities).map(grantedAuthorities -> grantedAuthorities.stream()
.map(GrantedAuthority::getAuthority).toList()); .map(GrantedAuthority::getAuthority).anyMatch(SpPermission.APPROVE_ROLLOUT::equals)).orElse(false);
} }
@Override @Override
@@ -55,8 +53,4 @@ public class DefaultRolloutApprovalStrategy implements RolloutApprovalStrategy {
private static Optional<Authentication> getCurrentAuthentication() { private static Optional<Authentication> getCurrentAuthentication() {
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()); return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication());
} }
private static boolean hasNoApproveRolloutPermission(final Collection<String> authorities) {
return authorities.stream().noneMatch(SpPermission.APPROVE_ROLLOUT::equals);
}
} }

View File

@@ -266,7 +266,7 @@ public class BaseEntityRepositoryACM<T extends AbstractJpaBaseEntity> implements
@NonNull @NonNull
public List<T> findAll(final Operation operation, @Nullable final Specification<T> spec) { public List<T> findAll(final Operation operation, @Nullable final Specification<T> spec) {
if (operation == null) { if (operation == null) {
return repository.findAll(spec); return spec == null ? repository.findAll() : repository.findAll(spec);
} else { } else {
return repository.findAll(accessController.appendAccessRules(operation, spec)); return repository.findAll(accessController.appendAccessRules(operation, spec));
} }

View File

@@ -66,7 +66,7 @@ public final class HawkbitBaseRepository<T, ID extends Serializable> extends Sim
@Override @Override
public Slice<T> findAllWithoutCount(@Nullable final Specification<T> spec, final Pageable pageable) { public Slice<T> findAllWithoutCount(@Nullable final Specification<T> spec, final Pageable pageable) {
final TypedQuery<T> query = getQuery(spec, pageable); final TypedQuery<T> query = getQuery(spec == null ? Specification.unrestricted() : spec, pageable);
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPageWithoutCount(query, pageable); return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPageWithoutCount(query, pageable);
} }
@@ -104,7 +104,7 @@ public final class HawkbitBaseRepository<T, ID extends Serializable> extends Sim
@Override @Override
public long count(@Nullable final Operation operation, @Nullable final Specification<T> spec) { public long count(@Nullable final Operation operation, @Nullable final Specification<T> spec) {
return count(spec); return spec == null ? count() : count(spec);
} }
@Override @Override

View File

@@ -46,10 +46,10 @@ public final class ConfigView extends VerticalLayout {
public ConfigView(final HawkbitMgmtClient hawkbitClient) { public ConfigView(final HawkbitMgmtClient hawkbitClient) {
setSpacing(false); setSpacing(false);
final Button saveButton = new Button("Save"); final Button saveButton = new Button("Save");
Optional.ofNullable( Optional.ofNullable(hawkbitClient.getTenantManagementRestApi().getTenantConfiguration().getBody()).ifPresent(config ->
hawkbitClient.getTenantManagementRestApi().getTenantConfiguration().getBody()).ifPresent(config ->
config.forEach((k, v) -> { config.forEach((k, v) -> {
if (v.getValue() instanceof String strValue) { switch (v.getValue()) {
case String strValue -> {
final TextField tf = new TextField(k, strValue, event -> { final TextField tf = new TextField(k, strValue, event -> {
final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest();
vre.setValue(event.getValue()); vre.setValue(event.getValue());
@@ -57,13 +57,13 @@ public final class ConfigView extends VerticalLayout {
}); });
tf.getElement().getStyle().set(WIDTH, PX_300); tf.getElement().getStyle().set(WIDTH, PX_300);
add(tf); add(tf);
} else if (v.getValue() instanceof Boolean boolValue) { }
add(new Checkbox(k, boolValue, event -> { case Boolean boolValue -> add(new Checkbox(k, boolValue, event -> {
final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest();
vre.setValue(event.getValue()); vre.setValue(event.getValue());
configValue.put(k, vre); configValue.put(k, vre);
})); }));
} else if (v.getValue() instanceof Long longValue) { case Long longValue -> {
final NumberField nf = new NumberField(k, (double) longValue, event -> { final NumberField nf = new NumberField(k, (double) longValue, event -> {
final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest();
vre.setValue(event.getValue()); vre.setValue(event.getValue());
@@ -71,7 +71,8 @@ public final class ConfigView extends VerticalLayout {
}); });
nf.getElement().getStyle().set(WIDTH, PX_300); nf.getElement().getStyle().set(WIDTH, PX_300);
add(nf); add(nf);
} else if (v.getValue() instanceof Integer intValue) { }
case Integer intValue -> {
final NumberField nf = new NumberField(k, (double) intValue, event -> { final NumberField nf = new NumberField(k, (double) intValue, event -> {
MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest();
vre.setValue(event.getValue()); vre.setValue(event.getValue());
@@ -79,8 +80,8 @@ public final class ConfigView extends VerticalLayout {
}); });
nf.getElement().getStyle().set(WIDTH, PX_300); nf.getElement().getStyle().set(WIDTH, PX_300);
add(nf); add(nf);
} else { }
log.debug("Unexpected value type: {} -> {} (class: {})", default -> log.debug("Unexpected value type: {} -> {} (class: {})",
k, v.getValue(), v.getValue() == null ? "null" : v.getValue().getClass()); k, v.getValue(), v.getValue() == null ? "null" : v.getValue().getClass());
} }
})); }));

View File

@@ -255,13 +255,10 @@ public final class TargetView extends TableView<TargetView.TargetWithDs, String>
} }
private static List<MgmtTargetFilterQuery> listFilters(HawkbitMgmtClient hawkbitClient) { private static List<MgmtTargetFilterQuery> listFilters(HawkbitMgmtClient hawkbitClient) {
return Optional return Optional.ofNullable(hawkbitClient.getTargetFilterQueryRestApi()
.ofNullable( .getFilters(null, 0, 30, null, null).getBody())
hawkbitClient.getTargetFilterQueryRestApi() .map(PagedList<MgmtTargetFilterQuery>::getContent)
.getFilters(null, 0, 30, null, null) .orElseGet(List::of);
.getBody()
.getContent())
.orElse(Collections.emptyList());
} }
private ComponentEventListener<ClickEvent<Button>> createBtnListener(HawkbitMgmtClient hawkbitClient) { private ComponentEventListener<ClickEvent<Button>> createBtnListener(HawkbitMgmtClient hawkbitClient) {
@@ -1091,9 +1088,8 @@ public final class TargetView extends TableView<TargetView.TargetWithDs, String>
public static TargetWithDs from(final HawkbitMgmtClient hawkbitClient, MgmtTarget target) { public static TargetWithDs from(final HawkbitMgmtClient hawkbitClient, MgmtTarget target) {
TargetWithDs targetWithDs = objectMapper.convertValue(target, TargetWithDs.class); TargetWithDs targetWithDs = objectMapper.convertValue(target, TargetWithDs.class);
targetWithDs.ds = Optional.ofNullable(hawkbitClient.getTargetRestApi().getInstalledDistributionSet(targetWithDs targetWithDs.ds = Optional.ofNullable(hawkbitClient.getTargetRestApi()
.getControllerId()) .getInstalledDistributionSet(targetWithDs.getControllerId()).getBody());
.getBody());
return targetWithDs; return targetWithDs;
} }