blob: 059d33b515cb1427f4aac24488ac70281766af6b [file] [log] [blame]
Michael Hanle25dea22015-09-24 19:37:56 +02001package de.ids_mannheim.korap.security.ac;
2
Michael Hanldaf86602016-05-12 14:31:52 +02003import de.ids_mannheim.korap.config.ContextHolder;
4import de.ids_mannheim.korap.config.BeansFactory;
Michael Hanle25dea22015-09-24 19:37:56 +02005import de.ids_mannheim.korap.exceptions.EmptyResultException;
6import de.ids_mannheim.korap.exceptions.KustvaktException;
7import de.ids_mannheim.korap.exceptions.NotAuthorizedException;
8import de.ids_mannheim.korap.exceptions.StatusCodes;
9import de.ids_mannheim.korap.interfaces.EncryptionIface;
Michael Hanlf21773f2015-10-16 23:02:31 +020010import de.ids_mannheim.korap.interfaces.db.PolicyHandlerIface;
11import de.ids_mannheim.korap.interfaces.db.ResourceOperationIface;
Michael Hanle25dea22015-09-24 19:37:56 +020012import de.ids_mannheim.korap.resources.KustvaktResource;
13import de.ids_mannheim.korap.resources.Permissions;
Michael Hanl88b49db2016-02-16 17:15:43 +010014import de.ids_mannheim.korap.resources.ResourceFactory;
Michael Hanle25dea22015-09-24 19:37:56 +020015import de.ids_mannheim.korap.security.Parameter;
16import de.ids_mannheim.korap.security.PermissionsBuffer;
17import de.ids_mannheim.korap.security.PolicyCondition;
18import de.ids_mannheim.korap.security.SecurityPolicy;
19import de.ids_mannheim.korap.user.User;
Michael Hanle25dea22015-09-24 19:37:56 +020020import org.slf4j.Logger;
Michael Hanlac113e52016-01-19 15:49:20 +010021import org.slf4j.LoggerFactory;
Michael Hanle25dea22015-09-24 19:37:56 +020022
23import java.util.*;
24
25/**
Michael Hanl8abaf9e2016-05-23 16:46:35 +020026 * should only be used if a resource is uniquely identifiable by
27 * either three methods: id, name or path!
28 * In any other case, use categorypolicies to retrieve policies of a
29 * certain type
30 *
Michael Hanle25dea22015-09-24 19:37:56 +020031 * @author hanl
32 * @date 15/01/2014
33 */
34
35// todo: add auditing mechanism to this!
36@SuppressWarnings("all")
37public class SecurityManager<T extends KustvaktResource> {
38
Michael Hanlac113e52016-01-19 15:49:20 +010039 private static final Logger jlog = LoggerFactory
Michael Hanlefb54c42016-01-16 18:54:03 +010040 .getLogger(SecurityManager.class);
41
Michael Hanle25dea22015-09-24 19:37:56 +020042 private static PolicyHandlerIface policydao;
Michael Hanl9b84eff2016-01-27 17:11:11 +010043 @Deprecated
Michael Hanle25dea22015-09-24 19:37:56 +020044 private static Map<Class<? extends KustvaktResource>, ResourceOperationIface> handlers;
45 private static EncryptionIface crypto;
46
47 private List<SecurityPolicy>[] policies;
48 private User user;
49
Michael Hanlf0785322015-11-13 16:14:45 +010050 private boolean silent;
Michael Hanle25dea22015-09-24 19:37:56 +020051 private PolicyEvaluator evaluator;
52 private T resource;
53
Michael Hanl8abaf9e2016-05-23 16:46:35 +020054
Michael Hanl19390652016-01-16 11:01:24 +010055 //todo: use simple user id if possible! --> or if not check that user has valid integer id (or use username as fallback instead)
Michael Hanl8abaf9e2016-05-23 16:46:35 +020056 private SecurityManager (User user) {
Michael Hanle25dea22015-09-24 19:37:56 +020057 this.policies = new List[1];
58 this.policies[0] = new ArrayList<>();
Michael Hanlf0785322015-11-13 16:14:45 +010059 this.silent = true;
Michael Hanle25dea22015-09-24 19:37:56 +020060 this.user = user;
Michael Hanldaf86602016-05-12 14:31:52 +020061 overrideProviders(null);
Michael Hanlf0785322015-11-13 16:14:45 +010062 }
63
Michael Hanl8abaf9e2016-05-23 16:46:35 +020064
65 public static void overrideProviders (ContextHolder beans) {
Michael Hanldaf86602016-05-12 14:31:52 +020066 if (beans == null)
67 beans = BeansFactory.getKustvaktContext();
68 if (policydao == null | crypto == null) {
69 SecurityManager.policydao = beans.getPolicyDbProvider();
70 SecurityManager.crypto = beans.getEncryption();
Michael Hanl9b84eff2016-01-27 17:11:11 +010071 SecurityManager.handlers = new HashMap<>();
Michael Hanldaf86602016-05-12 14:31:52 +020072 Collection<ResourceOperationIface> providers = beans
Michael Hanlf8fcc7a2016-06-03 17:41:07 +020073 .getResourceProviders();
Michael Hanldaf86602016-05-12 14:31:52 +020074 for (ResourceOperationIface op : providers)
75 SecurityManager.handlers.put(op.type(), op);
Michael Hanl9b84eff2016-01-27 17:11:11 +010076 }
77 if (policydao == null && crypto == null)
Michael Hanlf0785322015-11-13 16:14:45 +010078 throw new RuntimeException("providers not set!");
Michael Hanle25dea22015-09-24 19:37:56 +020079 }
80
Michael Hanl8abaf9e2016-05-23 16:46:35 +020081
Michael Hanl9b84eff2016-01-27 17:11:11 +010082 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +020083 public static final void setProviders (PolicyHandlerIface policyHandler,
Michael Hanl6a61e2b2016-01-27 21:10:47 +010084 EncryptionIface crypto, Collection<ResourceOperationIface> ifaces) {
Michael Hanle25dea22015-09-24 19:37:56 +020085 SecurityManager.policydao = policyHandler;
86 SecurityManager.crypto = crypto;
87 SecurityManager.handlers = new HashMap<>();
Michael Hanlefb54c42016-01-16 18:54:03 +010088 jlog.info("Registering handlers: {}", Arrays.asList(ifaces));
Michael Hanlc4446022016-02-12 18:03:17 +010089 // for (ResourceOperationIface iface : ifaces)
90 // handlers.put(iface.getType(), iface);
Michael Hanle25dea22015-09-24 19:37:56 +020091 }
92
Michael Hanl8abaf9e2016-05-23 16:46:35 +020093
Michael Hanle25dea22015-09-24 19:37:56 +020094 /**
Michael Hanl8abaf9e2016-05-23 16:46:35 +020095 * only allowed if the resource is uniquely identifiable by the
96 * name, if not, use path or id!
97 * Shortcut so resource values do not need to be retrieved
98 * afterwards!
99 *
Michael Hanle25dea22015-09-24 19:37:56 +0200100 * @param name
101 * @param user
102 * @param type
103 * @return
104 * @throws EmptyResultException
105 * @throws KustvaktException
106 */
Michael Hanlf0785322015-11-13 16:14:45 +0100107 //todo: implement a fall back that throws an exception when the user NULL, but the resource has restrictions!
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200108 public static SecurityManager findbyId (String id, User user, Class type,
109 Permissions.Permission ... perms) throws KustvaktException {
Michael Hanle25dea22015-09-24 19:37:56 +0200110 SecurityManager p = new SecurityManager(user);
111 p.findPolicies(id, false, perms);
112 p.resource = p.findResource(type);
113 return p;
114 }
115
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200116
117 public static SecurityManager findbyId (String id, User user,
118 Permissions.Permission ... perms) throws KustvaktException {
Michael Hanle25dea22015-09-24 19:37:56 +0200119 SecurityManager p = new SecurityManager(user);
120 p.findPolicies(id, false, perms);
121 p.resource = p.findResource(null);
122 return p;
123 }
124
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200125
126 public static SecurityManager findbyId (Integer id, User user,
127 Permissions.Permission ... perms) throws KustvaktException {
Michael Hanle25dea22015-09-24 19:37:56 +0200128 SecurityManager p = new SecurityManager(user);
129 p.findPolicies(id, false, perms);
130 p.resource = p.findResource(null);
131 return p;
132 }
133
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200134
135 public static SecurityManager findbyPath (String path, User user,
136 Permissions.Permission ... perms) throws NotAuthorizedException,
137 EmptyResultException {
Michael Hanle25dea22015-09-24 19:37:56 +0200138 SecurityManager manager = new SecurityManager(user);
139 manager.findPolicies(path, true, perms);
140 //fixme: need a match count. if match not unique, exception. also, does parent -child relation match hold up here?
141 return manager;
142 }
143
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200144
145 public static SecurityManager init (String id, User user,
146 Permissions.Permission ... perms) throws NotAuthorizedException,
147 EmptyResultException {
Michael Hanle25dea22015-09-24 19:37:56 +0200148 SecurityManager p = new SecurityManager(user);
149 p.findPolicies(id, false, perms);
150 return p;
151 }
152
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200153
Michael Hanle25dea22015-09-24 19:37:56 +0200154 /**
155 * enables retrieval for read access only!
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200156 *
Michael Hanle25dea22015-09-24 19:37:56 +0200157 * @return
158 * @throws NotAuthorizedException
159 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200160 public final T getResource () throws NotAuthorizedException {
Michael Hanl88b49db2016-02-16 17:15:43 +0100161 if (evaluator.isAllowed(Permissions.Permission.READ)) {
Michael Hanle25dea22015-09-24 19:37:56 +0200162 return this.resource;
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200163 }
164 else {
165 jlog.error(
166 "Reading the resource '{}' is not allowed for user '{}'",
Michael Hanlefb54c42016-01-16 18:54:03 +0100167 this.resource.getPersistentID(), this.user.getUsername());
Michael Hanle25dea22015-09-24 19:37:56 +0200168 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
169 evaluator.getResourceID());
170 }
171 }
172
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200173
174 public void updateResource (T resource) throws NotAuthorizedException,
175 KustvaktException {
Michael Hanl88b49db2016-02-16 17:15:43 +0100176 if (evaluator.isAllowed(Permissions.Permission.WRITE)) {
Michael Hanle25dea22015-09-24 19:37:56 +0200177 ResourceOperationIface iface = handlers.get(resource.getClass());
178 if (iface != null)
179 iface.updateResource(resource, this.user);
180 else
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200181 handlers.get(KustvaktResource.class).updateResource(resource,
182 this.user);
183 }
184 else {
Michael Hanlefb54c42016-01-16 18:54:03 +0100185 jlog.error(
186 "Updating the resource '{}' is not allowed for user '{}'",
187 this.resource.getPersistentID(), this.user.getUsername());
Michael Hanle25dea22015-09-24 19:37:56 +0200188 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
189 this.evaluator.getResourceID());
190 }
191
192 }
193
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200194
Michael Hanle25dea22015-09-24 19:37:56 +0200195 /**
196 * @throws NotAuthorizedException
197 * @throws KustvaktException
198 */
Michael Hanl19390652016-01-16 11:01:24 +0100199 // todo: delete only works with find, not with init constructor!
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200200 public void deleteResource () throws NotAuthorizedException,
201 KustvaktException {
Michael Hanl88b49db2016-02-16 17:15:43 +0100202 if (evaluator.isAllowed(Permissions.Permission.DELETE)) {
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200203 ResourceOperationIface iface = handlers.get(this.resource
204 .getClass());
Michael Hanle25dea22015-09-24 19:37:56 +0200205 if (iface != null)
206 iface.deleteResource(this.evaluator.getResourceID(), this.user);
207 else
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200208 handlers.get(KustvaktResource.class).deleteResource(
209 this.evaluator.getResourceID(), this.user);
210 this.policydao.deleteResourcePolicies(
211 this.evaluator.getResourceID(), this.user);
212 }
213 else
Michael Hanle25dea22015-09-24 19:37:56 +0200214 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
215 this.evaluator.getResourceID());
216 }
217
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200218
Michael Hanle25dea22015-09-24 19:37:56 +0200219 // todo: type should be deprecated and return type of policies should be containers!
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200220 private boolean findPolicies (Object id, boolean path,
221 Permissions.Permission ... perms) throws EmptyResultException {
Michael Hanle25dea22015-09-24 19:37:56 +0200222 PermissionsBuffer b = new PermissionsBuffer();
223 if (perms.length == 0)
Michael Hanl88b49db2016-02-16 17:15:43 +0100224 b.addPermission(Permissions.Permission.READ.toByte());
Michael Hanle25dea22015-09-24 19:37:56 +0200225 else
226 b.addPermissions(perms);
227 if (id instanceof String && !path)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200228 this.policies = policydao.getPolicies((String) id, this.user,
229 b.getPbyte());
Michael Hanle25dea22015-09-24 19:37:56 +0200230 if (id instanceof String && path)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200231 this.policies = policydao.findPolicies((String) id, this.user,
232 b.getPbyte());
Michael Hanle25dea22015-09-24 19:37:56 +0200233 if (id instanceof Integer)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200234 this.policies = policydao.getPolicies((Integer) id, this.user,
235 b.getPbyte());
Michael Hanle25dea22015-09-24 19:37:56 +0200236 this.evaluator = new PolicyEvaluator(this.user, this.policies);
237
238 if (this.policies == null) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100239 jlog.error("No policies found for resource id '{}' for user '{}'",
240 id, user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200241 throw new EmptyResultException(String.valueOf(id));
242 }
243 return true;
244 }
245
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200246
Michael Hanl19390652016-01-16 11:01:24 +0100247 // todo: security log shows id 'null' --> better way?
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200248 private T findResource (Class type) throws NotAuthorizedException,
249 KustvaktException {
Michael Hanle25dea22015-09-24 19:37:56 +0200250 if (!evaluator.isAllowed()) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100251 jlog.error("Permission denied for resource id '{}' for user '{}'",
252 this.evaluator.getResourceID(), user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200253 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
254 this.evaluator.getResourceID());
255 }
256
257 ResourceOperationIface iface = handlers.get(type);
258 if (iface == null)
259 iface = handlers.get(KustvaktResource.class);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200260 T resource = (T) iface.findbyId(this.evaluator.getResourceID(),
261 this.user);
Michael Hanldaf86602016-05-12 14:31:52 +0200262 // fixme: this
263 // fixme: deprecated!
Michael Hanle25dea22015-09-24 19:37:56 +0200264 resource.setManaged(this.evaluator.isManaged());
265 resource.setShared(this.evaluator.isShared());
266 return resource;
267 }
268
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200269
270 private boolean checkResource (String persistentID, User user)
Michael Hanle25dea22015-09-24 19:37:56 +0200271 throws KustvaktException {
272 ResourceOperationIface iface = handlers.get(KustvaktResource.class);
273 return iface.findbyId(persistentID, user) != null;
274 }
275
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200276
277 public static SecurityManager register (KustvaktResource resource, User user)
Michael Hanle25dea22015-09-24 19:37:56 +0200278 throws KustvaktException, NotAuthorizedException {
279 SecurityManager p = new SecurityManager(user);
Michael Hanldaf86602016-05-12 14:31:52 +0200280 if (!User.UserFactory.isDemo(user.getUsername())) {
Michael Hanle25dea22015-09-24 19:37:56 +0200281 if (resource.getParentID() != null) {
282 try {
283 // the owner has all rights per default, in order to be able derivate from a parent resource, he needs all permissions as well
284 // this is mostly for convenvience and database consistency, since a request query would result in not authorized, based on missing parent relation dependencies
285 // --> in order not to have a resource owner that is denied access due to missing parent relation dependency
286 SecurityManager.findbyId(resource.getParentID(), user,
Michael Hanl88b49db2016-02-16 17:15:43 +0100287 Permissions.Permission.ALL);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200288 }
289 catch (EmptyResultException e) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100290 jlog.error(
291 "No policies found for parent '{}' for user '{}'",
292 resource.getParentID(), user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200293 throw new KustvaktException(StatusCodes.EMPTY_RESULTS);
294 }
295 }
296 boolean newid = false;
297 // create persistent identifier for the resource
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200298 if (resource.getPersistentID() == null
299 || resource.getPersistentID().isEmpty()) {
Michael Hanl88b49db2016-02-16 17:15:43 +0100300 ResourceFactory.createID(resource);
Michael Hanle25dea22015-09-24 19:37:56 +0200301 newid = true;
302 }
303
304 if (newid | !p.checkResource(resource.getPersistentID(), user)) {
Michael Hanlc4446022016-02-12 18:03:17 +0100305 // resource.setOwner(user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200306
Michael Hanlefb54c42016-01-16 18:54:03 +0100307 jlog.info("Creating Access Control structure for resource '"
308 + resource.getPersistentID() + "@" + resource.getId()
Michael Hanlf1e85e72016-01-21 16:55:45 +0100309 + "', name: " + resource.getName());
Michael Hanle25dea22015-09-24 19:37:56 +0200310 // storing resource is called twice. first when this is register and later in idsbootstrap to create cstorage entry. how to unify this?
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200311 ResourceOperationIface iface = p.handlers.get(resource
312 .getClass());
Michael Hanle25dea22015-09-24 19:37:56 +0200313 if (iface != null)
314 resource.setId(iface.storeResource(resource, user));
315 else
316 // retrieve default handler for resource!
317 resource.setId(p.handlers.get(KustvaktResource.class)
318 .storeResource(resource, user));
319 }
320 p.resource = resource;
321 try {
Michael Hanl19390652016-01-16 11:01:24 +0100322 // todo: which is better? Integer id or String persistentID?
323 p.findPolicies(resource.getPersistentID(), false,
Michael Hanl88b49db2016-02-16 17:15:43 +0100324 Permissions.Permission.CREATE_POLICY,
325 Permissions.Permission.READ_POLICY,
326 Permissions.Permission.MODIFY_POLICY);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200327 }
328 catch (EmptyResultException e) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100329 jlog.error(
330 "No policies found for '{}' for user '{}'. Resource could not be registered!",
331 resource.getPersistentID(), user.getId());
Michael Hanlf0785322015-11-13 16:14:45 +0100332 throw new KustvaktException(user.getId(),
333 StatusCodes.POLICY_CREATE_ERROR,
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200334 "Resource could not be registered", resource.toString());
Michael Hanle25dea22015-09-24 19:37:56 +0200335 }
336 }
337 return p;
338 }
339
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200340
Michael Hanle25dea22015-09-24 19:37:56 +0200341 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200342 public List<SecurityPolicy> getPoliciesList (int i) {
Michael Hanle25dea22015-09-24 19:37:56 +0200343 if (i < this.policies.length)
344 return this.policies[i];
345 return Collections.emptyList();
346 }
347
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200348
Michael Hanle25dea22015-09-24 19:37:56 +0200349 // fixme: make protected
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200350 public SecurityPolicy getPolicy (Integer id) {
Michael Hanle25dea22015-09-24 19:37:56 +0200351 for (SecurityPolicy p : this.policies[0])
352 if (p.getID() == id)
353 return p;
354 return null;
355 }
356
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200357
Michael Hanle25dea22015-09-24 19:37:56 +0200358 // fixme: make protected
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200359 public PolicyCondition getExtensional (Permissions.Permission ... pps) {
Michael Hanle25dea22015-09-24 19:37:56 +0200360 for (SecurityPolicy p : this.policies[0]) {
361 if (p.equalsPermission(pps)) {
362 for (PolicyCondition c : p.getConditions()) {
363 if (c.isExtensional())
364 return c;
365 }
366 }
367 }
368 return null;
369 }
370
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200371
372 private boolean matchTarget (String target) {
373 return this.resource.getPersistentID() != null
374 && (this.resource.getPersistentID() == target);
Michael Hanle25dea22015-09-24 19:37:56 +0200375 }
376
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200377
378 public void addPolicy (SecurityPolicy policy, Parameter ... params)
Michael Hanle25dea22015-09-24 19:37:56 +0200379 throws KustvaktException, NotAuthorizedException {
380 if (policy.getConditions().isEmpty()) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100381 jlog.error("No conditions set for '{}' for user '{}'",
382 policy.toString(), this.user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200383 throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT,
384 policy.getTarget());
385 }
386
387 if (this.policies[0] == null) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100388 jlog.error("No policies found for '{}' for user '{}'",
389 this.evaluator.getResourceID(), this.user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200390 throw new NotAuthorizedException(StatusCodes.UNSUPPORTED_OPERATION,
391 policy.getTarget());
392 }
393
394 if (contains(policy)) {
395 modifyPolicy(policy);
396 return;
397 }
398
Michael Hanl88b49db2016-02-16 17:15:43 +0100399 if (evaluator.isAllowed(Permissions.Permission.CREATE_POLICY)) {
Michael Hanle25dea22015-09-24 19:37:56 +0200400 policydao.createPolicy(policy, this.user);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200401 }
402 else if (silent) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100403 jlog.error(
404 "Permission Denied (CREATE_POLICY) on '{}' for user '{}'",
405 this.evaluator.getResourceID(), this.user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200406 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
407 policy.getTarget());
408 }
409
410 if (params != null && params.length > 0) {
411 for (Parameter p : params) {
412 p.setPolicy(policy);
413 policydao.createParamBinding(p);
414 }
415 }
416 this.policies[0].add(policy);
Michael Hanlf1e85e72016-01-21 16:55:45 +0100417 try {
418 Thread.sleep(5);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200419 }
420 catch (InterruptedException e) {
Michael Hanlf1e85e72016-01-21 16:55:45 +0100421 e.printStackTrace();
422 }
Michael Hanle25dea22015-09-24 19:37:56 +0200423 }
424
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200425
426 public void deletePolicies () throws NotAuthorizedException,
427 KustvaktException {
Michael Hanle25dea22015-09-24 19:37:56 +0200428 for (SecurityPolicy p : new ArrayList<>(this.policies[0]))
429 deletePolicy(p);
430 }
431
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200432
433 public void retainPolicies (List<SecurityPolicy> policies)
Michael Hanle25dea22015-09-24 19:37:56 +0200434 throws NotAuthorizedException, KustvaktException {
435 for (SecurityPolicy p : new ArrayList<>(this.policies[0])) {
436 if (!policies.contains(p))
437 this.deletePolicy(p);
438 }
439 }
440
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200441
Michael Hanldaf86602016-05-12 14:31:52 +0200442 // todo:
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200443 public void deletePolicy (SecurityPolicy policy) throws KustvaktException,
444 NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200445 // todo: get rid of this: use sql to match policy id and target according to evaluator!
446 if (!matchTarget(policy.getTarget()))
447 // adjust message
448 throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT,
449 this.evaluator.getResourceID());
450
451 if (this.policies[0] == null) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100452 jlog.error("No policies found (DELETE_POLICY) on '{}' for '{}'",
453 this.evaluator.getResourceID(), this.user.getId());
Michael Hanlf0785322015-11-13 16:14:45 +0100454 throw new KustvaktException(user.getId(), StatusCodes.NO_POLICIES,
Michael Hanle25dea22015-09-24 19:37:56 +0200455 "no policy desicion possible",
456 this.evaluator.getResourceID());
457 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200458 if (contains(policy)
459 && (evaluator.isAllowed(Permissions.Permission.DELETE_POLICY))) {
Michael Hanle25dea22015-09-24 19:37:56 +0200460 policydao.deletePolicy(policy, this.user);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200461 }
462 else if (silent) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100463 jlog.error("Permission Denied (DELETE_POLICY) on '{}' for '{}'",
464 this.evaluator.getResourceID(), this.user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200465 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
466 "no policy desicion possible",
467 this.evaluator.getResourceID());
468 }
469 policydao.removeParamBinding(policy);
470
471 this.policies[0].remove(policy);
472 }
473
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200474
475 public void modifyPolicy (SecurityPolicy policy) throws KustvaktException,
476 NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200477 if (!matchTarget(policy.getTarget()))
478 throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT);
479
480 if (this.policies[0] == null) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100481 jlog.error(
482 "Operation not possible (MODIFY_POLICY) on '{}' for '{}'",
483 this.evaluator.getResourceID(), this.user.getId());
Michael Hanlf0785322015-11-13 16:14:45 +0100484 throw new KustvaktException(user.getId(), StatusCodes.NO_POLICIES,
Michael Hanle25dea22015-09-24 19:37:56 +0200485 "no policy desicion possible",
486 this.evaluator.getResourceID());
487 }
488
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200489 if (contains(policy)
490 && (evaluator.isAllowed(Permissions.Permission.MODIFY_POLICY))) {
Michael Hanle25dea22015-09-24 19:37:56 +0200491 policydao.updatePolicy(policy, this.user);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200492 }
493 else if (silent) {
Michael Hanlefb54c42016-01-16 18:54:03 +0100494 jlog.error("Permission Denied (DELETE_POLICY) on '{}' for '{}'",
495 this.evaluator.getResourceID(), this.user.getId());
Michael Hanle25dea22015-09-24 19:37:56 +0200496 throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED,
497 this.evaluator.getResourceID());
498 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200499 this.policies = policydao.getPolicies((int) this.resource.getId(),
500 this.user, null);
Michael Hanle25dea22015-09-24 19:37:56 +0200501 }
502
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200503
Michael Hanle25dea22015-09-24 19:37:56 +0200504 /**
505 * standard function for READ access on the resource
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200506 *
Michael Hanle25dea22015-09-24 19:37:56 +0200507 * @return boolean is action allowed for resource
508 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200509 public boolean isAllowed () {
Michael Hanle25dea22015-09-24 19:37:56 +0200510 return evaluator.isAllowed();
511 }
512
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200513
514 public boolean isAllowed (Permissions.Permission ... perm) {
Michael Hanle25dea22015-09-24 19:37:56 +0200515 return evaluator.isAllowed();
516 }
517
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200518
Michael Hanle25dea22015-09-24 19:37:56 +0200519 /**
520 * checks if that exact object already exists (compares name,
521 * conditional parameter)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200522 *
Michael Hanle25dea22015-09-24 19:37:56 +0200523 * @param policy
524 * @return
525 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200526 public boolean contains (SecurityPolicy policy) {
Michael Hanle25dea22015-09-24 19:37:56 +0200527 try {
528 return policydao.checkPolicy(policy, this.user) == 1;
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200529 }
530 catch (KustvaktException e) {
Michael Hanle25dea22015-09-24 19:37:56 +0200531 return false;
532 }
533 }
Michael Hanle25dea22015-09-24 19:37:56 +0200534}