简体   繁体   中英

How to connect to the Message Hub service on bluemix via node app using cfenv and vcap services

I have a Message Hub service instance on Bluemix which I am trying to connect to via node app. I am able to connect via cfenv as given in documentation:

var Cfenv = require('cfenv');
var appEnv = Cfenv.getAppEnv();
if(restEndpoint && apiKey) {
  appEnv.services = {
    "messagehub": [
       {
          "label": "messagehub",
          "credentials": {
             "api_key": apiKey,
             "kafka_rest_url": restEndpoint,
          }
       }
    ]
  };
} else {
  console.error('A REST Endpoint and API Key must be provided.');
  process.exit(1);
}

For this I am giving the API key and restEndpoint directly in my node app, which are created in the Bluemix service credentials.

restEndpoint = "https://kafka-rest-**************"

apiKey = "*******************"

But I don't want to give the above details in the code itself for security. So is there anyway that I can prevent giving the above credentials and call the service? I came to know we can use cfenv and VCAP_serives to achieve it, but I do not have any idea of how to do it. Can someone help me with some ideas?

Im editing my question here itself, Im connecting to message hub service from my node app using cfenv ,but the values are hardcoded as below.

restEndpoint = "https://kafka-rest-***************************"

apiKey = "******************************"


if(restEndpoint && apiKey) {
  appEnv.services = {
    "messagehub": [
       {
          "label": "messagehub",
          "credentials": {
             "api_key": apiKey,
             "kafka_rest_url": restEndpoint,
          }
       }
    ]
  };
} else {
  console.error('A REST Endpoint and API Key must be provided.');
  process.exit(1);
}
var instance = new MessageHub(appEnv.services);

But now after pushing to bluemix I got the environment variables(VCAP services )as

"messagehub": [
  {
     "credentials": {
        "mqlight_lookup_url": "https://mqlight*********",
        "api_key": "**************",
        "kafka_admin_url": "https://kafka-admin-***********",
        "kafka_rest_url": "https://kafka-rest-********",

        "user": "*****",
        "password": "*******"
     },
     "syslog_drain_url": null,
     "label": "messagehub",
     "provider": null,
     "plan": "standard",
     "name": "Message Hub",
     "tags": [
        "ibm_dedicated_public",
        "web_and_app",
        "ibm_created"
     ]
  }

]

So how can I connect to message hub service using the above VCAP_services rather than hardcoding the values in the code itself.

Thanks.

Yes you can retrieve your services' credentials at runtime.

When running in Bluemix, with Node.js, they are stored in process.env.VCAP_SERVICES .

You can also directly inspect this JSON object from the Bluemix UI. Navigate to your application's dashboard and go to Environment Variables.

Sample code for parsing VCAP_SERVICES:

var serviceName = 'messagehub';
var opts = {};
if (process.env.VCAP_SERVICES) {
    // Running in Bluemix
    var services = JSON.parse(process.env.VCAP_SERVICES);
    for (var key in services) {
        if (key.lastIndexOf(serviceName, 0) === 0) {
            messageHubService = services[key][0];
            opts.broker = messageHubService.credentials.kafka_brokers_sasl;
            opts.user = messageHubService.credentials.user;
            opts.password = messageHubService.credentials.password;
        }
    }
    if (!opts.hasOwnProperty('broker') || !opts.hasOwnProperty('user') || !opts.hasOwnProperty('password')) {
        throw 'Error - Check your app is bound to a ' + serviceName + ' service';
    }
} else {
    // Running locally
    opts.broker = 'localhost:9093'; 
    opts.user = 'username';
    opts.password = 'password'; 
}

When running locally, you have the choice to do it the way you want: - If you want to also run in Bluemix, setting up a local environment variable mimicking VCAP_SERVICES might be a good idea so both can use the same logic. - If not, you can just write a getter that fetches your credentials from a safe location.

cfenv ( https://www.npmjs.com/package/cfenv ) allows to abstract some of it, like detect if run locally or in Bluemix, parse VCAP_SERVICES, etc. But if run locally you still have to give it your values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM