Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am not yet accustomed with the way Git works (And wonder if someone besides Linus is ;)).

If you use Heroku to host you application, you need to have your code checked in a Git repo. If you work on an open-source project, you are more likely going to share this repo on Github or other Git hosts.

Some things should not be checked in the public repo; database passwords, API keys, certificates, etc... But these things still need to be part of the Git repo since you use it to push your code to Heroku.

How to work with this use case?

Note: I know that Heroku or PHPFog can use server variables to circumvent this problem. My question is more about how to "hide" parts of the code.

share|improve this question
2  
I'd start with using a private repo. Then, depending how much I trust it I may .gitignore my config files that contain the sensitive data and only version them locally. (Local being a non external central location which may be an in house server or who knows) –  Rig Sep 5 '12 at 14:35

4 Answers 4

The preferred method of keeping passwords/api keys secret on heroku is to set config values via the heroku commandline application. The following example taken from a heroku dev center article

(The below example, and my entire answer relate to rails apps)

$ cd myapp
$ heroku config:add S3_KEY=8N029N81 S3_SECRET=9s83109d3+583493190
Adding config vars and restarting myapp... done, v14
S3_KEY:     8N029N81
S3_SECRET:  9s83109d3+583493190

Then reference these config values in your code using the ENV[] variable

AWS::S3::Base.establish_connection!(
  :access_key_id     => ENV['S3_KEY'],
  :secret_access_key => ENV['S3_SECRET']
)

This way your sensitive passwords are not stored in the git repository. (Note: When running the app locally, set these values in your .bashrc file

Also, I'm not sure what type of application you are running, but in Rails, heroku does not use your database.yml file, it simply sets your database username/password according to your app settings. So you can avoid saving those credentials in git

Also, also, if you are running your own application and want it to remain private, a great alternative to github is bitbucket which offer free private repositories.

share|improve this answer

Several ideas... Public Key Cryptography is the most flexible answer.

Obfuscation (for code only)

For the parts of the code you want to hide, could you put them in a different project, compile them, and check in only the compiled code, not the source? This is not encryption and not suitable for encrypting passwords or keys. People can still reverse-engineer your compiled code, but they don't get the source.

Private GIT repository

Does it have to be a public git repository?

Server Storage

Can you store this info in a protected file in the home directory of the user account the application runs under? I would copy the way ssh does this with ~/.ssh/id_rsa and a chmod of 600. Failing that, an environment variable could be used. You need somewhere on the server to store some kind of key, or there is no way you can protect anything.

Symmetric Cryptography (just for you)

If you are the sole developer, you could put a key on the server and have that same key on your machine and use a symmetric encryption scheme to protect some data like a password or certificate. Sharing a symmetric key with friends gets messy.

Asymmetric Cryptography (for multiple developers)

If other developers need to check secret things into a public git repository, , public-key/private-key (asymmetric) cryptography was made for this kind of thing. Install a private key on your your server (do not check it into source control!) and generate a public key from it. Encrypt your secret data using the server's public key. Only the server can decrypt that data using its private key. You can even check the public key into source control so that other people can encrypt data using the same public key and only the server can decrypt it.

Tool

Openssl is probably the only cryptography tool you'll ever need. Do not write your own cryptography algorithm or your own implementation of a published algorithm.

Closing thoughts

If the "server" is a web server that uses https, then you should already have a secure keystore of some kind on the server to store the private key in. It's kind of mind-blowing that a hosting company wouldn't make allowances for this. Maybe they have some hints on how others solve the challenge you are facing?

share|improve this answer
    
To add, WRT to server side storage - I don't know if you can do this with Heroku, but I've seen setups where a script is fired from the post-receive hook and/or something like Capistrano on the deployment server to copy the deployment database file to the correct location. This allows you to keep the database file out of the repo completely, and have an automatic mechanism to ensure such information is still in place. –  Shauna Jan 9 '13 at 18:40

If you want to run your code on Heroku, you have to give it to them - you can't keep it "secret" from your hosting provider.

So far as public git repositories go, if your project is open source but you don't want to share hosting details, you would need to maintain a private fork of your project for the purposes of deployment.

share|improve this answer
1  
Thanks, but what is the workflow for this? –  Jonas Sep 4 '12 at 6:16

You should not be hiding parts of the code. The security of your system should not rely upon the secrecy of the code; that's known as "security through obscurity", which is frowned upon by security experts because it works very poorly.

Instead, passwords, crypto keys, etc. should be kept separate from the code. Store them in a separate configuration file or configuration value which is read by the code. You don't need to store them in git.

Important: Never hardcode cryptographic keys, passwords, or other secrets in your source code! That is very bad practice.

See also:

Plug: IT Security.SE is a great place to ask questions about security!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.