Skip to content

Read Application Creds from a json locale file. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
soufi opened this issue May 4, 2015 · 5 comments
Closed

Read Application Creds from a json locale file. #6

soufi opened this issue May 4, 2015 · 5 comments

Comments

@soufi
Copy link

soufi commented May 4, 2015

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
Copy link
Member

pmuellr commented May 5, 2015

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
Copy link
Author

soufi commented May 5, 2015

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
Copy link
Member

pmuellr commented May 5, 2015

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
Copy link
Author

soufi commented May 6, 2015

Understood.

Thank you for all these tips !

@pmuellr
Copy link
Member

pmuellr commented Oct 9, 2015

closing as I believe this has been resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants