|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2015 Time Warner Cable, Inc. |
| 4 | + * * |
| 5 | + * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * * you may not use this file except in compliance with the License. |
| 7 | + * * You may obtain a copy of the License at |
| 8 | + * * |
| 9 | + * * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * * |
| 11 | + * * Unless required by applicable law or agreed to in writing, software |
| 12 | + * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * * See the License for the specific language governing permissions and |
| 15 | + * * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package com.twcable.grabbit.jcr |
| 20 | + |
| 21 | +import groovy.transform.CompileStatic |
| 22 | +import groovy.util.logging.Slf4j |
| 23 | +import java.security.AccessControlException |
| 24 | +import java.security.Principal |
| 25 | +import javax.annotation.Nonnull |
| 26 | +import javax.jcr.PathNotFoundException |
| 27 | +import javax.jcr.Session |
| 28 | +import javax.jcr.Value |
| 29 | +import javax.jcr.security.AccessControlEntry |
| 30 | +import javax.jcr.security.AccessControlManager |
| 31 | +import javax.jcr.security.AccessControlPolicy |
| 32 | +import javax.jcr.security.AccessControlPolicyIterator |
| 33 | +import javax.jcr.security.Privilege |
| 34 | +import org.apache.jackrabbit.api.security.JackrabbitAccessControlList |
| 35 | +import org.apache.jackrabbit.api.security.principal.PrincipalManager |
| 36 | +import org.apache.sling.jcr.base.util.AccessControlUtil |
| 37 | + |
| 38 | + |
| 39 | +import static com.twcable.grabbit.proto.NodeProtos.Node as ProtoNode |
| 40 | + |
| 41 | +/** |
| 42 | + * Wraps a rep:policy (rep:ACL) node, providing the ability to write it, and it's child ACE, and restriction nodes |
| 43 | + */ |
| 44 | +@CompileStatic |
| 45 | +@Slf4j |
| 46 | +class ACLProtoNodeDecorator extends ProtoNodeDecorator { |
| 47 | + |
| 48 | + |
| 49 | + protected ACLProtoNodeDecorator(@Nonnull ProtoNode repACLNode, @Nonnull Collection<ProtoPropertyDecorator> protoProperties, String nameOverride) { |
| 50 | + this.innerProtoNode = repACLNode |
| 51 | + this.protoProperties = protoProperties |
| 52 | + this.nameOverride = nameOverride |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + JCRNodeDecorator writeToJcr(@Nonnull Session session) { |
| 58 | + /** |
| 59 | + * We don't write the rep:policy node directly. Rather, we find the rep:policy node's ACE(s) and add them to the |
| 60 | + * owner's existing policy; or we add them to a new policy. |
| 61 | + */ |
| 62 | + clearExistingEntries(session) |
| 63 | + innerProtoNode.mandatoryChildNodeList.each { ProtoNode node -> |
| 64 | + if(isGrantACEType(node)) { |
| 65 | + writeGrantACE(session, node) |
| 66 | + } |
| 67 | + else if(isDenyACEType(node)) { |
| 68 | + writeDenyACE(session, node) |
| 69 | + } |
| 70 | + } |
| 71 | + session.save() |
| 72 | + try { |
| 73 | + return new JCRNodeDecorator(session.getNode(getName())) |
| 74 | + } catch(PathNotFoundException ex) { |
| 75 | + //We may not have been able to write the policy node if for example the principal does not exist |
| 76 | + return new JCRNodeDecorator(session.getNode(getParentPath())) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + private void clearExistingEntries(final Session session) { |
| 82 | + final JackrabbitAccessControlList acl = getAccessControlList(session) |
| 83 | + if(acl.accessControlEntries.length != 0) { |
| 84 | + acl.accessControlEntries.each { AccessControlEntry entry -> |
| 85 | + acl.removeAccessControlEntry(entry) |
| 86 | + } |
| 87 | + getAccessControlManager(session).setPolicy(getParentPath(), acl) |
| 88 | + session.save() |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + private void writeGrantACE(final Session session, ProtoNode grantACENode) { |
| 94 | + writeACE(session, grantACENode, true) |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + private void writeDenyACE(final Session session, ProtoNode denyACENode) { |
| 99 | + writeACE(session, denyACENode, false) |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + private void writeACE(final Session session, ProtoNode aceNode, boolean grant) { |
| 104 | + JackrabbitAccessControlList acl = getAccessControlList(session) |
| 105 | + final String principalName = getPrincipalName(aceNode) |
| 106 | + Principal principal = getPrincipal(session, principalName) |
| 107 | + if(principal == null) { |
| 108 | + log.warn "Principal for name ${principalName} does not exist, or is not accessible. Can not write ACE/ACL information." |
| 109 | + return |
| 110 | + } |
| 111 | + Privilege[] privileges = getPrivilegeNames(aceNode).collect { String privilegeName -> |
| 112 | + if(isSupportedPrivilege(session, privilegeName)) { |
| 113 | + return getPrivilege(session, privilegeName) |
| 114 | + } |
| 115 | + else { |
| 116 | + throw new IllegalStateException("${privilegeName} is not a supported privilege on this JCR implementation. Bailing out") |
| 117 | + } |
| 118 | + } |
| 119 | + Map<String, Value> restrictionMap |
| 120 | + if(hasRestrictions(aceNode)) { |
| 121 | + ProtoNode restrictionNode = getRestrictionNode(aceNode) |
| 122 | + restrictionMap = extractRestrictionMapFrom(restrictionNode) |
| 123 | + } |
| 124 | + if(restrictionMap != null) { |
| 125 | + acl.addEntry(principal, privileges, grant, restrictionMap) |
| 126 | + } |
| 127 | + else { |
| 128 | + acl.addEntry(principal, privileges, grant) |
| 129 | + } |
| 130 | + getAccessControlManager(session).setPolicy(getParentPath(), acl) |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + private boolean isGrantACEType(ProtoNode node) { |
| 135 | + return node.propertiesList.any { new ProtoPropertyDecorator(it).isGrantACEType() } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + private boolean isDenyACEType(ProtoNode node) { |
| 140 | + return node.propertiesList.any { new ProtoPropertyDecorator(it).isDenyACEType() } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + private boolean hasRestrictions(ProtoNode aceNode) { |
| 145 | + return aceNode.mandatoryChildNodeList.any { ProtoNode childNode -> |
| 146 | + childNode.propertiesList.any { new ProtoPropertyDecorator(it).isRepRestrictionType() } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + private ProtoNode getRestrictionNode(ProtoNode aceNode) { |
| 152 | + return aceNode.mandatoryChildNodeList.find { ProtoNode childNode -> |
| 153 | + childNode.propertiesList.any { new ProtoPropertyDecorator(it).isRepRestrictionType() } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + private String getPrincipalName(ProtoNode node) { |
| 159 | + node.propertiesList.collect { new ProtoPropertyDecorator(it) }.find { ProtoPropertyDecorator property -> |
| 160 | + property.isPrincipalName() |
| 161 | + }.getStringValue() |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + private String[] getPrivilegeNames(ProtoNode node) { |
| 166 | + final ProtoPropertyDecorator privilegeProperty = node.propertiesList.collect { new ProtoPropertyDecorator(it) }.find { ProtoPropertyDecorator property -> |
| 167 | + property.isPrivilege() |
| 168 | + } |
| 169 | + return privilegeProperty.getPropertyValues().collect { Value value -> value.string }.toArray() as String[] |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + private Map<String, Value> extractRestrictionMapFrom(ProtoNode restrictionNode) { |
| 174 | + final Collection<ProtoPropertyDecorator> restrictionProperties = restrictionNode.propertiesList.findResults { |
| 175 | + final property = new ProtoPropertyDecorator(it) |
| 176 | + return !property.isPrimaryType() ? property : null |
| 177 | + } |
| 178 | + return restrictionProperties.collectEntries { ProtoPropertyDecorator property -> |
| 179 | + [(property.getName()) : property.getPropertyValue()] |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + private JackrabbitAccessControlList getAccessControlList(final Session session) { |
| 185 | + final AccessControlManager manager = getAccessControlManager(session) |
| 186 | + final String ownershipNodePath = getParentPath() |
| 187 | + //Check to see if existing policies exist for this node |
| 188 | + final AccessControlPolicy[] existingPolicies = manager.getPolicies(ownershipNodePath) |
| 189 | + //If no policies, we need to create a new policy |
| 190 | + if(existingPolicies.length == 0) { |
| 191 | + final AccessControlPolicyIterator policyIterator = manager.getApplicablePolicies(ownershipNodePath) |
| 192 | + //For Jackrabbit, the only policy type we are interested in is the JackrabbitAccessControlList |
| 193 | + while (policyIterator.hasNext()) { |
| 194 | + final AccessControlPolicy thisPolicy = policyIterator.nextAccessControlPolicy() |
| 195 | + if (thisPolicy instanceof JackrabbitAccessControlList) { |
| 196 | + return (JackrabbitAccessControlList)thisPolicy |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + //We have an existing policy. We just need to dig it out |
| 201 | + else { |
| 202 | + return existingPolicies.findResult { AccessControlPolicy policy -> |
| 203 | + if(policy instanceof JackrabbitAccessControlList) { |
| 204 | + return (JackrabbitAccessControlList)policy |
| 205 | + } |
| 206 | + } as JackrabbitAccessControlList |
| 207 | + } |
| 208 | + throw new IllegalStateException("Something went wrong when trying to find a ${JackrabbitAccessControlList.class.canonicalName} for this session on node ${ownershipNodePath}") |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | + private Principal getPrincipal(final Session session, final String principalName) { |
| 213 | + return getPrincipalManager(session).getPrincipal(principalName) |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | + private Privilege getPrivilege(final Session session, final String privilegeName) { |
| 218 | + try { |
| 219 | + return getAccessControlManager(session).privilegeFromName(privilegeName) |
| 220 | + } catch(AccessControlException ex) { |
| 221 | + log.error "Something went wrong while getting privilege ${privilegeName}. isSupportedPrivilege() may not be producing expected behavior " |
| 222 | + throw ex |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + private boolean isSupportedPrivilege(final Session session, final String privilegeName) { |
| 228 | + final Privilege[] supportedPrivileges = getAccessControlManager(session).getSupportedPrivileges(getParentPath()) |
| 229 | + return supportedPrivileges.any { Privilege privilege -> privilege.getName() == privilegeName } |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + PrincipalManager getPrincipalManager(final Session session) { |
| 234 | + return AccessControlUtil.getPrincipalManager(session) |
| 235 | + } |
| 236 | + |
| 237 | + |
| 238 | + AccessControlManager getAccessControlManager(final Session session) { |
| 239 | + return AccessControlUtil.getAccessControlManager(session) |
| 240 | + } |
| 241 | + |
| 242 | +} |
0 commit comments