Community

Keeping secrets – how your cloud application should access credentials and other private data

Share this post:

If you’re building a web app, you probably have secrets you have to deal with:

  • database credentials
  • session keys
  • etc

So, where do you keep these secrets? Typical ways are:

  • hard-code them into your source
  • require them to be passed on the command-line of your program
  • get them from a configuration file
  • get them from environment variables

Developers using Cloud Foundry based systems have another option:

This blog post will go over the advantages and disadvantages of these approaches. Examples are provided for node.js, but are applicable to any language.

secrets via hard-coding

The documentation for the express-session package shows the following example of hard-coding your secrets into your code:

This is awful:

  • If you need to change the secret, you need to change the code; apply
    some separation of concerns, and keep your code separate from your
    configuration.
  • If you happen to check this code into a source code management (SCM) system,
    like GitHub, then everyone with access to that SCM will have access to your
    password. That might be literally everyone.

Please, DO NOT DO THIS!

Don’t be one of these people. Instead, use one of the techniques below.

secrets via configuration files

Here is an example using require() to get a secret from
a JSON file:

This example takes advantage of the node.js feature of being able to load
a JSON file and get the parsed object as a result.

If you’re going to go this route, you should do the following:

  • Do NOT store the config file in your SCM, because otherwise you may still
    be making your secret available to everyone who has access to your SCM.
  • To keep the configuration file from being stored, add the file to your
    .gitignore file (or equivalent for your SCM).
  • Create an example configuration file, say secret-config-sample.json, which other developers
    can copy to the actual secret-config.json file, and use as an example.
  • Document the example configuration file usage.

You now have an issue of how to “manage” or save this file, since it’s not
being stored in an SCM.

secrets via command-line arguments

Here is an example using the nopt package to get a secret from a command-line argument:

You can then invoke your program using either of these commands:

<code>node secret-arg.js --sessionSecret "keyboard cat"
node secret-arg.js -s "keyboard cat"
</code>

This is certainly nicer than having secrets hard-coded in your app, but it also means you will be typing the secrets a lot. If you decide to “script” the command invocation, keep in mind your script now has your secrets in it. Use the “example file” pattern described above in “secrets via config files” to keep the secret out of your SCM.

secrets via environment variables

Here is an example using process.env to get a secret from an environment variable:

You can then invoke your program using the following command:

<code>SESSION_SECRET="keyboard cat" node secret-env.js
</code>

Like using command-line arguments, if you decide to create a script like this, keep in mind your secret will be visible to those having access to the script.

You likely have other ways of setting environment variables when you run your program. For instance, in Cloud Foundry, you can set environment variables via a manifest.yml file or with the cf set-env command.

If you decide to set the environment variable in your manifest.yml file, keep in mind your secret will be in the manifest. Use the “example file” pattern described above in “secrets via configuration files” to keep the secret out of your SCM. That is, put manifest.yml in your .gitignore file, and ship a manifest-sample.yml file instead.

secrets via Cloud Foundry user-provided services

Here is an example using the cfenv package to get a secret from a user-provided service:

This is my favorite way to store secrets for Cloud Foundry. In the example above, the code is expecting a service whose name matches the regular expression /session-secret/ to contain the secret in the credentials property named secret. You can create the user-provided service with the cf cups command:

<code>cf cups a-session-secret-of-mine -p secret
</code>

This will prompt you for the value of the property secret, and then create a new service named a-session-secret-of-mine. You will need to cf bind the service to your application to get access to it.

There are a number of advantages to storing your secrets in user-provided services:

  • A service can be bound to multiple applications; this is a great way to store secrets that need to be shared by “micro-services”, if you’re into that kind of thing.
  • Once created, these values are persistent until you delete the service or use the new cf uups command to update them.
  • These values are only visible to users who have the appropriate access to the service.
  • Using regular expression matching for services makes it easy to switch services by having multiple services with regexp matchable names, and binding only the one you want. See my project bluemix-service-switcher for an example of doing this.

secrets via multiple methods

Of course, for your all singing, all dancing wunder-app, you’ll want to allow folks to configure secrets in a variety of ways. Here’s an example that uses all of the techniques above – including hard-coding an undefined value in the code! That should be the only value you ever hard-code. 🙂

The example uses the defaults() function from underscore to apply precedence for obtaining a secret from multiple techniques.

More stories

How to Backup Your IBM Cloud Linux Server

This post covers how to backup entire partitions in Linux systems. The process utilizes the tar command in IBM Cloud's unique rescue environment and showcases the simplicity and flexibility of the process.

Continue reading

Welcome to the New and Improved IBM Cloud Catalog

We are excited to announce that the IBM Cloud Catalog has recently undergone a bit of a makeover. With simplified categories, featured services, and an improved UX design, users can discover IBM Cloud offerings without the need to understand IBM-specific terminology or branding.

Continue reading

Part II: Wimbledon Facebook Bot on IBM Cloud

In the second in a series of posts about how IBM iX designed, developed, and delivered the Facebook Messenger Bot available at Wimbledon 2018, we focus on the broadcast integration within Facebook and how we persisted user preferences using IBM Cloudant and Compose Redis.

Continue reading