By peterjf2
The current code I have set up works fine, but it is not secure.
const { Pool } = require('pg')
const pgPool = new Pool({
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
port: process.env.PGPORT,
ssl: {
rejectUnauthorized: false
},
})
What I would like the ssl object to look like is this.
ssl: {
rejectUnauthorized: true,
ca: process.env.CACERT,
},
Unfortunately I am having trouble figuring out how to put make the certificate available to my app.I would prefer not to commit the certificate to my source control…
Thanks, Peter
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there @peterjf2,
You would need to pass the CA certificate to your application as an environment variable.
You can refer to the CA_CERT variable which holds the CA certificate for the Managed databases.
For more information on how to use the environment variables on the App platform, I could suggest taking a look at the official documentation here:
https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/
Hope that this helps. Regards, Bobby
Okay, for anyone else who gets stuck on this, I had to put the entire cert into double quotes and add a \n after each line.
So now my database connection code looks like
const { Pool } = require('pg')
const fs = require('fs')
const pgPool = new Pool({
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
port: process.env.PGPORT,
ssl: {
rejectUnauthorized: true,
// ca: fs.readFileSync(
// `${process.cwd()}/cert/ca-certificate.crt`.toString()
// ),
ca: process.env.CA_CERT,
},
})
.on('connect', () => {
console.log('connected to the database!')
})
.on('error', (err) => {
console.log('error connecting to database ', err)
})
with the CA_CERT env for dotenv and my App Platform looks like:
CA_CERT="-----BEGIN CERTIFICATE-----\nVALUES HERE WITH NO SPACES AND A \n
AFTER EACH LINE\n-----END CERTIFICATE-----"
I go a little bit more into detail in my stack overflow question here: Example link
I do not think it is possible to do this with the $7 development database as I was unable to find a CA_cert anywhere to download. Would probably just have to leave the unauthorized portion as false.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.