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

Fix terraform force-unlock <lock_id> for Consul backend #25837

Merged
merged 2 commits into from
Oct 5, 2020
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
24 changes: 23 additions & 1 deletion backend/remote-state/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ func (c *RemoteClient) lock() (string, error) {
// store the session ID for correlation with consul logs
c.info.Info = "consul session: " + lockSession

// A random lock ID has been generated but we override it with the session
// ID as this will make it easier to manually invalidate the session
// if needed.
c.info.ID = lockSession

opts := &consulapi.LockOptions{
Key: c.lockPath() + lockSuffix,
Session: lockSession,
Expand Down Expand Up @@ -524,8 +529,25 @@ func (c *RemoteClient) Unlock(id string) error {
// the unlock implementation.
// Only to be called while holding Client.mu
func (c *RemoteClient) unlock(id string) error {
// this doesn't use the lock id, because the lock is tied to the consul client.
// This method can be called in two circumstances:
// - when the plan apply or destroy operation finishes and the lock needs to be released,
// the watchdog stopped and the session closed
// - when the user calls `terraform force-unlock <lock_id>` in which case
// we only need to release the lock.

if c.consulLock == nil || c.lockCh == nil {
// The user called `terraform force-unlock <lock_id>`, we just destroy
// the session which will release the lock, clean the KV store and quit.

_, err := c.Client.Session().Destroy(id, nil)
if err != nil {
return err
}
// We ignore the errors that may happen during cleanup
kv := c.Client.KV()
kv.Delete(c.lockPath()+lockSuffix, nil)
kv.Delete(c.lockPath()+lockInfoSuffix, nil)

return nil
}

Expand Down
50 changes: 42 additions & 8 deletions backend/remote-state/consul/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ func TestConsul_destroyLock(t *testing.T) {
fmt.Sprintf("tf-unit/%s/", time.Now().String()),
}

testLock := func(client *RemoteClient, lockPath string) {
// get the lock val
pair, _, err := client.Client.KV().Get(lockPath, nil)
if err != nil {
t.Fatal(err)
}
if pair != nil {
t.Fatalf("lock key not cleaned up at: %s", pair.Key)
}
}

for _, path := range testCases {
t.Run(path, func(*testing.T) {
// Get the backend
Expand All @@ -269,27 +280,50 @@ func TestConsul_destroyLock(t *testing.T) {
t.Fatalf("err: %s", err)
}

c := s.(*remote.State).Client.(*RemoteClient)
clientA := s.(*remote.State).Client.(*RemoteClient)

info := statemgr.NewLockInfo()
id, err := c.Lock(info)
id, err := clientA.Lock(info)
if err != nil {
t.Fatal(err)
}

lockPath := c.Path + lockSuffix
lockPath := clientA.Path + lockSuffix

if err := c.Unlock(id); err != nil {
if err := clientA.Unlock(id); err != nil {
t.Fatal(err)
}

// get the lock val
pair, _, err := c.Client.KV().Get(lockPath, nil)
testLock(clientA, lockPath)

// The release the lock from a second client to test the
// `terraform force-unlock <lock_id>` functionnality
s, err = b.StateMgr(backend.DefaultStateName)
if err != nil {
t.Fatalf("err: %s", err)
}

clientB := s.(*remote.State).Client.(*RemoteClient)

info = statemgr.NewLockInfo()
id, err = clientA.Lock(info)
if err != nil {
t.Fatal(err)
}

if err := clientB.Unlock(id); err != nil {
t.Fatal(err)
}
if pair != nil {
t.Fatalf("lock key not cleaned up at: %s", pair.Key)

testLock(clientA, lockPath)

err = clientA.Unlock(id)

if err == nil {
t.Fatal("consul lock should have been lost")
}
if err.Error() != "consul lock was lost" {
t.Fatal("got wrong error", err)
}
})
}
Expand Down