简体   繁体   English

如何使用 Laravel 连接到 Amazon Keyspaces (Cassandra)?

[英]How to connect to Amazon Keyspaces (Cassandra) using laravel?

I am trying to connect to Cassandra hosted at amazon keyspaces, and it is required to include the SSL certificate on each connection.我正在尝试连接到托管在 amazon keyspaces 的 Cassandra,并且需要在每个连接上包含 SSL 证书。

I already have the driver installed and the connection is successful, however the authentication fails as I am missing the certificated.我已经安装了驱动程序并且连接成功,但是身份验证失败,因为我缺少证书。

I have tried the following wrappers but none of those includes the parameter where the SSL certificate is configured:我尝试了以下包装器,但没有一个包含配置 SSL 证书的参数:

sonvq/laravel-cassandra Fuitad/laravel-cassandra cubettech/lacassa Sonvq/laravel-cassandra Fuitad/laravel-cassandra cubettech/lacassa

In my config/database.php file I have the following configured, and I am guessing the certificate goes in the "options" parameter but it does not work:在我的 config/database.php 文件中,我配置了以下内容,我猜证书在“options”参数中,但它不起作用:

   'cassandra' => [
        'driver' => 'cassandra',
        'host' => env('CASSANDRA_DB_HOST', ''),
        'port' => env('CASSANDRA_DB_PORT', 9142),
        'keyspace' => env('CASSANDRA_DB_DATABASE', ''),
        'username' => env('CASSANDRA_DB_USERNAME', ''),
        'password' => env('CASSANDRA_DB_PASSWORD', ''),
        'options'   => array(
        Cassandra::VERIFY_PEER_CERT    => '/home/user/AmazonRootCA1.pem',
       ),
     ]

Using "cqlsh" via command line works perfectly, as in that command it is easy to configure the certificate path, but on laravel I have not been able to find how to set that path.通过命令行使用“cqlsh”效果很好,因为在该命令中很容易配置证书路径,但在 laravel 上我无法找到如何设置该路径。

Any help would be great!任何帮助都会很棒! thanks!谢谢!

I was killing my head since the last two days trying to connect my laravel app to amazon keyspaces.自从过去两天试图将我的 Laravel 应用程序连接到亚马逊密钥空间以来,我一直在想办法。

Finally, making some changes to cubettech/lacassa I made it work最后,对cubettech/lacassa 进行了一些更改,我让它工作了

After reading the documentation at datastax, I found the way to load the amazon PEM certificate and finally I could connect.阅读datastax的文档后,我找到了加载亚马逊PEM证书的方法,终于可以连接了。

Would be great if the following changes are made on connection.php如果对 connection.php 进行以下更改,那就太好了

protected function createConnection(array $config)
    {
       $ssl = Cassandra::ssl()
               ->withVerifyFlags(Cassandra::VERIFY_PEER_CERT)
               ->withTrustedCerts($config['cert_path'])
               ->build();     
        
        $cluster   = Cassandra::cluster()
        ->withContactPoints($config['host'])
        ->withPort((int) $config['port'])
        ->withCredentials($config['username'],$config['password'])
        ->withSSL($ssl)
        ->withDefaultConsistency(Cassandra::CONSISTENCY_LOCAL_QUORUM)
        ->build();

        $keyspace  = $config['keyspace'];
        $connection   = $cluster->connect($keyspace);
        return $connection;

    }

It is required to set the certificate path variable as well.还需要设置证书路径变量。

For some reason I found that even if the environmental variables were configured, the connection was being always made to localhost, that is why I loaded the environmental variables using the $config array.出于某种原因,我发现即使配置了环境变量,也始终连接到 localhost,这就是我使用 $config 数组加载环境变量的原因。

The queries return a Cassandra/Rows Object , I have to convert them to string or reading them in loops but that is not a problem.查询返回一个 Cassandra/Rows Object ,我必须将它们转换为字符串或在循环中读取它们,但这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM