Clojure应用程序无法使用SSL连接到Heroku Postgres

时间:2013-09-13 06:25:41

标签: postgresql heroku jdbc clojure

我一直在尝试在Hero上托管的Compojure中创建一个基本的Web应用程序。我一直在关注这个网站上的教程:

http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/

现在已经在Lobos和Korma部分磨了大约2天。我的应用程序现在可以连接到我的本地Postgres服务器,但是当我尝试推送到Heroku或连接到我的Heroku Postgres数据库时,我收到以下错误:

PSQLException FATAL: no pg_hba.conf entry for host "the IP", user "the username", database "the dbname", SSL off  org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication (ConnectionFactoryImpl.java:291)

这是我的project.clj:

(defproject portfolio "1.0.0-SNAPSHOT"
  :description "My personal portfolio"
  :url "the URL"
  :license {:name "FIXME: choose"
            :url "http://example.com/FIXME"}
  :dependencies [[compojure "1.1.1"]
                 [ring/ring-jetty-adapter "1.1.0"]
                 [ring/ring-devel "1.1.0"]
                 [ring-basic-authentication "1.0.1"]
                 [environ "0.4.0"]
                 [com.cemerick/drawbridge "0.0.6"]
                 [hiccup "1.0.4"]
                 [lobos "1.0.0-beta1"]
                 [korma "0.3.0-RC5"]
                 [org.clojure/java.jdbc "0.2.3"]
                 [postgresql "9.1-901.jdbc4"]
                 [clj-yaml "0.3.1"]
                 [http.async.client "0.5.2"]
                 [clj-bonecp-url "0.1.0"]
                 [org.slf4j/slf4j-nop "1.7.2"]
                 [org.clojure/clojure "1.5.1"]]
  :min-lein-version "2.0.0"
  :plugins [[environ/environ.lein "0.2.1"]]
  :hooks [environ.leiningen.hooks]
  :profiles {:production {:env {:production true}}})

我正在使用lobos(https://github.com/budu/lobos)进行数据迁移。我按照github页面的建议编写了一个config.clj,我根据this page的建议进行了编辑。

(ns lobos.config
  (:refer-clojure :exclude [replace reverse])
  (:use [clojure.string :as str]
        lobos.connectivity)
  (:import (java.net URI)))

(defn heroku-db
  "Generate the db map according to Heroku environment when available."
  []
  (when (System/getenv "DATABASE_URL")
    (let [url (URI. (System/getenv "DATABASE_URL"))
          host (.getHost url)
          port (if (pos? (.getPort url)) (.getPort url) 5432)
          path (.getPath url)]
      (merge
       {:subname (str "//" host ":" port path)}
       (when-let [user-info (.getUserInfo url)]
         {:user (first (str/split user-info #":"))
          :password (second (str/split user-info #":"))})))))

(def db
  (merge {:classname "org.postgresql.Driver"
          :subprotocol "postgresql"
          :subname "//localhost:5432/blogdb"}
         (heroku-db)))

(defn open-global-when-necessary
  "Open a global connection only when necessary, that is, when no previous
  connection exist or when db-spec is different to the current global
  connection."
  [db-spec]
  ;; If the connection credentials has changed, close the connection.
  (when (and (@lobos.connectivity/global-connections :default-connection)
             (not= (:db-spec (@lobos.connectivity/global-connections :default-connection)) db-spec))
    (lobos.connectivity/close-global))
  ;; Open a new connection or return the existing one.
  (if (nil? (@lobos.connectivity/global-connections :default-connection))
    ((lobos.connectivity/open-global db-spec) :default-connection)
    (@lobos.connectivity/global-connections :default-connection)))


(open-global-when-necessary db)

这给了我上面提到的错误。

我设法弄清楚如何启用SSL,但是将:ssl "true"添加到config.clj中的db映射。但是,现在我遇到了一个新错误:

SunCertPathBuilderException unable to find valid certification path to requested target.

当我尝试推送到heroku时,无论SSL是打开还是关闭,我都会收到以下错误:

Exception in thread "main" org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections., compiling:(config.clj:44:1)

如果您需要更多细节,请与我们联系。

2 个答案:

答案 0 :(得分:3)

我也无法在heroku实例上对postgres插件进行简单查询。 具有类似设置的本地设置无法在heroku设置上运行。

我所做的是在我的db-spec映射中添加:sslmode“require”键值对。

e.g。这将是我的本地数据库规范有效。

(def db-str {:classname "org.postgresql.Driver"
         :subprotocol "postgresql"
         :subname "//localhost:5432/testdb"
         :user "postgres"
         :password "password"})

这将是我的heroku数据库规范。

(def db-str {:classname "org.postgresql.Driver"
         :subprotocol "postgresql"
         :subname "//remotehost:5432/testdb"
         :user "postgres"
         :password "password"
         :sslmode "require"})

注意:sslmode键及其值。 如果没有:sslmode键值对,“SunCertPathBuilderException无法找到请求目标的有效证书路径。”会发生。

答案 1 :(得分:0)

目前最好的事情可能是简单地使用@#'parse-properties-url技巧来解决clojure.java.jdbc中你需要的函数是私有的这一事实。

但是,此更改应该可以在以后的版本中更轻松地启用SSL:

https://github.com/clojure/java.jdbc/pull/35#issuecomment-32956962