func checkUpdateFails(t *testing.T, network, oldAddrString, newAddrString string) { oldAddr := util.MakeRawAddr(network, oldAddrString) newAddr := util.MakeRawAddr(network, newAddrString) retAddr, err := updatedAddr(oldAddr, newAddr) if err == nil { t.Fatalf("updatedAddr(%v, %v) should have failed; instead returned %v", oldAddr, newAddr, retAddr) } }
func checkUpdateMatches(t *testing.T, network, oldAddrString, newAddrString, expAddrString string) { oldAddr := util.MakeRawAddr(network, oldAddrString) newAddr := util.MakeRawAddr(network, newAddrString) expAddr := util.MakeRawAddr(network, expAddrString) retAddr, err := updatedAddr(oldAddr, newAddr) if err != nil { t.Fatalf("updatedAddr failed on %v, %v: %v", oldAddr, newAddr, err) } if retAddr.String() != expAddrString { t.Fatalf("updatedAddr(%v, %v) was %s; expected %s", oldAddr, newAddr, retAddr, expAddr) } }
// parseBootstrapAddresses parses the gossip bootstrap addresses // passed via -gossip command line flag. func (g *Gossip) parseBootstrapAddresses() { g.mu.Lock() defer g.mu.Unlock() if *GossipBootstrap != "" { addresses := strings.Split(*GossipBootstrap, ",") for _, addr := range addresses { addr = strings.TrimSpace(addr) _, err := net.ResolveTCPAddr("tcp", addr) if err != nil { log.Errorf("invalid gossip bootstrap address %s: %s", addr, err) continue } g.bootstraps.addAddr(util.MakeRawAddr("tcp", addr)) } } // If we have no bootstrap hosts, fatal exit. if g.bootstraps.len() == 0 { log.Fatalf("no hosts specified for gossip network (use -gossip)") } // Remove our own node address. if g.bootstraps.hasAddr(g.is.NodeAddr) { g.isBootstrap = true g.bootstraps.removeAddr(g.is.NodeAddr) } }
func newServer() (*server, error) { // Determine hostname in case it hasn't been specified in -rpc or -http. host, err := os.Hostname() if err != nil { host = "127.0.0.1" } // Resolve if strings.HasPrefix(*rpcAddr, ":") { *rpcAddr = host + *rpcAddr } _, err = net.ResolveTCPAddr("tcp", *rpcAddr) if err != nil { return nil, util.Errorf("unable to resolve RPC address %q: %v", *rpcAddr, err) } var tlsConfig *rpc.TLSConfig if *certDir == "" { tlsConfig = rpc.LoadInsecureTLSConfig() } else { var err error if tlsConfig, err = rpc.LoadTLSConfig(*certDir); err != nil { return nil, util.Errorf("unable to load TLS config: %v", err) } } s := &server{ host: host, mux: http.NewServeMux(), clock: hlc.NewClock(hlc.UnixNano), rpc: rpc.NewServer(util.MakeRawAddr("tcp", *rpcAddr), tlsConfig), } s.clock.SetMaxDrift(*maxDrift) s.gossip = gossip.New(tlsConfig) s.kvDB = kv.NewDB(kv.NewDistKV(s.gossip), s.clock) s.kvREST = rest.NewRESTServer(s.kvDB) s.node = NewNode(s.kvDB, s.gossip) s.admin = newAdminServer(s.kvDB) s.status = newStatusServer(s.kvDB) s.structuredDB = structured.NewDB(s.kvDB) s.structuredREST = structured.NewRESTServer(s.structuredDB) return s, nil }
// updatedAddr returns our "official" address based on the address we asked for // (oldAddr) and the address we successfully bound to (newAddr). It's kind of // hacky, but necessary to make TLS work. func updatedAddr(oldAddr, newAddr net.Addr) (net.Addr, error) { switch oldAddr.Network() { case "tcp", "tcp4", "tcp6": // After binding, it's possible that our host and/or port will be // different from what we requested. If the hostname is different, we // want to keep the original one since it's more likely to match our // TLS certificate. But if the port is different, it should be because // we asked for ":0" and got an arbitrary unused port; that needs to be // reflected in our addr. oldHost, oldPort, err := net.SplitHostPort(oldAddr.String()) if err != nil { return nil, fmt.Errorf("Unable to parse original addr '%s': %v", oldAddr.String(), err) } _, newPort, err := net.SplitHostPort(newAddr.String()) if err != nil { return nil, fmt.Errorf("Unable to parse new addr '%s': %v", newAddr.String(), err) } if newPort != oldPort && oldPort != "0" { log.Warningf("Asked for port %s, got %s", oldPort, newPort) } return util.MakeRawAddr("tcp", net.JoinHostPort(oldHost, newPort)), nil case "unix": if oldAddr.String() != newAddr.String() { return nil, fmt.Errorf("Asked for unix addr %s, got %s", oldAddr, newAddr) } return newAddr, nil default: return nil, fmt.Errorf("Unexpected network type: %s", oldAddr.Network()) } }