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.