【问题标题】:How to specify credentials in Embedded Cassandra如何在 Embedded Cassandra 中指定凭据
【发布时间】:2020-10-26 05:22:50
【问题描述】:

我的应用程序使用凭据连接到 Cassandra 集群。我正在创建一个 TestCassandra 的实例,如下所示:

val testCassandra = cqlStatementsOption.map(cqlStatements =>{
    new TestCassandra(factory,cqlStatements)})
    .getOrElse(new TestCassandra())

我想上面的语句会创建一个集群。如何为创建的集群指定用户名和密码?我的一个测试用例是,如果 cassandra 数据库的用户名/密码不正确,应用程序不应该启动。

【问题讨论】:

    标签: embedded-cassandra


    【解决方案1】:
    class SecureCassandra {
    
        private final TestCassandra testCassandra = new TestCassandra(createCassandraFactory(), createConnectionFactory(),
                CqlScript.classpath("init.cql"));
    
        private ConnectionFactory createConnectionFactory() {
            ClusterFactory clusterFactory = new ClusterFactory();
            clusterFactory.setUsername("cassandra");
            clusterFactory.setPassword("cassandra");
            return new ClusterConnectionFactory(clusterFactory);
        }
    
        private CassandraFactory createCassandraFactory() {
            LocalCassandraFactory cassandraFactory = new LocalCassandraFactory();
            cassandraFactory.setConfigurationFile(getClass().getResource("/cassandra-secure.yaml"));
            cassandraFactory.getJvmOptions().add("-Dcassandra.superuser_setup_delay_ms=0");
            return cassandraFactory;
        }
    
        @Test
        void testSecureCassandra() {
            this.testCassandra.start();
    
            try {
                Settings settings = this.testCassandra.getSettings();
                ClusterFactory clusterFactory = new ClusterFactory();
                clusterFactory.setUsername("test_user");
                clusterFactory.setPassword("test_pass");
                try (Cluster cluster = clusterFactory.create(settings)) {
                    try (Session session = cluster.connect("test")) {
                        //
                    }
                }
    
            }
            finally {
                this.testCassandra.stop();
            }
        }
    
    }
    

    init.cql:

    CREATE KEYSPACE test  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
    CREATE ROLE test_user with SUPERUSER = true AND LOGIN = true and PASSWORD = 'test_pass';
    
    

    cassandra-secure.yaml:

    cluster_name: "Test Cluster"
    num_tokens: 256
    commitlog_sync: periodic
    commitlog_sync_period_in_ms: 5000
    seed_provider:
      - class_name: org.apache.cassandra.locator.SimpleSeedProvider
        parameters:
          - seeds: "127.0.0.1"
    listen_address: localhost
    rpc_address: localhost
    start_native_transport: true
    endpoint_snitch: SimpleSnitch
    partitioner: org.apache.cassandra.dht.Murmur3Partitioner
    authenticator: PasswordAuthenticator
    role_manager: CassandraRoleManager
    authorizer: CassandraAuthorizer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 2013-01-28
      • 2018-02-09
      • 2016-12-30
      相关资源
      最近更新 更多