|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.instaclustr.cassandra.ldap.auth; |
| 19 | + |
| 20 | +import static java.lang.String.format; |
| 21 | +import static java.util.Collections.singletonList; |
| 22 | +import static org.apache.cassandra.db.ConsistencyLevel.LOCAL_ONE; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +import com.google.common.base.Function; |
| 27 | +import org.apache.cassandra.auth.AuthKeyspace; |
| 28 | +import org.apache.cassandra.auth.LDAPCassandraRoleManager.Role; |
| 29 | +import org.apache.cassandra.cql3.CQLStatement; |
| 30 | +import org.apache.cassandra.cql3.QueryOptions; |
| 31 | +import org.apache.cassandra.cql3.QueryProcessor; |
| 32 | +import org.apache.cassandra.cql3.UntypedResultSet; |
| 33 | +import org.apache.cassandra.cql3.UntypedResultSet.Row; |
| 34 | +import org.apache.cassandra.cql3.statements.CreateRoleStatement; |
| 35 | +import org.apache.cassandra.cql3.statements.SelectStatement; |
| 36 | +import org.apache.cassandra.cql3.statements.GrantRoleStatement; |
| 37 | +import org.apache.cassandra.db.ConsistencyLevel; |
| 38 | +import org.apache.cassandra.db.marshal.UTF8Type; |
| 39 | +import org.apache.cassandra.exceptions.RequestExecutionException; |
| 40 | +import org.apache.cassandra.exceptions.RequestValidationException; |
| 41 | +import org.apache.cassandra.service.ClientState; |
| 42 | +import org.apache.cassandra.service.QueryState; |
| 43 | +import org.apache.cassandra.transport.Dispatcher; |
| 44 | +import org.apache.cassandra.transport.messages.ResultMessage; |
| 45 | +import org.apache.cassandra.utils.ByteBufferUtil; |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
| 48 | + |
| 49 | +public class Cassandra50SystemAuthRoles implements SystemAuthRoles |
| 50 | +{ |
| 51 | + |
| 52 | + private static final Logger logger = LoggerFactory.getLogger(SystemAuthRoles.class); |
| 53 | + |
| 54 | + public static final String SELECT_ROLE_STATEMENT = "SELECT role FROM %s.%s where role = ?"; |
| 55 | + |
| 56 | + public static final String CREATE_ROLE_STATEMENT_WITH_LOGIN = "CREATE ROLE IF NOT EXISTS \"%s\" WITH LOGIN = true AND SUPERUSER = %s"; |
| 57 | + |
| 58 | + public static final String GRANT_ROLE_STATEMENT = "GRANT '%s' TO '%s'"; |
| 59 | + |
| 60 | + private ClientState clientState; |
| 61 | + |
| 62 | + public void setClientState(ClientState clientState) |
| 63 | + { |
| 64 | + this.clientState = clientState; |
| 65 | + } |
| 66 | + |
| 67 | + public ClientState getClientState() |
| 68 | + { |
| 69 | + return clientState; |
| 70 | + } |
| 71 | + |
| 72 | + public boolean hasAdminRole(String role) throws RequestExecutionException |
| 73 | + { |
| 74 | + // Try looking up the 'cassandra' default role first, to avoid the range query if possible. |
| 75 | + String defaultSUQuery = "SELECT * FROM system_auth.roles WHERE role = '" + role + "'"; |
| 76 | + String allUsersQuery = "SELECT * FROM system_auth.roles LIMIT 1"; |
| 77 | + return !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.ONE).isEmpty() |
| 78 | + || !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.QUORUM).isEmpty() |
| 79 | + || !QueryProcessor.process(allUsersQuery, ConsistencyLevel.QUORUM).isEmpty(); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public boolean hasAdminRole() throws RequestExecutionException |
| 84 | + { |
| 85 | + return hasAdminRole("cassandra"); |
| 86 | + } |
| 87 | + |
| 88 | + public CQLStatement prepare(String template, String keyspace, String table) { |
| 89 | + try { |
| 90 | + return QueryProcessor.parseStatement(String.format(template, keyspace, table)).prepare(ClientState.forInternalCalls()); |
| 91 | + } catch (RequestValidationException e) { |
| 92 | + throw new AssertionError(e); // not supposed to happen |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + protected ConsistencyLevel getConsistencyForRole(String defaultSuperUserName, String role, ConsistencyLevel roleConsistencyLevel) { |
| 97 | + ConsistencyLevel cl = role.equals(defaultSuperUserName) ? ConsistencyLevel.QUORUM : roleConsistencyLevel; |
| 98 | + |
| 99 | + logger.debug(String.format("Resolved consistency level for role %s: %s", role, cl)); |
| 100 | + |
| 101 | + return cl; |
| 102 | + } |
| 103 | + |
| 104 | + // NullObject returned when a supplied role name not found in AuthKeyspace.ROLES |
| 105 | + protected static final Role NULL_ROLE = new Role(null, false, false, Collections.<String>emptySet()); |
| 106 | + |
| 107 | + protected static final Function<Row, Role> ROW_TO_ROLE = new Function<UntypedResultSet.Row, Role>() { |
| 108 | + public Role apply(UntypedResultSet.Row row) { |
| 109 | + try { |
| 110 | + return new Role(row.getString("role"), |
| 111 | + row.getBoolean("is_superuser"), |
| 112 | + row.getBoolean("can_login"), |
| 113 | + row.has("member_of") ? row.getSet("member_of", UTF8Type.instance) |
| 114 | + : Collections.<String>emptySet()); |
| 115 | + } |
| 116 | + // Failing to deserialize a boolean in is_superuser or can_login will throw an NPE |
| 117 | + catch (NullPointerException e) { |
| 118 | + logger.warn("An invalid value has been detected in the {} table for role {}. If you are " + |
| 119 | + "unable to login, you may need to disable authentication and confirm " + |
| 120 | + "that values in that table are accurate", AuthKeyspace.ROLES, row.getString("role")); |
| 121 | + throw new RuntimeException(String.format("Invalid metadata has been detected for role %s", row.getString("role")), e); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + |
| 128 | + public boolean roleMissing(String dn) { |
| 129 | + assert getClientState() != null; |
| 130 | + |
| 131 | + final SelectStatement selStmt = (SelectStatement) QueryProcessor.getStatement(format(SELECT_ROLE_STATEMENT, |
| 132 | + "system_auth", |
| 133 | + AuthKeyspace.ROLES), |
| 134 | + getClientState()); |
| 135 | + |
| 136 | + final ResultMessage.Rows rows = selStmt.execute(new QueryState(getClientState()), |
| 137 | + QueryOptions.forInternalCalls(singletonList(ByteBufferUtil.bytes(dn))), |
| 138 | + Dispatcher.RequestTime.forImmediateExecution()); |
| 139 | + |
| 140 | + return rows.result.isEmpty(); |
| 141 | + } |
| 142 | + |
| 143 | + public void createRole(String roleName, boolean superUser, String defaultRoleMembership) |
| 144 | + { |
| 145 | + final CreateRoleStatement createStmt = (CreateRoleStatement) QueryProcessor.getStatement(format(CREATE_ROLE_STATEMENT_WITH_LOGIN, |
| 146 | + roleName, |
| 147 | + superUser), |
| 148 | + getClientState()); |
| 149 | + |
| 150 | + createStmt.execute(new QueryState(getClientState()), |
| 151 | + QueryOptions.forInternalCalls(LOCAL_ONE, singletonList(ByteBufferUtil.bytes(roleName))), |
| 152 | + Dispatcher.RequestTime.forImmediateExecution()); |
| 153 | + |
| 154 | + if (defaultRoleMembership != null) |
| 155 | + { |
| 156 | + if (roleMissing(defaultRoleMembership)) |
| 157 | + { |
| 158 | + logger.warn("Unable to add user to default role {} because it doesn't exist.", defaultRoleMembership); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + logger.debug("Adding user {} to default role {}", roleName, defaultRoleMembership); |
| 163 | + final GrantRoleStatement grantRoleStmt = (GrantRoleStatement) QueryProcessor.getStatement(format(GRANT_ROLE_STATEMENT, |
| 164 | + defaultRoleMembership, |
| 165 | + roleName), |
| 166 | + getClientState()); |
| 167 | + |
| 168 | + grantRoleStmt.execute(new QueryState(getClientState()), |
| 169 | + QueryOptions.forInternalCalls(LOCAL_ONE, singletonList(ByteBufferUtil.bytes(roleName))), |
| 170 | + Dispatcher.RequestTime.forImmediateExecution()); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public Role getRole(String name, ConsistencyLevel roleConsistencyLevel) |
| 177 | + throws RequestExecutionException, RequestValidationException { |
| 178 | + |
| 179 | + SelectStatement loadRoleStatement = (SelectStatement) prepare("SELECT * from %s.%s WHERE role = ?", "system_auth", "roles"); |
| 180 | + |
| 181 | + ResultMessage.Rows rows = loadRoleStatement.execute(QueryState.forInternalCalls(), |
| 182 | + QueryOptions.forInternalCalls(getConsistencyForRole("cassandra", name, roleConsistencyLevel), |
| 183 | + Collections.singletonList(ByteBufferUtil.bytes(name))), |
| 184 | + Dispatcher.RequestTime.forImmediateExecution()); |
| 185 | + |
| 186 | + if (rows.result.isEmpty()) { |
| 187 | + return NULL_ROLE; |
| 188 | + } |
| 189 | + |
| 190 | + return ROW_TO_ROLE.apply(UntypedResultSet.create(rows.result).one()); |
| 191 | + } |
| 192 | +} |
0 commit comments