|
@@ -3,6 +3,9 @@ package nebula
|
|
|
import (
|
|
|
"encoding/binary"
|
|
|
"fmt"
|
|
|
+ "github.com/sirupsen/logrus"
|
|
|
+ "github.com/slackhq/nebula/sshd"
|
|
|
+ "gopkg.in/yaml.v2"
|
|
|
"net"
|
|
|
"os"
|
|
|
"os/signal"
|
|
@@ -10,42 +13,38 @@ import (
|
|
|
"strings"
|
|
|
"syscall"
|
|
|
"time"
|
|
|
-
|
|
|
- "github.com/sirupsen/logrus"
|
|
|
- "github.com/slackhq/nebula/sshd"
|
|
|
- "gopkg.in/yaml.v2"
|
|
|
)
|
|
|
|
|
|
+// The caller should provide a real logger, we have one just in case
|
|
|
var l = logrus.New()
|
|
|
|
|
|
type m map[string]interface{}
|
|
|
|
|
|
-func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
- l.Out = os.Stdout
|
|
|
+type CommandRequest struct {
|
|
|
+ Command string
|
|
|
+ Callback chan error
|
|
|
+}
|
|
|
+
|
|
|
+func Main(config *Config, configTest bool, block bool, buildVersion string, logger *logrus.Logger, tunFd *int, commandChan <-chan CommandRequest) error {
|
|
|
+ l = logger
|
|
|
l.Formatter = &logrus.TextFormatter{
|
|
|
FullTimestamp: true,
|
|
|
}
|
|
|
|
|
|
- config := NewConfig()
|
|
|
- err := config.Load(configPath)
|
|
|
- if err != nil {
|
|
|
- l.WithError(err).Error("Failed to load config")
|
|
|
- os.Exit(1)
|
|
|
- }
|
|
|
-
|
|
|
// Print the config if in test, the exit comes later
|
|
|
if configTest {
|
|
|
b, err := yaml.Marshal(config.Settings)
|
|
|
if err != nil {
|
|
|
- l.Println(err)
|
|
|
- os.Exit(1)
|
|
|
+ return err
|
|
|
}
|
|
|
+
|
|
|
+ // Print the final config
|
|
|
l.Println(string(b))
|
|
|
}
|
|
|
|
|
|
- err = configLogger(config)
|
|
|
+ err := configLogger(config)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Error("Failed to configure the logger")
|
|
|
+ return NewContextualError("Failed to configure the logger", nil, err)
|
|
|
}
|
|
|
|
|
|
config.RegisterReloadCallback(func(c *Config) {
|
|
@@ -59,20 +58,20 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
trustedCAs, err = loadCAFromConfig(config)
|
|
|
if err != nil {
|
|
|
//The errors coming out of loadCA are already nicely formatted
|
|
|
- l.WithError(err).Fatal("Failed to load ca from config")
|
|
|
+ return NewContextualError("Failed to load ca from config", nil, err)
|
|
|
}
|
|
|
l.WithField("fingerprints", trustedCAs.GetFingerprints()).Debug("Trusted CA fingerprints")
|
|
|
|
|
|
cs, err := NewCertStateFromConfig(config)
|
|
|
if err != nil {
|
|
|
//The errors coming out of NewCertStateFromConfig are already nicely formatted
|
|
|
- l.WithError(err).Fatal("Failed to load certificate from config")
|
|
|
+ return NewContextualError("Failed to load certificate from config", nil, err)
|
|
|
}
|
|
|
l.WithField("cert", cs.certificate).Debug("Client nebula certificate")
|
|
|
|
|
|
fw, err := NewFirewallFromConfig(cs.certificate, config)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Error while loading firewall rules")
|
|
|
+ return NewContextualError("Error while loading firewall rules", nil, err)
|
|
|
}
|
|
|
l.WithField("firewallHash", fw.GetRuleHash()).Info("Firewall started")
|
|
|
|
|
@@ -80,11 +79,11 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
tunCidr := cs.certificate.Details.Ips[0]
|
|
|
routes, err := parseRoutes(config, tunCidr)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Could not parse tun.routes")
|
|
|
+ return NewContextualError("Could not parse tun.routes", nil, err)
|
|
|
}
|
|
|
unsafeRoutes, err := parseUnsafeRoutes(config, tunCidr)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Could not parse tun.unsafe_routes")
|
|
|
+ return NewContextualError("Could not parse tun.unsafe_routes", nil, err)
|
|
|
}
|
|
|
|
|
|
ssh, err := sshd.NewSSHServer(l.WithField("subsystem", "sshd"))
|
|
@@ -92,7 +91,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
if config.GetBool("sshd.enabled", false) {
|
|
|
err = configSSH(ssh, config)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Error while configuring the sshd")
|
|
|
+ return NewContextualError("Error while configuring the sshd", nil, err)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -105,17 +104,28 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
if !configTest {
|
|
|
config.CatchHUP()
|
|
|
|
|
|
- // set up our tun dev
|
|
|
- tun, err = newTun(
|
|
|
- config.GetString("tun.dev", ""),
|
|
|
- tunCidr,
|
|
|
- config.GetInt("tun.mtu", DEFAULT_MTU),
|
|
|
- routes,
|
|
|
- unsafeRoutes,
|
|
|
- config.GetInt("tun.tx_queue", 500),
|
|
|
- )
|
|
|
+ if tunFd != nil {
|
|
|
+ tun, err = newTunFromFd(
|
|
|
+ *tunFd,
|
|
|
+ tunCidr,
|
|
|
+ config.GetInt("tun.mtu", DEFAULT_MTU),
|
|
|
+ routes,
|
|
|
+ unsafeRoutes,
|
|
|
+ config.GetInt("tun.tx_queue", 500),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ tun, err = newTun(
|
|
|
+ config.GetString("tun.dev", ""),
|
|
|
+ tunCidr,
|
|
|
+ config.GetInt("tun.mtu", DEFAULT_MTU),
|
|
|
+ routes,
|
|
|
+ unsafeRoutes,
|
|
|
+ config.GetInt("tun.tx_queue", 500),
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to get a tun/tap device")
|
|
|
+ return NewContextualError("Failed to get a tun/tap device", nil, err)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -126,11 +136,28 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
if !configTest {
|
|
|
udpServer, err = NewListener(config.GetString("listen.host", "0.0.0.0"), config.GetInt("listen.port", 0), udpQueues > 1)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to open udp listener")
|
|
|
+ return NewContextualError("Failed to open udp listener", nil, err)
|
|
|
}
|
|
|
udpServer.reloadConfig(config)
|
|
|
}
|
|
|
|
|
|
+ sigChan := make(chan os.Signal)
|
|
|
+ killChan := make(chan CommandRequest)
|
|
|
+ if commandChan != nil {
|
|
|
+ go func() {
|
|
|
+ cmd := CommandRequest{}
|
|
|
+ for {
|
|
|
+ cmd = <-commandChan
|
|
|
+ switch cmd.Command {
|
|
|
+ case "rebind":
|
|
|
+ udpServer.Rebind()
|
|
|
+ case "exit":
|
|
|
+ killChan <- cmd
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ }
|
|
|
+
|
|
|
// Set up my internal host map
|
|
|
var preferredRanges []*net.IPNet
|
|
|
rawPreferredRanges := config.GetStringSlice("preferred_ranges", []string{})
|
|
@@ -139,7 +166,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
for _, rawPreferredRange := range rawPreferredRanges {
|
|
|
_, preferredRange, err := net.ParseCIDR(rawPreferredRange)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to parse preferred ranges")
|
|
|
+ return NewContextualError("Failed to parse preferred ranges", nil, err)
|
|
|
}
|
|
|
preferredRanges = append(preferredRanges, preferredRange)
|
|
|
}
|
|
@@ -152,7 +179,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
if rawLocalRange != "" {
|
|
|
_, localRange, err := net.ParseCIDR(rawLocalRange)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to parse local range")
|
|
|
+ return NewContextualError("Failed to parse local_range", nil, err)
|
|
|
}
|
|
|
|
|
|
// Check if the entry for local_range was already specified in
|
|
@@ -192,7 +219,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
if port == 0 && !configTest {
|
|
|
uPort, err := udpServer.LocalAddr()
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to get listening port")
|
|
|
+ return NewContextualError("Failed to get listening port", nil, err)
|
|
|
}
|
|
|
port = int(uPort.Port)
|
|
|
}
|
|
@@ -209,10 +236,10 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
for i, host := range rawLighthouseHosts {
|
|
|
ip := net.ParseIP(host)
|
|
|
if ip == nil {
|
|
|
- l.WithField("host", host).Fatalf("Unable to parse lighthouse host entry %v", i+1)
|
|
|
+ return NewContextualError("Unable to parse lighthouse host entry", m{"host": host, "entry": i + 1}, nil)
|
|
|
}
|
|
|
if !tunCidr.Contains(ip) {
|
|
|
- l.WithField("vpnIp", ip).WithField("network", tunCidr.String()).Fatalf("lighthouse host is not in our subnet, invalid")
|
|
|
+ return NewContextualError("lighthouse host is not in our subnet, invalid", m{"vpnIp": ip, "network": tunCidr.String()}, nil)
|
|
|
}
|
|
|
lighthouseHosts[i] = ip2int(ip)
|
|
|
}
|
|
@@ -232,13 +259,13 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
|
|
|
remoteAllowList, err := config.GetAllowList("lighthouse.remote_allow_list", false)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Invalid lighthouse.remote_allow_list")
|
|
|
+ return NewContextualError("Invalid lighthouse.remote_allow_list", nil, err)
|
|
|
}
|
|
|
lightHouse.SetRemoteAllowList(remoteAllowList)
|
|
|
|
|
|
localAllowList, err := config.GetAllowList("lighthouse.local_allow_list", true)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Invalid lighthouse.local_allow_list")
|
|
|
+ return NewContextualError("Invalid lighthouse.local_allow_list", nil, err)
|
|
|
}
|
|
|
lightHouse.SetLocalAllowList(localAllowList)
|
|
|
|
|
@@ -246,7 +273,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
for k, v := range config.GetMap("static_host_map", map[interface{}]interface{}{}) {
|
|
|
vpnIp := net.ParseIP(fmt.Sprintf("%v", k))
|
|
|
if !tunCidr.Contains(vpnIp) {
|
|
|
- l.WithField("vpnIp", vpnIp).WithField("network", tunCidr.String()).Fatalf("static_host_map key is not in our subnet, invalid")
|
|
|
+ return NewContextualError("static_host_map key is not in our subnet, invalid", m{"vpnIp": vpnIp, "network": tunCidr.String()}, nil)
|
|
|
}
|
|
|
vals, ok := v.([]interface{})
|
|
|
if ok {
|
|
@@ -257,7 +284,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
ip := addr.IP
|
|
|
port, err := strconv.Atoi(parts[1])
|
|
|
if err != nil {
|
|
|
- l.Fatalf("Static host address for %s could not be parsed: %s", vpnIp, v)
|
|
|
+ return NewContextualError("Static host address could not be parsed", m{"vpnIp": vpnIp}, err)
|
|
|
}
|
|
|
lightHouse.AddRemote(ip2int(vpnIp), NewUDPAddr(ip2int(ip), uint16(port)), true)
|
|
|
}
|
|
@@ -270,7 +297,7 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
ip := addr.IP
|
|
|
port, err := strconv.Atoi(parts[1])
|
|
|
if err != nil {
|
|
|
- l.Fatalf("Static host address for %s could not be parsed: %s", vpnIp, v)
|
|
|
+ return NewContextualError("Static host address could not be parsed", m{"vpnIp": vpnIp}, err)
|
|
|
}
|
|
|
lightHouse.AddRemote(ip2int(vpnIp), NewUDPAddr(ip2int(ip), uint16(port)), true)
|
|
|
}
|
|
@@ -330,14 +357,14 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
case "chachapoly":
|
|
|
noiseEndianness = binary.LittleEndian
|
|
|
default:
|
|
|
- l.Fatalf("Unknown cipher: %v", ifConfig.Cipher)
|
|
|
+ return fmt.Errorf("unknown cipher: %v", ifConfig.Cipher)
|
|
|
}
|
|
|
|
|
|
var ifce *Interface
|
|
|
if !configTest {
|
|
|
ifce, err = NewInterface(ifConfig)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to initialize interface")
|
|
|
+ return fmt.Errorf("failed to initialize interface: %s", err)
|
|
|
}
|
|
|
|
|
|
ifce.RegisterConfigChangeCallbacks(config)
|
|
@@ -348,11 +375,11 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
|
|
|
err = startStats(config, configTest)
|
|
|
if err != nil {
|
|
|
- l.WithError(err).Fatal("Failed to start stats emitter")
|
|
|
+ return NewContextualError("Failed to start stats emitter", nil, err)
|
|
|
}
|
|
|
|
|
|
if configTest {
|
|
|
- os.Exit(0)
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
//TODO: check if we _should_ be emitting stats
|
|
@@ -367,19 +394,33 @@ func Main(configPath string, configTest bool, buildVersion string) {
|
|
|
go dnsMain(hostMap, config)
|
|
|
}
|
|
|
|
|
|
- // Just sit here and be friendly, main thread.
|
|
|
- shutdownBlock(ifce)
|
|
|
+ if block {
|
|
|
+ // Just sit here and be friendly, main thread.
|
|
|
+ shutdownBlock(ifce, sigChan, killChan)
|
|
|
+ } else {
|
|
|
+ // Even though we aren't blocking we still want to shutdown gracefully
|
|
|
+ go shutdownBlock(ifce, sigChan, killChan)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
-func shutdownBlock(ifce *Interface) {
|
|
|
- var sigChan = make(chan os.Signal)
|
|
|
+func shutdownBlock(ifce *Interface, sigChan chan os.Signal, killChan chan CommandRequest) {
|
|
|
+ var cmd CommandRequest
|
|
|
+ var sig string
|
|
|
+
|
|
|
signal.Notify(sigChan, syscall.SIGTERM)
|
|
|
signal.Notify(sigChan, syscall.SIGINT)
|
|
|
|
|
|
- sig := <-sigChan
|
|
|
+ select {
|
|
|
+ case rawSig := <-sigChan:
|
|
|
+ sig = rawSig.String()
|
|
|
+ case cmd = <-killChan:
|
|
|
+ sig = "controlling app"
|
|
|
+ }
|
|
|
+
|
|
|
l.WithField("signal", sig).Info("Caught signal, shutting down")
|
|
|
|
|
|
- //TODO: stop tun and udp routines, the lock on hostMap does effectively does that though
|
|
|
+ //TODO: stop tun and udp routines, the lock on hostMap effectively does that though
|
|
|
//TODO: this is probably better as a function in ConnectionManager or HostMap directly
|
|
|
ifce.hostMap.Lock()
|
|
|
for _, h := range ifce.hostMap.Hosts {
|
|
@@ -392,5 +433,8 @@ func shutdownBlock(ifce *Interface) {
|
|
|
ifce.hostMap.Unlock()
|
|
|
|
|
|
l.WithField("signal", sig).Info("Goodbye")
|
|
|
- os.Exit(0)
|
|
|
+ select {
|
|
|
+ case cmd.Callback <- nil:
|
|
|
+ default:
|
|
|
+ }
|
|
|
}
|