cloudfoundry-community / node-cfenv

easy access to your Cloud Foundry application environment for node
Apache License 2.0
73 stars 20 forks source link

Read Application Creds from a json locale file. #6

Closed soufi closed 8 years ago

soufi commented 9 years ago

Hi,

I've been using nconf module and one of the greatest things is that you can read some parameters from a file and if the file doesn't exists, read from environment variables like this : nconf.file('local-settings.json').env();

Is it possible to add this to cfenv ? or it already exists ?

Thank you.

pmuellr commented 9 years ago

You can pass an object with the shape of a VCAP_SERVICES object to cfenv.getAppEnv(), in the vcap option.

I'm not sure how it would work to read environment variables for your VCAP_SERVICES, since it's not the easiest value to be setting as an env var.

I guess I'd like to see a somewhat thorough use case of using > 1 service, each having > 1 property in the services' credentials object, and how you would set these in a file, env vars, etc.

soufi commented 9 years ago

Thank you for your response.

Actually my issue was that I was able to use all the services connected to my application through credentials stored in a local JSON file. But when I push the application to Bluemix it just continues to crash because I couldn't get the credentials. Basically, I didn't understand well the difference between VCAP_SERVICES object and application credentials.

After reading your IBM page : https://developer.ibm.com/bluemix/2014/10/14/keeping-secrets-cloud-application-access-credentials-private-data/

I finally came up with this code, mixing the use of nconf and cfenv :

//loading credentials from file
nconf.file('local-settings.json');
//loading 
var appEnv = cfenv.getAppEnv();

//local configuration
if(nconf.get("applicationSecret")){
    config = {
        applicationRoute : nconf.get("applicationRoute"),
        applicationId : nconf.get("applicationId"),    
        applicationSecret: nconf.get("applicationSecret")
    };
}else { // prod configuration
    config = appEnv.getServiceCreds(/session-secret/);
}

Now, when I run the server in my local machine I can use the Bluemix services like ibmdata to access data from Bluemix. And when the server runs from Bluemix it uses the "custom service" containing credentials.

I am not sure that this is the best way to do so ? but it works just fine.

pmuellr commented 9 years ago

Given the code you've provided, if you ever accidently include local-settings.json, then presumably it will be used instead of VCAP_SERVICES. Of course, you may want to do that as well, and so that would be a good strategy.

cfenv includes a property in the appEnv object returned by cfenv.getAppEnv(), named isLocal. It's how I determine if I'm running locally or in Cloud Foundry (eg, Bluemix). All it does is check to see if the VCAP_APPLICATION environment is set - it will ALWAYS be set when running in Cloud Foundry, and I've never seen anyone set that env var when running locally.

Another option is to shape your local-setting.json to be the same shape as your VCAP_SERVICES, then you can do something like this:

var localVCAP = null
try { localVCAP = require("./local-vcap.json") } catch(e) {}

var appEnv = cfenv.getAppEnv({vcap: localVCAP})

The vcap property is only used by cfenv.getAppEnv() when running "locally" - it's ignored if you're running on Cloud Foundry.

Here's an example of a JSON file for your VCAP_SERVICES that you can use in this manner:

soufi commented 9 years ago

Understood.

Thank you for all these tips !

pmuellr commented 8 years ago

closing as I believe this has been resolved