security - Run Golang as www-data -


when run node http server app call custom function

function runaswww() {  try   {   process.setgid('www-data');   process.setuid('www-data');  } catch (err)   {   console.error('cowardly refusal keep process alive root.');   process.exit(1);  } } 

from server.listen(8080,'localhost',null,runaswww);

so server running www-data user offer better modicum of security. there similar can when start golang web server issuing go run index.go?

expanding on @jimb's answer:

use process supervisor run application specific user (and handle restarts/crashes, log re-direction, etc). setuid , setgid universally bad ideas multi-threaded applications.

either use os' process manager (upstart, systemd, sysvinit) or standalone process manager (supervisor, runit, monit, etc).

here's example supervisor:

[program:yourapp] command=/home/yourappuser/bin/yourapp # location of app autostart=true autorestart=true startretries=10 user=yourappuser # user app should run (i.e. *not* root!) directory=/srv/www/yourapp.com/ # application runs environment=app_settings="/srv/www/yourapp.com/prod.toml" # environmental variables redirect_stderr=true stdout_logfile=/var/log/supervisor/yourapp.log # name of log file. stdout_logfile_maxbytes=50mb stdout_logfile_backups=10 

further: if you're not reverse proxying , go application needs bind port < 1024 (e.g. port 80 or 443) use setcap - example: setcap cap_net_bind_service=+ep /home/yourappuser/bin/yourapp

ps: wrote a little article on how run go applications supervisor (starting "i don't have supervisor installed").


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -