path.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package localapi
  2. // DirName is the directory under /var/run that holds nebula's sockets by default.
  3. const DirName = "nebula"
  4. // CtlEnabledKey is the optional config key that turns the control socket off. It is on
  5. // unless the config says false, and reloadable: a reload that turns it off removes the
  6. // socket, and one that turns it back on brings it up again.
  7. const CtlEnabledKey = "local_api.control.enabled"
  8. // ControlEnabled reports whether a config leaves the control socket on. nebula ctl reads
  9. // it too, so a client pointed at a config that turned the socket off says so rather than
  10. // failing to dial.
  11. func ControlEnabled(c interface{ GetBool(string, bool) bool }) bool {
  12. return c.GetBool(CtlEnabledKey, true)
  13. }
  14. // CtlSocketKey is the optional config key naming the control socket. Unset, the socket
  15. // is DefaultSocketPath, one fixed path that suits a daemon running as root, which is
  16. // what the tun device needs anyway. A daemon run as another user, or a second daemon on
  17. // one host, names its own. The CLI reads the same key, so when both sides see the same
  18. // config file they agree on the socket with nothing more said.
  19. const CtlSocketKey = "local_api.control.socket"
  20. // CtlGroupKey is the optional config key naming a group that shares the control socket.
  21. // Unset, the socket admits the account nebula runs as and nobody else. Set, the socket
  22. // is handed to the group, 0660 on unix and an entry in the pipe's security descriptor on
  23. // windows, and the kernel's check on connect is the access control, as dockerd's -G
  24. // works; the daemon's own peer check stands aside, since on unix it could not tell a
  25. // member of the group from anyone else.
  26. const CtlGroupKey = "local_api.control.group"
  27. // controlAccessFrom returns the control socket's access a config implies: the group
  28. // CtlGroupKey names, if any, and the mode that goes with having one or not.
  29. func controlAccessFrom(c interface{ GetString(string, string) string }) (Access, error) {
  30. group, err := groupFromConfig(c, CtlGroupKey)
  31. if err != nil {
  32. return Access{}, err
  33. }
  34. if group == nil {
  35. return ownerOnly(), nil
  36. }
  37. return Access{Mode: groupSocketMode, Group: group}, nil
  38. }
  39. // DefaultSocketPath is the control socket when the config names none: one fixed path per
  40. // platform, see defaultSocketPath. Both the daemon and nebula ctl use it, which is what
  41. // lets the endpoint work with no configuration on either side.
  42. func DefaultSocketPath() string { return defaultSocketPath }
  43. // SocketPathFromConfig returns the control socket path a config implies: CtlSocketKey
  44. // when it is set, else DefaultSocketPath.
  45. func SocketPathFromConfig(c interface{ GetString(string, string) string }) (string, error) {
  46. if override := c.GetString(CtlSocketKey, ""); override != "" {
  47. return configuredSocketPath(CtlSocketKey, override)
  48. }
  49. return DefaultSocketPath(), nil
  50. }
  51. // configuredSocketPath checks the value of a socket path key and returns it as given. It
  52. // must be absolute, so that the daemon and nebula ctl, started from wherever the
  53. // operator happens to be, name the same socket from one config, and it must be a path
  54. // this platform can bind. key names the setting in the error.
  55. func configuredSocketPath(key, value string) (string, error) {
  56. if err := checkSocketPath(key, value); err != nil {
  57. return "", err
  58. }
  59. return value, nil
  60. }