wire.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package localapi
  2. import (
  3. "context"
  4. "errors"
  5. "log/slog"
  6. "github.com/slackhq/nebula/commands"
  7. "github.com/slackhq/nebula/config"
  8. )
  9. // Wire brings up the local control socket for a running nebula and keeps it pointed at
  10. // the configured path across reloads.
  11. //
  12. // Every entry point that runs nebula as a daemon calls this, so the endpoint behaves the
  13. // same whether nebula was started from a shell or by a service manager. It deliberately
  14. // does not live in nebula.Main: the endpoint is on by default with no key to disable it,
  15. // so an embedder inside an app sandbox would have no way to decline a socket it cannot
  16. // use. Wiring it at the daemon entry points means embedders get the command registry and
  17. // no listener.
  18. //
  19. // Every failure below is a warning, bar one. Nebula is expected to keep forwarding
  20. // packets whether or not anyone can reach it; but a name that another process holds is
  21. // not a misconfiguration, it is a process that will be answering nebula ctl in the
  22. // daemon's place, and that is logged as an error.
  23. //
  24. // Call it after Control.Start, so the listener only exists once the interface is up.
  25. func Wire(ctx context.Context, l *slog.Logger, c *config.C, reg *commands.Registry) *Server {
  26. l = l.With("subsystem", "localapi")
  27. srv := NewServer(l, reg)
  28. // Registered before the first attempt, so that a path or bind that is bad at startup
  29. // can be fixed with a config edit and a reload, the same as one that goes bad later,
  30. // and so that a HUP landing in between is harmless: Rebind on a server already at
  31. // the path is a no-op.
  32. reconcile := followControlConfig(ctx, l, c, srv)
  33. reconcile(c)
  34. return srv
  35. }
  36. // followControlConfig registers the reload callback that keeps the socket where
  37. // local_api.control.socket says it is, moving it when the key changes and bringing one
  38. // up while there is none, and returns it for the first attempt to call.
  39. //
  40. // The key is reloadable because the client finds the socket by reading the config: if the
  41. // two could disagree, the file on disk would name a socket that is not there.
  42. //
  43. // Every reload is reconciled rather than gated on the key having changed. Rebind on a
  44. // server already at the path is a no-op, so that costs nothing; and a bind that failed
  45. // for a reason outside the config, a runtime directory that was not there yet, is fixed
  46. // outside the config too, so the reload that follows the fix is a plain one. That holds
  47. // for a move as much as for the first bind: a daemon left on its old path by a move that
  48. // could not bind is one the client, reading the new path from the same file, cannot
  49. // reach, and the operator's fix is the directory and a reload, not an edit to an
  50. // unrelated line to make the key read as changed.
  51. //
  52. // The callback recovers from its own panics rather than relying on the config package,
  53. // whose callback loop has no isolation of its own. A panic there would take the daemon
  54. // down and skip every callback queued behind this one.
  55. func followControlConfig(ctx context.Context, l *slog.Logger, c *config.C, srv *Server) func(*config.C) {
  56. reconcile := func(c *config.C) {
  57. want, err := SocketPathFromConfig(c)
  58. if err != nil {
  59. if srv.Path() == "" {
  60. l.Warn("Could not determine the local control socket path, control commands will not be available", "error", err)
  61. } else {
  62. l.Warn("Ignoring unusable local control socket path from reloaded config", "error", err)
  63. }
  64. return
  65. }
  66. acc, err := controlAccessFrom(c)
  67. if err != nil {
  68. if srv.Path() == "" {
  69. l.Warn("Local control socket is not available, its group is unusable", "error", err)
  70. } else {
  71. l.Warn("Ignoring unusable local control socket group from reloaded config, keeping the current socket", "error", err)
  72. }
  73. return
  74. }
  75. old := srv.Path()
  76. err = srv.Rebind(ctx, want, acc)
  77. reportRebind(l, old, want, err, srv.Path())
  78. }
  79. c.RegisterReloadCallback(func(c *config.C, _ *config.Reload) {
  80. defer func() {
  81. if r := recover(); r != nil {
  82. l.Error("Recovered from panic reloading the local control socket", "panic", r)
  83. }
  84. }()
  85. reconcile(c)
  86. })
  87. return reconcile
  88. }
  89. // reportRebind logs what a Rebind did beyond the listening line the bind itself writes:
  90. // nothing for a server left where it was, a move with both paths, and a failure with
  91. // what the operator is left with. old is the path served before the call, "" for none,
  92. // want the one asked for, and now the path served after it: on unix a failed move keeps
  93. // the old socket, and now is old; on windows a change of access closes the pipe before
  94. // creating it again, and a failure there leaves nothing serving, so now is "".
  95. func reportRebind(l *slog.Logger, old, want string, err error, now string) {
  96. switch {
  97. case err == nil:
  98. if old != "" && old != want {
  99. l.Info("Moved the local control socket", "from", old, "to", want)
  100. }
  101. case old == "":
  102. bindFailure(l, err)("Could not start the local control socket, control commands will not be available until a reload binds it", "error", err, "path", want)
  103. default:
  104. bindFailure(l, err)("Could not bind the local control socket at the requested path",
  105. "error", err, "requested", want, "serving", now,
  106. )
  107. }
  108. }
  109. // errEndpointHeld is returned by listen when the endpoint's name is already held by
  110. // some other process, which nebula will not serve alongside: whoever holds it would be
  111. // answering nebula ctl. It is only raised on windows, where the default name can be
  112. // held by another administrator's process and an override outside the protected
  113. // namespace by any account's; on unix a path that is still being served is an earlier
  114. // nebula, which clearStale reports as in use.
  115. var errEndpointHeld = errors.New("the local control socket's name is held by another process")
  116. // bindFailure picks the level a failed bind is logged at. Nearly every failure is the
  117. // operator's environment, a missing runtime directory or a bad path, and a warning; a
  118. // name held by another process is logged as an error, since nothing else will say that
  119. // something is sitting where the daemon should be.
  120. func bindFailure(l *slog.Logger, err error) func(msg string, args ...any) {
  121. if errors.Is(err, errEndpointHeld) {
  122. return l.Error
  123. }
  124. return l.Warn
  125. }