|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2021 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.service; |
| 22 | + |
| 23 | +import eu.openanalytics.containerproxy.auth.IAuthenticationBackend; |
| 24 | +import eu.openanalytics.containerproxy.model.spec.AccessControl; |
| 25 | +import eu.openanalytics.containerproxy.model.spec.ProxySpec; |
| 26 | +import eu.openanalytics.containerproxy.spec.expression.SpecExpressionContext; |
| 27 | +import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver; |
| 28 | +import org.springframework.context.annotation.Lazy; |
| 29 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 30 | +import org.springframework.security.core.Authentication; |
| 31 | +import org.springframework.stereotype.Service; |
| 32 | + |
| 33 | +@Service |
| 34 | +public class AccessControlEvaluationService { |
| 35 | + |
| 36 | + private final IAuthenticationBackend authBackend; |
| 37 | + private final UserService userService; |
| 38 | + |
| 39 | + private final SpecExpressionResolver specExpressionResolver; |
| 40 | + |
| 41 | + public AccessControlEvaluationService(@Lazy IAuthenticationBackend authBackend, UserService userService, SpecExpressionResolver specExpressionResolver) { |
| 42 | + this.authBackend = authBackend; |
| 43 | + this.userService = userService; |
| 44 | + this.specExpressionResolver = specExpressionResolver; |
| 45 | + } |
| 46 | + |
| 47 | + public boolean checkAccess(Authentication auth, ProxySpec spec, AccessControl accessControl) { |
| 48 | + if (auth instanceof AnonymousAuthenticationToken) { |
| 49 | + // TODO test with parameters |
| 50 | + // if anonymous -> only allow access if the backend has no authorization enabled |
| 51 | + return !authBackend.hasAuthorization(); |
| 52 | + } |
| 53 | + |
| 54 | + if (hasAccessControl(accessControl)) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + if (allowedByGroups(auth, accessControl)) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + if (allowedByUsers(auth, accessControl)) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + if (allowedByExpression(auth, spec, accessControl)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + public boolean hasAccessControl(AccessControl accessControl) { |
| 74 | + if (accessControl == null) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + return !accessControl.hasGroupAccess() |
| 79 | + && !accessControl.hasUserAccess() |
| 80 | + && !accessControl.hasExpressionAccess(); |
| 81 | + } |
| 82 | + |
| 83 | + public boolean allowedByGroups(Authentication auth, AccessControl accessControl) { |
| 84 | + if (!accessControl.hasGroupAccess()) { |
| 85 | + // no groups defined -> this user has no access based on the groups |
| 86 | + return false; |
| 87 | + } |
| 88 | + for (String group : accessControl.getGroups()) { |
| 89 | + if (userService.isMember(auth, group)) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + public boolean allowedByUsers(Authentication auth, AccessControl accessControl) { |
| 97 | + if (!accessControl.hasUserAccess()) { |
| 98 | + // no users defined -> this user has no access based on the users |
| 99 | + return false; |
| 100 | + } |
| 101 | + for (String user : accessControl.getUsers()) { |
| 102 | + if (auth.getName().equals(user)) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + public boolean allowedByExpression(Authentication auth, ProxySpec spec, AccessControl accessControl) { |
| 110 | + if (!accessControl.hasExpressionAccess()) { |
| 111 | + // no expression defined -> this user has no access based on the expression |
| 112 | + return false; |
| 113 | + } |
| 114 | + SpecExpressionContext context = SpecExpressionContext.create(auth, auth.getPrincipal(), auth.getCredentials(), spec); |
| 115 | + return specExpressionResolver.evaluateToBoolean(accessControl.getExpression(), context); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments