-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
mfa: cancel TOTP prompt if U2F was used #6542
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
Copyright 2021 Gravitational, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package prompt | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"os" | ||
"sync" | ||
) | ||
|
||
var ( | ||
stdinOnce = &sync.Once{} | ||
stdin *ContextReader | ||
) | ||
|
||
// Stdin returns a singleton ContextReader wrapped around os.Stdin. | ||
// | ||
// os.Stdin should not be used directly after the first call to this function | ||
// to avoid losing data. Closing this ContextReader will prevent all future | ||
// reads for all callers. | ||
func Stdin() *ContextReader { | ||
stdinOnce.Do(func() { | ||
stdin = NewContextReader(os.Stdin) | ||
}) | ||
return stdin | ||
} | ||
|
||
// ErrReaderClosed is returned from ContextReader.Read after it was closed. | ||
var ErrReaderClosed = errors.New("ContextReader has been closed") | ||
|
||
// ContextReader is a wrapper around io.Reader where each individual | ||
// ReadContext call can be canceled using a context. | ||
type ContextReader struct { | ||
r io.Reader | ||
data chan []byte | ||
close chan struct{} | ||
|
||
mu sync.RWMutex | ||
err error | ||
} | ||
|
||
// NewContextReader creates a new ContextReader wrapping r. Callers should not | ||
// use r after creating this ContextReader to avoid loss of data (the last read | ||
// will be lost). | ||
// | ||
// Callers are responsible for closing the ContextReader to release associated | ||
// resources. | ||
func NewContextReader(r io.Reader) *ContextReader { | ||
cr := &ContextReader{ | ||
r: r, | ||
data: make(chan []byte), | ||
close: make(chan struct{}), | ||
} | ||
go cr.read() | ||
return cr | ||
} | ||
|
||
func (r *ContextReader) setErr(err error) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
if r.err != nil { | ||
// Keep only the first encountered error. | ||
return | ||
} | ||
r.err = err | ||
} | ||
|
||
func (r *ContextReader) getErr() error { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
return r.err | ||
} | ||
|
||
func (r *ContextReader) read() { | ||
defer close(r.data) | ||
|
||
for { | ||
// Allocate a new buffer for every read because we need to send it to | ||
// another goroutine. | ||
buf := make([]byte, 4*1024) // 4kB, matches Linux page size. | ||
n, err := r.r.Read(buf) | ||
r.setErr(err) | ||
buf = buf[:n] | ||
if n == 0 { | ||
return | ||
} | ||
select { | ||
case <-r.close: | ||
return | ||
case r.data <- buf: | ||
} | ||
} | ||
} | ||
|
||
// ReadContext returns the next chunk of output from the reader. If ctx is | ||
// canceled before any data is available, ReadContext will return too. If r | ||
// was closed, ReadContext will return immediately with ErrReaderClosed. | ||
func (r *ContextReader) ReadContext(ctx context.Context) ([]byte, error) { | ||
select { | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
case <-r.close: | ||
// Close was called, unblock immediately. | ||
// r.data might still be blocked if it's blocked on the Read call. | ||
return nil, r.getErr() | ||
case buf, ok := <-r.data: | ||
if !ok { | ||
// r.data was closed, so the read goroutine has finished. | ||
// No more data will be available, return the latest error. | ||
return nil, r.getErr() | ||
} | ||
return buf, nil | ||
} | ||
} | ||
|
||
// Close releases the background resources of r. All ReadContext calls will | ||
// unblock immediately. | ||
func (r *ContextReader) Close() { | ||
select { | ||
case <-r.close: | ||
// Already closed, do nothing. | ||
return | ||
default: | ||
close(r.close) | ||
r.setErr(ErrReaderClosed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2021 Gravitational, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package prompt | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestContextReader(t *testing.T) { | ||
pr, pw := io.Pipe() | ||
t.Cleanup(func() { pr.Close() }) | ||
t.Cleanup(func() { pw.Close() }) | ||
|
||
write := func(t *testing.T, s string) { | ||
_, err := pw.Write([]byte(s)) | ||
require.NoError(t, err) | ||
} | ||
ctx := context.Background() | ||
|
||
r := NewContextReader(pr) | ||
|
||
t.Run("simple read", func(t *testing.T) { | ||
go write(t, "hello") | ||
buf, err := r.ReadContext(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, string(buf), "hello") | ||
}) | ||
|
||
t.Run("cancelled read", func(t *testing.T) { | ||
cancelCtx, cancel := context.WithCancel(ctx) | ||
go cancel() | ||
buf, err := r.ReadContext(cancelCtx) | ||
require.ErrorIs(t, err, context.Canceled) | ||
require.Empty(t, buf) | ||
|
||
go write(t, "after cancel") | ||
buf, err = r.ReadContext(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, string(buf), "after cancel") | ||
}) | ||
|
||
t.Run("close underlying reader", func(t *testing.T) { | ||
go func() { | ||
write(t, "before close") | ||
pw.CloseWithError(io.EOF) | ||
}() | ||
|
||
// Read the last chunk of data successfully. | ||
buf, err := r.ReadContext(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, string(buf), "before close") | ||
|
||
// Next read fails because underlying reader is closed. | ||
buf, err = r.ReadContext(ctx) | ||
require.ErrorIs(t, err, io.EOF) | ||
require.Empty(t, buf) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This code is quite old but I think all "WrapWithMessage" calls here can be replaced with just regular "Wrap".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap
just callsWrapWithMessage
if there are any extra arguments.I don't see any difference between them, other than a bit of overhead