|
| 1 | +package com.twcable.grabbit.client.jcr |
| 2 | + |
| 3 | +import com.twcable.grabbit.jcr.JcrNodeDecorator |
| 4 | +import com.twcable.grabbit.jcr.ProtoNodeDecorator |
| 5 | +import com.twcable.grabbit.security.AuthorizablePrincipal |
| 6 | +import com.twcable.grabbit.security.InsufficientGrabbitPrivilegeException |
| 7 | +import groovy.transform.CompileStatic |
| 8 | +import groovy.transform.InheritConstructors |
| 9 | +import groovy.util.logging.Slf4j |
| 10 | +import org.apache.jackrabbit.api.security.user.Authorizable |
| 11 | +import org.apache.jackrabbit.api.security.user.User |
| 12 | +import org.apache.jackrabbit.api.security.user.UserManager |
| 13 | +import org.apache.jackrabbit.value.StringValue |
| 14 | +import org.apache.sling.jcr.base.util.AccessControlUtil |
| 15 | + |
| 16 | +import javax.annotation.Nonnull |
| 17 | +import javax.jcr.Session |
| 18 | +import java.lang.reflect.Field |
| 19 | +import java.lang.reflect.Method |
| 20 | +import java.lang.reflect.ReflectPermission |
| 21 | + |
| 22 | +/* |
| 23 | + * Copyright 2015 Time Warner Cable, Inc. |
| 24 | + * |
| 25 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 26 | + * you may not use this file except in compliance with the License. |
| 27 | + * You may obtain a copy of the License at |
| 28 | + * |
| 29 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 30 | + * |
| 31 | + * Unless required by applicable law or agreed to in writing, software |
| 32 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 33 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | + * See the License for the specific language governing permissions and |
| 35 | + * limitations under the License. |
| 36 | + */ |
| 37 | + |
| 38 | +/** |
| 39 | + * This class wraps a serialized node that represents an Authorizable. Authorizables are special system protected nodes, that can only be written under certain |
| 40 | + * trees, and can not be written directly by a client. |
| 41 | + */ |
| 42 | +@CompileStatic |
| 43 | +@InheritConstructors |
| 44 | +@Slf4j |
| 45 | +class AuthorizableProtoNodeDecorator extends ProtoNodeDecorator { |
| 46 | + |
| 47 | + @Override |
| 48 | + JcrNodeDecorator writeToJcr(@Nonnull Session session) { |
| 49 | + if(!checkSecurityPermissions()) { |
| 50 | + throw new InsufficientGrabbitPrivilegeException("JVM Permissions needed by Grabbit to sync Users/Groups were not found. See log for specific permissions needed, and add these to your security manager; or do not sync users and groups." + |
| 51 | + "Unfortunately, the way Jackrabbit goes about certain things requires us to do a bit of hacking in order to sync Authorizables securely, and efficiently.") |
| 52 | + } |
| 53 | + Authorizable authorizable = findAuthorizable(session) |
| 54 | + if(!authorizable) { |
| 55 | + authorizable = createNewAuthorizable(session) |
| 56 | + } |
| 57 | + else { |
| 58 | + updateAuthorizable(authorizable, session) |
| 59 | + } |
| 60 | + session.save() |
| 61 | + return new JcrNodeDecorator(session.getNode(authorizable.getPath())) |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return a new authorizable from this serialized node |
| 66 | + */ |
| 67 | + private Authorizable createNewAuthorizable(final Session session) { |
| 68 | + final UserManager userManager = AccessControlUtil.getUserManager(session) |
| 69 | + if(isUserType()) { |
| 70 | + //When set a temporary password for now, and then set the real password later in setPasswordForUser(). See the method for why. |
| 71 | + final newUser = userManager.createUser(authorizableID, 'temp', new AuthorizablePrincipal(authorizableID), getIntermediateAuthorizablePath()) |
| 72 | + //This is a special protected property for disabling user access |
| 73 | + if(hasProperty('rep:disabled')) { |
| 74 | + newUser.disable(getStringValueFrom('rep:disabled')) |
| 75 | + } |
| 76 | + //AEM writes this property directly on the user node for some reason. One known use is for setting leads on MCM campaigns. |
| 77 | + final authorizableCategory = 'cq:authorizableCategory' |
| 78 | + if(hasProperty(authorizableCategory)) { |
| 79 | + newUser.setProperty(authorizableCategory, new StringValue(getStringValueFrom(authorizableCategory))) |
| 80 | + } |
| 81 | + setPasswordForUser(newUser, session) |
| 82 | + return newUser |
| 83 | + } |
| 84 | + return userManager.createGroup(authorizableID, new AuthorizablePrincipal(authorizableID), getIntermediateAuthorizablePath()) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * From a client API perspective, there is really no way to truely update an existing authorizable node. All of the properties are protected, and there is no |
| 89 | + * known way to update them. What we do here is essentially remove the existing authorizable as denoted by the authorizableID, and recreate it. |
| 90 | + */ |
| 91 | + private void updateAuthorizable(final Authorizable authorizable, final Session session) { |
| 92 | + authorizable.remove() |
| 93 | + session.save() |
| 94 | + createNewAuthorizable(session) |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + private Authorizable findAuthorizable(final Session session) { |
| 99 | + final UserManager userManager = AccessControlUtil.getUserManager(session) |
| 100 | + return userManager.getAuthorizable(getAuthorizableID()) |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + private String getAuthorizableID() { |
| 105 | + return protoProperties.find { it.isAuthorizableIDType() }.stringValue |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + private String getIntermediateAuthorizablePath() { |
| 110 | + return getName() - "/${getName().tokenize('/').last()}" |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + private boolean isUserType() { |
| 115 | + return protoProperties.any { it.userType } |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + /** |
| 120 | + * Some JVM's have a SecurityManager set, which based on configuration, can potentially inhibit our hack {@code setPasswordForUser(User, Session)} from working. |
| 121 | + * We need to check security permissions before proceeding |
| 122 | + * @return true if we can sync this Authorizable |
| 123 | + */ |
| 124 | + private boolean checkSecurityPermissions() { |
| 125 | + final SecurityManager securityManager = System.getSecurityManager() |
| 126 | + //If no security manager is present, then we are in the clear; otherwise, we need to check certain permissions |
| 127 | + if(!securityManager){ |
| 128 | + log.debug "No SecurityManager found on this JVM. Sync of Users/Groups can continue" |
| 129 | + return true |
| 130 | + } |
| 131 | + final issues = [] |
| 132 | + final badPermissions = false |
| 133 | + log.debug "SecurityManager found on this JVM. Checking permissions.." |
| 134 | + try { |
| 135 | + //Needed to reflect on members for which this class does not normally have access to |
| 136 | + securityManager.checkPermission(new ReflectPermission('suppressAccessChecks')) |
| 137 | + } |
| 138 | + catch(SecurityException ex) { |
| 139 | + issues << 'suppressAccessChecks' |
| 140 | + badPermissions = true |
| 141 | + } |
| 142 | + try { |
| 143 | + //Needed to access all declared members of a class, including protected or private |
| 144 | + securityManager.checkPermission(new RuntimePermission('accessDeclaredMembers')) |
| 145 | + } |
| 146 | + catch(SecurityException ex) { |
| 147 | + issues << 'accessDeclaredMembers' |
| 148 | + badPermissions = true |
| 149 | + } |
| 150 | + try { |
| 151 | + //Needed to access classes directly within a potentially system protected package |
| 152 | + securityManager.checkPermission(new RuntimePermission('accessClassInPackage.{org.apache.jackrabbit.oak.security.user}')) |
| 153 | + } |
| 154 | + catch(SecurityException ex) { |
| 155 | + issues << 'accessClassInPackage.{org.apache.jackrabbit.oak.security.user}' |
| 156 | + badPermissions = true |
| 157 | + } |
| 158 | + if(badPermissions) { |
| 159 | + log.warn "A SecurityManager is enabled for this JVM, and permissions are not sufficient for Grabbit to sync Authorizables (Users/Groups). You must enable ${issues.join(', ')} permissions in your SecurityManager to use this functionality" + |
| 160 | + "Check https://docs.oracle.com/javase/7/docs/api/java/lang/RuntimePermission.html and https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/ReflectPermission.html to see what these permissions enable" |
| 161 | + return false |
| 162 | + } |
| 163 | + else { |
| 164 | + log.debug "Permissions check successful" |
| 165 | + return true |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + /** |
| 171 | + * Normally we would call org.apache.jackrabbit.oak.jcr.delegate.UserDelegator.changePassword(String password) to change a password (this is what is publicly available through the Jackrabbit API) |
| 172 | + * However, this method ALWAYS rehashes the password argument which is of no use to us, since we are trying to transfer an already hashed password. |
| 173 | + * |
| 174 | + * Internally, org.apache.jackrabbit.oak.jcr.delegate.UserDelegator calls it's delegate's org.apache.jackrabbit.oak.security.user.UserImpl.changePassword(String password) |
| 175 | + * which calls org.apache.jackrabbit.oak.security.user.UserManagerImpl.setPassword(Tree tree, String userId, String password, boolean forceHash) with forceHash always set to true |
| 176 | + * We really need forcehash set to false for our case, but this isn't publicly available. Here, we access internal objects to do this manipulation. org.apache.jackrabbit.oak.security.user.UserManagerImpl |
| 177 | + * simply ensures that forcehash is false, and that the password is not plain text, and it sets the password as-is. |
| 178 | + * |
| 179 | + * @throws IllegalStateException if security permissions required to run this are not there. @{code checkSecurityPermissions()} should be called before calling this method |
| 180 | + **/ |
| 181 | + private void setPasswordForUser(final User user, final Session session) { |
| 182 | + if(!checkSecurityPermissions()) throw new IllegalStateException("Security check failed for Grabbit. Can not set user passwords") |
| 183 | + //As a consumer we have access to org.apache.jackrabbit.oak.jcr.delegate.UserManagerDelegator below |
| 184 | + final userManager = AccessControlUtil.getUserManager(session) |
| 185 | + Class userManagerDelegatorClass = userManager.getClass() |
| 186 | + //Reach into the class of this delegator, and grab the core Jackrabbit object we delegate to |
| 187 | + Field userManagerDelegateField = userManagerDelegatorClass.getDeclaredField('userManagerDelegate') |
| 188 | + //The delegate field is private, so we need to make it accessible. Security checks above are imperative for this to work |
| 189 | + userManagerDelegateField.setAccessible(true) |
| 190 | + //Here we have a handle to the internal class org.apache.jackrabbit.oak.security.user.UserManagerImpl |
| 191 | + final userManagerDelegate = userManagerDelegateField.get(userManager) |
| 192 | + final userManagerDelegateClass = userManagerDelegate.getClass() |
| 193 | + //We need to set the 'setPassword' method as accessible. Again, security checks above are imperative for this to work |
| 194 | + Method setPasswordMethod = userManagerDelegateClass.getDeclaredMethod('setPassword', Class.forName('org.apache.jackrabbit.oak.api.Tree', true, userManagerDelegateClass.getClassLoader()), String, String, boolean) |
| 195 | + setPasswordMethod.setAccessible(true) |
| 196 | + /** |
| 197 | + * Step two. We need access to the internal Authorizable object's tree in order to call the internal setPassword method |
| 198 | + * User is an instance of org.apache.jackrabbit.oak.jcr.delegate.UserDelegator. We need to get the delegate off of this class's super class org.apache.jackrabbit.oak.jcr.delegate.AuthorizableDelegator |
| 199 | + */ |
| 200 | + Class authorizableDelegateClass = user.getClass().getSuperclass() |
| 201 | + Field authorizableDelegateField = authorizableDelegateClass.getDeclaredField('delegate') |
| 202 | + authorizableDelegateField.setAccessible(true) |
| 203 | + final authorizable = authorizableDelegateField.get(user) |
| 204 | + //Internal org.apache.jackrabbit.oak.security.user.AuthorizableImpl object. We can access the protected tree here |
| 205 | + Method getTreeMethod = authorizable.getClass().getSuperclass().getDeclaredMethod('getTree') |
| 206 | + getTreeMethod.setAccessible(true) |
| 207 | + |
| 208 | + /** |
| 209 | + * The last argument where we are passing in 'false' in the secret sauce we need. This parameter is forceHash. As long as forceHash is false, and the password is not |
| 210 | + * clear-text, which it isn't since we got it from another Jackrabbit instance, we can set the password as-is. |
| 211 | + */ |
| 212 | + setPasswordMethod.invoke(userManagerDelegate, getTreeMethod.invoke(authorizable), getAuthorizableID(), getStringValueFrom('rep:password'), false) |
| 213 | + } |
| 214 | +} |
0 commit comments