【问题标题】:Python/AWS Neptune getting "tornado.httpclient.HTTPError: HTTP 403: Forbidden"Python/AWS Neptune 得到“tornado.httpclient.HTTPError: HTTP 403: Forbidden”
【发布时间】:2021-09-14 15:43:10
【问题描述】:

所以我使用堡垒主机/SSH 隧道从本地计算机连接到 AWS Neptune。

ssh -N -i /Users/user1/.ssh/id_rsa -L 8182:my.xxx.us-east-1.neptune.amazonaws.com:8182 user1@transporter-int.mycloud.com

我用 gremlin 做了一个简单的 Neptune 连接测试。

from gremlin_python.process.graph_traversal import __
from gremlin_python.structure.graph import Graph
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.traversal import T

graph = Graph()

wss = 'wss://{}:{}/gremlin'.format('localhost', 8182)
remoteConn = DriverRemoteConnection(wss, 'g')
g = graph.traversal().withRemote(remoteConn)

print(g.V().limit(2).toList())
remoteConn.close()

得到这个错误:

*aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host 
localhost:8182 ssl:True [SSLCertVerificationError: (1, "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'localhost'. (_ssl.c:1124)")]*

根据@Taylor Riggan 的建议,我将 Mac 上的 /etc/hosts 更新为以下内容:

改用Python 3.6.12版gremlin-python 3.4.10版

127.0.0.1 localhost my.cluster-xxx.us-east-1.neptune.amazonaws.com

运行以下命令刷新主机设置

sudo dscacheutil -flushcache

在源代码中更新了这一行

wss = 'wss://{}:{}/gremlin'.format('my.cluster-xxx.us-east-1.neptune.amazonaws.com', 8182). 

现在出现以下错误,tornado 版本 4.5.3

  File "/Users/user1/myproj/tests/graph/venv/lib/python3.6/site-packages/gremlin_python/driver/client.py", line 148, in submitAsync
    return conn.write(message)
  File "/Users/user1/myproj/tests/graph/venv/lib/python3.6/site-packages/gremlin_python/driver/connection.py", line 55, in write
    self.connect()
  File "/Users/user1/myproj/tests/graph/venv/lib/python3.6/site-packages/gremlin_python/driver/connection.py", line 45, in connect
    self._transport.connect(self._url, self._headers)
  File "/Users/user1/myproj/tests/graph/venv/lib/python3.6/site-packages/gremlin_python/driver/tornado/transport.py", line 41, in connect
    lambda: websocket.websocket_connect(url, compression_options=self._compression_options))
  File "/Users/user1/myproj/tests/graph/venv/lib/python3.6/site-packages/tornado/ioloop.py", line 576, in run_sync
    return future_cell[0].result()
tornado.httpclient.HTTPClientError: HTTP 403: Forbidden

【问题讨论】:

    标签: python amazon-web-services ssl gremlin amazon-neptune


    【解决方案1】:

    最简单的解决方法是在开发桌面上的 /etc/hosts 文件中添加一个条目,以将 Neptune 端点解析为 localhost。然后证书验证应该通过。

    例如:

    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1   localhost myneptune-cluster.region.neptune.amazonaws.com
    255.255.255.255 broadcasthost
    ::1             localhost
    

    【讨论】:

    • 更改后出现错误:aiohttp.client_exceptions.WSServerHandshakeError.
    • 好的,我更新编辑问题以显示新的 WSServerHandshakeError。
    • 你使用的是什么版本的 gremlin-python?最新版本的 Neptune 最高支持 3.4.10。在 3.5 中,websocket 库发生了变化,您所看到的可能与此有关。我会尝试降级到 gremlin-python 3.4.10 并再试一次。
    • 所以我使用的是 gremlinpython==3.5.0
    • 所以我将 gremlin-python 降级到 3.4.10 版,现在它得到“tornado.httpclient.HTTPClientError: HTTP 403: Forbidden”
    【解决方案2】:

    查看指南Connect to AWS Neptune from the local system

    从本地系统连接到 AWS Neptune

    有很多方法可以从VPC 外部连接到Amazon Neptune,例如设置load balancerVPC peering

    Amazon Neptune 数据库集群只能在 Amazon Virtual Private Cloud (VPC) 中创建。从 VPC 外部连接到 Amazon Neptune 的一种方法是将 Amazon EC2 实例设置为同一 VPC 内的proxy server。使用这种方法,您还需要设置一个SSH tunnel to securely forward traffic to the VPC

    第 1 部分:设置 EC2 代理服务器。

    启动位于same region 中的Amazon EC2 instance 作为您的Neptune cluster。配置方面,可以使用Ubuntu。由于这是代理服务器,您可以选择最低的资源设置。

    确保 EC2 实例与您的 Neptune 集群位于同一 VPC 组中。要查找您的 Neptune 集群的 VPC 组,请检查Neptune > Subnet groups 下的控制台。实例的安全组需要能够在端口22 上发送和接收SSH 和端口8182Neptune。请参阅下面的示例安全组设置。

    Image

    最后,确保保存密钥对文件 (.pem) 并记下目录以供下一步使用。

    第 2 部分:设置 SSH 隧道。

    此步骤可能会有所不同,具体取决于您运行的是 Windows 还是 MacOS。

    1. 修改您的主机文件以将 localhost 映射到您的 Neptune 端点。

      Windows:以管理员身份打开 hosts 文件 (C:\Windows\System32\drivers\etc\hosts)

      MacOS:打开终端并输入命令:sudo nano /etc/hosts

      将以下行添加到主机文件,将文本替换为您的 Neptune 端点地址。

      127.0.0.1 localhost YourNeptuneEndpoint

      在 Windows 或 MacOS 终端以管理员身份打开命令提示符并运行以下命令。对于 Windows,您可能需要从 C:\Users\YourUsername\ 运行 SSH

      ssh -i path/to/keypairfilename.pem ec2-user@yourec2instanceendpoint -N -L 8182:YourNeptuneEndpoint:8182

      -N 标志设置为阻止与 EC2 的交互式 bash 会话并仅转发端口。初始成功连接会询问您是否要继续连接?输入 yes 并输入。

      要测试本地图形笔记本与 Amazon Neptune 的连接是否成功,请打开浏览器并导航至:

      https://YourNeptuneEndpoint:8182/status

      您应该会看到一份类似于下图的报告,指出您的特定集群的状态和详细信息:

       {
         "status": "healthy",
         "startTime": "Wed Nov 04 23:24:44 UTC 2020",
         "dbEngineVersion": "1.0.3.0.R1",
         "role": "writer",
         "gremlin": {
           "version": "tinkerpop-3.4.3"
         },
         "sparql": {
           "version": "sparql-1.1"
         },
         "labMode": {
           "ObjectIndex": "disabled",
           "DFEQueryEngine": "disabled",
           "ReadWriteConflictDetection": "enabled"
         }
       }
      

    关闭连接

    When you're ready to close the connection, use Ctrl+D to exit.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2018-07-15
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      相关资源
      最近更新 更多