|
| 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.ProxyAccessControl; |
| 25 | +import eu.openanalytics.containerproxy.model.spec.ProxySpec; |
| 26 | +import eu.openanalytics.containerproxy.spec.IProxySpecProvider; |
| 27 | +import eu.openanalytics.containerproxy.spec.expression.SpecExpressionContext; |
| 28 | +import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver; |
| 29 | +import org.apache.commons.lang3.tuple.Pair; |
| 30 | +import org.springframework.context.annotation.Lazy; |
| 31 | +import org.springframework.context.event.EventListener; |
| 32 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 33 | +import org.springframework.security.core.Authentication; |
| 34 | +import org.springframework.security.web.session.HttpSessionDestroyedEvent; |
| 35 | +import org.springframework.stereotype.Service; |
| 36 | +import org.springframework.web.context.request.RequestAttributes; |
| 37 | +import org.springframework.web.context.request.RequestContextHolder; |
| 38 | + |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | +import java.util.concurrent.ConcurrentHashMap; |
| 42 | + |
| 43 | +@Service |
| 44 | +public class AccessControlService { |
| 45 | + |
| 46 | + private final IAuthenticationBackend authBackend; |
| 47 | + private final UserService userService; |
| 48 | + private final IProxySpecProvider specProvider; |
| 49 | + private final SpecExpressionResolver specExpressionResolver; |
| 50 | + |
| 51 | + /* This map is used to cache whether a user has access to an app or not. |
| 52 | + * The reason is two-fold: |
| 53 | + * - for every request made (including static files of apps etc) the access control is checked |
| 54 | + * - when using the `access-expression` feature, checking the access control means evaluating a SpEL expression |
| 55 | + * I.e. the check can be complex and is performed a lot. |
| 56 | + * This cache uses the SessionId of the user and not the userId for two reasons: |
| 57 | + * - this ensures that the key is unique |
| 58 | + * - the roles/properties of a user change when they re-login |
| 59 | + */ |
| 60 | + private final Map<Pair<String, String>, Boolean> authorizationCache = new ConcurrentHashMap<>(); |
| 61 | + |
| 62 | + public AccessControlService(@Lazy IAuthenticationBackend authBackend, UserService userService, IProxySpecProvider specProvider, SpecExpressionResolver specExpressionResolver) { |
| 63 | + this.authBackend = authBackend; |
| 64 | + this.userService = userService; |
| 65 | + this.specProvider = specProvider; |
| 66 | + this.specExpressionResolver = specExpressionResolver; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean canAccess(Authentication auth, String specId) { |
| 70 | + return canAccess(auth, specProvider.getSpec(specId)); |
| 71 | + } |
| 72 | + |
| 73 | + public boolean canAccess(Authentication auth, ProxySpec spec) { |
| 74 | + Optional<String> sessionId = getSessionId(); |
| 75 | + if (!sessionId.isPresent()) { |
| 76 | + return checkAccess(auth, spec); |
| 77 | + } |
| 78 | + // we got a sessionId -> use the cache |
| 79 | + return authorizationCache.computeIfAbsent( |
| 80 | + Pair.of(sessionId.get(), spec.getId()), |
| 81 | + (k) -> checkAccess(auth, spec)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return the sessionId if the RequestContext is present |
| 86 | + */ |
| 87 | + private Optional<String> getSessionId() { |
| 88 | + return Optional |
| 89 | + .ofNullable(RequestContextHolder.getRequestAttributes()) |
| 90 | + .map(RequestAttributes::getSessionId); |
| 91 | + } |
| 92 | + |
| 93 | + private boolean checkAccess(Authentication auth, ProxySpec spec) { |
| 94 | + if (auth == null || spec == null) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + if (auth instanceof AnonymousAuthenticationToken) { |
| 99 | + // if anonymous -> only allow access if we the backend has no authorization enabled |
| 100 | + return !authBackend.hasAuthorization(); |
| 101 | + } |
| 102 | + |
| 103 | + if (specHasNoAccessControl(spec.getAccessControl())) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + if (allowedByGroups(auth, spec)) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + if (allowedByUsers(auth, spec)) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + if (allowedByExpression(auth, spec)) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + public boolean specHasNoAccessControl(ProxyAccessControl accessControl) { |
| 123 | + if (accessControl == null) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + return !accessControl.hasGroupAccess() |
| 128 | + && !accessControl.hasUserAccess() |
| 129 | + && !accessControl.hasExpressionAccess(); |
| 130 | + } |
| 131 | + |
| 132 | + public boolean allowedByGroups(Authentication auth, ProxySpec spec) { |
| 133 | + if (!spec.getAccessControl().hasGroupAccess()) { |
| 134 | + // no groups defined -> this user has no access based on the groups |
| 135 | + return false; |
| 136 | + } |
| 137 | + for (String group : spec.getAccessControl().getGroups()) { |
| 138 | + if (userService.isMember(auth, group)) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + } |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + public boolean allowedByUsers(Authentication auth, ProxySpec spec) { |
| 146 | + if (!spec.getAccessControl().hasUserAccess()) { |
| 147 | + // no users defined -> this user has no access based on the users |
| 148 | + return false; |
| 149 | + } |
| 150 | + for (String user : spec.getAccessControl().getUsers()) { |
| 151 | + if (auth.getName().equals(user)) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + public boolean allowedByExpression(Authentication auth, ProxySpec spec) { |
| 159 | + if (!spec.getAccessControl().hasExpressionAccess()) { |
| 160 | + // no expression defined -> this user has no access based on the expression |
| 161 | + return false; |
| 162 | + } |
| 163 | + SpecExpressionContext context = SpecExpressionContext.create(auth, auth.getPrincipal(), auth.getCredentials(), spec); |
| 164 | + return specExpressionResolver.evaluateToBoolean(spec.getAccessControl().getExpression(), context); |
| 165 | + } |
| 166 | + |
| 167 | + @EventListener |
| 168 | + public void onSessionDestroyedEvent(HttpSessionDestroyedEvent event) { |
| 169 | + // remove all entries in cache for this sessionId |
| 170 | + authorizationCache.keySet().removeIf(it -> it.getLeft().equals(event.getId())); |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments