Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide connection string, user name, pw when using source control?

I'm working on a small side-project and I'm using connection strings and also api keys and values that should not be seen or used by other people. I use a public GitHub account for source control. What is the usual method for using source control when these values are in plain text in web.config?

Do I need to remove the values manually before checking in code?

like image 280
Bill Avatar asked Jul 05 '10 02:07

Bill


People also ask

How do I protect my connection string?

The best way to secure the database connection string is to encrypt the value within the configuration file. The application would then load the encrypted value from the config file, decrypt the value, and then use the decrypted value as the connection string to connect to the database.

Where do I put ConnectionString in web config?

config file in the Views folder.) Find the <connectionStrings> element: Add the following connection string to the <connectionStrings> element in the Web. config file.

Is it safe to store connection string in web config?

config based connectionstring as seems is unsafe, because one can read it. But think about it, if a person can read your web. config, means he can edit any file on your server anyways as he probably already hack or gain access to file.


2 Answers

We keep sensitive and/or machine-specific configuration in separate config files, then use configSource to include them like so...

<connectionStrings configSource="cstrings.config"/> 

This way you can check in Web.config and a cstrings.config file that has a generic value that can be used on a dev machine. (e.g., (local),...MyAppNameDb...)

For production environments, mark the cstrings.config file as read-only and set up your deployment script so that you don't attempt to write over it. Your production connection string is protected by whatever security you have on that box. This keeps your sensitive strings out of version control.

cstrings.config:

<?xml version="1.0" encoding="utf-8" ?> <connectionStrings>     <add name="Default" connectionString="Server=localhost"/> </connectionStrings> 
like image 36
Rob Avatar answered Sep 26 '22 13:09

Rob


What I find works is to check in a version of the file with blanked or dummy values and then to run:

git update-index --assume-unchanged [fileName] 

Git will then stop monitoring changes to that file allowing you to put the real config info into it without fear of checking it in.

If you later make changes that you DO want to check in you can run:

git update-index --no-assume-unchanged [fileName] 
like image 169
Rupert Bates Avatar answered Sep 22 '22 13:09

Rupert Bates