Connecting NodeJS app with OracleDB using SecureGa

2019-08-20 06:06发布

问题:

I'm trying to connect my NodeJS app to a Oracle DB using SecureGateway but doesn't work.

I executed tests, and when I run

var exec = require('child_process').exec;
var sys = require('sys');
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("ping 192.168.10.8", puts);

for test my connection, I don't have results. So I think a don't created the connection between my app and my gateway.

When I was running in DataConnect, works normally.

I use require('bluemix-secure-gateway') for take the informations from my server.

The function that is used for create the tunnel are

const tls = require('tls');
const net = require('net');

var creations = 0;  // a running count of the number of open connections, when it becomes 0, the tunnel is closed.
var server;  // a server listening for certificate requests from the gateway server 

exports.create = function(port, options, callback) {
    if(creations == 0) {

        creations++;

        //server not currently running, create one
        server = net.createServer(function (conn) {
            connectFarside(conn, options, function(err, socket) {
                socket.pipe(conn);
                conn.pipe(socket);
            });
        });

        server.listen(port, function(){
            callback();
        });

    } else{
        //server already running
        creations++;
        callback()
    }
};

function connectFarside(conn, options, callback) {
    try {
        var socket = tls.connect(options, function() {
            callback(null, socket);
        });

        socket.on('error', function(err){
            console.log('Socket error: ' + JSON.stringify(err));
        });

    } catch(err) {
        callback(err);
    }
};

exports.close = function(){

    creations--;
    if(creations == 0){
        // close the server if this was 
        // the only connections running on it
        server.close();
    }
}

The result that I have is my local net.

回答1:

To have your application reach out to Secure Gateway, it just needs to use the cloud host:port provided by your destination. For the connection to be accepted, you need to have the Secure Gateway Client running in a location that can access your database.

For example, if I wanted to connect to a Mongo database running on my local machine, I could create a destination with the Resource Hostname set to localhost and the Resource Port set to 27017. Once this destination is created, it will be assigned a cloud host:port (e.g., cap-sg-prd-3.integration.ibmcloud.com:23432).

If my application usually connects to Mongo with a connection string like mongodb://localhost:27017/myproject, I would change that to mongodb://cap-sg-prd-3.integration.ibmcloud.com:23432/myproject so the connection will be routed via Secure Gateway.