Skip to content

Commit 3c3a910

Browse files
committed
Merge pull request eclipse-hawkbit#169 from bsinno/fix_forbidden_exception_after_login
run getting polling configuration in system-code
2 parents b1489f4 + 5220fa0 commit 3c3a910

File tree

5 files changed

+91
-15
lines changed

5 files changed

+91
-15
lines changed

hawkbit-repository/src/main/java/org/eclipse/hawkbit/RepositoryApplicationConfiguration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import org.eclipse.hawkbit.repository.model.helper.CacheManagerHolder;
1919
import org.eclipse.hawkbit.repository.model.helper.SecurityTokenGeneratorHolder;
2020
import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder;
21+
import org.eclipse.hawkbit.repository.model.helper.SystemSecurityContextHolder;
2122
import org.eclipse.hawkbit.repository.model.helper.TenantAwareHolder;
2223
import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder;
2324
import org.eclipse.hawkbit.security.SecurityTokenGenerator;
25+
import org.eclipse.hawkbit.security.SystemSecurityContext;
2426
import org.eclipse.hawkbit.tenancy.TenantAware;
2527
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2628
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
@@ -49,6 +51,16 @@
4951
@EnableAutoConfiguration
5052
public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
5153

54+
/**
55+
* @return the {@link SystemSecurityContext} singleton bean which make it
56+
* accessible in beans which cannot access the service directly,
57+
* e.g. JPA entities.
58+
*/
59+
@Bean
60+
public SystemSecurityContextHolder systemSecurityContextHolder() {
61+
return SystemSecurityContextHolder.getInstance();
62+
}
63+
5264
/**
5365
* @return the {@link TenantConfigurationManagement} singleton bean which
5466
* make it accessible in beans which cannot access the service

hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ protected <T> TenantConfigurationValue<T> buildTenantConfigurationValueByKey(
150150
* if the property cannot be converted to the given
151151
* {@code propertyType}
152152
*/
153-
@PreAuthorize(value = SpringEvalExpressions.HAS_AUTH_TENANT_CONFIGURATION)
153+
@PreAuthorize(value = SpringEvalExpressions.HAS_AUTH_TENANT_CONFIGURATION + SpringEvalExpressions.HAS_AUTH_OR
154+
+ SpringEvalExpressions.IS_SYSTEM_CODE)
154155
public TenantConfigurationValue<?> getConfigurationValue(final TenantConfigurationKey configurationKey) {
155156
return getConfigurationValue(configurationKey, configurationKey.getDataType());
156157
}
@@ -175,7 +176,8 @@ public TenantConfigurationValue<?> getConfigurationValue(final TenantConfigurati
175176
* if the property cannot be converted to the given
176177
* {@code propertyType}
177178
*/
178-
@PreAuthorize(value = SpringEvalExpressions.HAS_AUTH_TENANT_CONFIGURATION)
179+
@PreAuthorize(value = SpringEvalExpressions.HAS_AUTH_TENANT_CONFIGURATION + SpringEvalExpressions.HAS_AUTH_OR
180+
+ SpringEvalExpressions.IS_SYSTEM_CODE)
179181
public <T> T getGlobalConfigurationValue(final TenantConfigurationKey configurationKey,
180182
final Class<T> propertyType) {
181183

hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/TargetInfo.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.persistence.Table;
3939
import javax.persistence.Transient;
4040

41+
import org.eclipse.hawkbit.repository.model.helper.SystemSecurityContextHolder;
4142
import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder;
4243
import org.eclipse.hawkbit.tenancy.configuration.DurationHelper;
4344
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
@@ -245,19 +246,21 @@ public PollStatus getPollStatus() {
245246
if (lastTargetQuery == null) {
246247
return null;
247248
}
248-
249-
final Duration pollTime = DurationHelper.formattedStringToDuration(
250-
TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement()
251-
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue());
252-
final Duration overdueTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagementHolder
253-
.getInstance().getTenantConfigurationManagement()
254-
.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class).getValue());
255-
final LocalDateTime currentDate = LocalDateTime.now();
256-
final LocalDateTime lastPollDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(lastTargetQuery),
257-
ZoneId.systemDefault());
258-
final LocalDateTime nextPollDate = lastPollDate.plus(pollTime);
259-
final LocalDateTime overdueDate = nextPollDate.plus(overdueTime);
260-
return new PollStatus(lastPollDate, nextPollDate, overdueDate, currentDate);
249+
return SystemSecurityContextHolder.getInstance().getSystemSecurityContext().runAsSystem(() -> {
250+
final Duration pollTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagementHolder
251+
.getInstance().getTenantConfigurationManagement()
252+
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue());
253+
final Duration overdueTime = DurationHelper.formattedStringToDuration(
254+
TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement()
255+
.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class)
256+
.getValue());
257+
final LocalDateTime currentDate = LocalDateTime.now();
258+
final LocalDateTime lastPollDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(lastTargetQuery),
259+
ZoneId.systemDefault());
260+
final LocalDateTime nextPollDate = lastPollDate.plus(pollTime);
261+
final LocalDateTime overdueDate = nextPollDate.plus(overdueTime);
262+
return new PollStatus(lastPollDate, nextPollDate, overdueDate, currentDate);
263+
});
261264
}
262265

