1
0
Fork 0

Join and Notify set credentials

master
Philip O'Toole 10 months ago
parent 58405f3c7c
commit 097382001d

@ -68,6 +68,7 @@ type Bootstrapper struct {
provider AddressProvider
client *Client
creds *Credentials
logger *log.Logger
Interval time.Duration
@ -87,6 +88,11 @@ func NewBootstrapper(p AddressProvider, client *Client) *Bootstrapper {
return bs
}
// SetCredentials sets the credentials for the Bootstrapper.
func (b *Bootstrapper) SetCredentials(creds *Credentials) {
b.creds = creds
}
// Boot performs the bootstrapping process for this node. This means it will
// ensure this node becomes part of a cluster. It does this by either joining
// an existing cluster by explicitly joining it through one of these nodes,
@ -107,6 +113,7 @@ func (b *Bootstrapper) Boot(id, raftAddr string, done func() bool, timeout time.
defer tickerT.Stop()
joiner := NewJoiner(b.client, numJoinAttempts, requestTimeout)
joiner.SetCredentials(b.creds)
for {
select {
case <-timeoutT.C:

@ -337,7 +337,7 @@ func (c *Client) Notify(nr *command.NotifyRequest, nodeAddr string, timeout time
}
// Join joins this node to a cluster at the remote address nodeAddr.
func (c *Client) Join(jr *command.JoinRequest, nodeAddr string, timeout time.Duration) error {
func (c *Client) Join(jr *command.JoinRequest, nodeAddr string, creds *Credentials, timeout time.Duration) error {
for {
conn, err := c.dial(nodeAddr, c.timeout)
if err != nil {
@ -351,6 +351,7 @@ func (c *Client) Join(jr *command.JoinRequest, nodeAddr string, timeout time.Dur
Request: &Command_JoinRequest{
JoinRequest: jr,
},
Credentials: creds,
}
if err := writeCommand(conn, command, timeout); err != nil {

@ -285,7 +285,7 @@ func Test_ClientJoinNode(t *testing.T) {
req := &command.JoinRequest{
Address: "test-node-addr",
}
err := c.Join(req, srv.Addr(), time.Second)
err := c.Join(req, srv.Addr(), nil, time.Second)
if err != nil {
t.Fatal(err)
}

@ -26,6 +26,7 @@ type Joiner struct {
attemptInterval time.Duration
client *Client
creds *Credentials
logger *log.Logger
}
@ -39,6 +40,11 @@ func NewJoiner(client *Client, numAttempts int, attemptInterval time.Duration) *
}
}
// SetCredentials sets the credentials for the Joiner.
func (j *Joiner) SetCredentials(creds *Credentials) {
j.creds = creds
}
// Do makes the actual join request. If the join is successful with any address,
// that address is returned. Otherwise, an error is returned.
func (j *Joiner) Do(targetAddrs []string, id, addr string, voter bool) (string, error) {
@ -75,7 +81,7 @@ func (j *Joiner) join(targetAddr, id, addr string, voter bool) (string, error) {
}
// Attempt to join.
if err := j.client.Join(req, targetAddr, time.Second); err != nil {
if err := j.client.Join(req, targetAddr, j.creds, time.Second); err != nil {
return "", err
}
return targetAddr, nil

@ -480,7 +480,7 @@ func Test_ServiceJoinNode(t *testing.T) {
req := &command.JoinRequest{
Address: expNodeAddr,
}
err := c.Join(req, s.Addr(), longWait)
err := c.Join(req, s.Addr(), nil, longWait)
if err != nil {
t.Fatalf("failed to join node: %s", err.Error())
}
@ -547,7 +547,7 @@ func Test_ServiceJoinNodeForwarded(t *testing.T) {
req := &command.JoinRequest{
Address: "some client",
}
err := c.Join(req, sF.Addr(), longWait)
err := c.Join(req, sF.Addr(), nil, longWait)
if err != nil {
t.Fatalf("failed to join node: %s", err.Error())
}

@ -356,7 +356,7 @@ func Test_NewServiceJoin(t *testing.T) {
// Test by connecting to itself.
c := NewClient(ml, 30*time.Second)
err := c.Join(jr, s.Addr(), 5*time.Second)
err := c.Join(jr, s.Addr(), nil, 5*time.Second)
if err != nil {
t.Fatalf("failed to join node: %s", err)
}
@ -366,7 +366,7 @@ func Test_NewServiceJoin(t *testing.T) {
// Test when auth is enabled
credStr.HasPermOK = false
err = c.Join(jr, s.Addr(), 5*time.Second)
err = c.Join(jr, s.Addr(), nil, 5*time.Second)
if err == nil {
t.Fatal("should have failed to join node due to lack of auth")
}

@ -476,6 +476,7 @@ func createCluster(cfg *Config, hasPeers bool, client *cluster.Client, str *stor
}
joiner := cluster.NewJoiner(client, cfg.JoinAttempts, cfg.JoinInterval)
joiner.SetCredentials(credentialsFor(credStr, cfg.JoinAs))
if joins != nil && cfg.BootstrapExpect == 0 {
// Explicit join operation requested, so do it.
j, err := joiner.Do(joins, str.ID(), cfg.RaftAdv, !cfg.RaftNonVoter)
@ -489,6 +490,7 @@ func createCluster(cfg *Config, hasPeers bool, client *cluster.Client, str *stor
if joins != nil && cfg.BootstrapExpect > 0 {
// Bootstrap with explicit join addresses requests.
bs := cluster.NewBootstrapper(cluster.NewAddressProviderString(joins), client)
bs.SetCredentials(credentialsFor(credStr, cfg.JoinAs))
return bs.Boot(str.ID(), cfg.RaftAdv, isClustered, cfg.BootstrapExpectTimeout)
}
@ -531,6 +533,7 @@ func createCluster(cfg *Config, hasPeers bool, client *cluster.Client, str *stor
}
bs := cluster.NewBootstrapper(provider, client)
bs.SetCredentials(credentialsFor(credStr, cfg.JoinAs))
httpServ.RegisterStatus("disco", provider)
return bs.Boot(str.ID(), cfg.RaftAdv, isClustered, cfg.BootstrapExpectTimeout)
@ -597,3 +600,14 @@ func networkCheckJoinAddrs(cfg *Config, joinAddrs []string) error {
}
return nil
}
func credentialsFor(credStr *auth.CredentialsStore, username string) *cluster.Credentials {
pw, ok := credStr.Password(username)
if !ok {
return nil
}
return &cluster.Credentials{
Username: username,
Password: pw,
}
}

@ -671,7 +671,8 @@ func mustNodeEncrypted(dir string, enableSingle, httpEncrypt bool, mux *tcp.Mux,
node.RaftAddr = node.Store.Addr()
node.ID = node.Store.ID()
clstr := cluster.New(mux.Listen(cluster.MuxClusterHeader), node.Store, node.Store, mustNewMockCredentialStore())
credStr := mustNewMockCredentialStore()
clstr := cluster.New(mux.Listen(cluster.MuxClusterHeader), node.Store, node.Store, credStr)
if err := clstr.Open(); err != nil {
panic("failed to open Cluster service)")
}

Loading…
Cancel
Save