config.go 34 KB

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