【问题标题】:Connecting to PostgreSQL hosted on Azure using SSL with Elixir backend使用带有 Elixir 后端的 SSL 连接到托管在 Azure 上的 PostgreSQL
【发布时间】:2020-09-11 11:13:53
【问题描述】:

我正在尝试使用 SSL 在本地连接到托管在 Azure 上的 PostgreSQL (v. 9.6)。不幸的是,它没有按预期工作。后端运行在一个 docker 容器中,以 Alpine Linux 作为基础镜像,Elixir 1.7.4,当前的 ecto 版本,带有 ecto-sql (v. 3.0.0)。

在 Azure 上启用Enforce SSL connection 并设置相应的防火墙规则。

我用本地的数据库工具(TablePlus)查了一下,可以很方便的连接到在线数据库。

这是我的配置:

config :my_app, MyApp.Repo,
  username: "admin@postgres-dev",
  password: "secret",
  database: "dev",
  port: 5432,
  hostname: "postgres-dev.postgres.database.azure.com",
  ssl: true,
  ssl_opts: [
    versions: [:"tlsv1.2"],
    cacertfile: "priv/root.pem"
  ],
  pool_size: 15

我还在mix.exs 中的extra_applications 中添加了:ssl

由于 stackoverflow 上的 answer 在这里,我明确设置了 versions。 我从Azure 获得了证书,并按照那里的描述使用 OpenSSL 对其进行了解码。

我在没有ssl_opts 的情况下进行了尝试,仅在ssl_opts 设置了一个属性,并且还使用了database_url(有和没有查询参数:?ssl=true),而不是单独描述所有属性。

我总是遇到同样的错误:

(Mix) The database for MyApp.Repo couldn't be created: connection not available and request was dropped from queue after 2844ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information

使用不同的versions 也会导致错误:

14:39:26.222 [error] GenServer #PID<0.500.0> terminating
** (DBConnection.ConnectionError) ssl connect: Invalid TLS option: {'tlsv1.3',{versions,['tlsv1.3']}} - {:options, {:"tlsv1.3", {:versions, [:"tlsv1.3"]}}}
(db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for MyApp.Repo couldn't be created: killed

所以这绝对不是解决方案

当不设置ssl: true时,我会按预期报错:

10:19:02.036 [error] GenServer #PID<0.251.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) SSL connection is required. Please specify SSL options and retry.
(db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for MyApp.Repo couldn't be created: killed

有没有人已经尝试过并且可以提供帮助?

【问题讨论】:

    标签: postgresql azure ssl elixir ecto


    【解决方案1】:

    在这里使用 TSL 1.2。

    在 TLS 握手中,您正在连接的服务器将发送其证书。你不需要提供它。

    ssl: true,
    ssl_opts: [
      versions: [:"tlsv1.2"]
    ]
    

    为了避免超时问题......

    timeout: 120_000,
    queue_target: 10_000,
    queue_interval: 100_000
    

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2015-11-19
      • 2019-08-28
      • 2016-12-24
      • 2019-02-19
      相关资源
      最近更新 更多