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

Capture stderr from "tsh db connect" and reformat redis error #13843

Merged
merged 9 commits into from
Jul 5, 2022
2 changes: 1 addition & 1 deletion docs/pages/database-access/guides/redis-cluster.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This guide will help you to:

- Redis version `6.0` or newer.

- `redis-cli` installed and added to your system's `PATH` environment variable.
- `redis-cli` version `6.2` or newer installed and added to your system's `PATH` environment variable.

- A host where you will run the Teleport Database Service. Teleport version 9.0
or newer must be installed.
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/database-access/guides/redis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This guide will help you to:

- Redis version `6.0` or newer.

- `redis-cli` installed and added to your system's `PATH` environment variable.
- `redis-cli` version `6.2` or newer installed and added to your system's `PATH` environment variable.

- A host where you will run the Teleport Database Service. Teleport version 9.0
or newer must be installed.
Expand Down
50 changes: 50 additions & 0 deletions lib/client/db/dbcmd/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2022 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 dbcmd

import (
"os/exec"
"path/filepath"
"strings"

"github.com/gravitational/trace"
)

// ConvertCommandError translates some common errors to more user friendly
// messages.
//
// This helps in situations where the user does not have the full context to
// decipher errors when the database command is executed internally (e.g.
// command executed through "tsh db connect").
func ConvertCommandError(cmd *exec.Cmd, err error, peakStderr string) error {
switch filepath.Base(cmd.Path) {
case redisBin:
// This redis-cli "Unrecognized option ..." error can be confusing to
// users that they may think it is the `tsh` binary that is not
// recognizing the flag.
if strings.HasPrefix(peakStderr, "Unrecognized option or bad number of args for") {
// TLS support starting 6.0. "--insecure" flag starting 6.2.
return trace.BadParameter(
"'%s' has exited with the above error. Please make sure '%s' with version 6.2 or newer is installed.",
cmd.Path,
redisBin,
)
}
}

return trace.Wrap(err)
}
53 changes: 53 additions & 0 deletions lib/utils/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2022 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 utils

// CaptureNBytesWriter is an io.Writer thats captures up to first n bytes
// of the incoming data in memory, and then it ignores the rest of the incoming
// data.
type CaptureNBytesWriter struct {
capture []byte
maxRemaining int
}

// NewCaptureNBytesWriter creates a new CaptureNBytesWriter.
func NewCaptureNBytesWriter(max int) *CaptureNBytesWriter {
return &CaptureNBytesWriter{
maxRemaining: max,
}
}

// Write implements io.Writer.
func (w *CaptureNBytesWriter) Write(p []byte) (int, error) {
if w.maxRemaining > 0 {
capture := p[:]
if len(capture) > w.maxRemaining {
capture = capture[:w.maxRemaining]
}

w.capture = append(w.capture, capture...)
w.maxRemaining -= len(capture)
}

// Always pretend to be successful.
return len(p), nil
greedy52 marked this conversation as resolved.
Show resolved Hide resolved
}

// Bytes returns all captured bytes.
func (w CaptureNBytesWriter) Bytes() []byte {
return w.capture
}
46 changes: 46 additions & 0 deletions lib/utils/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2022 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 utils

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestCaptureNBytesWriter(t *testing.T) {
data := []byte("abcdef")
w := NewCaptureNBytesWriter(10)

// Write 6 bytes. Captured 6 bytes in total.
n, err := w.Write(data)
require.Equal(t, 6, n)
require.NoError(t, err)
require.Equal(t, "abcdef", string(w.Bytes()))

// Write 6 bytes. Captured 10 bytes in total.
n, err = w.Write(data)
require.Equal(t, 6, n)
require.NoError(t, err)
require.Equal(t, "abcdefabcd", string(w.Bytes()))

// Write 6 bytes. Captured 10 bytes in total.
n, err = w.Write(data)
require.Equal(t, 6, n)
require.NoError(t, err)
require.Equal(t, "abcdefabcd", string(w.Bytes()))
}
8 changes: 6 additions & 2 deletions tool/tsh/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/base64"
"fmt"
"io"
"net"
"os"
"sort"
Expand Down Expand Up @@ -659,12 +660,15 @@ func onDatabaseConnect(cf *CLIConf) error {
return trace.Wrap(err)
}
log.Debug(cmd.String())

peakStderr := utils.NewCaptureNBytesWriter(100)
gabrielcorado marked this conversation as resolved.
Show resolved Hide resolved
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stderr = io.MultiWriter(os.Stderr, peakStderr)
cmd.Stdin = os.Stdin

err = cmd.Run()
if err != nil {
return trace.Wrap(err)
return dbcmd.ConvertCommandError(cmd, err, string(peakStderr.Bytes()))
}
return nil
}
Expand Down