【问题标题】:Connect to a Google Cloud SQL Postgres DB over SSL using psycopg2使用 psycopg2 通过 SSL 连接到 Google Cloud SQL Postgres DB
【发布时间】:2018-04-19 16:52:40
【问题描述】:

直到今天我才对加密一无所知,所以如果我的问题是基本的,请原谅我。

我有一个 GCP SQL 实例,它是为使用 SSL 加密的 Postgres 设置的。我在 GCP 上创建了一个客户端证书并下载并存储了:

  • 服务器-ca.pem
  • client-cert.pem
  • client-key.pem

我电脑中的文件。

我正在尝试使用 python3.6 中的 psycopg2 连接到远程数据库(使用 conda 安装)。我已经检查了documentation 以建立连接,显然需要使用上述文件才能建立连接。具体在psycopg2.connect() 函数中我使用参数:

  • sslmode='verify-ca'
  • sslcert=[local path of client-cert.pem file]
  • sslkey=[local path of client-key.pem file]
  • sslrootcert=[local path of server-ca.pem file]

显然会有错误,因为根据this上述文件需要以以下格式结尾:.crt.key

经过研究,我发现我(可能)必须使用openssl 来生成.crt.key 格式。我该怎么做?

如果我转换.pem 文件并将转换后的文件传递给psycopg2.connect(),我可以连接到我的远程数据库吗?

【问题讨论】:

    标签: python ssl openssl google-cloud-platform psycopg2


    【解决方案1】:

    使用 openssl 将 .pem 文件转换为 .crt 和 .key 文件

    首先使用命令提示符/终端转到存储 .pem 文件的目录。

    .crt 文件类型:

    • openssl x509 -in client-cert.pem -out ssl-cert.crt
    • openssl x509 -in server-ca.pem -out ca-cert.crt

    对于 .key 文件类型:

    • openssl rsa -in client-key.pem -out ssl-key.key

    最后使用psycopg2.connect() 连接到数据库只需将上述文件的文件路径传递给sslcert , sslkey and sslrootcert 参数。

    【讨论】:

    • 对我来说仍然超时:(
    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多