Let's say I have R or Python scripts which connect to a SQL database using SQL authentication on a schedule. In the scripts, one way or another, the username/password must be passed through the functions of RJDBC (in R) or SQLAlchemy (in Python). What is the best way to protect these passwords from an outsider who gains access to the server and scripts?

  • If the script has access to use the username/password without actually seeing it, what difference does it make? Or, perhaps you theorize an attack where the attacker cannot edit the script, just see its contents? Nov 1, 2016 at 18:14
  • Turtles All The Way Down is a good talk on this subject. Nov 1, 2016 at 23:59
  • Store passwords in (os) keyring. Usage in R. Mar 1, 2019 at 9:48

1 Answer 1


If you fear that the server is compromised and the attacker gets access to the scripts you need to store the passwords outside of the scripts where the attacker has no access. If this is possible depends on the kind of attack you expect:

  • If you expect only a limited attack where the attacker gets only limited access to a few data then you might store the credentials in a file where the script user has access but not the attacker.
  • If instead you expect a full compromise then you need to store the credentials on a different system. But it will be hard to make the script then make automatically use these remote credentials. It might be possible to automatically feed the credentials from remote into your application but how this is done depends on your specific environment and use case.
  • My question is geared mostly towards someone outside the network compromising a limited set of scripts. Storing in a different system wouldn't be feasible, given the automatic nature of it (and I don't think I'm well-versed enough to set something up like that). I am using full-disk encryption, but the scripts run on a server which is active 24/7 (i.e. I don't log out of the user, so the disk space where the scripts are never really end up encrypted). Nov 1, 2016 at 16:11
  • It makes sense to store the passwords outside of the scripts, but how far outside of the server? If in my scripts I use a key to decrypt a file storing the username/passwords, couldn't an attacker just use the key that's in the script and decrypt the file themselves? Nov 1, 2016 at 16:13
  • @ManWithAMiddleName: I don't understand your use case based on your comments. Because in the first you say that storing the credentials on a different system is infeasible for you while in the second you ask for details how this could be done ("outside of the server"). Also, it would be better if you would add details about your use case to the question instead of a comment. Nov 1, 2016 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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