config.go 33 KB

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