Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go/vt/topo/consultopo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/spf13/pflag"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vterrors"
Expand Down Expand Up @@ -92,6 +93,10 @@ func getClientCreds() (creds map[string]*ClientAuthCred, err error) {
err = vterrors.Wrapf(err, "Error parsing consul_auth_static_file")
return creds, err
}
if len(creds) == 0 {
err = vterrors.New(vtrpc.Code_FAILED_PRECONDITION, "Found no credentials in consul_auth_static_file")
return creds, err
}
return creds, nil
}

Expand Down
52 changes: 34 additions & 18 deletions go/vt/topo/consultopo/server_flaky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ import (
"testing"
"time"

"vitess.io/vitess/go/vt/log"

"github.com/hashicorp/consul/api"

"vitess.io/vitess/go/testfiles"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/test"

Expand Down Expand Up @@ -297,25 +296,42 @@ func TestConsulTopoWithAuthFailure(t *testing.T) {

consulAuthClientStaticFile = tmpFile.Name()

jsonConfig := "{\"global\":{\"acl_token\":\"badtoken\"}}"
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
t.Fatalf("couldn't write temp file: %v", err)
}
// check valid, empty json causes error
{
jsonConfig := "{}"
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
t.Fatalf("couldn't write temp file: %v", err)
}

// Create the server on the new root.
ts, err := topo.OpenServer("consul", serverAddr, path.Join("globalRoot", topo.GlobalCell))
if err != nil {
t.Fatalf("OpenServer() failed: %v", err)
// Create the server on the new root.
_, err := topo.OpenServer("consul", serverAddr, path.Join("globalRoot", topo.GlobalCell))
if err == nil {
t.Fatal("Expected OpenServer() to return an error due to bad config, got nil")
}
}

// Attempt to Create the CellInfo.
err = ts.CreateCellInfo(context.Background(), test.LocalCellName, &topodatapb.CellInfo{
ServerAddress: serverAddr,
Root: path.Join("globalRoot", test.LocalCellName),
})
// check bad token causes error
{
jsonConfig := "{\"global\":{\"acl_token\":\"badtoken\"}}"
if err := os.WriteFile(tmpFile.Name(), []byte(jsonConfig), 0600); err != nil {
t.Fatalf("couldn't write temp file: %v", err)
}

// Create the server on the new root.
ts, err := topo.OpenServer("consul", serverAddr, path.Join("globalRoot", topo.GlobalCell))
if err != nil {
t.Fatalf("OpenServer() failed: %v", err)
}

// Attempt to Create the CellInfo.
err = ts.CreateCellInfo(context.Background(), test.LocalCellName, &topodatapb.CellInfo{
ServerAddress: serverAddr,
Root: path.Join("globalRoot", test.LocalCellName),
})

want := "Failed request: ACL not found"
if err == nil || err.Error() != want {
t.Errorf("Expected CreateCellInfo to fail: got %v, want %s", err, want)
want := "Failed request: ACL not found"
if err == nil || err.Error() != want {
t.Errorf("Expected CreateCellInfo to fail: got %v, want %s", err, want)
}
}
}
Loading