Skip to content

Commit 9b54a2e

Browse files
committed
Ref #25533: access control tests
1 parent e475868 commit 9b54a2e

7 files changed

Lines changed: 448 additions & 24 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/service/AccessControlEvaluationService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public AccessControlEvaluationService(@Lazy IAuthenticationBackend authBackend,
4646

4747
public boolean checkAccess(Authentication auth, ProxySpec spec, AccessControl accessControl) {
4848
if (auth instanceof AnonymousAuthenticationToken) {
49-
// TODO test with parameters
5049
// if anonymous -> only allow access if the backend has no authorization enabled
5150
return !authBackend.hasAuthorization();
5251
}

src/main/java/eu/openanalytics/containerproxy/service/ParametersService.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
import org.apache.commons.lang3.StringUtils;
3030
import org.apache.commons.lang3.tuple.Pair;
3131
import org.springframework.security.core.Authentication;
32-
import org.springframework.security.core.context.SecurityContextHolder;
3332
import org.springframework.stereotype.Service;
3433

3534
import javax.annotation.PostConstruct;
36-
import javax.inject.Inject;
3735
import java.util.ArrayList;
3836
import java.util.Comparator;
3937
import java.util.HashMap;
@@ -47,14 +45,17 @@
4745
@Service
4846
public class ParametersService {
4947

50-
@Inject
51-
private IProxySpecProvider baseSpecProvider;
48+
private final IProxySpecProvider baseSpecProvider;
5249

53-
@Inject
54-
private AccessControlEvaluationService accessControlEvaluationService;
50+
private final AccessControlEvaluationService accessControlEvaluationService;
5551

5652
private static final Pattern PARAMETER_ID_PATTERN = Pattern.compile("[a-zA-Z\\d_-]*");
5753

54+
public ParametersService(IProxySpecProvider baseSpecProvider, AccessControlEvaluationService accessControlEvaluationService) {
55+
this.baseSpecProvider = baseSpecProvider;
56+
this.accessControlEvaluationService = accessControlEvaluationService;
57+
}
58+
5859
@PostConstruct
5960
public void init() {
6061
for (ProxySpec spec : baseSpecProvider.getSpecs()) {
@@ -117,7 +118,7 @@ private void validateSpec(ProxySpec spec) {
117118

118119
}
119120

120-
public boolean validateRequest(ProxySpec resolvedSpec, ProvidedParameters providedParameters) throws InvalidParametersException {
121+
public boolean validateRequest(Authentication auth, ProxySpec resolvedSpec, ProvidedParameters providedParameters) throws InvalidParametersException {
121122
Parameters parameters = resolvedSpec.getParameters();
122123
if (parameters == null) {
123124
return false;
@@ -139,8 +140,6 @@ public boolean validateRequest(ProxySpec resolvedSpec, ProvidedParameters provid
139140
}
140141
}
141142

142-
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
143-
144143
// check if the combination of values is allowed
145144
for (Parameters.ValueSet valueSet : parameters.getValueSets()) {
146145
if (!accessControlEvaluationService.checkAccess(auth, resolvedSpec, valueSet.getAccessControl())) {
@@ -168,16 +167,15 @@ private boolean areParametersAllowedByValueSet(List<String> parameterIds, Parame
168167
return true;
169168
}
170169

171-
public AllowedParametersForUser calculateAllowedParametersForUser(ProxySpec proxySpec) {
170+
public AllowedParametersForUser calculateAllowedParametersForUser(Authentication auth, ProxySpec proxySpec) {
172171
Parameters parameters = proxySpec.getParameters();
173172
if (parameters == null) {
174173
return new AllowedParametersForUser(new HashMap<>(), new HashSet<>());
175174
}
176175
List<String> parameterIds = parameters.getIds();
177176

178177
// 1. check which ValueSets are allowed for this
179-
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
180-
List< Parameters.ValueSet> allowedValueSets = parameters.getValueSets().stream()
178+
List<Parameters.ValueSet> allowedValueSets = parameters.getValueSets().stream()
181179
.filter(v -> accessControlEvaluationService.checkAccess(auth, proxySpec, v.getAccessControl()))
182180
.collect(Collectors.toList());
183181

src/main/java/eu/openanalytics/containerproxy/service/ProxyService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public Proxy startProxy(ProxySpec spec, boolean ignoreAccessControl, List<Runtim
259259
if (runtimeValues != null) {
260260
proxy.addRuntimeValues(runtimeValues);
261261
}
262-
if (parametersService.validateRequest(spec, parameters)) {
262+
if (parametersService.validateRequest(userService.getCurrentAuth(), spec, parameters)) {
263263
proxy.addRuntimeValue(new RuntimeValue(ParametersKey.inst, parameters));
264264
}
265265

src/test/java/eu/openanalytics/containerproxy/test/auth/AccessControlServiceTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver;
3131
import org.junit.jupiter.api.Assertions;
3232
import org.junit.jupiter.api.Test;
33-
import org.springframework.context.ApplicationContext;
33+
import org.springframework.context.support.GenericApplicationContext;
3434
import org.springframework.security.authentication.AnonymousAuthenticationToken;
3535
import org.springframework.security.core.Authentication;
36+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
37+
38+
import java.util.Collection;
39+
import java.util.Collections;
3640

3741
import static org.mockito.Mockito.mock;
3842
import static org.mockito.Mockito.when;
@@ -48,7 +52,7 @@ public AccessControlServiceTest() {
4852
authBackend = mock(IAuthenticationBackend.class);
4953
userService = mock(UserService.class);
5054
specProvider = mock(IProxySpecProvider.class);
51-
SpecExpressionResolver specExpressionResolver = new SpecExpressionResolver(mock(ApplicationContext.class));
55+
SpecExpressionResolver specExpressionResolver = new SpecExpressionResolver(new GenericApplicationContext());
5256
accessControlService = new ProxyAccessControlService(specProvider, new AccessControlEvaluationService(authBackend, userService, specExpressionResolver));
5357
}
5458

@@ -182,6 +186,23 @@ public void combinationOfGroupAndUserTest() {
182186
Assertions.assertTrue(accessControlService.canAccess(auth4, createProxySpec(proxyAccessControl)));
183187
}
184188

189+
@Test
190+
public void expressionTest() {
191+
when(authBackend.hasAuthorization()).thenReturn(true);
192+
AccessControl proxyAccessControl = new AccessControl();
193+
proxyAccessControl.setExpression("#{groups.contains('DEV')}");
194+
195+
// user is not part of the DEV group-> no access
196+
Authentication auth1 = mock(Authentication.class);
197+
when(auth1.getAuthorities()).thenReturn((Collection) Collections.singletonList(new SimpleGrantedAuthority("ROLE_PROD")));
198+
Assertions.assertFalse(accessControlService.canAccess(auth1, createProxySpec(proxyAccessControl)));
199+
200+
// user is part of the DEV group -> has access
201+
Authentication auth2 = mock(Authentication.class);
202+
when(auth2.getAuthorities()).thenReturn((Collection) Collections.singletonList(new SimpleGrantedAuthority("ROLE_DEV")));
203+
Assertions.assertTrue(accessControlService.canAccess(auth2, createProxySpec(proxyAccessControl)));
204+
}
205+
185206
private ProxySpec createProxySpec(AccessControl proxyAccessControl) {
186207
ProxySpec proxySpec = new ProxySpec();
187208
proxySpec.setId("myId");

0 commit comments

Comments
 (0)