Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I run the migration with SSL enabled? #154

Closed
STRd6 opened this issue Jul 15, 2015 · 16 comments
Closed

How do I run the migration with SSL enabled? #154

STRd6 opened this issue Jul 15, 2015 · 16 comments

Comments

@STRd6
Copy link

STRd6 commented Jul 15, 2015

I'm trying to run a migration on a remote postgres host that requires SSL, passing --url=... on the command line. How do I enable SSL?

@dmcquay
Copy link

dmcquay commented Jul 27, 2015

I have the same question, but I am using the config file. I added "ssl": true, but it still does not connect via SSL.

@ben-ng
Copy link

ben-ng commented Aug 13, 2015

+1

1 similar comment
@ryanwalters
Copy link

+1

@sdepold
Copy link
Member

sdepold commented Sep 30, 2015

Hm I have a bit of hard times to answer this question, as I don't have an SSL enabled endpoint available.

@ryanwalters
Copy link

You can spin one up for free on Heroku.

@sdepold
Copy link
Member

sdepold commented Oct 5, 2015

🆒 Will try to do that ASAP (which is probably one evening during this week)

@ryanwalters
Copy link

The SSL Heroku connection string looks something like:

postgres://username:password@ec2-12-34-56-78.compute-1.amazonaws.com:5432/database?ssl=true

The ?ssl=true comes from Heroku's Docs. It is required when connecting remotely.

So back to sequelize, I try to pass this value into --url=..., including ?ssl=true and I get an error like:

Unable to connect to database: SequelizeConnectionError: no pg_hba.conf entry for host "12.34.56.78", user "username", database "database?ssl=true", SSL off

You'll notice that the database value that is being parsed includes the query parameter ?ssl=true and SSL is not being enabled.

Digging in a little bit, I see that urlStringToConfigHash ignores the query string.

It seems like two things might help here:

  1. Remove query string from database

  2. If ssl=true is being passed, enable it

    // Tried this just for testing, and it doesn't work. How do we enable SSL?
    if (urlParts.query) {
        result = _.assign(result, {
            ssl: true
        });
    }
    

Any thoughts?

@icemilo
Copy link

icemilo commented Nov 23, 2015

My PR has been merged, could you test if specifying dialectOptions will work for postgres?

@gofish
Copy link

gofish commented Dec 2, 2015

⚠️ The dialectOptions PR #220 allows users to specify the requisite ssl options, fulfilling this issue as far as the ssl feature request component, but the --url parameter parsing is still broken. A flag named '--url' is reasonably expected to support valid URL forms, with or without query parameters. The database component is specified by the URI path, not including any part of the query.

It is unfortunate that node's url.parse incorrectly ambiguates the path component with the query component, and thus does not conform to RFC3986 terminology [1]. As such, urlStringToConfigHash should apparently be using the urlParts.pathname field, not urlParts.path, a fairly understandable mistake.

[1] "The path is terminated by the first question mark ("?") or number sign ("#") character, or by the end of the URI."

@joshuakarjala
Copy link

@gofish could you please give an example of how you solve this issue using dialectOptions for postgres.

@gofish
Copy link

gofish commented Dec 30, 2015

@joshuakarjala You need to use a config file and specify "dialectOptions" with an "ssl" entry.

Example:

{
  "url": "postgres://localhost/database",
  "dialectOptions": {
    "ssl": {
      "ca": "path/to/ca.crt"
    }
}

The default config file is "config/config.json" (or config/config.js). Specify an alternative using --config .

@joshuakarjala
Copy link

Got it to work by doing:

{
   "use_env_variable":"DB_CONNECTION_STRING",
   "dialect":"postgres",
   "ssl":true,
   "dialectOptions":{
      "ssl":{
         "require":true
      }
   }
}

Still would be much appreciated if ?ssl=true as part of the URL was supported

@sushantdhiman
Copy link
Contributor

@metapink
Copy link

metapink commented Aug 15, 2018

This seems to be related, but has slightly different information https://github.com/sequelize/cli/tree/master/docs#dialect-specific-options
It might also be incomplete however, https://stackoverflow.com/questions/43948920/how-to-connect-via-ssl-to-sequelize-db

igorski89 added a commit to igorski89/cli that referenced this issue May 31, 2019
codetriage-readme-bot pushed a commit to codetriage-readme-bot/cli that referenced this issue Jun 5, 2019
Add List command to display a list of available exercises
@somratpro
Copy link

hey @joshuakarjala

I am going throw the same problem. I have created a file config/config.json in my project. and paste your code there. changed the env_variable to DATABASE_URL . but still heroku giving me the same error. can you help me? I am looking for help last 5 days.

error_log: {"error":true,"message":"no pg_hba.conf entry for host \"176.34.207.82\", user \"okqjgsiepkuary\", database \"d6s00lqc8oi37u\", SSL off"}

@RedJanvier
Copy link

RedJanvier commented Oct 17, 2021

Got it to work by doing:

{
   "use_env_variable":"DB_CONNECTION_STRING",
   "dialect":"postgres",
   "ssl":true,
   "dialectOptions":{
      "ssl":{
         "require":true
      }
   }
}

Still would be much appreciated if ?ssl=true as part of the URL was supported

Thank you! I had an issue that the certificate was self-signed which was resolved by adding rejectUnauthorized: false to the dialectOptions as follows:

{
   "use_env_variable":"DB_CONNECTION_STRING",
   "dialect":"postgres",
   "ssl":true,
   "dialectOptions":{
      "ssl":{
         "require":true
         "rejectUnauthorized": false
      }
   }
}

💡 Referrence: brianc/node-postgres#2009 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.