diff --git a/go/vt/topo/consultopo/server.go b/go/vt/topo/consultopo/server.go index 0a059b12f65..afa3e67e8b8 100644 --- a/go/vt/topo/consultopo/server.go +++ b/go/vt/topo/consultopo/server.go @@ -31,6 +31,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" @@ -104,6 +105,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 } diff --git a/go/vt/topo/consultopo/server_flaky_test.go b/go/vt/topo/consultopo/server_flaky_test.go index a987336dd01..3a3a6ad3205 100644 --- a/go/vt/topo/consultopo/server_flaky_test.go +++ b/go/vt/topo/consultopo/server_flaky_test.go @@ -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" @@ -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) + } } }