Having trouble setting up Postgres server to accept SSL connections - node.js

I'm on a Mac using Postgres.app to run a Postgres server.
I'm connecting to the server in Node.js (code copied from Heroku docs):
pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL || 'postgres://localhost:5432/my-project', function(err, client) {
if (err) throw err;
console.log('Connected to postgres! Getting schemas...');
client
.query('SELECT table_schema,table_name FROM information_schema.tables;')
.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
I then followed the instructions here to allow my Postgres server to accept SSL connections. I changed the ssl setting to on in my postgresql.conf file. I also generated the required files, server.key and server.crt.
However, when I run my Node server, I get this error:
Error: The server does not support SSL connections
I ran psql and did show ssl. It returned off. So then I thought that maybe I had the wrong config file...but then I did show config_file and I'm definitely in the right place. What else am I missing?

Very likely you forgot to restart the PostgreSQL server.
In case of problems, set log_connections = on and check the PostgreSQL server log.

Related

Node js can't upload files to FTP when deployed and running on production server

I'm using Node JS (12.13.0) and NPM (6.13.19) with basic-ftp. Everything works fine and I can upload files to the remote FTP (without SSL, my remote FTP doesn't allow this) when I run the code on my development machine from localhost.
The production server is hosted on Digital Ocean (Ubuntu 18.04.3) I have tried to disable the firewall, because I thought this might be the reason to the problem. I used sudo ufw disable and just to make sure it's disabled I check the current status with sudo ufw status which returns Status: inactive.
This is my code
async function uploadImageToFtp(fileName, path) {
const client = new ftp.Client()
client.ftp.verbose = true
try {
await client.access({
host: process.env.FTP_HOST,
user: process.env.FTP_USER,
password: process.env.FTP_PASSWORD,
secure: false
})
await client.uploadFrom(path, "images/bd/" + fileName)
} catch (err) {
console.log(err)
}
client.close()
}
Response on production
Connected to EXTERNAL_IP_ADDRESS < 220 server ready - login please Login
security: No encryption
> USER username < 331 password required
> PASS ###
Again on localhost everything works and we get past this step and starts uploading the file(s) to the same server and credentials.
After this I never get any response, except for a timeout with Bad Gateway 502 from my request.
I don't know the library, but the problem sounds like the FTP session is running in active mode. That can often be a problem, so if it is in active mode, I'd recommend trying setting your client to ask for passive mode.
There is issue with AWS dynamic routing. your instance is under vpc and nat is not able to resolve the address back from ftp server to your instance.
You can try by adding route entry in ip table. Check here
This tells the nat to resolve particular ftp to specific address. i hope this will help.

connect to mongodb in docker from node app in AWS SAM

I am getting errors connecting to mongodb running in a docker container from my Nodejs app running in AWS SAM (used to say "in my host").
I run mongodb like:
$ docker run --name mongo-myapp --rm -d -p 27018:27017 mongo
and I see the results:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab8248d17d2d mongo "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:27018->27017/tcp mongo-myapp
~~I can successfully connect and insert data using clients running on my host like MongoDB Compass and Robo 3T Community Edition, specifying port 27018.~~
When I attempt to connect with this code running on my host, (not in a docker container):
const { MongoClient } = require('mongodb');
const mongoConnection = 'mongodb://127.0.0.1:27018';
MongoClient.connect(mongoConnection, (err, db) => {
if (err) {
console.error("Failed to connect to mongodb server", err);
return reject(err);
}
console.log("Connected successfully to mongodb server");
resolve(db);
});
I always see the error:
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27018
I get the same error using another port with all steps, like 27017.
UPDATE
It turns out my code was not running on the host. It was running in another docker container. I did not realize AWS SAM would put my code into a docker container so I did not mention it in the original question.
Now I run my code with mocha test to make sure it will run on my host, and it connects to the mongo database with no problems.
When I launched a local server using AWS SAM's start-api, I had problems. Perhaps the solution will be to specify a network when starting the mongo container as well as the SAM environment.
Now that the problem is known that the Nodejs code was running within a docker container created by AWS SAM, we can help the Nodejs code connect with Mongodb running in a separate docker container with a port exposed on the host with at least one solution:
Change the connection string to mongodb://host.docker.internal:27018 which helps the code in the container to use a service running on the host.
Install the necessary dependency for mongodb in node.js https://www.npmjs.com/package/mongodb
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27018/testdb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Since you are able to connect from other clients in the same host, I assume that the container is bound to specific external facing ip of the host.
Can you try connecting to the ip of the host instead of 127.0.0.1
Eg: mongodb://external-ip:27018
Though the mapping -p 27018:27017 should bind the port to all ips, you can enforce this by -p 0.0.0.0:27018:27017.

Failed to connect mongodb from shell / robomongo. But connected via nodejs code

I have tried to connect the mongodb from shell using the following command.
mongo xxxxx.mongolab.com:47612/dbname -u user -p password
But I am getting the error : Error: 18 Authentication failed.
Same authorization failure when I try to connect from Robomongo.
But when I tried to connect mongodb from my NodeJS code, its working perfect.
NodeJS Code:
mongoose.connect('mongodb://user:password#xxxxx.mongolab.com:47612/dbname',
function (err){
if (err) throw err;
console.log("db connected")
});
Also I tried to change the bind IP in mongod.conf from 127.0.0.1 to 0.0.0.0
Issue still exists.

Nodejs net does not throw error when trying to connect to a host thats not listening

I'm trying to test whether a remote host is listening or not using Node.js net module:
var net = require('net')
var client = net.connect({port:3000, host:remoteHostIP},function(){
});
client.on('error', function(err){
console.log("Error: "+err.message);
});
I would expect that net.connect would throw an error if it can't connect but that's not the case.
Also client.on('error') does not throw an error.
How can I check if the connection has been possible?
It will throw an error when the connection times out which is about one minute of no data transfer.
Error: connect ETIMEDOUT <ip>:3000
Use client.setTimeout() to fire a callback if there's no activity within the allocated time.
The code posted in the question works fine:
node app.js
Error: connect ECONNREFUSED
The issue has been caused by a Virtualbox VM for Docker (running on the developer machine) which had portforwarding configured for port 3000 and grabbed the net connect request for localhost.

Node connecting to Mongodb in my VPS with port 27017, no if I change the port

I've got a Digital Ocean VPS, and followed their tutorial:
link
It's working the app.js and connecting to the database.
Here is the code of the apps file:
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
});
But if I change the port 127.0.0.1:27017 to 127.0.0.1:3500 the one I want to connect, it's not working.
Here is my ufw
ufw allow 22/tcp
ufw allow 3500/tcp
ufw allow 80/tcp
ufw allow 27017/tcp
Any help?
Thank you
Man, you did a bad thing. You opened MongoDB to whole world. If your node.js app is on the same server with MongoDb, then no reasons to open 27017 & 3500 for internet. Close these ports ASAP.
Why do you think that you changed mongoDb port? Please show mongoDb config file with port configuration row. Also after you changed the mongodb config file it requires restart mongodb servic/daemon.

Resources