Can't Connect MongoDB With SSL in Azure machine with another MVC application in Azure - mongodb

I'm having problem in connecting MongoDB which is configured using SSL. I have MongoDB enterprise server in Azure virtual machine which has the following configuration.
net:
bindIp: 0.0.0.0
port: 27017
ssl:
CAFile: 'C:\openssl-0.9.8h-1-bin\bin\rCA.pem'
PEMKeyFile: 'C:\openssl-0.9.8h-1-bin\bin\rser.pem'
allowConnectionsWithoutCertificates: false
allowInvalidHostnames: true
mode: requireSSL
storage:
dbPath: 'C:\data\db'
I have a C# sample to connect mongodb with certificate data passed as byte array.
MongoClientSettings settings = new MongoClientSettings
{
Server = new MongoServerAddress("mongo_azure_host", 27017),
UseSsl = true,
RetryWrites = true
};
settings.VerifySslCertificate = false;
var SslCertificateData = FilePathHelper.ReadFile(Server, mySslClientCertificate);
var certificate = new X509Certificate2(SslCertificateData, "pwd");
settings.SslSettings = new SslSettings()
{
ClientCertificates = new[] { certificate }
};
}
MongoClient mongoClient = new MongoClient(settings);
mongoClient.GetServer().Connect();
This works fine if the sample is in my local environment. But if I pubish the same in Azure web app and tried to connect, it throws the following exception
system.componentmodel.win32exception: the credentials supplied to the package were not recognized

Related

Cannot connect to MongoDB v6 in docker in ec2 after enabling SSL/TLS in .NET core app

I think I am bit confused on MongoDB v& in docker with TLS enabled. Everything seems to be working fine on the EC2 instance and also with those public key on my local MongoDB Compass tool.
But come to my .NET application, it is not connecting.
I have tried everything and I need some help with connection string.
Here is the code:
string conn = #"mongodb://username:Password#ec2instance.compute.amazonaws.com:27017/?tls=true&authMechanism=SCRAM-SHA-1";
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl(conn));
clientSettings.AllowInsecureTls = true;
clientSettings.DirectConnection = true;
clientSettings.Scheme = ConnectionStringScheme.MongoDB;
clientSettings.UseTls = true;
clientSettings.ServerApi = new ServerApi(ServerApiVersion.V1);
SslSettings sslSettings = new SslSettings
{
ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true,
CheckCertificateRevocation = false,
EnabledSslProtocols = SslProtocols.Tls12,
ClientCertificates = new List<X509Certificate>() {
new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + #"Files\intermediate_pfx.pfx", "Phaseuser")
},
ClientCertificateSelectionCallback = (sender, host, certificates, certificate, issuers) => clientSettings.SslSettings.ClientCertificates.ToList()[0],
};
clientSettings.SslSettings = sslSettings;
MongoClient client = new MongoClient(clientSettings);
var dbList = client.ListDatabases().ToList();
And here is my compose.yaml file:
services:
mongo:
image: mongo
command: ["--bind_ip_all", "--tlsMode", "requireTLS", "--tlsCAFile", "/usr/local/ca-certificates/root_ca.crt", "--tlsCertificateKeyFile", "/run/secrets/server-certificate"]
environment:
MONGO_INITDB_ROOT_USERNAME: username
MONGO_INITDB_ROOT_PASSWORD: Password
restart: always
volumes:
- $PWD/ca-certs:/usr/local/ca-certificates
- $PWD/db:/data/db
secrets:
- server-certificate
ports:
- '27017-27019:27017-27019'
secrets:
server-certificate:
file: mongo.pem
Everytime when I tried to connect to it, its says timeout.
Any suggestions would be really helpful.

PeerJS Server connecting SSL

I started my own PeerJS server, everything works, but now I don't know how to connect an ssl certificate to it. The documentation says:
Simply pass in PEM-encoded certificate and key.
const fs = require('fs');
const { PeerServer } = require('peer');
const peerServer = PeerServer({
port: 9000,
ssl: {
key: fs.readFileSync('/path/to/your/ssl/key/here.key'),
cert: fs.readFileSync('/path/to/your/ssl/certificate/here.crt')
}
});
and where to insert this code, where this file is located?
please explain who knows...
This code should be added to the server.js file

Unable to connect to postgres using deno.js

Unable to connect to postgres in deno.js.
Here is the configuration:
const dbCreds = {
applicationName: "appname",
user: "user_sfhjwre",
database: "d9iu8mve7nen",
password: "68790f31eelkhlashdlkagsvADSDa52f9d8faed894c037ef6f9c9f09885603",
hostname: "ec2-345-34-97-212.eu-east-1.xx.amazonaws.com",
port: 5432,
};
export { dbCreds };
Usage:
import { Client } from "https://deno.land/x/postgres/mod.ts";
import { dbCreds } from "../config.ts";
const client = new Client(dbCreds);
await client.connect();
Also tried:
config = "postgres://user#localhost:5432/test?application_name=my_custom_app";
const client = new Client(config);
await client.connect();
Same result:
Uncaught Error: Unknown auth message code 1397113172
Is there anything wrong with the syntax, I can connect to the same db using prisma.
I have the PostgreSQL server in a remote server and, each time my public IP changes, I need to change the pg_hba.conf in order to set my new public IP as authorized for remote access.
Hope this helps.
Best regards.

Connect with mongodb server on digital ocean

I followed
DigitalOcean Mongodb Install
sudo ufw allow from your_other_server_ip/32 to any
I set your other server as 127.0.01 as I will be connecting it with express local device as localhost:3000
sudo ufw allow from 127.0.0.1/32 to any
and created admin user.
I have also updated mongodb.conf to
logappend=true
bind_ip = 127.0.0.1,139.**.*.**
port = 27017
How can I make connection with mongoose now.
I tried with a gui with ssh connection and it worked.
How can I connect it with HTTP URL.
EDIT - 1
I installed mongodb
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04
And with
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-mongodb-on-ubuntu-16-04#part-two-securing-mongodb
Step 3 — Testing the Remote Connection
I am getting
MongoDB shell version v3.6.3
Enter password:
connecting to: mongodb://139.**.*.***:27017/
MongoDB server version: 3.6.3
And running
> show users
{
"_id" : "testdb.testusr",
"user" : "testusr",
"db" : "testdb",
"roles" : [
{
"role" : "readWrite",
"db" : "testdb"
}
]
}
If i try to connect with mongoose with below code
var connectionString = "mongodb://testusr:testpwd#139.**.*.***:27017/testdb";
mongoose
.connect(connectionString, {
keepAlive: 1,
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(`DB Connection Error: ${err.message}`);
});
I am getting below output
DB Connection Error: Server selection timed out after 30000 ms

How can I configure a play-slick-db connection with ssh config?

This is my current db connection in play slick
slick.dbs.default {
driver="utils.db.PostgresDriver$"
db {
driver = org.postgresql.Driver
url = "jdbc:postgresql://127.0.0.1/testdb"
user = "root"
password = ""
keepAliveConnection = true
}
}
My question is, how will I connect to a remote db that requires ssh authentication?