【问题标题】:Connecting AWS cassandra using c#使用 c# 连接 AWS cassandra
【发布时间】:2020-09-13 12:15:33
【问题描述】:

我正在尝试使用此代码连接到 AWS Cassandra:

       var pathToCAFile = $"{Directory.GetCurrentDirectory()}/AmazonRootCA1.pem";
        X509Certificate2[] certs = new X509Certificate2[] { new X509Certificate2(pathToCAFile, "amazon") };
        X509Certificate2Collection certificateCollection = new X509Certificate2Collection(certs);
        var options = new Cassandra.SSLOptions(SslProtocols.Tls11, true, ValidateServerCertificate);
        options.SetCertificateCollection(certificateCollection);
        Cluster cluster = Cluster
                            .Builder()
                            .WithCredentials("username", "password")
                            .WithPort(9142)
                            .AddContactPoint("cassandra.us-east-1.amazonaws.com")
                           .WithSSL()
                            .WithLoadBalancingPolicy(new DefaultLoadBalancingPolicy("us-east-1"))
                            .Build();

        // Connect to the nodes using a keyspace
        var session = cluster.Connect("system_distributed");

 

        // Execute a query on a connection synchronously
        var rs = session.Execute("select * from system.peers");

这是我得到的错误:

"Cassandra.NoHostAvailableException: '所有主机尝试查询失败 (试过 3.83.169.143:9142: AuthenticationException '远程 根据验证程序,证书无效。')'

这个异常最初是在这个调用栈中抛出的:

Cassandra.Connections.Control.ControlConnection.Connect(bool) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() Cassandra.Connections.Control.ControlConnection.InitAsync() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() ... [调用堆栈被截断]

【问题讨论】:

    标签: c# amazon-web-services cassandra


    【解决方案1】:

    我假设您使用的是 DataStax C# 驱动程序。如果是这种情况,请参阅有关 SSL/TLS 的文档部分,其中还包含几个示例的链接:https://docs.datastax.com/en/developer/csharp-driver/3.15/features/tls/

    如果该 sn-p 是准确的,那么您实际上并没有在 Builder.WithSSL() 方法上设置 SSLOptions。

    如果这不起作用并且代码示例对您没有帮助,请向我们展示ValidateServerCertificate 方法,以便我们查看证书验证可能出现的问题。


    编辑(来自我下面的评论):

    TLS/SSL documentation page 有一个部分与此处相关:使用自定义根证书启用服务器身份验证。

    如文档中所述,您要么必须在运行应用程序的机器上安装该证书,要么必须提供类似于this one 的自定义证书验证器。

    SSLOptions.SetCertificateCollection() 方法用于客户端身份验证,因此它不适用于您需要服务器身份验证的情况。

    【讨论】:

    • public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { if (sslPolicyErrors == SslPolicyErrors.None) return true; Console.WriteLine("证书错误:{0}", sslPolicyErrors); // 不允许此客户端与未经身份验证的服务器通信。返回假;这是 ValidateServerCertificate 方法
    • 在控制台中我收到 RemoteCertificateNameMisMatch 错误
    • docs.datastax.com/en/developer/csharp-driver/3.15/features/tls 我也试过从这里..然后我得到同样的错误“ Cassandra.NoHostAvailableException: '所有主机尝试查询失败(尝试 3.83.169.143:9142: AuthenticationException '远程根据验证程序,证书无效。')'
    • 在该页面上有一个与此处相关的部分:“使用自定义根证书启用服务器身份验证”。如文档中所述,您要么必须在运行应用程序的机器上安装该证书,要么必须提供与此类似的自定义证书验证器:github.com/datastax/csharp-driver/blob/master/examples/Ssl/…
    • SetCertificateCollection() 方法用于客户端身份验证,因此对于需要服务器身份验证的情况没有用处。
    猜你喜欢
    • 2020-08-06
    • 2018-09-22
    • 2021-08-31
    • 2021-11-12
    • 2020-11-22
    • 2020-08-08
    • 1970-01-01
    • 2020-05-31
    • 2015-05-21
    相关资源
    最近更新 更多