From 8015b0e3f1c3d05e00bc788451dfd510179a6478 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Wed, 15 Apr 2026 14:39:28 +0300 Subject: [PATCH] Fix sonar findings (2) (#3016) Signed-off-by: Avgustin Marinov --- .../hawkbit/context/AccessContext.java | 17 +++--- .../jpa/DefaultRolloutApprovalStrategy.java | 12 +--- .../repository/BaseEntityRepositoryACM.java | 2 +- .../jpa/repository/HawkbitBaseRepository.java | 4 +- .../eclipse/hawkbit/ui/view/ConfigView.java | 61 ++++++++++--------- .../eclipse/hawkbit/ui/view/TargetView.java | 58 ++++++++---------- 6 files changed, 73 insertions(+), 81 deletions(-) diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/context/AccessContext.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/context/AccessContext.java index 68f6c0fae..6bcbbce94 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/context/AccessContext.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/context/AccessContext.java @@ -66,10 +66,10 @@ public class AccessContext { * @return the current tenant */ public static String tenant() { - final SecurityContext context = SecurityContextHolder.getContext(); - if (context.getAuthentication() != null) { - final Object principal = context.getAuthentication().getPrincipal(); - if (context.getAuthentication().getDetails() instanceof TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails) { + final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null) { + final Object principal = authentication.getPrincipal(); + if (authentication.getDetails() instanceof TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails) { return tenantAwareAuthenticationDetails.tenant(); } else if (principal instanceof TenantAwareUser tenantAwareUser) { return tenantAwareUser.getTenant(); @@ -283,7 +283,7 @@ public class AccessContext { if (principal instanceof OidcUser oidcUser) { return oidcUser.getPreferredUsername(); } - return principal.toString(); + return principal == null ? null : principal.toString(); } private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); @@ -337,7 +337,9 @@ public class AccessContext { private String[] authorities; 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()) { throw new IllegalStateException("Only authenticated context could be serialized"); } @@ -361,8 +363,7 @@ public class AccessContext { final SecurityContext ctx = SecurityContextHolder.createEmptyContext(); final Object details = tenant == null ? null : new TenantAwareAuthenticationDetails(tenant, false); final ActorAware principal = () -> auditor; - final Collection grantedAuthorities = - Stream.of(authorities).map(SimpleGrantedAuthority::new).toList(); + final Collection grantedAuthorities = Stream.of(authorities).map(SimpleGrantedAuthority::new).toList(); ctx.setAuthentication(new Authentication() { @Override diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/DefaultRolloutApprovalStrategy.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/DefaultRolloutApprovalStrategy.java index 361d7ca10..6a91f10bd 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/DefaultRolloutApprovalStrategy.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/DefaultRolloutApprovalStrategy.java @@ -9,8 +9,6 @@ */ package org.eclipse.hawkbit.repository.jpa; -import java.util.Collection; -import java.util.List; import java.util.Optional; import lombok.AccessLevel; @@ -37,9 +35,9 @@ public class DefaultRolloutApprovalStrategy implements RolloutApprovalStrategy { */ @Override public boolean isApprovalNeeded(final Rollout rollout) { - return TenantConfigHelper.getAsSystem(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class) - && hasNoApproveRolloutPermission(getCurrentAuthentication().map(Authentication::getAuthorities).orElseGet(List::of).stream() - .map(GrantedAuthority::getAuthority).toList()); + return TenantConfigHelper.getAsSystem(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class) // if approval enabled for tenant + && !getCurrentAuthentication().map(Authentication::getAuthorities).map(grantedAuthorities -> grantedAuthorities.stream() + .map(GrantedAuthority::getAuthority).anyMatch(SpPermission.APPROVE_ROLLOUT::equals)).orElse(false); } @Override @@ -55,8 +53,4 @@ public class DefaultRolloutApprovalStrategy implements RolloutApprovalStrategy { private static Optional getCurrentAuthentication() { return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()); } - - private static boolean hasNoApproveRolloutPermission(final Collection authorities) { - return authorities.stream().noneMatch(SpPermission.APPROVE_ROLLOUT::equals); - } } \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/BaseEntityRepositoryACM.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/BaseEntityRepositoryACM.java index 11ca58586..c608b47d9 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/BaseEntityRepositoryACM.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/BaseEntityRepositoryACM.java @@ -266,7 +266,7 @@ public class BaseEntityRepositoryACM implements @NonNull public List findAll(final Operation operation, @Nullable final Specification spec) { if (operation == null) { - return repository.findAll(spec); + return spec == null ? repository.findAll() : repository.findAll(spec); } else { return repository.findAll(accessController.appendAccessRules(operation, spec)); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/HawkbitBaseRepository.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/HawkbitBaseRepository.java index e1f09eae0..ca9bbf12a 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/HawkbitBaseRepository.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/HawkbitBaseRepository.java @@ -66,7 +66,7 @@ public final class HawkbitBaseRepository extends Sim @Override public Slice findAllWithoutCount(@Nullable final Specification spec, final Pageable pageable) { - final TypedQuery query = getQuery(spec, pageable); + final TypedQuery query = getQuery(spec == null ? Specification.unrestricted() : spec, pageable); return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPageWithoutCount(query, pageable); } @@ -104,7 +104,7 @@ public final class HawkbitBaseRepository extends Sim @Override public long count(@Nullable final Operation operation, @Nullable final Specification spec) { - return count(spec); + return spec == null ? count() : count(spec); } @Override diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/ConfigView.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/ConfigView.java index 074cc97d6..fa94bd2a9 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/ConfigView.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/ConfigView.java @@ -46,41 +46,42 @@ public final class ConfigView extends VerticalLayout { public ConfigView(final HawkbitMgmtClient hawkbitClient) { setSpacing(false); final Button saveButton = new Button("Save"); - Optional.ofNullable( - hawkbitClient.getTenantManagementRestApi().getTenantConfiguration().getBody()).ifPresent(config -> + Optional.ofNullable(hawkbitClient.getTenantManagementRestApi().getTenantConfiguration().getBody()).ifPresent(config -> config.forEach((k, v) -> { - if (v.getValue() instanceof String strValue) { - final TextField tf = new TextField(k, strValue, event -> { - final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); - vre.setValue(event.getValue()); - configValue.put(k, vre); - }); - tf.getElement().getStyle().set(WIDTH, PX_300); - add(tf); - } else if (v.getValue() instanceof Boolean boolValue) { - add(new Checkbox(k, boolValue, event -> { + switch (v.getValue()) { + case String strValue -> { + final TextField tf = new TextField(k, strValue, event -> { + final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); + vre.setValue(event.getValue()); + configValue.put(k, vre); + }); + tf.getElement().getStyle().set(WIDTH, PX_300); + add(tf); + } + case Boolean boolValue -> add(new Checkbox(k, boolValue, event -> { final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); vre.setValue(event.getValue()); configValue.put(k, vre); })); - } else if (v.getValue() instanceof Long longValue) { - final NumberField nf = new NumberField(k, (double) longValue, event -> { - final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); - vre.setValue(event.getValue()); - configValue.put(k, vre); - }); - nf.getElement().getStyle().set(WIDTH, PX_300); - add(nf); - } else if (v.getValue() instanceof Integer intValue) { - final NumberField nf = new NumberField(k, (double) intValue, event -> { - MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); - vre.setValue(event.getValue()); - configValue.put(k, vre); - }); - nf.getElement().getStyle().set(WIDTH, PX_300); - add(nf); - } else { - log.debug("Unexpected value type: {} -> {} (class: {})", + case Long longValue -> { + final NumberField nf = new NumberField(k, (double) longValue, event -> { + final MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); + vre.setValue(event.getValue()); + configValue.put(k, vre); + }); + nf.getElement().getStyle().set(WIDTH, PX_300); + add(nf); + } + case Integer intValue -> { + final NumberField nf = new NumberField(k, (double) intValue, event -> { + MgmtSystemTenantConfigurationValueRequest vre = new MgmtSystemTenantConfigurationValueRequest(); + vre.setValue(event.getValue()); + configValue.put(k, vre); + }); + nf.getElement().getStyle().set(WIDTH, PX_300); + add(nf); + } + default -> log.debug("Unexpected value type: {} -> {} (class: {})", k, v.getValue(), v.getValue() == null ? "null" : v.getValue().getClass()); } })); diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/TargetView.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/TargetView.java index ab113e162..9c7470c36 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/TargetView.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/view/TargetView.java @@ -255,13 +255,10 @@ public final class TargetView extends TableView } private static List listFilters(HawkbitMgmtClient hawkbitClient) { - return Optional - .ofNullable( - hawkbitClient.getTargetFilterQueryRestApi() - .getFilters(null, 0, 30, null, null) - .getBody() - .getContent()) - .orElse(Collections.emptyList()); + return Optional.ofNullable(hawkbitClient.getTargetFilterQueryRestApi() + .getFilters(null, 0, 30, null, null).getBody()) + .map(PagedList::getContent) + .orElseGet(List::of); } private ComponentEventListener> createBtnListener(HawkbitMgmtClient hawkbitClient) { @@ -409,11 +406,11 @@ public final class TargetView extends TableView this.hawkbitClient = hawkbitClient; description.setMinLength(2); Stream.of( - description, - createdBy, createdAt, - lastModifiedBy, lastModifiedAt, - securityToken, lastPoll, group, targetAddress, targetAttributes - ) + description, + createdBy, createdAt, + lastModifiedBy, lastModifiedAt, + securityToken, lastPoll, group, targetAddress, targetAttributes + ) .forEach(field -> { field.setReadOnly(true); add(field); @@ -483,18 +480,18 @@ public final class TargetView extends TableView Optional.ofNullable(supplier.get()) .map(ResponseEntity::getBody) .ifPresentOrElse(value -> { - final String description = """ - Name: %s - Version: %s - %s - """.replace("\n", System.lineSeparator()); - textArea.setValueWithLink(description.formatted( - value.getName(), - value.getVersion(), - value.getModules().stream().map(module -> module.getTypeName() + ": " + module.getVersion()) - .collect(Collectors.joining(System.lineSeparator())) - ), "q=id%3D%3D" + value.getId().toString()); - }, + final String description = """ + Name: %s + Version: %s + %s + """.replace("\n", System.lineSeparator()); + textArea.setValueWithLink(description.formatted( + value.getName(), + value.getVersion(), + value.getModules().stream().map(module -> module.getTypeName() + ": " + module.getVersion()) + .collect(Collectors.joining(System.lineSeparator())) + ), "q=id%3D%3D" + value.getId().toString()); + }, () -> textArea.setValueWithLink("", null)); } } @@ -603,7 +600,7 @@ public final class TargetView extends TableView int offset = 0; do { List page = Optional.ofNullable( - hawkbitClient.getTargetTagRestApi().getTargetTags(null, offset, 50, Constants.NAME_ASC).getBody()) + hawkbitClient.getTargetTagRestApi().getTargetTags(null, offset, 50, Constants.NAME_ASC).getBody()) .map(PagedList::getContent) .orElse(Collections.emptyList()); tags.addAll(page); @@ -647,7 +644,7 @@ public final class TargetView extends TableView final Button addBtn = new Button("Add"); addBtn.addClickListener(e -> new AddMetadataDialog(hawkbitClient, target, this::refreshMetadatas).result()); addBtn.setEnabled(true); - final HorizontalLayout tools = new HorizontalLayout(); + final HorizontalLayout tools = new HorizontalLayout(); tools.setWidthFull(); tools.add(addBtn); add(tools); @@ -665,7 +662,7 @@ public final class TargetView extends TableView private void refreshMetadatas() { metadataArea.setItems( Optional.ofNullable( - hawkbitClient.getTargetRestApi().getMetadata(target.getControllerId()).getBody()) + hawkbitClient.getTargetRestApi().getMetadata(target.getControllerId()).getBody()) .map(PagedList::getContent) .orElse(Collections.emptyList())); } @@ -845,7 +842,7 @@ public final class TargetView extends TableView request.setTargetType(type.getValue().getId()); } hawkbitClient.getTargetRestApi().createTargets( - List.of(request)) + List.of(request)) .getBody() .stream() .findFirst() @@ -1091,9 +1088,8 @@ public final class TargetView extends TableView public static TargetWithDs from(final HawkbitMgmtClient hawkbitClient, MgmtTarget target) { TargetWithDs targetWithDs = objectMapper.convertValue(target, TargetWithDs.class); - targetWithDs.ds = Optional.ofNullable(hawkbitClient.getTargetRestApi().getInstalledDistributionSet(targetWithDs - .getControllerId()) - .getBody()); + targetWithDs.ds = Optional.ofNullable(hawkbitClient.getTargetRestApi() + .getInstalledDistributionSet(targetWithDs.getControllerId()).getBody()); return targetWithDs; }