config.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package embed
  15. import (
  16. "crypto/tls"
  17. "fmt"
  18. "io/ioutil"
  19. "net"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "path/filepath"
  24. "sort"
  25. "strings"
  26. "sync"
  27. "syscall"
  28. "time"
  29. "github.com/coreos/etcd/compactor"
  30. "github.com/coreos/etcd/etcdserver"
  31. "github.com/coreos/etcd/pkg/flags"
  32. "github.com/coreos/etcd/pkg/logutil"
  33. "github.com/coreos/etcd/pkg/netutil"
  34. "github.com/coreos/etcd/pkg/srv"
  35. "github.com/coreos/etcd/pkg/transport"
  36. "github.com/coreos/etcd/pkg/types"
  37. "github.com/coreos/pkg/capnslog"
  38. "github.com/ghodss/yaml"
  39. "go.uber.org/zap"
  40. "google.golang.org/grpc"
  41. "google.golang.org/grpc/grpclog"
  42. )
  43. const (
  44. ClusterStateFlagNew = "new"
  45. ClusterStateFlagExisting = "existing"
  46. DefaultName = "default"
  47. DefaultMaxSnapshots = 5
  48. DefaultMaxWALs = 5
  49. DefaultMaxTxnOps = uint(128)
  50. DefaultMaxRequestBytes = 1.5 * 1024 * 1024
  51. DefaultGRPCKeepAliveMinTime = 5 * time.Second
  52. DefaultGRPCKeepAliveInterval = 2 * time.Hour
  53. DefaultGRPCKeepAliveTimeout = 20 * time.Second
  54. DefaultListenPeerURLs = "http://localhost:2380"
  55. DefaultListenClientURLs = "http://localhost:2379"
  56. DefaultLogOutput = "default"
  57. // DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
  58. // It's enabled by default.
  59. DefaultStrictReconfigCheck = true
  60. // DefaultEnableV2 is the default value for "--enable-v2" flag.
  61. // v2 is enabled by default.
  62. // TODO: disable v2 when deprecated.
  63. DefaultEnableV2 = true
  64. // maxElectionMs specifies the maximum value of election timeout.
  65. // More details are listed in ../Documentation/tuning.md#time-parameters.
  66. maxElectionMs = 50000
  67. )
  68. var (
  69. ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
  70. "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"")
  71. ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
  72. DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
  73. DefaultAdvertiseClientURLs = "http://localhost:2379"
  74. defaultHostname string
  75. defaultHostStatus error
  76. )
  77. var (
  78. // CompactorModePeriodic is periodic compaction mode
  79. // for "Config.AutoCompactionMode" field.
  80. // If "AutoCompactionMode" is CompactorModePeriodic and
  81. // "AutoCompactionRetention" is "1h", it automatically compacts
  82. // compacts storage every hour.
  83. CompactorModePeriodic = compactor.ModePeriodic
  84. // CompactorModeRevision is revision-based compaction mode
  85. // for "Config.AutoCompactionMode" field.
  86. // If "AutoCompactionMode" is CompactorModeRevision and
  87. // "AutoCompactionRetention" is "1000", it compacts log on
  88. // revision 5000 when the current revision is 6000.
  89. // This runs every 5-minute if enough of logs have proceeded.
  90. CompactorModeRevision = compactor.ModeRevision
  91. )
  92. func init() {
  93. defaultHostname, defaultHostStatus = netutil.GetDefaultHost()
  94. }
  95. // Config holds the arguments for configuring an etcd server.
  96. type Config struct {
  97. Name string `json:"name"`
  98. Dir string `json:"data-dir"`
  99. WalDir string `json:"wal-dir"`
  100. SnapCount uint64 `json:"snapshot-count"`
  101. MaxSnapFiles uint `json:"max-snapshots"`
  102. MaxWalFiles uint `json:"max-wals"`
  103. // TickMs is the number of milliseconds between heartbeat ticks.
  104. // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
  105. // make ticks a cluster wide configuration.
  106. TickMs uint `json:"heartbeat-interval"`
  107. ElectionMs uint `json:"election-timeout"`
  108. // InitialElectionTickAdvance is true, then local member fast-forwards
  109. // election ticks to speed up "initial" leader election trigger. This
  110. // benefits the case of larger election ticks. For instance, cross
  111. // datacenter deployment may require longer election timeout of 10-second.
  112. // If true, local node does not need wait up to 10-second. Instead,
  113. // forwards its election ticks to 8-second, and have only 2-second left
  114. // before leader election.
  115. //
  116. // Major assumptions are that:
  117. // - cluster has no active leader thus advancing ticks enables faster
  118. // leader election, or
  119. // - cluster already has an established leader, and rejoining follower
  120. // is likely to receive heartbeats from the leader after tick advance
  121. // and before election timeout.
  122. //
  123. // However, when network from leader to rejoining follower is congested,
  124. // and the follower does not receive leader heartbeat within left election
  125. // ticks, disruptive election has to happen thus affecting cluster
  126. // availabilities.
  127. //
  128. // Disabling this would slow down initial bootstrap process for cross
  129. // datacenter deployments. Make your own tradeoffs by configuring
  130. // --initial-election-tick-advance at the cost of slow initial bootstrap.
  131. //
  132. // If single-node, it advances ticks regardless.
  133. //
  134. // See https://github.com/coreos/etcd/issues/9333 for more detail.
  135. InitialElectionTickAdvance bool `json:"initial-election-tick-advance"`
  136. QuotaBackendBytes int64 `json:"quota-backend-bytes"`
  137. MaxTxnOps uint `json:"max-txn-ops"`
  138. MaxRequestBytes uint `json:"max-request-bytes"`
  139. LPUrls, LCUrls []url.URL
  140. APUrls, ACUrls []url.URL
  141. ClientTLSInfo transport.TLSInfo
  142. ClientAutoTLS bool
  143. PeerTLSInfo transport.TLSInfo
  144. PeerAutoTLS bool
  145. ClusterState string `json:"initial-cluster-state"`
  146. DNSCluster string `json:"discovery-srv"`
  147. DNSClusterServiceName string `json:"discovery-srv-name"`
  148. Dproxy string `json:"discovery-proxy"`
  149. Durl string `json:"discovery"`
  150. InitialCluster string `json:"initial-cluster"`
  151. InitialClusterToken string `json:"initial-cluster-token"`
  152. StrictReconfigCheck bool `json:"strict-reconfig-check"`
  153. EnableV2 bool `json:"enable-v2"`
  154. // AutoCompactionMode is either 'periodic' or 'revision'.
  155. AutoCompactionMode string `json:"auto-compaction-mode"`
  156. // AutoCompactionRetention is either duration string with time unit
  157. // (e.g. '5m' for 5-minute), or revision unit (e.g. '5000').
  158. // If no time unit is provided and compaction mode is 'periodic',
  159. // the unit defaults to hour. For example, '5' translates into 5-hour.
  160. AutoCompactionRetention string `json:"auto-compaction-retention"`
  161. // GRPCKeepAliveMinTime is the minimum interval that a client should
  162. // wait before pinging server. When client pings "too fast", server
  163. // sends goaway and closes the connection (errors: too_many_pings,
  164. // http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
  165. // Server expects client pings only when there is any active streams
  166. // (PermitWithoutStream is set false).
  167. GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
  168. // GRPCKeepAliveInterval is the frequency of server-to-client ping
  169. // to check if a connection is alive. Close a non-responsive connection
  170. // after an additional duration of Timeout. 0 to disable.
  171. GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
  172. // GRPCKeepAliveTimeout is the additional duration of wait
  173. // before closing a non-responsive connection. 0 to disable.
  174. GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`
  175. // PreVote is true to enable Raft Pre-Vote.
  176. // If enabled, Raft runs an additional election phase
  177. // to check whether it would get enough votes to win
  178. // an election, thus minimizing disruptions.
  179. // TODO: enable by default in 3.5.
  180. PreVote bool `json:"pre-vote"`
  181. CORS map[string]struct{}
  182. // HostWhitelist lists acceptable hostnames from HTTP client requests.
  183. // Client origin policy protects against "DNS Rebinding" attacks
  184. // to insecure etcd servers. That is, any website can simply create
  185. // an authorized DNS name, and direct DNS to "localhost" (or any
  186. // other address). Then, all HTTP endpoints of etcd server listening
  187. // on "localhost" becomes accessible, thus vulnerable to DNS rebinding
  188. // attacks. See "CVE-2018-5702" for more detail.
  189. //
  190. // 1. If client connection is secure via HTTPS, allow any hostnames.
  191. // 2. If client connection is not secure and "HostWhitelist" is not empty,
  192. // only allow HTTP requests whose Host field is listed in whitelist.
  193. //
  194. // Note that the client origin policy is enforced whether authentication
  195. // is enabled or not, for tighter controls.
  196. //
  197. // By default, "HostWhitelist" is "*", which allows any hostnames.
  198. // Note that when specifying hostnames, loopback addresses are not added
  199. // automatically. To allow loopback interfaces, leave it empty or set it "*",
  200. // or add them to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
  201. //
  202. // CVE-2018-5702 reference:
  203. // - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
  204. // - https://github.com/transmission/transmission/pull/468
  205. // - https://github.com/coreos/etcd/issues/9353
  206. HostWhitelist map[string]struct{}
  207. // UserHandlers is for registering users handlers and only used for
  208. // embedding etcd into other applications.
  209. // The map key is the route path for the handler, and
  210. // you must ensure it can't be conflicted with etcd's.
  211. UserHandlers map[string]http.Handler `json:"-"`
  212. // ServiceRegister is for registering users' gRPC services. A simple usage example:
  213. // cfg := embed.NewConfig()
  214. // cfg.ServerRegister = func(s *grpc.Server) {
  215. // pb.RegisterFooServer(s, &fooServer{})
  216. // pb.RegisterBarServer(s, &barServer{})
  217. // }
  218. // embed.StartEtcd(cfg)
  219. ServiceRegister func(*grpc.Server) `json:"-"`
  220. AuthToken string `json:"auth-token"`
  221. ExperimentalInitialCorruptCheck bool `json:"experimental-initial-corrupt-check"`
  222. ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
  223. ExperimentalEnableV2V3 string `json:"experimental-enable-v2v3"`
  224. // ForceNewCluster starts a new cluster even if previously started; unsafe.
  225. ForceNewCluster bool `json:"force-new-cluster"`
  226. EnablePprof bool `json:"enable-pprof"`
  227. Metrics string `json:"metrics"`
  228. ListenMetricsUrls []url.URL
  229. ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`
  230. // logger logs server-side operations. The default is nil,
  231. // and "setupLogging" must be called before starting server.
  232. // Do not set logger directly.
  233. loggerMu *sync.RWMutex
  234. logger *zap.Logger
  235. loggerConfig zap.Config
  236. // Logger is logger options: "zap", "capnslog".
  237. // WARN: "capnslog" is being deprecated in v3.5.
  238. Logger string `json:"logger"`
  239. // LogOutput is either:
  240. // - "default" as os.Stderr,
  241. // - "stderr" as os.Stderr,
  242. // - "stdout" as os.Stdout,
  243. // - file path to append server logs to.
  244. // It can be multiple when "Logger" is zap.
  245. LogOutput []string `json:"log-output"`
  246. // Debug is true, to enable debug level logging.
  247. Debug bool `json:"debug"`
  248. // LogPkgLevels is being deprecated in v3.5.
  249. // Only valid if "logger" option is "capnslog".
  250. // WARN: DO NOT USE THIS!
  251. LogPkgLevels string `json:"log-package-levels"`
  252. }
  253. // configYAML holds the config suitable for yaml parsing
  254. type configYAML struct {
  255. Config
  256. configJSON
  257. }
  258. // configJSON has file options that are translated into Config options
  259. type configJSON struct {
  260. LPUrlsJSON string `json:"listen-peer-urls"`
  261. LCUrlsJSON string `json:"listen-client-urls"`
  262. APUrlsJSON string `json:"initial-advertise-peer-urls"`
  263. ACUrlsJSON string `json:"advertise-client-urls"`
  264. CORSJSON string `json:"cors"`
  265. HostWhitelistJSON string `json:"host-whitelist"`
  266. ClientSecurityJSON securityConfig `json:"client-transport-security"`
  267. PeerSecurityJSON securityConfig `json:"peer-transport-security"`
  268. }
  269. type securityConfig struct {
  270. CertFile string `json:"cert-file"`
  271. KeyFile string `json:"key-file"`
  272. CertAuth bool `json:"client-cert-auth"`
  273. TrustedCAFile string `json:"trusted-ca-file"`
  274. AutoTLS bool `json:"auto-tls"`
  275. }
  276. // NewConfig creates a new Config populated with default values.
  277. func NewConfig() *Config {
  278. lpurl, _ := url.Parse(DefaultListenPeerURLs)
  279. apurl, _ := url.Parse(DefaultInitialAdvertisePeerURLs)
  280. lcurl, _ := url.Parse(DefaultListenClientURLs)
  281. acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
  282. cfg := &Config{
  283. MaxSnapFiles: DefaultMaxSnapshots,
  284. MaxWalFiles: DefaultMaxWALs,
  285. Name: DefaultName,
  286. SnapCount: etcdserver.DefaultSnapCount,
  287. MaxTxnOps: DefaultMaxTxnOps,
  288. MaxRequestBytes: DefaultMaxRequestBytes,
  289. GRPCKeepAliveMinTime: DefaultGRPCKeepAliveMinTime,
  290. GRPCKeepAliveInterval: DefaultGRPCKeepAliveInterval,
  291. GRPCKeepAliveTimeout: DefaultGRPCKeepAliveTimeout,
  292. TickMs: 100,
  293. ElectionMs: 1000,
  294. InitialElectionTickAdvance: true,
  295. LPUrls: []url.URL{*lpurl},
  296. LCUrls: []url.URL{*lcurl},
  297. APUrls: []url.URL{*apurl},
  298. ACUrls: []url.URL{*acurl},
  299. ClusterState: ClusterStateFlagNew,
  300. InitialClusterToken: "etcd-cluster",
  301. StrictReconfigCheck: DefaultStrictReconfigCheck,
  302. Metrics: "basic",
  303. EnableV2: DefaultEnableV2,
  304. CORS: map[string]struct{}{"*": {}},
  305. HostWhitelist: map[string]struct{}{"*": {}},
  306. AuthToken: "simple",
  307. PreVote: false, // TODO: enable by default in v3.5
  308. loggerMu: new(sync.RWMutex),
  309. logger: nil,
  310. Logger: "capnslog",
  311. LogOutput: []string{DefaultLogOutput},
  312. Debug: false,
  313. LogPkgLevels: "",
  314. }
  315. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  316. return cfg
  317. }
  318. func logTLSHandshakeFailure(conn *tls.Conn, err error) {
  319. state := conn.ConnectionState()
  320. remoteAddr := conn.RemoteAddr().String()
  321. serverName := state.ServerName
  322. if len(state.PeerCertificates) > 0 {
  323. cert := state.PeerCertificates[0]
  324. ips, dns := cert.IPAddresses, cert.DNSNames
  325. plog.Infof("rejected connection from %q (error %q, ServerName %q, IPAddresses %q, DNSNames %q)", remoteAddr, err.Error(), serverName, ips, dns)
  326. } else {
  327. plog.Infof("rejected connection from %q (error %q, ServerName %q)", remoteAddr, err.Error(), serverName)
  328. }
  329. }
  330. // GetLogger returns the logger.
  331. func (cfg Config) GetLogger() *zap.Logger {
  332. cfg.loggerMu.RLock()
  333. l := cfg.logger
  334. cfg.loggerMu.RUnlock()
  335. return l
  336. }
  337. // for testing
  338. var grpcLogOnce = new(sync.Once)
  339. // setupLogging initializes etcd logging.
  340. // Must be called after flag parsing or finishing configuring embed.Config.
  341. func (cfg *Config) setupLogging() error {
  342. switch cfg.Logger {
  343. case "capnslog": // TODO: deprecate this in v3.5
  344. cfg.ClientTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  345. cfg.PeerTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  346. if cfg.Debug {
  347. capnslog.SetGlobalLogLevel(capnslog.DEBUG)
  348. grpc.EnableTracing = true
  349. // enable info, warning, error
  350. grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  351. } else {
  352. capnslog.SetGlobalLogLevel(capnslog.INFO)
  353. // only discard info
  354. grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, os.Stderr, os.Stderr))
  355. }
  356. // TODO: deprecate with "capnslog"
  357. if cfg.LogPkgLevels != "" {
  358. repoLog := capnslog.MustRepoLogger("github.com/coreos/etcd")
  359. settings, err := repoLog.ParseLogLevelConfig(cfg.LogPkgLevels)
  360. if err != nil {
  361. plog.Warningf("couldn't parse log level string: %s, continuing with default levels", err.Error())
  362. return nil
  363. }
  364. repoLog.SetLogLevel(settings)
  365. }
  366. if len(cfg.LogOutput) != 1 {
  367. fmt.Printf("expected only 1 value in 'log-output', got %v\n", cfg.LogOutput)
  368. os.Exit(1)
  369. }
  370. // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
  371. // where NewDefaultFormatter returns NewJournaldFormatter when syscall.Getppid() == 1
  372. // specify 'stdout' or 'stderr' to skip journald logging even when running under systemd
  373. output := cfg.LogOutput[0]
  374. switch output {
  375. case "stdout":
  376. capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug))
  377. case "stderr":
  378. capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, cfg.Debug))
  379. case DefaultLogOutput:
  380. default:
  381. plog.Panicf(`unknown log-output %q (only supports %q, "stdout", "stderr")`, output, DefaultLogOutput)
  382. }
  383. case "zap":
  384. if len(cfg.LogOutput) == 0 {
  385. cfg.LogOutput = []string{DefaultLogOutput}
  386. }
  387. if len(cfg.LogOutput) > 1 {
  388. for _, v := range cfg.LogOutput {
  389. if v == DefaultLogOutput {
  390. panic(fmt.Errorf("multi logoutput for %q is not supported yet", DefaultLogOutput))
  391. }
  392. }
  393. }
  394. // TODO: use zapcore to support more features?
  395. lcfg := zap.Config{
  396. Level: zap.NewAtomicLevelAt(zap.InfoLevel),
  397. Development: false,
  398. Sampling: &zap.SamplingConfig{
  399. Initial: 100,
  400. Thereafter: 100,
  401. },
  402. Encoding: "json",
  403. EncoderConfig: zap.NewProductionEncoderConfig(),
  404. OutputPaths: make([]string, 0),
  405. ErrorOutputPaths: make([]string, 0),
  406. }
  407. outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{})
  408. for _, v := range cfg.LogOutput {
  409. switch v {
  410. case DefaultLogOutput:
  411. if syscall.Getppid() == 1 {
  412. // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
  413. // where "NewDefaultFormatter" returns "NewJournaldFormatter"
  414. // when syscall.Getppid() == 1, specify 'stdout' or 'stderr' to
  415. // skip journald logging even when running under systemd
  416. // TODO: capnlog.NewJournaldFormatter()
  417. fmt.Println("running under init, which may be systemd!")
  418. outputPaths["stderr"] = struct{}{}
  419. errOutputPaths["stderr"] = struct{}{}
  420. continue
  421. }
  422. outputPaths["stderr"] = struct{}{}
  423. errOutputPaths["stderr"] = struct{}{}
  424. case "stderr":
  425. outputPaths["stderr"] = struct{}{}
  426. errOutputPaths["stderr"] = struct{}{}
  427. case "stdout":
  428. outputPaths["stdout"] = struct{}{}
  429. errOutputPaths["stdout"] = struct{}{}
  430. default:
  431. outputPaths[v] = struct{}{}
  432. errOutputPaths[v] = struct{}{}
  433. }
  434. }
  435. for v := range outputPaths {
  436. lcfg.OutputPaths = append(lcfg.OutputPaths, v)
  437. }
  438. for v := range errOutputPaths {
  439. lcfg.ErrorOutputPaths = append(lcfg.ErrorOutputPaths, v)
  440. }
  441. sort.Strings(lcfg.OutputPaths)
  442. sort.Strings(lcfg.ErrorOutputPaths)
  443. if cfg.Debug {
  444. lcfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
  445. grpc.EnableTracing = true
  446. }
  447. var err error
  448. cfg.logger, err = lcfg.Build()
  449. if err != nil {
  450. return err
  451. }
  452. cfg.loggerConfig = lcfg
  453. grpcLogOnce.Do(func() {
  454. // debug true, enable info, warning, error
  455. // debug false, only discard info
  456. var gl grpclog.LoggerV2
  457. gl, err = logutil.NewGRPCLoggerV2(lcfg)
  458. if err == nil {
  459. grpclog.SetLoggerV2(gl)
  460. }
  461. })
  462. if err != nil {
  463. return err
  464. }
  465. logTLSHandshakeFailure := func(conn *tls.Conn, err error) {
  466. state := conn.ConnectionState()
  467. remoteAddr := conn.RemoteAddr().String()
  468. serverName := state.ServerName
  469. if len(state.PeerCertificates) > 0 {
  470. cert := state.PeerCertificates[0]
  471. ips := make([]string, 0, len(cert.IPAddresses))
  472. for i := range cert.IPAddresses {
  473. ips[i] = cert.IPAddresses[i].String()
  474. }
  475. cfg.logger.Warn(
  476. "rejected connection",
  477. zap.String("remote-addr", remoteAddr),
  478. zap.String("server-name", serverName),
  479. zap.Strings("ip-addresses", ips),
  480. zap.Strings("dns-names", cert.DNSNames),
  481. zap.Error(err),
  482. )
  483. } else {
  484. cfg.logger.Warn(
  485. "rejected connection",
  486. zap.String("remote-addr", remoteAddr),
  487. zap.String("server-name", serverName),
  488. zap.Error(err),
  489. )
  490. }
  491. }
  492. cfg.ClientTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  493. cfg.PeerTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  494. default:
  495. return fmt.Errorf("unknown logger option %q", cfg.Logger)
  496. }
  497. return nil
  498. }
  499. func ConfigFromFile(path string) (*Config, error) {
  500. cfg := &configYAML{Config: *NewConfig()}
  501. if err := cfg.configFromFile(path); err != nil {
  502. return nil, err
  503. }
  504. return &cfg.Config, nil
  505. }
  506. func (cfg *configYAML) configFromFile(path string) error {
  507. b, err := ioutil.ReadFile(path)
  508. if err != nil {
  509. return err
  510. }
  511. defaultInitialCluster := cfg.InitialCluster
  512. err = yaml.Unmarshal(b, cfg)
  513. if err != nil {
  514. return err
  515. }
  516. if cfg.LPUrlsJSON != "" {
  517. u, err := types.NewURLs(strings.Split(cfg.LPUrlsJSON, ","))
  518. if err != nil {
  519. fmt.Fprintf(os.Stderr, "unexpected error setting up listen-peer-urls: %v\n", err)
  520. os.Exit(1)
  521. }
  522. cfg.LPUrls = []url.URL(u)
  523. }
  524. if cfg.LCUrlsJSON != "" {
  525. u, err := types.NewURLs(strings.Split(cfg.LCUrlsJSON, ","))
  526. if err != nil {
  527. fmt.Fprintf(os.Stderr, "unexpected error setting up listen-client-urls: %v\n", err)
  528. os.Exit(1)
  529. }
  530. cfg.LCUrls = []url.URL(u)
  531. }
  532. if cfg.APUrlsJSON != "" {
  533. u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
  534. if err != nil {
  535. fmt.Fprintf(os.Stderr, "unexpected error setting up initial-advertise-peer-urls: %v\n", err)
  536. os.Exit(1)
  537. }
  538. cfg.APUrls = []url.URL(u)
  539. }
  540. if cfg.ACUrlsJSON != "" {
  541. u, err := types.NewURLs(strings.Split(cfg.ACUrlsJSON, ","))
  542. if err != nil {
  543. fmt.Fprintf(os.Stderr, "unexpected error setting up advertise-peer-urls: %v\n", err)
  544. os.Exit(1)
  545. }
  546. cfg.ACUrls = []url.URL(u)
  547. }
  548. if cfg.ListenMetricsUrlsJSON != "" {
  549. u, err := types.NewURLs(strings.Split(cfg.ListenMetricsUrlsJSON, ","))
  550. if err != nil {
  551. fmt.Fprintf(os.Stderr, "unexpected error setting up listen-metrics-urls: %v\n", err)
  552. os.Exit(1)
  553. }
  554. cfg.ListenMetricsUrls = []url.URL(u)
  555. }
  556. if cfg.CORSJSON != "" {
  557. uv := flags.NewUniqueURLsWithExceptions(cfg.CORSJSON, "*")
  558. cfg.CORS = uv.Values
  559. }
  560. if cfg.HostWhitelistJSON != "" {
  561. uv := flags.NewUniqueStringsValue(cfg.HostWhitelistJSON)
  562. cfg.HostWhitelist = uv.Values
  563. }
  564. // If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
  565. if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
  566. cfg.InitialCluster = ""
  567. }
  568. if cfg.ClusterState == "" {
  569. cfg.ClusterState = ClusterStateFlagNew
  570. }
  571. copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
  572. tls.CertFile = ysc.CertFile
  573. tls.KeyFile = ysc.KeyFile
  574. tls.ClientCertAuth = ysc.CertAuth
  575. tls.TrustedCAFile = ysc.TrustedCAFile
  576. }
  577. copySecurityDetails(&cfg.ClientTLSInfo, &cfg.ClientSecurityJSON)
  578. copySecurityDetails(&cfg.PeerTLSInfo, &cfg.PeerSecurityJSON)
  579. cfg.ClientAutoTLS = cfg.ClientSecurityJSON.AutoTLS
  580. cfg.PeerAutoTLS = cfg.PeerSecurityJSON.AutoTLS
  581. return cfg.Validate()
  582. }
  583. // Validate ensures that '*embed.Config' fields are properly configured.
  584. func (cfg *Config) Validate() error {
  585. if err := cfg.setupLogging(); err != nil {
  586. return err
  587. }
  588. if err := checkBindURLs(cfg.LPUrls); err != nil {
  589. return err
  590. }
  591. if err := checkBindURLs(cfg.LCUrls); err != nil {
  592. return err
  593. }
  594. if err := checkBindURLs(cfg.ListenMetricsUrls); err != nil {
  595. return err
  596. }
  597. if err := checkHostURLs(cfg.APUrls); err != nil {
  598. addrs := cfg.getAPURLs()
  599. return fmt.Errorf(`--initial-advertise-peer-urls %q must be "host:port" (%v)`, strings.Join(addrs, ","), err)
  600. }
  601. if err := checkHostURLs(cfg.ACUrls); err != nil {
  602. addrs := cfg.getACURLs()
  603. return fmt.Errorf(`--advertise-client-urls %q must be "host:port" (%v)`, strings.Join(addrs, ","), err)
  604. }
  605. // Check if conflicting flags are passed.
  606. nSet := 0
  607. for _, v := range []bool{cfg.Durl != "", cfg.InitialCluster != "", cfg.DNSCluster != ""} {
  608. if v {
  609. nSet++
  610. }
  611. }
  612. if cfg.ClusterState != ClusterStateFlagNew && cfg.ClusterState != ClusterStateFlagExisting {
  613. return fmt.Errorf("unexpected clusterState %q", cfg.ClusterState)
  614. }
  615. if nSet > 1 {
  616. return ErrConflictBootstrapFlags
  617. }
  618. if cfg.TickMs <= 0 {
  619. return fmt.Errorf("--heartbeat-interval must be >0 (set to %dms)", cfg.TickMs)
  620. }
  621. if cfg.ElectionMs <= 0 {
  622. return fmt.Errorf("--election-timeout must be >0 (set to %dms)", cfg.ElectionMs)
  623. }
  624. if 5*cfg.TickMs > cfg.ElectionMs {
  625. return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
  626. }
  627. if cfg.ElectionMs > maxElectionMs {
  628. return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
  629. }
  630. // check this last since proxying in etcdmain may make this OK
  631. if cfg.LCUrls != nil && cfg.ACUrls == nil {
  632. return ErrUnsetAdvertiseClientURLsFlag
  633. }
  634. switch cfg.AutoCompactionMode {
  635. case "":
  636. case CompactorModeRevision, CompactorModePeriodic:
  637. default:
  638. return fmt.Errorf("unknown auto-compaction-mode %q", cfg.AutoCompactionMode)
  639. }
  640. return nil
  641. }
  642. // PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
  643. func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error) {
  644. token = cfg.InitialClusterToken
  645. switch {
  646. case cfg.Durl != "":
  647. urlsmap = types.URLsMap{}
  648. // If using discovery, generate a temporary cluster based on
  649. // self's advertised peer URLs
  650. urlsmap[cfg.Name] = cfg.APUrls
  651. token = cfg.Durl
  652. case cfg.DNSCluster != "":
  653. clusterStrs, cerr := cfg.GetDNSClusterNames()
  654. lg := cfg.logger
  655. if cerr != nil {
  656. if lg != nil {
  657. lg.Error("failed to resolve during SRV discovery", zap.Error(cerr))
  658. } else {
  659. plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
  660. }
  661. return nil, "", cerr
  662. }
  663. for _, s := range clusterStrs {
  664. if lg != nil {
  665. lg.Info("got bootstrap from DNS for etcd-server", zap.String("node", s))
  666. } else {
  667. plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
  668. }
  669. }
  670. clusterStr := strings.Join(clusterStrs, ",")
  671. if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.TrustedCAFile == "" {
  672. cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
  673. }
  674. urlsmap, err = types.NewURLsMap(clusterStr)
  675. // only etcd member must belong to the discovered cluster.
  676. // proxy does not need to belong to the discovered cluster.
  677. if which == "etcd" {
  678. if _, ok := urlsmap[cfg.Name]; !ok {
  679. return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.Name)
  680. }
  681. }
  682. default:
  683. // We're statically configured, and cluster has appropriately been set.
  684. urlsmap, err = types.NewURLsMap(cfg.InitialCluster)
  685. }
  686. return urlsmap, token, err
  687. }
  688. // GetDNSClusterNames uses DNS SRV records to get a list of initial nodes for cluster bootstrapping.
  689. func (cfg *Config) GetDNSClusterNames() ([]string, error) {
  690. var (
  691. clusterStrs []string
  692. cerr error
  693. serviceNameSuffix string
  694. )
  695. if cfg.DNSClusterServiceName != "" {
  696. serviceNameSuffix = "-" + cfg.DNSClusterServiceName
  697. }
  698. // Use both etcd-server-ssl and etcd-server for discovery. Combine the results if both are available.
  699. clusterStrs, cerr = srv.GetCluster("https", "etcd-server-ssl"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.APUrls)
  700. defaultHTTPClusterStrs, httpCerr := srv.GetCluster("http", "etcd-server"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.APUrls)
  701. if cerr != nil {
  702. clusterStrs = make([]string, 0)
  703. }
  704. if httpCerr != nil {
  705. clusterStrs = append(clusterStrs, defaultHTTPClusterStrs...)
  706. }
  707. return clusterStrs, cerr
  708. }
  709. func (cfg Config) InitialClusterFromName(name string) (ret string) {
  710. if len(cfg.APUrls) == 0 {
  711. return ""
  712. }
  713. n := name
  714. if name == "" {
  715. n = DefaultName
  716. }
  717. for i := range cfg.APUrls {
  718. ret = ret + "," + n + "=" + cfg.APUrls[i].String()
  719. }
  720. return ret[1:]
  721. }
  722. func (cfg Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
  723. func (cfg Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }
  724. func (cfg Config) defaultPeerHost() bool {
  725. return len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs
  726. }
  727. func (cfg Config) defaultClientHost() bool {
  728. return len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs
  729. }
  730. func (cfg *Config) ClientSelfCert() (err error) {
  731. if cfg.ClientAutoTLS && cfg.ClientTLSInfo.Empty() {
  732. chosts := make([]string, len(cfg.LCUrls))
  733. for i, u := range cfg.LCUrls {
  734. chosts[i] = u.Host
  735. }
  736. cfg.ClientTLSInfo, err = transport.SelfCert(cfg.logger, filepath.Join(cfg.Dir, "fixtures", "client"), chosts)
  737. return err
  738. } else if cfg.ClientAutoTLS {
  739. if cfg.logger != nil {
  740. cfg.logger.Warn("ignoring client auto TLS since certs given")
  741. } else {
  742. plog.Warningf("ignoring client auto TLS since certs given")
  743. }
  744. }
  745. return nil
  746. }
  747. func (cfg *Config) PeerSelfCert() (err error) {
  748. if cfg.PeerAutoTLS && cfg.PeerTLSInfo.Empty() {
  749. phosts := make([]string, len(cfg.LPUrls))
  750. for i, u := range cfg.LPUrls {
  751. phosts[i] = u.Host
  752. }
  753. cfg.PeerTLSInfo, err = transport.SelfCert(cfg.logger, filepath.Join(cfg.Dir, "fixtures", "peer"), phosts)
  754. return err
  755. } else if cfg.PeerAutoTLS {
  756. if cfg.logger != nil {
  757. cfg.logger.Warn("ignoring peer auto TLS since certs given")
  758. } else {
  759. plog.Warningf("ignoring peer auto TLS since certs given")
  760. }
  761. }
  762. return nil
  763. }
  764. // UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host,
  765. // if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
  766. // e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380
  767. // then the advertise peer host would be updated with machine's default host,
  768. // while keeping the listen URL's port.
  769. // User can work around this by explicitly setting URL with 127.0.0.1.
  770. // It returns the default hostname, if used, and the error, if any, from getting the machine's default host.
  771. // TODO: check whether fields are set instead of whether fields have default value
  772. func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error) {
  773. if defaultHostname == "" || defaultHostStatus != nil {
  774. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  775. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  776. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  777. }
  778. return "", defaultHostStatus
  779. }
  780. used := false
  781. pip, pport := cfg.LPUrls[0].Hostname(), cfg.LPUrls[0].Port()
  782. if cfg.defaultPeerHost() && pip == "0.0.0.0" {
  783. cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, pport)}
  784. used = true
  785. }
  786. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  787. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  788. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  789. }
  790. cip, cport := cfg.LCUrls[0].Hostname(), cfg.LCUrls[0].Port()
  791. if cfg.defaultClientHost() && cip == "0.0.0.0" {
  792. cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, cport)}
  793. used = true
  794. }
  795. dhost := defaultHostname
  796. if !used {
  797. dhost = ""
  798. }
  799. return dhost, defaultHostStatus
  800. }
  801. // checkBindURLs returns an error if any URL uses a domain name.
  802. func checkBindURLs(urls []url.URL) error {
  803. for _, url := range urls {
  804. if url.Scheme == "unix" || url.Scheme == "unixs" {
  805. continue
  806. }
  807. host, _, err := net.SplitHostPort(url.Host)
  808. if err != nil {
  809. return err
  810. }
  811. if host == "localhost" {
  812. // special case for local address
  813. // TODO: support /etc/hosts ?
  814. continue
  815. }
  816. if net.ParseIP(host) == nil {
  817. return fmt.Errorf("expected IP in URL for binding (%s)", url.String())
  818. }
  819. }
  820. return nil
  821. }
  822. func checkHostURLs(urls []url.URL) error {
  823. for _, url := range urls {
  824. host, _, err := net.SplitHostPort(url.Host)
  825. if err != nil {
  826. return err
  827. }
  828. if host == "" {
  829. return fmt.Errorf("unexpected empty host (%s)", url.String())
  830. }
  831. }
  832. return nil
  833. }
  834. func (cfg *Config) getAPURLs() (ss []string) {
  835. ss = make([]string, len(cfg.APUrls))
  836. for i := range cfg.APUrls {
  837. ss[i] = cfg.APUrls[i].String()
  838. }
  839. return ss
  840. }
  841. func (cfg *Config) getLPURLs() (ss []string) {
  842. ss = make([]string, len(cfg.LPUrls))
  843. for i := range cfg.LPUrls {
  844. ss[i] = cfg.LPUrls[i].String()
  845. }
  846. return ss
  847. }
  848. func (cfg *Config) getACURLs() (ss []string) {
  849. ss = make([]string, len(cfg.ACUrls))
  850. for i := range cfg.ACUrls {
  851. ss[i] = cfg.ACUrls[i].String()
  852. }
  853. return ss
  854. }
  855. func (cfg *Config) getLCURLs() (ss []string) {
  856. ss = make([]string, len(cfg.LCUrls))
  857. for i := range cfg.LCUrls {
  858. ss[i] = cfg.LCUrls[i].String()
  859. }
  860. return ss
  861. }
  862. func (cfg *Config) getMetricsURLs() (ss []string) {
  863. ss = make([]string, len(cfg.ListenMetricsUrls))
  864. for i := range cfg.ListenMetricsUrls {
  865. ss[i] = cfg.ListenMetricsUrls[i].String()
  866. }
  867. return ss
  868. }