config.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. "strings"
  25. "time"
  26. "github.com/coreos/etcd/compactor"
  27. "github.com/coreos/etcd/etcdserver"
  28. "github.com/coreos/etcd/pkg/flags"
  29. "github.com/coreos/etcd/pkg/netutil"
  30. "github.com/coreos/etcd/pkg/srv"
  31. "github.com/coreos/etcd/pkg/transport"
  32. "github.com/coreos/etcd/pkg/types"
  33. "github.com/coreos/pkg/capnslog"
  34. "github.com/ghodss/yaml"
  35. "go.uber.org/zap"
  36. "google.golang.org/grpc"
  37. "google.golang.org/grpc/grpclog"
  38. )
  39. const (
  40. ClusterStateFlagNew = "new"
  41. ClusterStateFlagExisting = "existing"
  42. DefaultName = "default"
  43. DefaultMaxSnapshots = 5
  44. DefaultMaxWALs = 5
  45. DefaultMaxTxnOps = uint(128)
  46. DefaultMaxRequestBytes = 1.5 * 1024 * 1024
  47. DefaultGRPCKeepAliveMinTime = 5 * time.Second
  48. DefaultGRPCKeepAliveInterval = 2 * time.Hour
  49. DefaultGRPCKeepAliveTimeout = 20 * time.Second
  50. DefaultListenPeerURLs = "http://localhost:2380"
  51. DefaultListenClientURLs = "http://localhost:2379"
  52. DefaultLogOutput = "default"
  53. // DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
  54. // It's enabled by default.
  55. DefaultStrictReconfigCheck = true
  56. // DefaultEnableV2 is the default value for "--enable-v2" flag.
  57. // v2 is enabled by default.
  58. // TODO: disable v2 when deprecated.
  59. DefaultEnableV2 = true
  60. // maxElectionMs specifies the maximum value of election timeout.
  61. // More details are listed in ../Documentation/tuning.md#time-parameters.
  62. maxElectionMs = 50000
  63. )
  64. var (
  65. ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
  66. "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"")
  67. ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
  68. DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
  69. DefaultAdvertiseClientURLs = "http://localhost:2379"
  70. defaultHostname string
  71. defaultHostStatus error
  72. )
  73. var (
  74. // CompactorModePeriodic is periodic compaction mode
  75. // for "Config.AutoCompactionMode" field.
  76. // If "AutoCompactionMode" is CompactorModePeriodic and
  77. // "AutoCompactionRetention" is "1h", it automatically compacts
  78. // compacts storage every hour.
  79. CompactorModePeriodic = compactor.ModePeriodic
  80. // CompactorModeRevision is revision-based compaction mode
  81. // for "Config.AutoCompactionMode" field.
  82. // If "AutoCompactionMode" is CompactorModeRevision and
  83. // "AutoCompactionRetention" is "1000", it compacts log on
  84. // revision 5000 when the current revision is 6000.
  85. // This runs every 5-minute if enough of logs have proceeded.
  86. CompactorModeRevision = compactor.ModeRevision
  87. )
  88. func init() {
  89. defaultHostname, defaultHostStatus = netutil.GetDefaultHost()
  90. }
  91. // Config holds the arguments for configuring an etcd server.
  92. type Config struct {
  93. LPUrls, LCUrls []url.URL
  94. Dir string `json:"data-dir"`
  95. WalDir string `json:"wal-dir"`
  96. MaxSnapFiles uint `json:"max-snapshots"`
  97. MaxWalFiles uint `json:"max-wals"`
  98. Name string `json:"name"`
  99. SnapCount uint64 `json:"snapshot-count"`
  100. // AutoCompactionMode is either 'periodic' or 'revision'.
  101. AutoCompactionMode string `json:"auto-compaction-mode"`
  102. // AutoCompactionRetention is either duration string with time unit
  103. // (e.g. '5m' for 5-minute), or revision unit (e.g. '5000').
  104. // If no time unit is provided and compaction mode is 'periodic',
  105. // the unit defaults to hour. For example, '5' translates into 5-hour.
  106. AutoCompactionRetention string `json:"auto-compaction-retention"`
  107. // TickMs is the number of milliseconds between heartbeat ticks.
  108. // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
  109. // make ticks a cluster wide configuration.
  110. TickMs uint `json:"heartbeat-interval"`
  111. ElectionMs uint `json:"election-timeout"`
  112. QuotaBackendBytes int64 `json:"quota-backend-bytes"`
  113. MaxTxnOps uint `json:"max-txn-ops"`
  114. MaxRequestBytes uint `json:"max-request-bytes"`
  115. // GRPCKeepAliveMinTime is the minimum interval that a client should
  116. // wait before pinging server. When client pings "too fast", server
  117. // sends goaway and closes the connection (errors: too_many_pings,
  118. // http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
  119. // Server expects client pings only when there is any active streams
  120. // (PermitWithoutStream is set false).
  121. GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
  122. // GRPCKeepAliveInterval is the frequency of server-to-client ping
  123. // to check if a connection is alive. Close a non-responsive connection
  124. // after an additional duration of Timeout. 0 to disable.
  125. GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
  126. // GRPCKeepAliveTimeout is the additional duration of wait
  127. // before closing a non-responsive connection. 0 to disable.
  128. GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`
  129. APUrls, ACUrls []url.URL
  130. ClusterState string `json:"initial-cluster-state"`
  131. DNSCluster string `json:"discovery-srv"`
  132. DNSClusterServiceName string `json:"discovery-srv-name"`
  133. Dproxy string `json:"discovery-proxy"`
  134. Durl string `json:"discovery"`
  135. InitialCluster string `json:"initial-cluster"`
  136. InitialClusterToken string `json:"initial-cluster-token"`
  137. StrictReconfigCheck bool `json:"strict-reconfig-check"`
  138. EnableV2 bool `json:"enable-v2"`
  139. // PreVote is true to enable Raft Pre-Vote.
  140. // If enabled, Raft runs an additional election phase
  141. // to check whether it would get enough votes to win
  142. // an election, thus minimizing disruptions.
  143. // TODO: enable by default in 3.5.
  144. PreVote bool `json:"pre-vote"`
  145. ClientTLSInfo transport.TLSInfo
  146. ClientAutoTLS bool
  147. PeerTLSInfo transport.TLSInfo
  148. PeerAutoTLS bool
  149. CORS map[string]struct{}
  150. // HostWhitelist lists acceptable hostnames from HTTP client requests.
  151. // Client origin policy protects against "DNS Rebinding" attacks
  152. // to insecure etcd servers. That is, any website can simply create
  153. // an authorized DNS name, and direct DNS to "localhost" (or any
  154. // other address). Then, all HTTP endpoints of etcd server listening
  155. // on "localhost" becomes accessible, thus vulnerable to DNS rebinding
  156. // attacks. See "CVE-2018-5702" for more detail.
  157. //
  158. // 1. If client connection is secure via HTTPS, allow any hostnames.
  159. // 2. If client connection is not secure and "HostWhitelist" is not empty,
  160. // only allow HTTP requests whose Host field is listed in whitelist.
  161. //
  162. // Note that the client origin policy is enforced whether authentication
  163. // is enabled or not, for tighter controls.
  164. //
  165. // By default, "HostWhitelist" is "*", which allows any hostnames.
  166. // Note that when specifying hostnames, loopback addresses are not added
  167. // automatically. To allow loopback interfaces, leave it empty or set it "*",
  168. // or add them to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
  169. //
  170. // CVE-2018-5702 reference:
  171. // - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
  172. // - https://github.com/transmission/transmission/pull/468
  173. // - https://github.com/coreos/etcd/issues/9353
  174. HostWhitelist map[string]struct{}
  175. // Logger logs server-side operations.
  176. // If nil, all logs are discarded.
  177. // TODO: make it configurable with existing logger.
  178. // Currently, only logs TLS transport.
  179. Logger *zap.Logger
  180. Debug bool `json:"debug"`
  181. LogPkgLevels string `json:"log-package-levels"`
  182. LogOutput string `json:"log-output"`
  183. EnablePprof bool `json:"enable-pprof"`
  184. Metrics string `json:"metrics"`
  185. ListenMetricsUrls []url.URL
  186. ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`
  187. // UserHandlers is for registering users handlers and only used for
  188. // embedding etcd into other applications.
  189. // The map key is the route path for the handler, and
  190. // you must ensure it can't be conflicted with etcd's.
  191. UserHandlers map[string]http.Handler `json:"-"`
  192. // ServiceRegister is for registering users' gRPC services. A simple usage example:
  193. // cfg := embed.NewConfig()
  194. // cfg.ServerRegister = func(s *grpc.Server) {
  195. // pb.RegisterFooServer(s, &fooServer{})
  196. // pb.RegisterBarServer(s, &barServer{})
  197. // }
  198. // embed.StartEtcd(cfg)
  199. ServiceRegister func(*grpc.Server) `json:"-"`
  200. AuthToken string `json:"auth-token"`
  201. ExperimentalInitialCorruptCheck bool `json:"experimental-initial-corrupt-check"`
  202. ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
  203. ExperimentalEnableV2V3 string `json:"experimental-enable-v2v3"`
  204. // ForceNewCluster starts a new cluster even if previously started; unsafe.
  205. ForceNewCluster bool `json:"force-new-cluster"`
  206. }
  207. // configYAML holds the config suitable for yaml parsing
  208. type configYAML struct {
  209. Config
  210. configJSON
  211. }
  212. // configJSON has file options that are translated into Config options
  213. type configJSON struct {
  214. LPUrlsJSON string `json:"listen-peer-urls"`
  215. LCUrlsJSON string `json:"listen-client-urls"`
  216. APUrlsJSON string `json:"initial-advertise-peer-urls"`
  217. ACUrlsJSON string `json:"advertise-client-urls"`
  218. CORSJSON string `json:"cors"`
  219. HostWhitelistJSON string `json:"host-whitelist"`
  220. ClientSecurityJSON securityConfig `json:"client-transport-security"`
  221. PeerSecurityJSON securityConfig `json:"peer-transport-security"`
  222. }
  223. type securityConfig struct {
  224. CertFile string `json:"cert-file"`
  225. KeyFile string `json:"key-file"`
  226. CertAuth bool `json:"client-cert-auth"`
  227. TrustedCAFile string `json:"trusted-ca-file"`
  228. AutoTLS bool `json:"auto-tls"`
  229. }
  230. // NewConfig creates a new Config populated with default values.
  231. func NewConfig() *Config {
  232. lpurl, _ := url.Parse(DefaultListenPeerURLs)
  233. apurl, _ := url.Parse(DefaultInitialAdvertisePeerURLs)
  234. lcurl, _ := url.Parse(DefaultListenClientURLs)
  235. acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
  236. lg, _ := zap.NewProduction()
  237. cfg := &Config{
  238. MaxSnapFiles: DefaultMaxSnapshots,
  239. MaxWalFiles: DefaultMaxWALs,
  240. Name: DefaultName,
  241. SnapCount: etcdserver.DefaultSnapCount,
  242. MaxTxnOps: DefaultMaxTxnOps,
  243. MaxRequestBytes: DefaultMaxRequestBytes,
  244. GRPCKeepAliveMinTime: DefaultGRPCKeepAliveMinTime,
  245. GRPCKeepAliveInterval: DefaultGRPCKeepAliveInterval,
  246. GRPCKeepAliveTimeout: DefaultGRPCKeepAliveTimeout,
  247. TickMs: 100,
  248. ElectionMs: 1000,
  249. LPUrls: []url.URL{*lpurl},
  250. LCUrls: []url.URL{*lcurl},
  251. APUrls: []url.URL{*apurl},
  252. ACUrls: []url.URL{*acurl},
  253. ClusterState: ClusterStateFlagNew,
  254. InitialClusterToken: "etcd-cluster",
  255. StrictReconfigCheck: DefaultStrictReconfigCheck,
  256. Logger: lg,
  257. LogOutput: DefaultLogOutput,
  258. Metrics: "basic",
  259. EnableV2: DefaultEnableV2,
  260. CORS: map[string]struct{}{"*": {}},
  261. HostWhitelist: map[string]struct{}{"*": {}},
  262. AuthToken: "simple",
  263. PreVote: false, // TODO: enable by default in v3.5
  264. }
  265. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  266. return cfg
  267. }
  268. func logTLSHandshakeFailure(conn *tls.Conn, err error) {
  269. state := conn.ConnectionState()
  270. remoteAddr := conn.RemoteAddr().String()
  271. serverName := state.ServerName
  272. if len(state.PeerCertificates) > 0 {
  273. cert := state.PeerCertificates[0]
  274. ips, dns := cert.IPAddresses, cert.DNSNames
  275. plog.Infof("rejected connection from %q (error %q, ServerName %q, IPAddresses %q, DNSNames %q)", remoteAddr, err.Error(), serverName, ips, dns)
  276. } else {
  277. plog.Infof("rejected connection from %q (error %q, ServerName %q)", remoteAddr, err.Error(), serverName)
  278. }
  279. }
  280. // SetupLogging initializes etcd logging.
  281. // Must be called after flag parsing.
  282. func (cfg *Config) SetupLogging() {
  283. cfg.ClientTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  284. cfg.PeerTLSInfo.HandshakeFailure = logTLSHandshakeFailure
  285. capnslog.SetGlobalLogLevel(capnslog.INFO)
  286. if cfg.Debug {
  287. cfg.Logger = zap.NewExample()
  288. capnslog.SetGlobalLogLevel(capnslog.DEBUG)
  289. grpc.EnableTracing = true
  290. // enable info, warning, error
  291. grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  292. } else {
  293. // only discard info
  294. grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, os.Stderr, os.Stderr))
  295. }
  296. if cfg.LogPkgLevels != "" {
  297. repoLog := capnslog.MustRepoLogger("github.com/coreos/etcd")
  298. settings, err := repoLog.ParseLogLevelConfig(cfg.LogPkgLevels)
  299. if err != nil {
  300. plog.Warningf("couldn't parse log level string: %s, continuing with default levels", err.Error())
  301. return
  302. }
  303. repoLog.SetLogLevel(settings)
  304. }
  305. // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
  306. // where NewDefaultFormatter returns NewJournaldFormatter when syscall.Getppid() == 1
  307. // specify 'stdout' or 'stderr' to skip journald logging even when running under systemd
  308. switch cfg.LogOutput {
  309. case "stdout":
  310. capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug))
  311. case "stderr":
  312. capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, cfg.Debug))
  313. case DefaultLogOutput:
  314. default:
  315. plog.Panicf(`unknown log-output %q (only supports %q, "stdout", "stderr")`, cfg.LogOutput, DefaultLogOutput)
  316. }
  317. }
  318. func ConfigFromFile(path string) (*Config, error) {
  319. cfg := &configYAML{Config: *NewConfig()}
  320. if err := cfg.configFromFile(path); err != nil {
  321. return nil, err
  322. }
  323. return &cfg.Config, nil
  324. }
  325. func (cfg *configYAML) configFromFile(path string) error {
  326. b, err := ioutil.ReadFile(path)
  327. if err != nil {
  328. return err
  329. }
  330. defaultInitialCluster := cfg.InitialCluster
  331. err = yaml.Unmarshal(b, cfg)
  332. if err != nil {
  333. return err
  334. }
  335. if cfg.LPUrlsJSON != "" {
  336. u, err := types.NewURLs(strings.Split(cfg.LPUrlsJSON, ","))
  337. if err != nil {
  338. plog.Fatalf("unexpected error setting up listen-peer-urls: %v", err)
  339. }
  340. cfg.LPUrls = []url.URL(u)
  341. }
  342. if cfg.LCUrlsJSON != "" {
  343. u, err := types.NewURLs(strings.Split(cfg.LCUrlsJSON, ","))
  344. if err != nil {
  345. plog.Fatalf("unexpected error setting up listen-client-urls: %v", err)
  346. }
  347. cfg.LCUrls = []url.URL(u)
  348. }
  349. if cfg.APUrlsJSON != "" {
  350. u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
  351. if err != nil {
  352. plog.Fatalf("unexpected error setting up initial-advertise-peer-urls: %v", err)
  353. }
  354. cfg.APUrls = []url.URL(u)
  355. }
  356. if cfg.ACUrlsJSON != "" {
  357. u, err := types.NewURLs(strings.Split(cfg.ACUrlsJSON, ","))
  358. if err != nil {
  359. plog.Fatalf("unexpected error setting up advertise-peer-urls: %v", err)
  360. }
  361. cfg.ACUrls = []url.URL(u)
  362. }
  363. if cfg.ListenMetricsUrlsJSON != "" {
  364. u, err := types.NewURLs(strings.Split(cfg.ListenMetricsUrlsJSON, ","))
  365. if err != nil {
  366. plog.Fatalf("unexpected error setting up listen-metrics-urls: %v", err)
  367. }
  368. cfg.ListenMetricsUrls = []url.URL(u)
  369. }
  370. if cfg.CORSJSON != "" {
  371. uv := flags.NewUniqueURLsWithExceptions(cfg.CORSJSON, "*")
  372. cfg.CORS = uv.Values
  373. }
  374. if cfg.HostWhitelistJSON != "" {
  375. uv := flags.NewUniqueStringsValue(cfg.HostWhitelistJSON)
  376. cfg.HostWhitelist = uv.Values
  377. }
  378. // If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
  379. if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
  380. cfg.InitialCluster = ""
  381. }
  382. if cfg.ClusterState == "" {
  383. cfg.ClusterState = ClusterStateFlagNew
  384. }
  385. copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
  386. tls.CertFile = ysc.CertFile
  387. tls.KeyFile = ysc.KeyFile
  388. tls.ClientCertAuth = ysc.CertAuth
  389. tls.TrustedCAFile = ysc.TrustedCAFile
  390. }
  391. copySecurityDetails(&cfg.ClientTLSInfo, &cfg.ClientSecurityJSON)
  392. copySecurityDetails(&cfg.PeerTLSInfo, &cfg.PeerSecurityJSON)
  393. cfg.ClientAutoTLS = cfg.ClientSecurityJSON.AutoTLS
  394. cfg.PeerAutoTLS = cfg.PeerSecurityJSON.AutoTLS
  395. return cfg.Validate()
  396. }
  397. // Validate ensures that '*embed.Config' fields are properly configured.
  398. func (cfg *Config) Validate() error {
  399. if err := checkBindURLs(cfg.LPUrls); err != nil {
  400. return err
  401. }
  402. if err := checkBindURLs(cfg.LCUrls); err != nil {
  403. return err
  404. }
  405. if err := checkBindURLs(cfg.ListenMetricsUrls); err != nil {
  406. return err
  407. }
  408. if err := checkHostURLs(cfg.APUrls); err != nil {
  409. addrs := make([]string, len(cfg.APUrls))
  410. for i := range cfg.APUrls {
  411. addrs[i] = cfg.APUrls[i].String()
  412. }
  413. return fmt.Errorf(`--initial-advertise-peer-urls %q must be "host:port" (%v)`, strings.Join(addrs, ","), err)
  414. }
  415. if err := checkHostURLs(cfg.ACUrls); err != nil {
  416. addrs := make([]string, len(cfg.ACUrls))
  417. for i := range cfg.ACUrls {
  418. addrs[i] = cfg.ACUrls[i].String()
  419. }
  420. return fmt.Errorf(`--advertise-client-urls %q must be "host:port" (%v)`, strings.Join(addrs, ","), err)
  421. }
  422. // Check if conflicting flags are passed.
  423. nSet := 0
  424. for _, v := range []bool{cfg.Durl != "", cfg.InitialCluster != "", cfg.DNSCluster != ""} {
  425. if v {
  426. nSet++
  427. }
  428. }
  429. if cfg.ClusterState != ClusterStateFlagNew && cfg.ClusterState != ClusterStateFlagExisting {
  430. return fmt.Errorf("unexpected clusterState %q", cfg.ClusterState)
  431. }
  432. if nSet > 1 {
  433. return ErrConflictBootstrapFlags
  434. }
  435. if cfg.TickMs <= 0 {
  436. return fmt.Errorf("--heartbeat-interval must be >0 (set to %dms)", cfg.TickMs)
  437. }
  438. if cfg.ElectionMs <= 0 {
  439. return fmt.Errorf("--election-timeout must be >0 (set to %dms)", cfg.ElectionMs)
  440. }
  441. if 5*cfg.TickMs > cfg.ElectionMs {
  442. return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
  443. }
  444. if cfg.ElectionMs > maxElectionMs {
  445. return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
  446. }
  447. // check this last since proxying in etcdmain may make this OK
  448. if cfg.LCUrls != nil && cfg.ACUrls == nil {
  449. return ErrUnsetAdvertiseClientURLsFlag
  450. }
  451. switch cfg.AutoCompactionMode {
  452. case "":
  453. case CompactorModeRevision, CompactorModePeriodic:
  454. default:
  455. return fmt.Errorf("unknown auto-compaction-mode %q", cfg.AutoCompactionMode)
  456. }
  457. return nil
  458. }
  459. // PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
  460. func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error) {
  461. token = cfg.InitialClusterToken
  462. switch {
  463. case cfg.Durl != "":
  464. urlsmap = types.URLsMap{}
  465. // If using discovery, generate a temporary cluster based on
  466. // self's advertised peer URLs
  467. urlsmap[cfg.Name] = cfg.APUrls
  468. token = cfg.Durl
  469. case cfg.DNSCluster != "":
  470. clusterStrs, cerr := cfg.GetDNSClusterNames()
  471. if cerr != nil {
  472. plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
  473. return nil, "", cerr
  474. }
  475. for _, s := range clusterStrs {
  476. plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
  477. }
  478. clusterStr := strings.Join(clusterStrs, ",")
  479. if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.TrustedCAFile == "" {
  480. cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
  481. }
  482. urlsmap, err = types.NewURLsMap(clusterStr)
  483. // only etcd member must belong to the discovered cluster.
  484. // proxy does not need to belong to the discovered cluster.
  485. if which == "etcd" {
  486. if _, ok := urlsmap[cfg.Name]; !ok {
  487. return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.Name)
  488. }
  489. }
  490. default:
  491. // We're statically configured, and cluster has appropriately been set.
  492. urlsmap, err = types.NewURLsMap(cfg.InitialCluster)
  493. }
  494. return urlsmap, token, err
  495. }
  496. // GetDNSClusterNames uses DNS SRV records to get a list of initial nodes for cluster bootstrapping.
  497. func (cfg *Config) GetDNSClusterNames() ([]string, error) {
  498. var (
  499. clusterStrs []string
  500. cerr error
  501. serviceNameSuffix string
  502. )
  503. if cfg.DNSClusterServiceName != "" {
  504. serviceNameSuffix = "-" + cfg.DNSClusterServiceName
  505. }
  506. // Use both etcd-server-ssl and etcd-server for discovery. Combine the results if both are available.
  507. clusterStrs, cerr = srv.GetCluster("https", "etcd-server-ssl"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.APUrls)
  508. defaultHTTPClusterStrs, httpCerr := srv.GetCluster("http", "etcd-server"+serviceNameSuffix, cfg.Name, cfg.DNSCluster, cfg.APUrls)
  509. if cerr != nil {
  510. clusterStrs = make([]string, 0)
  511. }
  512. if httpCerr != nil {
  513. clusterStrs = append(clusterStrs, defaultHTTPClusterStrs...)
  514. }
  515. return clusterStrs, cerr
  516. }
  517. func (cfg Config) InitialClusterFromName(name string) (ret string) {
  518. if len(cfg.APUrls) == 0 {
  519. return ""
  520. }
  521. n := name
  522. if name == "" {
  523. n = DefaultName
  524. }
  525. for i := range cfg.APUrls {
  526. ret = ret + "," + n + "=" + cfg.APUrls[i].String()
  527. }
  528. return ret[1:]
  529. }
  530. func (cfg Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
  531. func (cfg Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }
  532. func (cfg Config) defaultPeerHost() bool {
  533. return len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs
  534. }
  535. func (cfg Config) defaultClientHost() bool {
  536. return len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs
  537. }
  538. func (cfg *Config) ClientSelfCert() (err error) {
  539. if cfg.ClientAutoTLS && cfg.ClientTLSInfo.Empty() {
  540. chosts := make([]string, len(cfg.LCUrls))
  541. for i, u := range cfg.LCUrls {
  542. chosts[i] = u.Host
  543. }
  544. cfg.ClientTLSInfo, err = transport.SelfCert(cfg.Logger, filepath.Join(cfg.Dir, "fixtures", "client"), chosts)
  545. return err
  546. } else if cfg.ClientAutoTLS {
  547. plog.Warningf("ignoring client auto TLS since certs given")
  548. }
  549. return nil
  550. }
  551. func (cfg *Config) PeerSelfCert() (err error) {
  552. if cfg.PeerAutoTLS && cfg.PeerTLSInfo.Empty() {
  553. phosts := make([]string, len(cfg.LPUrls))
  554. for i, u := range cfg.LPUrls {
  555. phosts[i] = u.Host
  556. }
  557. cfg.PeerTLSInfo, err = transport.SelfCert(cfg.Logger, filepath.Join(cfg.Dir, "fixtures", "peer"), phosts)
  558. return err
  559. } else if cfg.PeerAutoTLS {
  560. plog.Warningf("ignoring peer auto TLS since certs given")
  561. }
  562. return nil
  563. }
  564. // UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host,
  565. // if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
  566. // e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380
  567. // then the advertise peer host would be updated with machine's default host,
  568. // while keeping the listen URL's port.
  569. // User can work around this by explicitly setting URL with 127.0.0.1.
  570. // It returns the default hostname, if used, and the error, if any, from getting the machine's default host.
  571. // TODO: check whether fields are set instead of whether fields have default value
  572. func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error) {
  573. if defaultHostname == "" || defaultHostStatus != nil {
  574. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  575. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  576. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  577. }
  578. return "", defaultHostStatus
  579. }
  580. used := false
  581. pip, pport := cfg.LPUrls[0].Hostname(), cfg.LPUrls[0].Port()
  582. if cfg.defaultPeerHost() && pip == "0.0.0.0" {
  583. cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, pport)}
  584. used = true
  585. }
  586. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  587. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  588. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  589. }
  590. cip, cport := cfg.LCUrls[0].Hostname(), cfg.LCUrls[0].Port()
  591. if cfg.defaultClientHost() && cip == "0.0.0.0" {
  592. cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, cport)}
  593. used = true
  594. }
  595. dhost := defaultHostname
  596. if !used {
  597. dhost = ""
  598. }
  599. return dhost, defaultHostStatus
  600. }
  601. // checkBindURLs returns an error if any URL uses a domain name.
  602. func checkBindURLs(urls []url.URL) error {
  603. for _, url := range urls {
  604. if url.Scheme == "unix" || url.Scheme == "unixs" {
  605. continue
  606. }
  607. host, _, err := net.SplitHostPort(url.Host)
  608. if err != nil {
  609. return err
  610. }
  611. if host == "localhost" {
  612. // special case for local address
  613. // TODO: support /etc/hosts ?
  614. continue
  615. }
  616. if net.ParseIP(host) == nil {
  617. return fmt.Errorf("expected IP in URL for binding (%s)", url.String())
  618. }
  619. }
  620. return nil
  621. }
  622. func checkHostURLs(urls []url.URL) error {
  623. for _, url := range urls {
  624. host, _, err := net.SplitHostPort(url.Host)
  625. if err != nil {
  626. return err
  627. }
  628. if host == "" {
  629. return fmt.Errorf("unexpected empty host (%s)", url.String())
  630. }
  631. }
  632. return nil
  633. }