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

nixos/stunnel: add module #33151

Merged
merged 2 commits into from Jan 21, 2018
Merged

nixos/stunnel: add module #33151

merged 2 commits into from Jan 21, 2018

Conversation

lschuermann
Copy link
Member

@lschuermann lschuermann commented Dec 28, 2017

Motivation for this change

stunnel is a TLS endpoint and useful in many situations, especially for services which don't have native TLS support. While it is already packaged for NixOS, there was no way to configure it.

This PR aims to provide basic configuration options for the most important settings. It is a work in progress and while already in use for server-mode configurations, it needs more testing especially with client-mode configurations.

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
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • 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.

This is my first NixOS module and I don't have that much experience yet. There are still some things I would like to accomplish:

  • At least one server- or client-config needs to be present, otherwise stunnel won't start. Can this be checked?
  • In the client-mode configuration I need to supply a path to a file containing root certificates. (ca-certificates package on most other systems) Where can I find this file on NixOS to make it the default value?
  • Currently, stunnel runs in foreground which works pretty well. There is an option to fork into background and create a PID file, but I couldn't get that to work. Is it bad to run the process in the foreground inside the systemd service?

Thanks.

@bjornfor
Copy link
Contributor

At least one server- or client-config needs to be present, otherwise stunnel won't start. Can this be checked?

Find inspiration via git grep assertions nixos (in the nixpkgs repo).

In the client-mode configuration I need to supply a path to a file containing root certificates. (ca-certificates package on most other systems) Where can I find this file on NixOS to make it the default value?

I don't know if there is a canonical setting for this path, but at least until something better comes along you can use "/etc/ssl/certs/ca-bundle.crt". This is where NixOS places all configured certificates.

Currently, stunnel runs in foreground which works pretty well. There is an option to fork into background and create a PID file, but I couldn't get that to work. Is it bad to run the process in the foreground inside the systemd service?

It matters if other services depend on this service, and you want to have correct dependency ordering. Some services fork after they are properly configured and ready to serve clients (at least that's the systemd model). systemd itself doesn't care. Look for "Type=" in "man systemd.service" to read more about the various service start-up types.

};

CAPath = mkOption {
type = types.string;
Copy link
Member

Choose a reason for hiding this comment

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

types.path


environment.etc."stunnel.cfg".text = ''
${ if cfg.user != null then "setuid = ${ cfg.user }" else "" }
${ if cfg.group != null then "setgid = ${ cfg.group }" else "" }
Copy link
Member

Choose a reason for hiding this comment

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

${optionalString (cfg.user != null) "setuid = ${ cfg.user }"}

@lschuermann
Copy link
Member Author

The module has now gotten a lot of improvements and bug fixes, also the client-configuration should work completely.

I have to admit that I didn't manage to get the stunnel client-mode to work with certificate validation, although I'm generating the correct configuration. The issue might have to do with SNI and I have to do some more experimenting to get it to work. I guess this is a problem with stunnel itself rather than NixOS, though it would be nice if someone could get it to work.

The systemd-service somehow manages to work correctly even with Type = "forking"; set (as expected), I must've made some stupid mistake before. However, running it with a PID file still fails.

debug = ${cfg.logLevel}

${ optionalString cfg.fipsMode "fips = yes" }
${ optionalString cfg.enableInsecureSSLv3 "options = -NO_SSLv3" }
Copy link
Member

Choose a reason for hiding this comment

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

Here it says NO_SSLv3, are you sure this would if enableInsecureSSLv3 is true, this would have the desired effect?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have to admit this is quite confusing. As you can see in the manual and in the example configuration, options prefixed by a dash get removed. This is because NO_SSLv3 is already the default.
I was very scared of making the mistake of leaving out the dash or negating the Boolean somehow. In my option, this is an extraordinarily bad design and I'm happy there exists a Nix language to abstract it. ;)

systemd.services.stunnel = {
description = "stunnel TLS tunneling service";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
Copy link
Member

@Mic92 Mic92 Jan 20, 2018

Choose a reason for hiding this comment

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

network.target should be enough. Then stunnel will be stopped before the network is on shutdown.

@Mic92 Mic92 changed the title Add basic stunnel module nixos/stunnel: add module Jan 21, 2018
@Mic92 Mic92 merged commit 04c4c01 into NixOS:master Jan 21, 2018
@Mic92
Copy link
Member

Mic92 commented Jan 21, 2018

Thanks!

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

5 participants