| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 1 | package consensus |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/x509" |
| 6 | "fmt" |
| 7 | "net" |
| 8 | "net/url" |
| 9 | "strconv" |
| 10 | "time" |
| 11 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 12 | clientv3 "go.etcd.io/etcd/client/v3" |
| 13 | "go.etcd.io/etcd/server/v3/embed" |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 14 | |
| 15 | "source.monogon.dev/metropolis/node" |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 16 | "source.monogon.dev/metropolis/node/core/localstorage" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 17 | "source.monogon.dev/osbase/pki" |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | // Config describes the startup configuration of a consensus instance. |
| 21 | type Config struct { |
| 22 | // Data directory (persistent, encrypted storage) for etcd. |
| 23 | Data *localstorage.DataEtcdDirectory |
| 24 | // Ephemeral directory for etcd. |
| 25 | Ephemeral *localstorage.EphemeralConsensusDirectory |
| 26 | |
| 27 | // JoinCluster is set if this instance is to join an existing cluster for the |
| 28 | // first time. If not set, it's assumed this instance has ran before and has all |
| 29 | // the state on disk required to become part of whatever cluster it was before. |
| 30 | // If that data is not present, a new cluster will be bootstrapped. |
| 31 | JoinCluster *JoinCluster |
| 32 | |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame^] | 33 | // NodeID is the node ID, which is also used to identify consensus nodes. |
| 34 | NodeID string |
| 35 | |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 36 | // NodePrivateKey is the node's main private key which is also used for |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame^] | 37 | // Metropolis PKI. The same key will be used for consensus nodes, but |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 38 | // different certificates will be used. |
| 39 | NodePrivateKey ed25519.PrivateKey |
| 40 | |
| 41 | testOverrides testOverrides |
| 42 | } |
| 43 | |
| 44 | // JoinCluster is all the data required for a node to join (for the first time) |
| 45 | // an already running cluster. This data is available from an already running |
| 46 | // consensus member by performing AddNode, which is called by the Curator when |
| 47 | // new etcd nodes are added to the cluster. |
| 48 | type JoinCluster struct { |
| 49 | CACertificate *x509.Certificate |
| 50 | NodeCertificate *x509.Certificate |
| 51 | // ExistingNodes are an arbitrarily ordered list of other consensus members that |
| 52 | // the node should attempt to contact. |
| 53 | ExistingNodes []ExistingNode |
| 54 | // InitialCRL is a certificate revocation list for this cluster. After the node |
| 55 | // starts, a CRL on disk will be maintained reflecting the PKI state within etcd. |
| 56 | InitialCRL *pki.CRL |
| 57 | } |
| 58 | |
| 59 | // ExistingNode is the peer URL and name of an already running consensus instance. |
| 60 | type ExistingNode struct { |
| 61 | Name string |
| 62 | URL string |
| 63 | } |
| 64 | |
| 65 | func (e *ExistingNode) connectionString() string { |
| 66 | return fmt.Sprintf("%s=%s", e.Name, e.URL) |
| 67 | } |
| 68 | |
| 69 | func (c *Config) nodePublicKey() ed25519.PublicKey { |
| 70 | return c.NodePrivateKey.Public().(ed25519.PublicKey) |
| 71 | } |
| 72 | |
| 73 | // testOverrides are available to test code to make some things easier in a test |
| 74 | // environment. |
| 75 | type testOverrides struct { |
| 76 | // externalPort overrides the default port used by the node. |
| 77 | externalPort int |
| 78 | // externalAddress overrides the address of the node, which is usually its ID. |
| 79 | externalAddress string |
| Tim Windelschmidt | c37a886 | 2023-07-19 16:33:21 +0200 | [diff] [blame] | 80 | // etcdMetricsPort overrides the default etcd metrics port used by the node. |
| 81 | etcdMetricsPort int |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // build takes a Config and returns an etcd embed.Config. |
| 85 | // |
| 86 | // enablePeers selects whether the etcd instance will listen for peer traffic |
| 87 | // over TLS. This requires TLS credentials to be present on disk, and will be |
| 88 | // disabled for bootstrapping the instance. |
| 89 | func (c *Config) build(enablePeers bool) *embed.Config { |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 90 | port := int(node.ConsensusPort) |
| 91 | if p := c.testOverrides.externalPort; p != 0 { |
| 92 | port = p |
| 93 | } |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame^] | 94 | host := c.NodeID |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 95 | if c.testOverrides.externalAddress != "" { |
| 96 | host = c.testOverrides.externalAddress |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 97 | } |
| Tim Windelschmidt | c37a886 | 2023-07-19 16:33:21 +0200 | [diff] [blame] | 98 | etcdPort := int(node.MetricsEtcdListenerPort) |
| 99 | if p := c.testOverrides.etcdMetricsPort; p != 0 { |
| 100 | etcdPort = p |
| 101 | } |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 102 | |
| 103 | cfg := embed.NewConfig() |
| 104 | |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame^] | 105 | cfg.Name = c.NodeID |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 106 | cfg.ClusterState = "existing" |
| 107 | cfg.InitialClusterToken = "METROPOLIS" |
| 108 | cfg.Logger = "zap" |
| 109 | cfg.LogOutputs = []string{c.Ephemeral.ServerLogsFIFO.FullPath()} |
| Tim Windelschmidt | c37a886 | 2023-07-19 16:33:21 +0200 | [diff] [blame] | 110 | cfg.ListenMetricsUrls = []url.URL{ |
| 111 | {Scheme: "http", Host: net.JoinHostPort("127.0.0.1", fmt.Sprintf("%d", etcdPort))}, |
| 112 | } |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 113 | |
| 114 | cfg.Dir = c.Data.Data.FullPath() |
| 115 | |
| 116 | // Client URL, ie. local UNIX socket to listen on for trusted, unauthenticated |
| 117 | // traffic. |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 118 | cfg.ListenClientUrls = []url.URL{{ |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 119 | Scheme: "unix", |
| 120 | Path: c.Ephemeral.ClientSocket.FullPath() + ":0", |
| 121 | }} |
| 122 | |
| 123 | if enablePeers { |
| 124 | cfg.PeerTLSInfo.CertFile = c.Data.PeerPKI.Certificate.FullPath() |
| 125 | cfg.PeerTLSInfo.KeyFile = c.Data.PeerPKI.Key.FullPath() |
| 126 | cfg.PeerTLSInfo.TrustedCAFile = c.Data.PeerPKI.CACertificate.FullPath() |
| 127 | cfg.PeerTLSInfo.ClientCertAuth = true |
| 128 | cfg.PeerTLSInfo.CRLFile = c.Data.PeerCRL.FullPath() |
| 129 | |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 130 | cfg.ListenPeerUrls = []url.URL{{ |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 131 | Scheme: "https", |
| 132 | Host: fmt.Sprintf("[::]:%d", port), |
| 133 | }} |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 134 | cfg.AdvertisePeerUrls = []url.URL{{ |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 135 | Scheme: "https", |
| 136 | Host: net.JoinHostPort(host, strconv.Itoa(port)), |
| 137 | }} |
| 138 | } else { |
| 139 | // When not enabling peer traffic, listen on loopback. We would not listen at |
| 140 | // all, but etcd seems to prevent us from doing that. |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 141 | cfg.ListenPeerUrls = []url.URL{{ |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 142 | Scheme: "http", |
| 143 | Host: fmt.Sprintf("127.0.0.1:%d", port), |
| 144 | }} |
| Lorenz Brun | 6211e4d | 2023-11-14 19:09:40 +0100 | [diff] [blame] | 145 | cfg.AdvertisePeerUrls = []url.URL{{ |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 146 | Scheme: "http", |
| 147 | Host: fmt.Sprintf("127.0.0.1:%d", port), |
| 148 | }} |
| 149 | } |
| 150 | |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame^] | 151 | cfg.InitialCluster = cfg.InitialClusterFromName(c.NodeID) |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 152 | if c.JoinCluster != nil { |
| 153 | for _, n := range c.JoinCluster.ExistingNodes { |
| 154 | cfg.InitialCluster += "," + n.connectionString() |
| 155 | } |
| 156 | } |
| 157 | return cfg |
| 158 | } |
| 159 | |
| 160 | // localClient returns an etcd client connected to the socket as configured in |
| 161 | // Config. |
| 162 | func (c *Config) localClient() (*clientv3.Client, error) { |
| 163 | socket := c.Ephemeral.ClientSocket.FullPath() |
| 164 | return clientv3.New(clientv3.Config{ |
| 165 | Endpoints: []string{fmt.Sprintf("unix://%s:0", socket)}, |
| Serge Bazanski | b76b8d1 | 2023-03-16 00:46:56 +0100 | [diff] [blame] | 166 | DialTimeout: 2 * time.Second, |
| Serge Bazanski | f05e80a | 2021-10-12 11:53:34 +0200 | [diff] [blame] | 167 | }) |
| 168 | } |