Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestSysRekey_Verification would fail sometimes when recovery=true #7710

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 4 additions & 7 deletions vault/external_tests/api/sys_rekey_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,9 @@ func testSysRekey_Verification(t *testing.T, recovery bool, legacyShamir bool) {
// Sealing should clear state, so after this we should be able to perform
// the above again
cluster.EnsureCoresSealed(t)
if recovery {
cluster.UnsealWithStoredKeys(t)
} else {
cluster.UnsealCores(t)
if err := cluster.UnsealCoresWithError(recovery); err != nil {
t.Fatal(err)
}
vault.TestWaitActive(t, cluster.Cores[0].Core)
doRekeyInitialSteps()

doStartVerify := func() {
Expand Down Expand Up @@ -258,7 +255,7 @@ func testSysRekey_Verification(t *testing.T, recovery bool, legacyShamir bool) {
cluster.Start()
defer cluster.Cleanup()

if err := cluster.UnsealCoresWithError(); err == nil {
if err := cluster.UnsealCoresWithError(false); err == nil {
t.Fatal("expected error")
}

Expand All @@ -272,7 +269,7 @@ func testSysRekey_Verification(t *testing.T, recovery bool, legacyShamir bool) {
newKeyBytes = append(newKeyBytes, val)
}
cluster.BarrierKeys = newKeyBytes
if err := cluster.UnsealCoresWithError(); err != nil {
if err := cluster.UnsealCoresWithError(false); err != nil {
t.Fatal(err)
}
} else {
Expand Down
54 changes: 21 additions & 33 deletions vault/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,19 +829,29 @@ func (c *TestCluster) Start() {
// UnsealCores uses the cluster barrier keys to unseal the test cluster cores
func (c *TestCluster) UnsealCores(t testing.T) {
t.Helper()
if err := c.UnsealCoresWithError(); err != nil {
if err := c.UnsealCoresWithError(false); err != nil {
t.Fatal(err)
}
}

func (c *TestCluster) UnsealCoresWithError() error {
numCores := len(c.Cores)
func (c *TestCluster) UnsealCoresWithError(useStoredKeys bool) error {
unseal := func(core *Core) error {
for _, key := range c.BarrierKeys {
if _, err := core.Unseal(TestKeyCopy(key)); err != nil {
return err
}
}
return nil
}
if useStoredKeys {
unseal = func(core *Core) error {
return core.UnsealWithStoredKeys(context.Background())
}
}

// Unseal first core
for _, key := range c.BarrierKeys {
if _, err := c.Cores[0].Unseal(TestKeyCopy(key)); err != nil {
return fmt.Errorf("unseal core %d err: %s", 0, err)
}
if err := unseal(c.Cores[0].Core); err != nil {
return fmt.Errorf("unseal core %d err: %s", 0, err)
}

// Verify unsealed
Expand All @@ -854,11 +864,9 @@ func (c *TestCluster) UnsealCoresWithError() error {
}

// Unseal other cores
for i := 1; i < numCores; i++ {
for _, key := range c.BarrierKeys {
if _, err := c.Cores[i].Core.Unseal(TestKeyCopy(key)); err != nil {
return fmt.Errorf("unseal core %d err: %s", i, err)
}
for i := 1; i < len(c.Cores); i++ {
if err := unseal(c.Cores[i].Core); err != nil {
return fmt.Errorf("unseal core %d err: %s", i, err)
}
}

Expand All @@ -867,7 +875,7 @@ func (c *TestCluster) UnsealCoresWithError() error {

// Ensure cluster connection info is populated.
// Other cores should not come up as leaders.
for i := 1; i < numCores; i++ {
for i := 1; i < len(c.Cores); i++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check that Core 0 is in fact the leader?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestWaitActiveWithError does so. Leader == Active Node.

isLeader, _, _, err := c.Cores[i].Leader()
if err != nil {
return err
Expand Down Expand Up @@ -989,26 +997,6 @@ func (c *TestCluster) ensureCoresSealed() error {
return nil
}

// UnsealWithStoredKeys uses stored keys to unseal the test cluster cores
func (c *TestCluster) UnsealWithStoredKeys(t testing.T) error {
for _, core := range c.Cores {
if err := core.UnsealWithStoredKeys(context.Background()); err != nil {
return err
}
timeout := time.Now().Add(60 * time.Second)
for {
if time.Now().After(timeout) {
return fmt.Errorf("timeout waiting for core to unseal")
}
if !core.Sealed() {
break
}
time.Sleep(250 * time.Millisecond)
}
}
return nil
}

func SetReplicationFailureMode(core *TestClusterCore, mode uint32) {
atomic.StoreUint32(core.Core.replicationFailure, mode)
}
Expand Down