| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.security.ac; |
| 2 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.config.ContextHolder; |
| 4 | import de.ids_mannheim.korap.config.BeansFactory; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.exceptions.EmptyResultException; |
| 6 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 7 | import de.ids_mannheim.korap.exceptions.NotAuthorizedException; |
| 8 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 9 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 10 | import de.ids_mannheim.korap.interfaces.db.PolicyHandlerIface; |
| 11 | import de.ids_mannheim.korap.interfaces.db.ResourceOperationIface; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 12 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| 13 | import de.ids_mannheim.korap.resources.Permissions; |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 14 | import de.ids_mannheim.korap.resources.ResourceFactory; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 15 | import de.ids_mannheim.korap.security.Parameter; |
| 16 | import de.ids_mannheim.korap.security.PermissionsBuffer; |
| 17 | import de.ids_mannheim.korap.security.PolicyCondition; |
| 18 | import de.ids_mannheim.korap.security.SecurityPolicy; |
| 19 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 21 | import org.slf4j.LoggerFactory; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 22 | |
| 23 | import java.util.*; |
| 24 | |
| 25 | /** |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 26 | * 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 Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 31 | * @author hanl |
| 32 | * @date 15/01/2014 |
| 33 | */ |
| 34 | |
| 35 | // todo: add auditing mechanism to this! |
| 36 | @SuppressWarnings("all") |
| 37 | public class SecurityManager<T extends KustvaktResource> { |
| 38 | |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 39 | private static final Logger jlog = LoggerFactory |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 40 | .getLogger(SecurityManager.class); |
| 41 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 42 | private static PolicyHandlerIface policydao; |
| Michael Hanl | 9b84eff | 2016-01-27 17:11:11 +0100 | [diff] [blame] | 43 | @Deprecated |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 44 | 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 Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 50 | private boolean silent; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 51 | private PolicyEvaluator evaluator; |
| 52 | private T resource; |
| 53 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 54 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 55 | //todo: use simple user id if possible! --> or if not check that user has valid integer id (or use username as fallback instead) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 56 | private SecurityManager (User user) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 57 | this.policies = new List[1]; |
| 58 | this.policies[0] = new ArrayList<>(); |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 59 | this.silent = true; |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 60 | this.user = user; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 61 | overrideProviders(null); |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 64 | |
| 65 | public static void overrideProviders (ContextHolder beans) { |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 66 | if (beans == null) |
| 67 | beans = BeansFactory.getKustvaktContext(); |
| 68 | if (policydao == null | crypto == null) { |
| 69 | SecurityManager.policydao = beans.getPolicyDbProvider(); |
| 70 | SecurityManager.crypto = beans.getEncryption(); |
| Michael Hanl | 9b84eff | 2016-01-27 17:11:11 +0100 | [diff] [blame] | 71 | SecurityManager.handlers = new HashMap<>(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 72 | Collection<ResourceOperationIface> providers = beans |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 73 | .getResourceProviders(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 74 | for (ResourceOperationIface op : providers) |
| 75 | SecurityManager.handlers.put(op.type(), op); |
| Michael Hanl | 9b84eff | 2016-01-27 17:11:11 +0100 | [diff] [blame] | 76 | } |
| 77 | if (policydao == null && crypto == null) |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 78 | throw new RuntimeException("providers not set!"); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 81 | |
| Michael Hanl | 9b84eff | 2016-01-27 17:11:11 +0100 | [diff] [blame] | 82 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 83 | public static final void setProviders (PolicyHandlerIface policyHandler, |
| Michael Hanl | 6a61e2b | 2016-01-27 21:10:47 +0100 | [diff] [blame] | 84 | EncryptionIface crypto, Collection<ResourceOperationIface> ifaces) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 85 | SecurityManager.policydao = policyHandler; |
| 86 | SecurityManager.crypto = crypto; |
| 87 | SecurityManager.handlers = new HashMap<>(); |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 88 | jlog.info("Registering handlers: {}", Arrays.asList(ifaces)); |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 89 | // for (ResourceOperationIface iface : ifaces) |
| 90 | // handlers.put(iface.getType(), iface); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 93 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 94 | /** |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 95 | * 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 Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 100 | * @param name |
| 101 | * @param user |
| 102 | * @param type |
| 103 | * @return |
| 104 | * @throws EmptyResultException |
| 105 | * @throws KustvaktException |
| 106 | */ |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 107 | //todo: implement a fall back that throws an exception when the user NULL, but the resource has restrictions! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 108 | public static SecurityManager findbyId (String id, User user, Class type, |
| 109 | Permissions.Permission ... perms) throws KustvaktException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 110 | SecurityManager p = new SecurityManager(user); |
| 111 | p.findPolicies(id, false, perms); |
| 112 | p.resource = p.findResource(type); |
| 113 | return p; |
| 114 | } |
| 115 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 116 | |
| 117 | public static SecurityManager findbyId (String id, User user, |
| 118 | Permissions.Permission ... perms) throws KustvaktException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 119 | SecurityManager p = new SecurityManager(user); |
| 120 | p.findPolicies(id, false, perms); |
| 121 | p.resource = p.findResource(null); |
| 122 | return p; |
| 123 | } |
| 124 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 125 | |
| 126 | public static SecurityManager findbyId (Integer id, User user, |
| 127 | Permissions.Permission ... perms) throws KustvaktException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 128 | SecurityManager p = new SecurityManager(user); |
| 129 | p.findPolicies(id, false, perms); |
| 130 | p.resource = p.findResource(null); |
| 131 | return p; |
| 132 | } |
| 133 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 134 | |
| 135 | public static SecurityManager findbyPath (String path, User user, |
| 136 | Permissions.Permission ... perms) throws NotAuthorizedException, |
| 137 | EmptyResultException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 138 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 144 | |
| 145 | public static SecurityManager init (String id, User user, |
| 146 | Permissions.Permission ... perms) throws NotAuthorizedException, |
| 147 | EmptyResultException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 148 | SecurityManager p = new SecurityManager(user); |
| 149 | p.findPolicies(id, false, perms); |
| 150 | return p; |
| 151 | } |
| 152 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 153 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 154 | /** |
| 155 | * enables retrieval for read access only! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 156 | * |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 157 | * @return |
| 158 | * @throws NotAuthorizedException |
| 159 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 160 | public final T getResource () throws NotAuthorizedException { |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 161 | if (evaluator.isAllowed(Permissions.Permission.READ)) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 162 | return this.resource; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 163 | } |
| 164 | else { |
| 165 | jlog.error( |
| 166 | "Reading the resource '{}' is not allowed for user '{}'", |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 167 | this.resource.getPersistentID(), this.user.getUsername()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 168 | throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED, |
| 169 | evaluator.getResourceID()); |
| 170 | } |
| 171 | } |
| 172 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 173 | |
| 174 | public void updateResource (T resource) throws NotAuthorizedException, |
| 175 | KustvaktException { |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 176 | if (evaluator.isAllowed(Permissions.Permission.WRITE)) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 177 | ResourceOperationIface iface = handlers.get(resource.getClass()); |
| 178 | if (iface != null) |
| 179 | iface.updateResource(resource, this.user); |
| 180 | else |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 181 | handlers.get(KustvaktResource.class).updateResource(resource, |
| 182 | this.user); |
| 183 | } |
| 184 | else { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 185 | jlog.error( |
| 186 | "Updating the resource '{}' is not allowed for user '{}'", |
| 187 | this.resource.getPersistentID(), this.user.getUsername()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 188 | throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED, |
| 189 | this.evaluator.getResourceID()); |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 194 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 195 | /** |
| 196 | * @throws NotAuthorizedException |
| 197 | * @throws KustvaktException |
| 198 | */ |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 199 | // todo: delete only works with find, not with init constructor! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 200 | public void deleteResource () throws NotAuthorizedException, |
| 201 | KustvaktException { |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 202 | if (evaluator.isAllowed(Permissions.Permission.DELETE)) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 203 | ResourceOperationIface iface = handlers.get(this.resource |
| 204 | .getClass()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 205 | if (iface != null) |
| 206 | iface.deleteResource(this.evaluator.getResourceID(), this.user); |
| 207 | else |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 208 | 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 Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 214 | throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED, |
| 215 | this.evaluator.getResourceID()); |
| 216 | } |
| 217 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 218 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 219 | // todo: type should be deprecated and return type of policies should be containers! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 220 | private boolean findPolicies (Object id, boolean path, |
| 221 | Permissions.Permission ... perms) throws EmptyResultException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 222 | PermissionsBuffer b = new PermissionsBuffer(); |
| 223 | if (perms.length == 0) |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 224 | b.addPermission(Permissions.Permission.READ.toByte()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 225 | else |
| 226 | b.addPermissions(perms); |
| 227 | if (id instanceof String && !path) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 228 | this.policies = policydao.getPolicies((String) id, this.user, |
| 229 | b.getPbyte()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 230 | if (id instanceof String && path) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 231 | this.policies = policydao.findPolicies((String) id, this.user, |
| 232 | b.getPbyte()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 233 | if (id instanceof Integer) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 234 | this.policies = policydao.getPolicies((Integer) id, this.user, |
| 235 | b.getPbyte()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 236 | this.evaluator = new PolicyEvaluator(this.user, this.policies); |
| 237 | |
| 238 | if (this.policies == null) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 239 | jlog.error("No policies found for resource id '{}' for user '{}'", |
| 240 | id, user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 241 | throw new EmptyResultException(String.valueOf(id)); |
| 242 | } |
| 243 | return true; |
| 244 | } |
| 245 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 246 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 247 | // todo: security log shows id 'null' --> better way? |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 248 | private T findResource (Class type) throws NotAuthorizedException, |
| 249 | KustvaktException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 250 | if (!evaluator.isAllowed()) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 251 | jlog.error("Permission denied for resource id '{}' for user '{}'", |
| 252 | this.evaluator.getResourceID(), user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 253 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 260 | T resource = (T) iface.findbyId(this.evaluator.getResourceID(), |
| 261 | this.user); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 262 | // fixme: this |
| 263 | // fixme: deprecated! |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 264 | resource.setManaged(this.evaluator.isManaged()); |
| 265 | resource.setShared(this.evaluator.isShared()); |
| 266 | return resource; |
| 267 | } |
| 268 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 269 | |
| 270 | private boolean checkResource (String persistentID, User user) |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 271 | throws KustvaktException { |
| 272 | ResourceOperationIface iface = handlers.get(KustvaktResource.class); |
| 273 | return iface.findbyId(persistentID, user) != null; |
| 274 | } |
| 275 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 276 | |
| 277 | public static SecurityManager register (KustvaktResource resource, User user) |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 278 | throws KustvaktException, NotAuthorizedException { |
| 279 | SecurityManager p = new SecurityManager(user); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 280 | if (!User.UserFactory.isDemo(user.getUsername())) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 281 | 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 Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 287 | Permissions.Permission.ALL); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 288 | } |
| 289 | catch (EmptyResultException e) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 290 | jlog.error( |
| 291 | "No policies found for parent '{}' for user '{}'", |
| 292 | resource.getParentID(), user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 293 | throw new KustvaktException(StatusCodes.EMPTY_RESULTS); |
| 294 | } |
| 295 | } |
| 296 | boolean newid = false; |
| 297 | // create persistent identifier for the resource |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 298 | if (resource.getPersistentID() == null |
| 299 | || resource.getPersistentID().isEmpty()) { |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 300 | ResourceFactory.createID(resource); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 301 | newid = true; |
| 302 | } |
| 303 | |
| 304 | if (newid | !p.checkResource(resource.getPersistentID(), user)) { |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 305 | // resource.setOwner(user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 306 | |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 307 | jlog.info("Creating Access Control structure for resource '" |
| 308 | + resource.getPersistentID() + "@" + resource.getId() |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 309 | + "', name: " + resource.getName()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 310 | // storing resource is called twice. first when this is register and later in idsbootstrap to create cstorage entry. how to unify this? |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 311 | ResourceOperationIface iface = p.handlers.get(resource |
| 312 | .getClass()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 313 | 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 Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 322 | // todo: which is better? Integer id or String persistentID? |
| 323 | p.findPolicies(resource.getPersistentID(), false, |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 324 | Permissions.Permission.CREATE_POLICY, |
| 325 | Permissions.Permission.READ_POLICY, |
| 326 | Permissions.Permission.MODIFY_POLICY); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 327 | } |
| 328 | catch (EmptyResultException e) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 329 | jlog.error( |
| 330 | "No policies found for '{}' for user '{}'. Resource could not be registered!", |
| 331 | resource.getPersistentID(), user.getId()); |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 332 | throw new KustvaktException(user.getId(), |
| 333 | StatusCodes.POLICY_CREATE_ERROR, |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 334 | "Resource could not be registered", resource.toString()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | return p; |
| 338 | } |
| 339 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 340 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 341 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 342 | public List<SecurityPolicy> getPoliciesList (int i) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 343 | if (i < this.policies.length) |
| 344 | return this.policies[i]; |
| 345 | return Collections.emptyList(); |
| 346 | } |
| 347 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 348 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 349 | // fixme: make protected |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 350 | public SecurityPolicy getPolicy (Integer id) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 351 | for (SecurityPolicy p : this.policies[0]) |
| 352 | if (p.getID() == id) |
| 353 | return p; |
| 354 | return null; |
| 355 | } |
| 356 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 357 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 358 | // fixme: make protected |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 359 | public PolicyCondition getExtensional (Permissions.Permission ... pps) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 360 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 371 | |
| 372 | private boolean matchTarget (String target) { |
| 373 | return this.resource.getPersistentID() != null |
| 374 | && (this.resource.getPersistentID() == target); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 377 | |
| 378 | public void addPolicy (SecurityPolicy policy, Parameter ... params) |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 379 | throws KustvaktException, NotAuthorizedException { |
| 380 | if (policy.getConditions().isEmpty()) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 381 | jlog.error("No conditions set for '{}' for user '{}'", |
| 382 | policy.toString(), this.user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 383 | throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT, |
| 384 | policy.getTarget()); |
| 385 | } |
| 386 | |
| 387 | if (this.policies[0] == null) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 388 | jlog.error("No policies found for '{}' for user '{}'", |
| 389 | this.evaluator.getResourceID(), this.user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 390 | throw new NotAuthorizedException(StatusCodes.UNSUPPORTED_OPERATION, |
| 391 | policy.getTarget()); |
| 392 | } |
| 393 | |
| 394 | if (contains(policy)) { |
| 395 | modifyPolicy(policy); |
| 396 | return; |
| 397 | } |
| 398 | |
| Michael Hanl | 88b49db | 2016-02-16 17:15:43 +0100 | [diff] [blame] | 399 | if (evaluator.isAllowed(Permissions.Permission.CREATE_POLICY)) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 400 | policydao.createPolicy(policy, this.user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 401 | } |
| 402 | else if (silent) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 403 | jlog.error( |
| 404 | "Permission Denied (CREATE_POLICY) on '{}' for user '{}'", |
| 405 | this.evaluator.getResourceID(), this.user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 406 | 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 Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 417 | try { |
| 418 | Thread.sleep(5); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 419 | } |
| 420 | catch (InterruptedException e) { |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 421 | e.printStackTrace(); |
| 422 | } |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 425 | |
| 426 | public void deletePolicies () throws NotAuthorizedException, |
| 427 | KustvaktException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 428 | for (SecurityPolicy p : new ArrayList<>(this.policies[0])) |
| 429 | deletePolicy(p); |
| 430 | } |
| 431 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 432 | |
| 433 | public void retainPolicies (List<SecurityPolicy> policies) |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 434 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 441 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 442 | // todo: |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 443 | public void deletePolicy (SecurityPolicy policy) throws KustvaktException, |
| 444 | NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 445 | // 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 Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 452 | jlog.error("No policies found (DELETE_POLICY) on '{}' for '{}'", |
| 453 | this.evaluator.getResourceID(), this.user.getId()); |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 454 | throw new KustvaktException(user.getId(), StatusCodes.NO_POLICIES, |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 455 | "no policy desicion possible", |
| 456 | this.evaluator.getResourceID()); |
| 457 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 458 | if (contains(policy) |
| 459 | && (evaluator.isAllowed(Permissions.Permission.DELETE_POLICY))) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 460 | policydao.deletePolicy(policy, this.user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 461 | } |
| 462 | else if (silent) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 463 | jlog.error("Permission Denied (DELETE_POLICY) on '{}' for '{}'", |
| 464 | this.evaluator.getResourceID(), this.user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 465 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 474 | |
| 475 | public void modifyPolicy (SecurityPolicy policy) throws KustvaktException, |
| 476 | NotAuthorizedException { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 477 | if (!matchTarget(policy.getTarget())) |
| 478 | throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT); |
| 479 | |
| 480 | if (this.policies[0] == null) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 481 | jlog.error( |
| 482 | "Operation not possible (MODIFY_POLICY) on '{}' for '{}'", |
| 483 | this.evaluator.getResourceID(), this.user.getId()); |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 484 | throw new KustvaktException(user.getId(), StatusCodes.NO_POLICIES, |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 485 | "no policy desicion possible", |
| 486 | this.evaluator.getResourceID()); |
| 487 | } |
| 488 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 489 | if (contains(policy) |
| 490 | && (evaluator.isAllowed(Permissions.Permission.MODIFY_POLICY))) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 491 | policydao.updatePolicy(policy, this.user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 492 | } |
| 493 | else if (silent) { |
| Michael Hanl | efb54c4 | 2016-01-16 18:54:03 +0100 | [diff] [blame] | 494 | jlog.error("Permission Denied (DELETE_POLICY) on '{}' for '{}'", |
| 495 | this.evaluator.getResourceID(), this.user.getId()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 496 | throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED, |
| 497 | this.evaluator.getResourceID()); |
| 498 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 499 | this.policies = policydao.getPolicies((int) this.resource.getId(), |
| 500 | this.user, null); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 503 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 504 | /** |
| 505 | * standard function for READ access on the resource |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 506 | * |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 507 | * @return boolean is action allowed for resource |
| 508 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 509 | public boolean isAllowed () { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 510 | return evaluator.isAllowed(); |
| 511 | } |
| 512 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 513 | |
| 514 | public boolean isAllowed (Permissions.Permission ... perm) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 515 | return evaluator.isAllowed(); |
| 516 | } |
| 517 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 518 | |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 519 | /** |
| 520 | * checks if that exact object already exists (compares name, |
| 521 | * conditional parameter) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 522 | * |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 523 | * @param policy |
| 524 | * @return |
| 525 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 526 | public boolean contains (SecurityPolicy policy) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 527 | try { |
| 528 | return policydao.checkPolicy(policy, this.user) == 1; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 529 | } |
| 530 | catch (KustvaktException e) { |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 531 | return false; |
| 532 | } |
| 533 | } |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 534 | } |