Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vnstat: add option to start vnstatd #19809

Merged
merged 2 commits into from Jan 24, 2017
Merged

Conversation

KaiHa
Copy link
Contributor

@KaiHa KaiHa commented Oct 23, 2016

Motivation for this change

Without a started vnstatd daemon vnstat can not monitor the network usage.

Things done
  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • OS X
    • Linux
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@mention-bot
Copy link

@KaiHa, thanks for your PR! By analyzing the history of the files in this pull request, we identified @edolstra, @bjornfor and @offlinehacker to be potential reviewers.

cfg = config.services.vnstat;
in {
options.services.vnstat = {
enable = mkEnableOption "If enabled, vnstatd will be started to update the network usage statistics.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will render poorly. mkEnableOption already adds some boilerplate text to the description. Also, I think the consensus is to phase out usage of mkEnableOption.

config = mkIf cfg.enable {
systemd.services.vnstat = {
description = "vnStat network traffic monitor";
path = [ pkgs.coreutils ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only for the kill call below, path is unnecessary.

path = [ pkgs.coreutils ];
wantedBy = [ "multi-user.target" ];
serviceConfig.ExecStart = "${pkgs.vnstat}/bin/vnstatd -n";
serviceConfig.ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this runs as root, would you consider constraining the capabilities available to the main process and restrain access to the filesystem (e.g., protect home at least).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joachifm can you elaborate on this, how would you do this? Is there documentation or examples you can point me to? I have seen that vnstatd supports being run as a different user, maybe this simplifies things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

systemd supports lots of mechanisms for isolating services. For example, you can do

let # for syntax highlighting only
serviceConfig = {
  ProtectHome = true;
  PrivateDevices = true;
  PrivateTmp = true;
}

to have the service run with an empty /home, a minimal /dev and a private /tmp. See systemd.exec(5) and systemd.resource-control(5) for details. As long as the service is running with full privileges, these directives mainly serve to reduce the risk of accidental data loss/information leaking, so it'd be better if it also ran under an unprivileged account.

@joachifm
Copy link
Contributor

Please change the target branch to master

@KaiHa KaiHa changed the base branch from release-16.09 to master October 24, 2016 19:29
@KaiHa
Copy link
Contributor Author

KaiHa commented Oct 24, 2016

@joachifm OK, I have changed the base to master and did some cleanups. Furthermore is vnstatd now run as unprivileged user, this should prevent it from wreak havoc.

Copy link
Contributor

@joachifm joachifm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me

after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
unitConfig.documentation = "man:vnstatd(1) man:vnstat(1) man:vnstat.conf(5)";
preStart = "chmod 755 /var/lib/vnstat";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do serviceConfig.PermissionsStartOnly = true to run preStart as root, otherwise it might fail if ownership/permissions ever get out of whack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That chmod is there so others have read permissions on /var/lib/vnstat/
As long as vnstatd is the owner of /var/lib/vnstat I believe that chmod will succeed. If /var/lib/vnstat is owned by someone else, a chmod alone is not sufficient to fix this. Should I add also a chown into the preStart rule? For me that would feel a little weird.

Copy link
Contributor

@joachifm joachifm Oct 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of the case where vnstatd no longer owns the folder, yes. I don't think it's weird to ensure consistent ownership, why do you think so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By weird I mean, /var/lib/vnstat is the home directory of vnstatd. If it is not owned by vnstatd then someone must have willingly changed the owner of /var/lib/vnstat. And where to draw the line, should I also create /var/lib/vnstat if someone has deleted it? Do you get what I mean?

in {
options.services.vnstat = {
enable = mkOption {
default = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify type = types.bool.

enable = mkOption {
default = false;
description = ''
Wether to enable update of network usage statistics via vnstatd.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether

@joachifm joachifm merged commit 25d86bd into NixOS:master Jan 24, 2017
@KaiHa KaiHa deleted the start-vnstatd branch October 31, 2020 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants