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

Restore TTY when aborting TOTP ticker with Ctrl+C #2342

Merged
merged 3 commits into from
Sep 20, 2022
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
45 changes: 24 additions & 21 deletions internal/action/otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,35 @@ func tickingBar(ctx context.Context, expiresAt time.Time, bar *termio.ProgressBa
}
}

func waitForKeyPress(ctx context.Context, cancel context.CancelFunc) {
tty, err := tty.Open()
func waitForKeyPress(ctx context.Context, cancel context.CancelFunc) (func(), func()) {
tty1, err := tty.Open()
if err != nil {
out.Errorf(ctx, "Unexpected error opening tty: %v", err)
cancel()
}

defer func() {
_ = tty.Close()
}()
return func() {
for {
select {
case <-ctx.Done():
return // returning not to leak the goroutine.
default:
}

for {
select {
case <-ctx.Done():
return // returning not to leak the goroutine.
default:
}

r, err := tty.ReadRune()
if err != nil {
out.Errorf(ctx, "Unexpected error opening tty: %v", err)
}
r, err := tty1.ReadRune()
if err != nil {
out.Errorf(ctx, "Unexpected error opening tty: %v", err)
}

if r == 'q' || r == 'x' || err != nil {
cancel()
if r == 'q' || r == 'x' || err != nil {
cancel()

return
return
}
}
}, func() {
_ = tty1.Close()
}
}
}

// nolint: cyclop
Expand All @@ -97,7 +97,9 @@ func (s *Action) otp(ctx context.Context, name, qrf string, clip, pw, recurse bo
skip := ctxutil.IsHidden(ctx) || pw || qrf != "" || !ctxutil.IsTerminal(ctx) || !ctxutil.IsInteractive(ctx) || clip
if !skip {
// let us monitor key presses for cancellation:.
go waitForKeyPress(ctx, cancel)
runFn, cleanupFn := waitForKeyPress(ctx, cancel)
go runFn()
defer cleanupFn()
dominikschulz marked this conversation as resolved.
Show resolved Hide resolved
}

// only used for the HOTP case as a fallback
Expand Down Expand Up @@ -188,6 +190,7 @@ func (s *Action) otp(ctx context.Context, name, qrf string, clip, pw, recurse bo
select {
case <-ctx.Done():
bar.Done()
cancel()

return nil
default:
Expand Down
20 changes: 5 additions & 15 deletions pkg/gopass/secrets/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,15 @@ username: myuser@test.com
Test / test.com
`

t.Run("read back the secret", func(t *testing.T) {
t.Parallel()

t.Run("read back the secret", func(t *testing.T) { //nolint:paralleltest
assert.Equal(t, mlOut, string(s.Bytes()))
})

t.Run("no_duplicate_keys", func(t *testing.T) {
t.Parallel()

t.Run("no_duplicate_keys", func(t *testing.T) { //nolint:paralleltest
assert.Equal(t, []string{"password", "url", "username"}, s.Keys())
})

t.Run("read some keys", func(t *testing.T) {
t.Parallel()

t.Run("read some keys", func(t *testing.T) { //nolint:paralleltest
for k, v := range map[string]string{
"password": "bar",
"url": "http://www.test.com/",
Expand All @@ -62,9 +56,7 @@ Test / test.com
assert.Equal(t, "somepasswd", s.Password())
})

t.Run("remove a key", func(t *testing.T) {
t.Parallel()

t.Run("remove a key", func(t *testing.T) { //nolint:paralleltest
assert.NoError(t, s.Set("foobar", "baz"))
v, ok := s.Get("foobar")
assert.True(t, ok)
Expand All @@ -76,9 +68,7 @@ Test / test.com
assert.Equal(t, "", v)
})

t.Run("read the body", func(t *testing.T) {
t.Parallel()

t.Run("read the body", func(t *testing.T) { //nolint:paralleltest
body := "Test / test.com\n"
assert.Equal(t, body, s.Body())
assert.Equal(t, body, s.Body())
Expand Down