SSL connection issues between go Scratch container

2019-06-14 15:33发布

问题:

UPDATE

I believe I resolved this issue with this connection...

db, err := gorm.Open("postgres", "host='postgres'&user:docker&port=5432&dbname='docker'&password='password'&sslmode=disable")

I am getting a connection refused between a Docker PG container and a GoLang Scratch container. The error is:

============   exiting ==========
todo_1      | pq: SSL is not enabled on the server
todo_1      | panic: failed to connect database e

The docker-compose.yml file

The main.go file which is panicking

Complete code base with Docker files

OTHER NOTE:

  • I am using GORM library to connect with PG.

REQUEST: Please look over code and give some hints on where and how to resolve this SSL issue.

回答1:

You can follow the code to find the documentation on how to use the Postgres calls.

  • github.com/jinzhu/gorm calls
  • database/sql which calls
  • lib/pq

And lib/pq documents it's usage including:

  • dbname - The name of the database to connect to
  • user - The user to sign in as
  • password - The user's password
  • host - The host to connect to. Values that start with / are for unix domain sockets. (default is localhost)
  • port - The port to bind to. (default is 5432)
  • sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)
  • fallback_application_name - An application_name to fall back to if one isn't provided.
  • connect_timeout - Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.
  • sslcert - Cert file location. The file must contain PEM encoded data.
  • sslkey - Key file location. The file must contain PEM encoded data.
  • sslrootcert - The location of the root certificate file. The file must contain PEM encoded data.

And:

  • disable - No SSL
  • require - Always SSL (skip verification)
  • verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
  • verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name
    matches the one in the certificate)

So your connect string for a database without SSL is simply:

db, err := gorm.Open("postgres", "host='postgres' port=5432 user=docker dbname='docker' password='password' sslmode=disable")

Or you could configure Postgres with an SSL key. That's less trivial, but to implement you'd need to modify the postgresql.conf file to enable the ssl settings and mount the TLS key pair as a volume, or preferably a secret with swarm mode. One of the many examples of how to do this can be found at: https://gist.github.com/likwid/86193ef581c530ea55d3