<p>So our web server apps need to connect to the database, and some other apps have startup scripts that execute at boot time.</p>

<p>What's the best way to store the name/password for these applications, in terms of</p>

<ul>
<li>security, e.g. perhaps we don't want sysadmins to know the database password</li>
<li>maintainability, e.g. making the configuration easy to change when the password changes, etc.</li>
</ul>

<p>both windows and linux solutions appreciated!</p>

[{'answer_id': 10734, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 1, 'selected': False, 'text': "<p>plain text? If they're on your server, I would hope the server is secure enough not to allow unauthorised access. If people can access your config files on the server, something has gone wrong much earlier.</p>\n"}, {'answer_id': 10745, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 1, 'selected': False, 'text': '<p>clarification: in terms of security, maintainability (e.g. if the login needs to change, can I find it later, etc)</p>\n\n<p>@lomax:  perhaps I might not want everyone with access to the physical server (e.g. sysadmins) to see the password.</p>\n\n<p>Thanks!</p>\n'}, {'answer_id': 10764, 'author': 'Nick Brosnahan', 'author_id': 528, 'author_profile': 'https://Stackoverflow.com/users/528', 'pm_score': 0, 'selected': False, 'text': '<p>You can bake a symmetric encryption key into your binary, and have that binary read an encrypted username/password from a file on disk when it starts up.  </p>\n\n<p>However, this is not really much more than obfuscation, since your code is likely to be stored in some source repository somewhere.  </p>\n\n<p>I would suggest that you would be better served to control access to your servers both physically and over the network using a firewall and a private network bubble, and store the passwords in the clear (or base-64 encoded) on disk with permissions locked down to the run user for your web app.</p>\n\n<p>You can also lock down the database server to only accept connections from your web app machines by IP.</p>\n\n<p>Ultimately, your problem is that the key (your DB username/password pair) needs to be available for programmatic, unattended use by your web apps.</p>\n'}, {'answer_id': 10766, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 2, 'selected': False, 'text': "<p>I agree with lomaxx: if somebody is already on the server or has wide ranging access to it (like a sysadmin), the game is pretty much over. So the idea would be to use a server you trust that it is secure to the degree you want it to be. Specifically:</p>\n\n<ul>\n<li>You need to trust the sysadmins</li>\n<li>You need to trust anybody else who is running code on the same server (this is why shared hosting is a big no-no for me)</li>\n</ul>\n\n<p>Beyond that, environment variables seem to be a popular choice for storing these types of credentials, because this means that access to the source only (for example by compromising the dev box) doesn't reveal it directly and also it can be nicely localized for each server (dev, test, etc).</p>\n"}, {'answer_id': 10779, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 1, 'selected': False, 'text': "<p>In most cases, I believe it is sufficient to obfuscate the password in a plain text file (eg. with base64). You cannot completely protect a stored password against a determined sysadmin with root access, so there's not really any need to try. Simple obfuscation, however, protects against accidentally revealing the password to a shoulder surfer.</p>\n\n<p>A more complex alternative is to set up a dedicated secure password server that either:</p>\n\n<ul>\n<li>provides a password decryption service</li>\n<li>actually stores the passwords for use by other less secure servers</li>\n</ul>\n\n<p>Depending on the network protocols used, this may not protect against a rogue sysadmin with tcpdump. And it probably won't protect against a determined sysadmin with a debugger, either. At that point, it might be time to look at something like Kerberos tickets.</p>\n"}, {'answer_id': 10820, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 4, 'selected': True, 'text': '<p>The best way to secure your password is to stop using one. Use a trusted connection:\n<a href="http://msdn.microsoft.com/en-us/library/ms998292.aspx" rel="nofollow noreferrer">How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0</a>. \nThen you have nothing to hide - publish your web.config and source to the world, they still can\'t hit your database.</p>\n\n<p>If that won\'t work for you, use the built in <a href="http://msdn.microsoft.com/en-us/magazine/cc163614.aspx" rel="nofollow noreferrer">configuration encryption system in ASP.NET</a>.</p>\n'}, {'answer_id': 2902632, 'author': 'user349653', 'author_id': 349653, 'author_profile': 'https://Stackoverflow.com/users/349653', 'pm_score': 3, 'selected': False, 'text': '<p>PostgreSQL offers a <a href="http://www.postgresql.org/docs/current/static/ssh-tunnels.html" rel="nofollow noreferrer">nice solution</a> for this kind of situation in their documentation.  Essentially, you use ssh to bridge a port on your machine to the PostgreSQL server port on the remote machine.  This has three stages of authentication:</p>\n\n<ol>\n<li>Restrict access to the local port, such as only letting a particular user connect to it.</li>\n<li>Set up password-less connection to the PostgreSQL host with ssh as a particular user.</li>\n<li>Allow the user ssh connects as to have local access to PostgreSQL without a password.</li>\n</ol>\n\n<p>This reduces the security to whether your user accounts are secured and your ssh configuration is sound, and you have no need of a password stored anywhere.</p>\n\n<p><strong>Edit</strong>: I should add that this will work with any database that listens to a TCP/IP port.  It just happens to be described in PostgreSQL.  And you will want iptables (or the equivalent off Linux) to do the port restrictions.  See <a href="http://www.linuxjournal.com/article/6091" rel="nofollow noreferrer">this</a>.</p>\n'}]
['https://Stackoverflow.com/questions/10731', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/116/']