daemon.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. package main
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "fmt"
  6. "net"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "time"
  12. containerddefaults "github.com/containerd/containerd/defaults"
  13. "github.com/docker/docker/api"
  14. apiserver "github.com/docker/docker/api/server"
  15. buildbackend "github.com/docker/docker/api/server/backend/build"
  16. "github.com/docker/docker/api/server/middleware"
  17. "github.com/docker/docker/api/server/router"
  18. "github.com/docker/docker/api/server/router/build"
  19. checkpointrouter "github.com/docker/docker/api/server/router/checkpoint"
  20. "github.com/docker/docker/api/server/router/container"
  21. distributionrouter "github.com/docker/docker/api/server/router/distribution"
  22. grpcrouter "github.com/docker/docker/api/server/router/grpc"
  23. "github.com/docker/docker/api/server/router/image"
  24. "github.com/docker/docker/api/server/router/network"
  25. pluginrouter "github.com/docker/docker/api/server/router/plugin"
  26. sessionrouter "github.com/docker/docker/api/server/router/session"
  27. swarmrouter "github.com/docker/docker/api/server/router/swarm"
  28. systemrouter "github.com/docker/docker/api/server/router/system"
  29. "github.com/docker/docker/api/server/router/volume"
  30. buildkit "github.com/docker/docker/builder/builder-next"
  31. "github.com/docker/docker/builder/dockerfile"
  32. "github.com/docker/docker/cli/debug"
  33. "github.com/docker/docker/daemon"
  34. "github.com/docker/docker/daemon/cluster"
  35. "github.com/docker/docker/daemon/config"
  36. "github.com/docker/docker/daemon/listeners"
  37. "github.com/docker/docker/dockerversion"
  38. "github.com/docker/docker/libcontainerd/supervisor"
  39. dopts "github.com/docker/docker/opts"
  40. "github.com/docker/docker/pkg/authorization"
  41. "github.com/docker/docker/pkg/homedir"
  42. "github.com/docker/docker/pkg/jsonmessage"
  43. "github.com/docker/docker/pkg/pidfile"
  44. "github.com/docker/docker/pkg/plugingetter"
  45. "github.com/docker/docker/pkg/signal"
  46. "github.com/docker/docker/pkg/sysinfo"
  47. "github.com/docker/docker/pkg/system"
  48. "github.com/docker/docker/plugin"
  49. "github.com/docker/docker/rootless"
  50. "github.com/docker/docker/runconfig"
  51. "github.com/docker/go-connections/tlsconfig"
  52. swarmapi "github.com/docker/swarmkit/api"
  53. "github.com/moby/buildkit/session"
  54. "github.com/pkg/errors"
  55. "github.com/sirupsen/logrus"
  56. "github.com/spf13/pflag"
  57. )
  58. // DaemonCli represents the daemon CLI.
  59. type DaemonCli struct {
  60. *config.Config
  61. configFile *string
  62. flags *pflag.FlagSet
  63. api *apiserver.Server
  64. d *daemon.Daemon
  65. authzMiddleware *authorization.Middleware // authzMiddleware enables to dynamically reload the authorization plugins
  66. }
  67. // NewDaemonCli returns a daemon CLI
  68. func NewDaemonCli() *DaemonCli {
  69. return &DaemonCli{}
  70. }
  71. func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
  72. stopc := make(chan bool)
  73. defer close(stopc)
  74. opts.SetDefaultOptions(opts.flags)
  75. if cli.Config, err = loadDaemonCliConfig(opts); err != nil {
  76. return err
  77. }
  78. warnOnDeprecatedConfigOptions(cli.Config)
  79. if err := configureDaemonLogs(cli.Config); err != nil {
  80. return err
  81. }
  82. logrus.Info("Starting up")
  83. cli.configFile = &opts.configFile
  84. cli.flags = opts.flags
  85. if cli.Config.Debug {
  86. debug.Enable()
  87. }
  88. if cli.Config.Experimental {
  89. logrus.Warn("Running experimental build")
  90. }
  91. if cli.Config.IsRootless() {
  92. logrus.Warn("Running in rootless mode. This mode has feature limitations.")
  93. }
  94. if rootless.RunningWithRootlessKit() {
  95. logrus.Info("Running with RootlessKit integration")
  96. if !cli.Config.IsRootless() {
  97. return fmt.Errorf("rootless mode needs to be enabled for running with RootlessKit")
  98. }
  99. }
  100. // return human-friendly error before creating files
  101. if runtime.GOOS == "linux" && os.Geteuid() != 0 {
  102. return fmt.Errorf("dockerd needs to be started with root. To see how to run dockerd in rootless mode with unprivileged user, see the documentation")
  103. }
  104. system.InitLCOW(cli.Config.Experimental)
  105. if err := setDefaultUmask(); err != nil {
  106. return err
  107. }
  108. // Create the daemon root before we create ANY other files (PID, or migrate keys)
  109. // to ensure the appropriate ACL is set (particularly relevant on Windows)
  110. if err := daemon.CreateDaemonRoot(cli.Config); err != nil {
  111. return err
  112. }
  113. if err := system.MkdirAll(cli.Config.ExecRoot, 0700); err != nil {
  114. return err
  115. }
  116. potentiallyUnderRuntimeDir := []string{cli.Config.ExecRoot}
  117. if cli.Pidfile != "" {
  118. pf, err := pidfile.New(cli.Pidfile)
  119. if err != nil {
  120. return errors.Wrap(err, "failed to start daemon")
  121. }
  122. potentiallyUnderRuntimeDir = append(potentiallyUnderRuntimeDir, cli.Pidfile)
  123. defer func() {
  124. if err := pf.Remove(); err != nil {
  125. logrus.Error(err)
  126. }
  127. }()
  128. }
  129. if cli.Config.IsRootless() {
  130. // Set sticky bit if XDG_RUNTIME_DIR is set && the file is actually under XDG_RUNTIME_DIR
  131. if _, err := homedir.StickRuntimeDirContents(potentiallyUnderRuntimeDir); err != nil {
  132. // StickRuntimeDirContents returns nil error if XDG_RUNTIME_DIR is just unset
  133. logrus.WithError(err).Warn("cannot set sticky bit on files under XDG_RUNTIME_DIR")
  134. }
  135. }
  136. serverConfig, err := newAPIServerConfig(cli)
  137. if err != nil {
  138. return errors.Wrap(err, "failed to create API server")
  139. }
  140. cli.api = apiserver.New(serverConfig)
  141. hosts, err := loadListeners(cli, serverConfig)
  142. if err != nil {
  143. return errors.Wrap(err, "failed to load listeners")
  144. }
  145. ctx, cancel := context.WithCancel(context.Background())
  146. waitForContainerDShutdown, err := cli.initContainerD(ctx)
  147. if waitForContainerDShutdown != nil {
  148. defer waitForContainerDShutdown(10 * time.Second)
  149. }
  150. if err != nil {
  151. cancel()
  152. return err
  153. }
  154. defer cancel()
  155. signal.Trap(func() {
  156. cli.stop()
  157. <-stopc // wait for daemonCli.start() to return
  158. }, logrus.StandardLogger())
  159. // Notify that the API is active, but before daemon is set up.
  160. preNotifyReady()
  161. pluginStore := plugin.NewStore()
  162. if err := cli.initMiddlewares(cli.api, serverConfig, pluginStore); err != nil {
  163. logrus.Fatalf("Error creating middlewares: %v", err)
  164. }
  165. d, err := daemon.NewDaemon(ctx, cli.Config, pluginStore)
  166. if err != nil {
  167. return errors.Wrap(err, "failed to start daemon")
  168. }
  169. d.StoreHosts(hosts)
  170. // validate after NewDaemon has restored enabled plugins. Don't change order.
  171. if err := validateAuthzPlugins(cli.Config.AuthorizationPlugins, pluginStore); err != nil {
  172. return errors.Wrap(err, "failed to validate authorization plugin")
  173. }
  174. cli.d = d
  175. if err := startMetricsServer(cli.Config.MetricsAddress); err != nil {
  176. return errors.Wrap(err, "failed to start metrics server")
  177. }
  178. c, err := createAndStartCluster(cli, d)
  179. if err != nil {
  180. logrus.Fatalf("Error starting cluster component: %v", err)
  181. }
  182. // Restart all autostart containers which has a swarm endpoint
  183. // and is not yet running now that we have successfully
  184. // initialized the cluster.
  185. d.RestartSwarmContainers()
  186. logrus.Info("Daemon has completed initialization")
  187. routerOptions, err := newRouterOptions(cli.Config, d)
  188. if err != nil {
  189. return err
  190. }
  191. routerOptions.api = cli.api
  192. routerOptions.cluster = c
  193. initRouter(routerOptions)
  194. go d.ProcessClusterNotifications(ctx, c.GetWatchStream())
  195. cli.setupConfigReloadTrap()
  196. // The serve API routine never exits unless an error occurs
  197. // We need to start it as a goroutine and wait on it so
  198. // daemon doesn't exit
  199. serveAPIWait := make(chan error)
  200. go cli.api.Wait(serveAPIWait)
  201. // after the daemon is done setting up we can notify systemd api
  202. notifyReady()
  203. // Daemon is fully initialized and handling API traffic
  204. // Wait for serve API to complete
  205. errAPI := <-serveAPIWait
  206. c.Cleanup()
  207. // notify systemd that we're shutting down
  208. notifyStopping()
  209. shutdownDaemon(d)
  210. // Stop notification processing and any background processes
  211. cancel()
  212. if errAPI != nil {
  213. return errors.Wrap(errAPI, "shutting down due to ServeAPI error")
  214. }
  215. logrus.Info("Daemon shutdown complete")
  216. return nil
  217. }
  218. type routerOptions struct {
  219. sessionManager *session.Manager
  220. buildBackend *buildbackend.Backend
  221. features *map[string]bool
  222. buildkit *buildkit.Builder
  223. daemon *daemon.Daemon
  224. api *apiserver.Server
  225. cluster *cluster.Cluster
  226. }
  227. func newRouterOptions(config *config.Config, d *daemon.Daemon) (routerOptions, error) {
  228. opts := routerOptions{}
  229. sm, err := session.NewManager()
  230. if err != nil {
  231. return opts, errors.Wrap(err, "failed to create sessionmanager")
  232. }
  233. manager, err := dockerfile.NewBuildManager(d.BuilderBackend(), d.IdentityMapping())
  234. if err != nil {
  235. return opts, err
  236. }
  237. cgroupParent := newCgroupParent(config)
  238. bk, err := buildkit.New(buildkit.Opt{
  239. SessionManager: sm,
  240. Root: filepath.Join(config.Root, "buildkit"),
  241. Dist: d.DistributionServices(),
  242. NetworkController: d.NetworkController(),
  243. DefaultCgroupParent: cgroupParent,
  244. RegistryHosts: d.RegistryHosts(),
  245. BuilderConfig: config.Builder,
  246. Rootless: d.Rootless(),
  247. IdentityMapping: d.IdentityMapping(),
  248. DNSConfig: config.DNSConfig,
  249. })
  250. if err != nil {
  251. return opts, err
  252. }
  253. bb, err := buildbackend.NewBackend(d.ImageService(), manager, bk, d.EventsService)
  254. if err != nil {
  255. return opts, errors.Wrap(err, "failed to create buildmanager")
  256. }
  257. return routerOptions{
  258. sessionManager: sm,
  259. buildBackend: bb,
  260. buildkit: bk,
  261. features: d.Features(),
  262. daemon: d,
  263. }, nil
  264. }
  265. func (cli *DaemonCli) reloadConfig() {
  266. reload := func(c *config.Config) {
  267. // Revalidate and reload the authorization plugins
  268. if err := validateAuthzPlugins(c.AuthorizationPlugins, cli.d.PluginStore); err != nil {
  269. logrus.Fatalf("Error validating authorization plugin: %v", err)
  270. return
  271. }
  272. cli.authzMiddleware.SetPlugins(c.AuthorizationPlugins)
  273. if err := cli.d.Reload(c); err != nil {
  274. logrus.Errorf("Error reconfiguring the daemon: %v", err)
  275. return
  276. }
  277. if c.IsValueSet("debug") {
  278. debugEnabled := debug.IsEnabled()
  279. switch {
  280. case debugEnabled && !c.Debug: // disable debug
  281. debug.Disable()
  282. case c.Debug && !debugEnabled: // enable debug
  283. debug.Enable()
  284. }
  285. }
  286. }
  287. if err := config.Reload(*cli.configFile, cli.flags, reload); err != nil {
  288. logrus.Error(err)
  289. }
  290. }
  291. func (cli *DaemonCli) stop() {
  292. cli.api.Close()
  293. }
  294. // shutdownDaemon just wraps daemon.Shutdown() to handle a timeout in case
  295. // d.Shutdown() is waiting too long to kill container or worst it's
  296. // blocked there
  297. func shutdownDaemon(d *daemon.Daemon) {
  298. shutdownTimeout := d.ShutdownTimeout()
  299. ch := make(chan struct{})
  300. go func() {
  301. d.Shutdown()
  302. close(ch)
  303. }()
  304. if shutdownTimeout < 0 {
  305. <-ch
  306. logrus.Debug("Clean shutdown succeeded")
  307. return
  308. }
  309. timeout := time.NewTimer(time.Duration(shutdownTimeout) * time.Second)
  310. defer timeout.Stop()
  311. select {
  312. case <-ch:
  313. logrus.Debug("Clean shutdown succeeded")
  314. case <-timeout.C:
  315. logrus.Error("Force shutdown daemon")
  316. }
  317. }
  318. func loadDaemonCliConfig(opts *daemonOptions) (*config.Config, error) {
  319. conf := opts.daemonConfig
  320. flags := opts.flags
  321. conf.Debug = opts.Debug
  322. conf.Hosts = opts.Hosts
  323. conf.LogLevel = opts.LogLevel
  324. if opts.flags.Changed(FlagTLS) {
  325. conf.TLS = &opts.TLS
  326. }
  327. if opts.flags.Changed(FlagTLSVerify) {
  328. conf.TLSVerify = &opts.TLSVerify
  329. v := true
  330. conf.TLS = &v
  331. }
  332. conf.CommonTLSOptions = config.CommonTLSOptions{}
  333. if opts.TLSOptions != nil {
  334. conf.CommonTLSOptions.CAFile = opts.TLSOptions.CAFile
  335. conf.CommonTLSOptions.CertFile = opts.TLSOptions.CertFile
  336. conf.CommonTLSOptions.KeyFile = opts.TLSOptions.KeyFile
  337. }
  338. if conf.TrustKeyPath == "" {
  339. daemonConfDir, err := getDaemonConfDir(conf.Root)
  340. if err != nil {
  341. return nil, err
  342. }
  343. conf.TrustKeyPath = filepath.Join(daemonConfDir, defaultTrustKeyFile)
  344. }
  345. if flags.Changed("graph") && flags.Changed("data-root") {
  346. return nil, errors.New(`cannot specify both "--graph" and "--data-root" option`)
  347. }
  348. if opts.configFile != "" {
  349. c, err := config.MergeDaemonConfigurations(conf, flags, opts.configFile)
  350. if err != nil {
  351. if flags.Changed("config-file") || !os.IsNotExist(err) {
  352. return nil, errors.Wrapf(err, "unable to configure the Docker daemon with file %s", opts.configFile)
  353. }
  354. }
  355. // the merged configuration can be nil if the config file didn't exist.
  356. // leave the current configuration as it is if when that happens.
  357. if c != nil {
  358. conf = c
  359. }
  360. }
  361. if err := config.Validate(conf); err != nil {
  362. return nil, err
  363. }
  364. if flags.Changed("graph") {
  365. logrus.Warnf(`The "-g / --graph" flag is deprecated. Please use "--data-root" instead`)
  366. }
  367. // Check if duplicate label-keys with different values are found
  368. newLabels, err := config.GetConflictFreeLabels(conf.Labels)
  369. if err != nil {
  370. return nil, err
  371. }
  372. conf.Labels = newLabels
  373. // Regardless of whether the user sets it to true or false, if they
  374. // specify TLSVerify at all then we need to turn on TLS
  375. if conf.IsValueSet(FlagTLSVerify) {
  376. v := true
  377. conf.TLS = &v
  378. }
  379. if conf.TLSVerify == nil && conf.TLS != nil {
  380. conf.TLSVerify = conf.TLS
  381. }
  382. return conf, nil
  383. }
  384. func warnOnDeprecatedConfigOptions(config *config.Config) {
  385. if config.ClusterAdvertise != "" {
  386. logrus.Warn(`The "cluster-advertise" option is deprecated. To be removed soon.`)
  387. }
  388. if config.ClusterStore != "" {
  389. logrus.Warn(`The "cluster-store" option is deprecated. To be removed soon.`)
  390. }
  391. if len(config.ClusterOpts) > 0 {
  392. logrus.Warn(`The "cluster-store-opt" option is deprecated. To be removed soon.`)
  393. }
  394. }
  395. func initRouter(opts routerOptions) {
  396. decoder := runconfig.ContainerDecoder{
  397. GetSysInfo: func() *sysinfo.SysInfo {
  398. return opts.daemon.RawSysInfo(true)
  399. },
  400. }
  401. routers := []router.Router{
  402. // we need to add the checkpoint router before the container router or the DELETE gets masked
  403. checkpointrouter.NewRouter(opts.daemon, decoder),
  404. container.NewRouter(opts.daemon, decoder, opts.daemon.RawSysInfo(true).CgroupUnified),
  405. image.NewRouter(opts.daemon.ImageService()),
  406. systemrouter.NewRouter(opts.daemon, opts.cluster, opts.buildkit, opts.features),
  407. volume.NewRouter(opts.daemon.VolumesService()),
  408. build.NewRouter(opts.buildBackend, opts.daemon, opts.features),
  409. sessionrouter.NewRouter(opts.sessionManager),
  410. swarmrouter.NewRouter(opts.cluster),
  411. pluginrouter.NewRouter(opts.daemon.PluginManager()),
  412. distributionrouter.NewRouter(opts.daemon.ImageService()),
  413. }
  414. grpcBackends := []grpcrouter.Backend{}
  415. for _, b := range []interface{}{opts.daemon, opts.buildBackend} {
  416. if b, ok := b.(grpcrouter.Backend); ok {
  417. grpcBackends = append(grpcBackends, b)
  418. }
  419. }
  420. if len(grpcBackends) > 0 {
  421. routers = append(routers, grpcrouter.NewRouter(grpcBackends...))
  422. }
  423. if opts.daemon.NetworkControllerEnabled() {
  424. routers = append(routers, network.NewRouter(opts.daemon, opts.cluster))
  425. }
  426. if opts.daemon.HasExperimental() {
  427. for _, r := range routers {
  428. for _, route := range r.Routes() {
  429. if experimental, ok := route.(router.ExperimentalRoute); ok {
  430. experimental.Enable()
  431. }
  432. }
  433. }
  434. }
  435. opts.api.InitRouter(routers...)
  436. }
  437. // TODO: remove this from cli and return the authzMiddleware
  438. func (cli *DaemonCli) initMiddlewares(s *apiserver.Server, cfg *apiserver.Config, pluginStore plugingetter.PluginGetter) error {
  439. v := cfg.Version
  440. exp := middleware.NewExperimentalMiddleware(cli.Config.Experimental)
  441. s.UseMiddleware(exp)
  442. vm := middleware.NewVersionMiddleware(v, api.DefaultVersion, api.MinVersion)
  443. s.UseMiddleware(vm)
  444. if cfg.CorsHeaders != "" {
  445. c := middleware.NewCORSMiddleware(cfg.CorsHeaders)
  446. s.UseMiddleware(c)
  447. }
  448. cli.authzMiddleware = authorization.NewMiddleware(cli.Config.AuthorizationPlugins, pluginStore)
  449. cli.Config.AuthzMiddleware = cli.authzMiddleware
  450. s.UseMiddleware(cli.authzMiddleware)
  451. return nil
  452. }
  453. func (cli *DaemonCli) getContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
  454. opts, err := cli.getPlatformContainerdDaemonOpts()
  455. if err != nil {
  456. return nil, err
  457. }
  458. if cli.Config.Debug {
  459. opts = append(opts, supervisor.WithLogLevel("debug"))
  460. } else if cli.Config.LogLevel != "" {
  461. opts = append(opts, supervisor.WithLogLevel(cli.Config.LogLevel))
  462. }
  463. if !cli.Config.CriContainerd {
  464. opts = append(opts, supervisor.WithPlugin("cri", nil))
  465. }
  466. return opts, nil
  467. }
  468. func newAPIServerConfig(cli *DaemonCli) (*apiserver.Config, error) {
  469. serverConfig := &apiserver.Config{
  470. Logging: true,
  471. SocketGroup: cli.Config.SocketGroup,
  472. Version: dockerversion.Version,
  473. CorsHeaders: cli.Config.CorsHeaders,
  474. }
  475. if cli.Config.TLS != nil && *cli.Config.TLS {
  476. tlsOptions := tlsconfig.Options{
  477. CAFile: cli.Config.CommonTLSOptions.CAFile,
  478. CertFile: cli.Config.CommonTLSOptions.CertFile,
  479. KeyFile: cli.Config.CommonTLSOptions.KeyFile,
  480. ExclusiveRootPools: true,
  481. }
  482. if cli.Config.TLSVerify == nil || *cli.Config.TLSVerify {
  483. // server requires and verifies client's certificate
  484. tlsOptions.ClientAuth = tls.RequireAndVerifyClientCert
  485. }
  486. tlsConfig, err := tlsconfig.Server(tlsOptions)
  487. if err != nil {
  488. return nil, err
  489. }
  490. serverConfig.TLSConfig = tlsConfig
  491. }
  492. if len(cli.Config.Hosts) == 0 {
  493. cli.Config.Hosts = make([]string, 1)
  494. }
  495. return serverConfig, nil
  496. }
  497. // checkTLSAuthOK checks basically for an explicitly disabled TLS/TLSVerify
  498. // Going forward we do not want to support a scenario where dockerd listens
  499. // on TCP without either TLS client auth (or an explicit opt-in to disable it)
  500. func checkTLSAuthOK(c *config.Config) bool {
  501. if c.TLS == nil {
  502. // Either TLS is enabled by default, in which case TLS verification should be enabled by default, or explicitly disabled
  503. // Or TLS is disabled by default... in any of these cases, we can just take the default value as to how to proceed
  504. return DefaultTLSValue
  505. }
  506. if !*c.TLS {
  507. // TLS is explicitly disabled, which is supported
  508. return true
  509. }
  510. if c.TLSVerify == nil {
  511. // this actually shouldn't happen since we set TLSVerify on the config object anyway
  512. // But in case it does get here, be cautious and assume this is not supported.
  513. return false
  514. }
  515. // Either TLSVerify is explicitly enabled or disabled, both cases are supported
  516. return true
  517. }
  518. func loadListeners(cli *DaemonCli, serverConfig *apiserver.Config) ([]string, error) {
  519. var hosts []string
  520. seen := make(map[string]struct{}, len(cli.Config.Hosts))
  521. useTLS := DefaultTLSValue
  522. if cli.Config.TLS != nil {
  523. useTLS = *cli.Config.TLS
  524. }
  525. for i := 0; i < len(cli.Config.Hosts); i++ {
  526. var err error
  527. if cli.Config.Hosts[i], err = dopts.ParseHost(useTLS, honorXDG, cli.Config.Hosts[i]); err != nil {
  528. return nil, errors.Wrapf(err, "error parsing -H %s", cli.Config.Hosts[i])
  529. }
  530. if _, ok := seen[cli.Config.Hosts[i]]; ok {
  531. continue
  532. }
  533. seen[cli.Config.Hosts[i]] = struct{}{}
  534. protoAddr := cli.Config.Hosts[i]
  535. protoAddrParts := strings.SplitN(protoAddr, "://", 2)
  536. if len(protoAddrParts) != 2 {
  537. return nil, fmt.Errorf("bad format %s, expected PROTO://ADDR", protoAddr)
  538. }
  539. proto := protoAddrParts[0]
  540. addr := protoAddrParts[1]
  541. // It's a bad idea to bind to TCP without tlsverify.
  542. authEnabled := serverConfig.TLSConfig != nil && serverConfig.TLSConfig.ClientAuth == tls.RequireAndVerifyClientCert
  543. if proto == "tcp" && !authEnabled {
  544. logrus.WithField("host", protoAddr).Warn("Binding to IP address without --tlsverify is insecure and gives root access on this machine to everyone who has access to your network.")
  545. logrus.WithField("host", protoAddr).Warn("Binding to an IP address, even on localhost, can also give access to scripts run in a browser. Be safe out there!")
  546. time.Sleep(time.Second)
  547. // If TLSVerify is explicitly set to false we'll take that as "Please let me shoot myself in the foot"
  548. // We do not want to continue to support a default mode where tls verification is disabled, so we do some extra warnings here and eventually remove support
  549. if !checkTLSAuthOK(cli.Config) {
  550. ipAddr, _, err := net.SplitHostPort(addr)
  551. if err != nil {
  552. return nil, errors.Wrap(err, "error parsing tcp address")
  553. }
  554. // shortcut all this extra stuff for literal "localhost"
  555. // -H supports specifying hostnames, since we want to bypass this on loopback interfaces we'll look it up here.
  556. if ipAddr != "localhost" {
  557. ip := net.ParseIP(ipAddr)
  558. if ip == nil {
  559. ipA, err := net.ResolveIPAddr("ip", ipAddr)
  560. if err != nil {
  561. logrus.WithError(err).WithField("host", ipAddr).Error("Error looking up specified host address")
  562. }
  563. if ipA != nil {
  564. ip = ipA.IP
  565. }
  566. }
  567. if ip == nil || !ip.IsLoopback() {
  568. logrus.WithField("host", protoAddr).Warn("Binding to an IP address without --tlsverify is deprecated. Startup is intentionally being slowed down to show this message")
  569. logrus.WithField("host", protoAddr).Warn("Please consider generating tls certificates with client validation to prevent exposing unauthenticated root access to your network")
  570. logrus.WithField("host", protoAddr).Warnf("You can override this by explicitly specifying '--%s=false' or '--%s=false'", FlagTLS, FlagTLSVerify)
  571. logrus.WithField("host", protoAddr).Warnf("Support for listening on TCP without authentication or explicit intent to run without authentication will be removed in the next release")
  572. time.Sleep(15 * time.Second)
  573. }
  574. }
  575. }
  576. }
  577. ls, err := listeners.Init(proto, addr, serverConfig.SocketGroup, serverConfig.TLSConfig)
  578. if err != nil {
  579. return nil, err
  580. }
  581. // If we're binding to a TCP port, make sure that a container doesn't try to use it.
  582. if proto == "tcp" {
  583. if err := allocateDaemonPort(addr); err != nil {
  584. return nil, err
  585. }
  586. }
  587. logrus.Debugf("Listener created for HTTP on %s (%s)", proto, addr)
  588. hosts = append(hosts, protoAddrParts[1])
  589. cli.api.Accept(addr, ls...)
  590. }
  591. return hosts, nil
  592. }
  593. func createAndStartCluster(cli *DaemonCli, d *daemon.Daemon) (*cluster.Cluster, error) {
  594. name, _ := os.Hostname()
  595. // Use a buffered channel to pass changes from store watch API to daemon
  596. // A buffer allows store watch API and daemon processing to not wait for each other
  597. watchStream := make(chan *swarmapi.WatchMessage, 32)
  598. c, err := cluster.New(cluster.Config{
  599. Root: cli.Config.Root,
  600. Name: name,
  601. Backend: d,
  602. VolumeBackend: d.VolumesService(),
  603. ImageBackend: d.ImageService(),
  604. PluginBackend: d.PluginManager(),
  605. NetworkSubnetsProvider: d,
  606. DefaultAdvertiseAddr: cli.Config.SwarmDefaultAdvertiseAddr,
  607. RaftHeartbeatTick: cli.Config.SwarmRaftHeartbeatTick,
  608. RaftElectionTick: cli.Config.SwarmRaftElectionTick,
  609. RuntimeRoot: cli.getSwarmRunRoot(),
  610. WatchStream: watchStream,
  611. })
  612. if err != nil {
  613. return nil, err
  614. }
  615. d.SetCluster(c)
  616. err = c.Start()
  617. return c, err
  618. }
  619. // validates that the plugins requested with the --authorization-plugin flag are valid AuthzDriver
  620. // plugins present on the host and available to the daemon
  621. func validateAuthzPlugins(requestedPlugins []string, pg plugingetter.PluginGetter) error {
  622. for _, reqPlugin := range requestedPlugins {
  623. if _, err := pg.Get(reqPlugin, authorization.AuthZApiImplements, plugingetter.Lookup); err != nil {
  624. return err
  625. }
  626. }
  627. return nil
  628. }
  629. func systemContainerdRunning(honorXDG bool) (string, bool, error) {
  630. addr := containerddefaults.DefaultAddress
  631. if honorXDG {
  632. runtimeDir, err := homedir.GetRuntimeDir()
  633. if err != nil {
  634. return "", false, err
  635. }
  636. addr = filepath.Join(runtimeDir, "containerd", "containerd.sock")
  637. }
  638. _, err := os.Lstat(addr)
  639. return addr, err == nil, nil
  640. }
  641. // configureDaemonLogs sets the logrus logging level and formatting
  642. func configureDaemonLogs(conf *config.Config) error {
  643. if conf.LogLevel != "" {
  644. lvl, err := logrus.ParseLevel(conf.LogLevel)
  645. if err != nil {
  646. return fmt.Errorf("unable to parse logging level: %s", conf.LogLevel)
  647. }
  648. logrus.SetLevel(lvl)
  649. } else {
  650. logrus.SetLevel(logrus.InfoLevel)
  651. }
  652. logrus.SetFormatter(&logrus.TextFormatter{
  653. TimestampFormat: jsonmessage.RFC3339NanoFixed,
  654. DisableColors: conf.RawLogs,
  655. FullTimestamp: true,
  656. })
  657. return nil
  658. }