263266
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*/
9+
package org.eclipse.hawkbit.repository.model.helper;
10+
11+
import org.eclipse.hawkbit.security.SystemSecurityContext;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
14+
/**
15+
* A singleton bean which holds {@link SystemSecurityContext} service and makes
16+
* it accessible to beans which are not managed by spring, e.g. JPA entities.
17+
*/
18+
public final class SystemSecurityContextHolder {
19+
20+
private static final SystemSecurityContextHolder INSTANCE = new SystemSecurityContextHolder();
21+
22+
@Autowired
23+
private SystemSecurityContext systemSecurityContext;
24+
25+
private SystemSecurityContextHolder() {
26+
}
27+
28+
/**
29+
* @return the singleton {@link SystemSecurityContextHolder} instance
30+
*/
31+
public static SystemSecurityContextHolder getInstance() {
32+
return INSTANCE;
33+
}
34+
35+
/**
36+
* @return the {@link SystemSecurityContext} service
37+
*/
38+
public SystemSecurityContext getSystemSecurityContext() {
39+
return systemSecurityContext;
40+
}
41+
}

hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/TargetManagementTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.junit.Assert.assertTrue;
1515
import static org.junit.Assert.fail;
1616

17+
import java.net.URI;
1718
import java.util.ArrayList;
1819
import java.util.HashMap;
1920
import java.util.HashSet;
@@ -29,6 +30,7 @@
2930

3031
import org.eclipse.hawkbit.AbstractIntegrationTest;
3132
import org.eclipse.hawkbit.TestDataUtil;
33+
import org.eclipse.hawkbit.WithSpringAuthorityRule;
3234
import org.eclipse.hawkbit.WithUser;
3335
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
3436
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
@@ -724,4 +726,20 @@ public void findTargetsWithNoTag() {
724726
assertThat(25).as("Targets with no tag").isEqualTo(targetsListWithNoTag.size());
725727

726728
}
729+
730+
@Test
731+
@Description("Tests the a target can be read with only the read target permission")
732+
public void targetCanBeReadWithOnlyReadTargetPermission() throws Exception {
733+
final String knownTargetControllerId = "readTarget";
734+
controllerManagament.findOrRegisterTargetIfItDoesNotexist(knownTargetControllerId, new URI("http://127.0.0.1"));
735+
736+
securityRule.runAs(WithSpringAuthorityRule.withUser("bumlux", "READ_TARGET"), () -> {
737+
final Target findTargetByControllerID = targetManagement.findTargetByControllerID(knownTargetControllerId);
738+
assertThat(findTargetByControllerID).isNotNull();
739+
assertThat(findTargetByControllerID.getTargetInfo()).isNotNull();
740+
assertThat(findTargetByControllerID.getTargetInfo().getPollStatus()).isNotNull();
741+
return null;
742+
});
743+
744+
}
727745
}

0 commit comments

Comments
 (0)