You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/05/26 06:20:55 UTC

[GitHub] [james-project] chibenwa commented on a diff in pull request #1024: JAMES-3756 Cassandra implementation for DelegationStore

chibenwa commented on code in PR #1024:
URL: https://github.com/apache/james-project/pull/1024#discussion_r882351531


##########
server/data/data-cassandra/src/main/java/org/apache/james/user/cassandra/CassandraUsersDAO.java:
##########
@@ -154,6 +194,34 @@ public void updateUser(User user) throws UsersRepositoryException {
         }
     }
 
+    public Mono<Boolean> addAuthorizedUsers(Username baseUser, Username userWithAccess) {
+        return executor.executeReturnApplied(
+                addAuthorizedUserStatement.bind()
+                    .setSet(AUTHORIZED_USERS, Collections.singleton(userWithAccess.asString()))
+                    .setString(NAME, baseUser.asString()));
+    }
+
+    public Mono<Boolean> removeAuthorizedUser(Username baseUser, Username userWithAccess) {
+        return executor.executeReturnApplied(
+            removeAuthorizedUserStatement.bind()
+                .setSet(AUTHORIZED_USERS, Collections.singleton(userWithAccess.asString()))
+                .setString(NAME, baseUser.asString()));
+    }
+
+    public Mono<Boolean> removeAllAuthorizedUsers(Username baseUser) {
+        return executor.executeReturnApplied(
+            removeAllAuthorizedUsersStatement.bind()
+                .setString(NAME, baseUser.asString()));
+    }
+
+    public Flux<Username> getAuthorizedUsers(Username name) {
+        return executor.executeSingleRow(
+                getAuthorizedUsersStatement.bind()
+                    .setString(NAME, name.asString()))
+            .map(row -> row.getSet(AUTHORIZED_USERS, String.class).stream().map(Username::of))
+            .flatMapMany(Flux::fromStream);

Review Comment:
   Use flatMapIterable



##########
server/data/data-cassandra/src/main/java/org/apache/james/user/cassandra/CassandraDelegationStore.java:
##########
@@ -0,0 +1,71 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.cassandra;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.user.api.DelegationStore;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.reactivestreams.Publisher;
+
+public class CassandraDelegationStore implements DelegationStore {
+    private final CassandraUsersDAO cassandraUsersDAO;
+
+    @Inject
+    public CassandraDelegationStore(CassandraUsersDAO cassandraUsersDAO) {
+        this.cassandraUsersDAO = cassandraUsersDAO;
+    }
+
+    @Override
+    public Publisher<Username> authorizedUsers(Username baseUser) {
+        return cassandraUsersDAO.getAuthorizedUsers(baseUser);
+    }
+
+    @Override
+    public Publisher<Void> clear(Username baseUser) {
+        return cassandraUsersDAO.removeAllAuthorizedUsers(baseUser)
+            .handle((isSucceed, sink) -> {
+                if (isSucceed.equals(false)) {
+                    sink.error(new UsersRepositoryException(String.format("Unable to clear delegate users. The base user %s is not existed.", baseUser.asString())));
+                }
+            });
+    }
+
+    @Override
+    public Publisher<Void> addAuthorizedUser(Username baseUser, Username userWithAccess) {
+        return cassandraUsersDAO.addAuthorizedUsers(baseUser, userWithAccess)
+            .handle((isSucceed, sink) -> {
+                if (isSucceed.equals(false)) {
+                    sink.error(new UsersRepositoryException(String.format("Unable to add delegate user. The base user %s is not existed.", baseUser.asString())));
+                }
+            });
+    }
+
+    @Override
+    public Publisher<Void> removeAuthorizedUser(Username baseUser, Username userWithAccess) {
+        return cassandraUsersDAO.removeAuthorizedUser(baseUser, userWithAccess)
+            .handle((isSucceed, sink) -> {
+                if (isSucceed.equals(false)) {
+                    sink.error(new UsersRepositoryException(String.format("Unable to remove delegate user. The base user %s is not existed.", baseUser.asString())));
+                }
+            });

Review Comment:
   Idem



##########
upgrade-instructions.md:
##########
@@ -16,7 +16,22 @@ Changes to apply between 3.7.x and 3.8.x will be reported here.
 
 Change list:
 
- - No changes yet
+- [Adding authorized_users column to user table](#adding-authorized_users-column-to-user-table)
+
+### Adding authorized_users column to user table
+
+Date 26/05/2022
+
+JIRA: https://issues.apache.org/jira/browse/JAMES-3756
+
+Concerned product: Distributed James, Cassandra James Server
+
+Add `authorized_users` column to `user` table in order to store delegated users.
+
+In order to add this `authorized_users` column we advise you to run the following CQL command:

Review Comment:
   ```suggestion
   In order to add this `authorized_users` column you need to run the following CQL command:
   ```



##########
server/data/data-cassandra/src/test/java/org/apache/james/user/cassandra/CassandraDelegationStoreTest.java:
##########
@@ -0,0 +1,85 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.cassandra;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Set;
+
+import org.apache.james.backends.cassandra.CassandraClusterExtension;
+import org.apache.james.core.Username;
+import org.apache.james.user.api.DelegationStore;
+import org.apache.james.user.api.DelegationStoreContract;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import com.github.fge.lambdas.Throwing;
+
+import reactor.core.publisher.Mono;
+
+public class CassandraDelegationStoreTest implements DelegationStoreContract {
+    @RegisterExtension
+    static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraUsersRepositoryModule.MODULE);
+
+    private final CassandraUsersDAO usersDAO = new CassandraUsersDAO(cassandraCluster.getCassandraCluster().getConf());
+
+    private CassandraDelegationStore testee;
+
+    @BeforeEach
+    void setUp() {
+        testee = new CassandraDelegationStore(usersDAO);
+        createBaseUsers(Set.of(ALICE, BOB, CEDRIC, DAMIEN));
+    }
+
+    @Override
+    public DelegationStore testee() {
+        return testee;
+    }
+
+    private void createBaseUsers(Set<Username> usernameSet) {
+        usernameSet.forEach(Throwing.consumer(username -> usersDAO.addUser(username, "secret")));
+    }
+
+    @Test
+    void addAuthorizedUserShouldThrowUsersRepositoryExceptionWhenBaseUserIsNotExisted() throws UsersRepositoryException {
+        usersDAO.removeUser(ALICE);
+
+        assertThatThrownBy(() -> Mono.from(testee.addAuthorizedUser(ALICE, BOB)).block())
+            .hasRootCause(new UsersRepositoryException(String.format("Unable to add delegate user. The base user %s is not existed.", ALICE.asString())));
+    }
+
+    @Test
+    void removeAuthorizedUserShouldThrowUsersRepositoryExceptionWhenBaseUserIsNotExisted() throws UsersRepositoryException {
+        usersDAO.removeUser(ALICE);
+
+        assertThatThrownBy(() -> Mono.from(testee.removeAuthorizedUser(ALICE, BOB)).block())
+            .hasRootCause(new UsersRepositoryException(String.format("Unable to remove delegate user. The base user %s is not existed.", ALICE.asString())));
+    }
+
+    @Test
+    void clearAlAuthorizedUserSShouldThrowUsersRepositoryExceptionWhenBaseUserIsNotExisted() throws UsersRepositoryException {
+        usersDAO.removeUser(ALICE);
+
+        assertThatThrownBy(() -> Mono.from(testee.clear(ALICE)).block())
+            .hasRootCause(new UsersRepositoryException(String.format("Unable to clear delegate users. The base user %s is not existed.", ALICE.asString())));
+    }

Review Comment:
   IMO drop those tests.
   
   We might want to use delegation on top of LDAP. With this constraint we cannot.



##########
server/data/data-cassandra/src/main/java/org/apache/james/user/cassandra/CassandraDelegationStore.java:
##########
@@ -0,0 +1,71 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.cassandra;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.user.api.DelegationStore;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.reactivestreams.Publisher;
+
+public class CassandraDelegationStore implements DelegationStore {
+    private final CassandraUsersDAO cassandraUsersDAO;
+
+    @Inject
+    public CassandraDelegationStore(CassandraUsersDAO cassandraUsersDAO) {
+        this.cassandraUsersDAO = cassandraUsersDAO;
+    }
+
+    @Override
+    public Publisher<Username> authorizedUsers(Username baseUser) {
+        return cassandraUsersDAO.getAuthorizedUsers(baseUser);
+    }
+
+    @Override
+    public Publisher<Void> clear(Username baseUser) {
+        return cassandraUsersDAO.removeAllAuthorizedUsers(baseUser)
+            .handle((isSucceed, sink) -> {
+                if (isSucceed.equals(false)) {
+                    sink.error(new UsersRepositoryException(String.format("Unable to clear delegate users. The base user %s is not existed.", baseUser.asString())));
+                }
+            });
+    }
+
+    @Override
+    public Publisher<Void> addAuthorizedUser(Username baseUser, Username userWithAccess) {
+        return cassandraUsersDAO.addAuthorizedUsers(baseUser, userWithAccess)
+            .handle((isSucceed, sink) -> {
+                if (isSucceed.equals(false)) {
+                    sink.error(new UsersRepositoryException(String.format("Unable to add delegate user. The base user %s is not existed.", baseUser.asString())));
+                }
+            });

Review Comment:
   Idem



##########
server/data/data-cassandra/src/main/java/org/apache/james/user/cassandra/CassandraDelegationStore.java:
##########
@@ -0,0 +1,71 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.cassandra;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.user.api.DelegationStore;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.reactivestreams.Publisher;
+
+public class CassandraDelegationStore implements DelegationStore {
+    private final CassandraUsersDAO cassandraUsersDAO;
+
+    @Inject
+    public CassandraDelegationStore(CassandraUsersDAO cassandraUsersDAO) {
+        this.cassandraUsersDAO = cassandraUsersDAO;
+    }
+
+    @Override
+    public Publisher<Username> authorizedUsers(Username baseUser) {
+        return cassandraUsersDAO.getAuthorizedUsers(baseUser);
+    }
+
+    @Override
+    public Publisher<Void> clear(Username baseUser) {
+        return cassandraUsersDAO.removeAllAuthorizedUsers(baseUser)
+            .handle((isSucceed, sink) -> {
+                if (isSucceed.equals(false)) {
+                    sink.error(new UsersRepositoryException(String.format("Unable to clear delegate users. The base user %s is not existed.", baseUser.asString())));
+                }
+            });

Review Comment:
   Do we care? IMO no...



##########
server/data/data-cassandra/src/main/java/org/apache/james/user/cassandra/CassandraUsersDAO.java:
##########
@@ -123,6 +136,33 @@ private PreparedStatement prepareGetUserStatement(Session session) {
             .where(eq(NAME, bindMarker(NAME))));
     }
 
+    private PreparedStatement prepareAddAuthorizedUserStatement(Session session) {
+        return session.prepare(update(TABLE_NAME)
+            .with(addAll(AUTHORIZED_USERS, bindMarker(AUTHORIZED_USERS)))
+            .where(eq(NAME, bindMarker(NAME)))
+            .ifExists());

Review Comment:
   Here and below I think we can avoid the cost of lightweight transactions...



##########
server/data/data-cassandra/src/main/java/org/apache/james/user/cassandra/CassandraUsersDAO.java:
##########
@@ -154,6 +194,34 @@ public void updateUser(User user) throws UsersRepositoryException {
         }
     }
 
+    public Mono<Boolean> addAuthorizedUsers(Username baseUser, Username userWithAccess) {
+        return executor.executeReturnApplied(
+                addAuthorizedUserStatement.bind()
+                    .setSet(AUTHORIZED_USERS, Collections.singleton(userWithAccess.asString()))
+                    .setString(NAME, baseUser.asString()));
+    }
+
+    public Mono<Boolean> removeAuthorizedUser(Username baseUser, Username userWithAccess) {
+        return executor.executeReturnApplied(
+            removeAuthorizedUserStatement.bind()
+                .setSet(AUTHORIZED_USERS, Collections.singleton(userWithAccess.asString()))
+                .setString(NAME, baseUser.asString()));
+    }
+
+    public Mono<Boolean> removeAllAuthorizedUsers(Username baseUser) {
+        return executor.executeReturnApplied(
+            removeAllAuthorizedUsersStatement.bind()
+                .setString(NAME, baseUser.asString()));
+    }
+
+    public Flux<Username> getAuthorizedUsers(Username name) {
+        return executor.executeSingleRow(
+                getAuthorizedUsersStatement.bind()
+                    .setString(NAME, name.asString()))
+            .map(row -> row.getSet(AUTHORIZED_USERS, String.class).stream().map(Username::of))

Review Comment:
   Move the map on the resulting flux to avoid intermediate streaming.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org