Environment management Node.js plugins/methods for Heroku apps? - node.js

It seems that my workflow would be improved if I had some way to automatically manage my environment variables so I can have different sets load automatically depending on if the server is running locally or running on Heroku.
What are my choices? Heroku must be using something on their end, because there are variable like local.env.PORT. What are they using? I was thinking that maybe if I use the same thing it would be easier.

I believe you can use the Foreman ruby gem with Heroku even if you are deploying apps using Node.js.
Foreman allows you to specify different application configurations on your local and remote application servers. I use it to control and manage my workers, web instances on Heroku for my rails apps.
Here's a link to a Heroku page showing how to manage environment variables using foreman:
https://devcenter.heroku.com/articles/config-vars

You're looking for something like figaro which is quite useful. There is a node-figaro project inspired by figaro, but the docs are unclear on how to actually use the project to initialize environment variables.

Related

How to manage .env configuration in continous integration

I develop an application with nodejs and react. I use dotenv for configuration in my different environment.
I use TFS 2017 for build and release my application.
What is the best practise for add my .env file of production environment?
Production configs can be difficult to maintain. You can use any module or even write your own custom module to load environment variable in to your application.
However, maintaining production .env files locally for each product (i.e. committing them or putting them inside your docker image is a bad idea. If you ever happen to change some of those configs, you will have to manually change in the code for each application, build docker image (if required) and redeploy. If there are just a couple of applications, it might be easy. But if the number of related applications grow in size (as usually happens in case of microservice based architecture) - all of them sharing credentials, ips for database connections etc. it becomes a hectic task to manually change code and redeploy all the applications.
Usually, developers tend to keep a central repository of all credentials and environment variables. For examples, AWS offers parameter store and MS Azure offers Azure Key vault. In such a case, all the parameters are fetched during start time, and all we have to do is restart the application.
So usually people set only one global varibale NODE_ENV (as prod or dev), dynamically fetch all environment variables based on NODE_ENV running something like env $(node read-env-variables.js) node app.js.

Read Azure App Settings in Angular4 CLI

I have an Angular4 web app, deployed on Azure. Now I want to deploy this app to other environments on Azure: one for testing, one for acceptance and one for production. Every environment has different API endpoints and may have other variables, like Application Insights. All those environments run Angular in production mode.
The way Angular advises you to do this, is by the Enviroment files (environment.test.ts, enviroment.acc.ts, environment.prod.ts). I could configure all the different API endpoints in those files, and run my build with --prod for production for example.
But that is not the way I want to do this. I want to use the exact same application package deployed to test for my acceptance environment, without rebuilding the project. In Visual Studio Online, this is also really simple to configure.
The point is: how can I make my API endpoints differ per environment in that way?
The way I want to do this, is by the App Settings in Azure. But Angular can't get to those environment variables because it's running on the client side. Node.js is running on serverside and could get those App Settings - but if that's the way I need to do it, how do I make Node.js (used in Angular4 CLI) to send those server variables to the client side? And what about performance impact for this solution?
How did you fix this problem for your Angular4 apps on Azure? Is it just impossible to fix this problem with the Azure App Settings?
For everyone with the same question: I didn't fix this problem the way I described above.
At the end, I did it the way Angular wants you to do it: so rebuild for dev, rebuild for acc and rebuild for prod.
In Visual Studio Online, at build time, it builds and tests our code and it saves the uncompiled/unminified code. At release time, it builds en tests it again and releases it to the right environment with the right environment variables (--prod for example).
I don't think there is another way to fix this.
The solution is pretty old school but it works! Although you can use branching or tag for this purpose instead of cloning the code to the package.
The best solution as you said is Azure app settings will be saved as environment variable so you should implement an API with node.js and share the variables you want.
Of course there is an impact because of additional http call, but it's just one time at application start which is about max 5ms and depends on each program policy whether is impact or not.
Another option could be move the variables to the JSON file in the asset folder, and change it at deploy runtime with release pipeline. that's easier implementation but the disadvantage is you will have to use release variables instead of app settings and if you have config changes you will have to update the variable value first and redeploy it, although that works most of the times but sometimes you want to change just like a connection string and you will have to redeploy.

Proper way to store my own access tokens/secrets on server

When you learn front-end development, the creed is to never store passwords on the client--only on the server/db. So now I'm building API's and using third-parties like Twitter and I'm realizing that since I'm using Github, and later pushing to Heroku, I have no place on the server to store my tokens/secrets (since Heroku pulls from Github I can't add to a .gitignore).
I've come to two solutions:
1) Store in a db. That option seems trivial for smaller apps but scalability.
2) Encrypt the info on the server and upload that way.
What are the best practices when you have sensitive info, and are pushing to Heroku from Github?
The answer here is this (for anyone who finds this thread): use Heroku environment variables.
Heroku encourages you to store any application specific sensitive information in environment variables. You can set these variables via the Heroku command line tool, or the Heroku dashboard.
If you'd like to set Heroku environment variables on the command line, you can do so like this:
$ heroku config:set MY_VARIABLE=my_value
That will store the environment variable.
Since you're using Node.js, you can then read the value of these variables in your Heroku code by doing something like this:
console.log(process.env.MY_VARIABLE);
In Node, you can access environment variables via process.env as an object =)

Is it possible to control Heroku via a Node.js app (like the Heroku Ruby gem does)?

I'm used to being able to control my Heroku app instance using the Heroku gem. Is there a Node package that provides the same functionality? I would like to create some Hubot scripts for doing things like restarting my app.
Just grab the source code (https://github.com/heroku/heroku) and look at the API calls and replicate this in your node.js code
There's nothing public AFAIK but the Heroku gem merely interacts with Heroku's (unpublished) REST api - I'm pretty sure it would be easy to reverse engineer the few calls you'd need from the existing Ruby Gem so you can perform similar actions via Node.

How to store database credentials for an open-source Heroku Node.js app?

I am building a Node.js application and need to store database credentials (and other runtime properties) in such a way that they can be read when deployed on Heroku. My source is available in a public GitHub repository.
I am currently using environment variables, configured using heroku config:add, but am looking to understand if there are any alternatives. I would potentially like to use Cloud9 IDE, but it does not currently support environment variables.
Another option is to store the parameters in a config. file, but I believe the file would need to be checked in to Git (and as such, be publicly available) in order to be pushed to Heroku.
Thanks for your help.
ENV vars are generally considered the way to go, and the way Heroku do it themselves for database_urls and the like.
As you and your app are the only people with access to the env vars, you're generally OK security wise.
Putting credentials in Git or similar is a bad idea as it's another place that needs to be secured.
The one way I know of to solve the problem for development using command-line arguments. These can be specified in your run/debug configuration. You can then access the parameters in process.argv. Of course this means that they will be stored in your Cloud9IDE dev environment. You could then use the ENV variables in a retail production. This will at least prevent the credentials from being visible in source or config files.

Resources