Replace golang.org/x/crypto/ssh/terminal with golang.org/x/term

The golang.org/x/crypto/ssh/terminal package has been deprecated and
replaced upstream by golang.org/x/term, so switch to that.  It's a
simple 1:1 replacement.

[NO NEW TESTS NEEDED]

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2021-08-24 15:30:13 -04:00
parent fc1e256f50
commit 1ec4983d50
10 changed files with 26 additions and 102 deletions

View File

@ -31,8 +31,8 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/syndtr/gocapability/capability"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
"golang.org/x/term"
)
const (
@ -138,13 +138,13 @@ func RunUsingChroot(spec *specs.Spec, bundlePath, homeDir string, stdin io.Reade
// Set our terminal's mode to raw, to pass handling of special
// terminal input to the terminal in the container.
if spec.Process.Terminal && terminal.IsTerminal(unix.Stdin) {
state, err := terminal.MakeRaw(unix.Stdin)
if spec.Process.Terminal && term.IsTerminal(unix.Stdin) {
state, err := term.MakeRaw(unix.Stdin)
if err != nil {
logrus.Warnf("error setting terminal state: %v", err)
} else {
defer func() {
if err = terminal.Restore(unix.Stdin, state); err != nil {
if err = term.Restore(unix.Stdin, state); err != nil {
logrus.Errorf("unable to restore terminal state: %v", err)
}
}()
@ -275,7 +275,7 @@ func runUsingChrootMain() {
winsize.Row = uint16(options.Spec.Process.ConsoleSize.Height)
winsize.Col = uint16(options.Spec.Process.ConsoleSize.Width)
} else {
if terminal.IsTerminal(unix.Stdin) {
if term.IsTerminal(unix.Stdin) {
// Use the size of our terminal.
winsize, err = unix.IoctlGetWinsize(unix.Stdin, unix.TIOCGWINSZ)
if err != nil {

View File

@ -12,7 +12,7 @@ import (
"github.com/containers/buildah/define"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
type infoResults struct {
@ -79,14 +79,14 @@ func infoCmd(c *cobra.Command, iopts infoResults) error {
if err = t.Execute(os.Stdout, info); err != nil {
return err
}
if terminal.IsTerminal(int(os.Stdout.Fd())) {
if term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Println()
}
return nil
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if terminal.IsTerminal(int(os.Stdout.Fd())) {
if term.IsTerminal(int(os.Stdout.Fd())) {
enc.SetEscapeHTML(false)
}
return enc.Encode(info)

View File

@ -12,7 +12,7 @@ import (
"github.com/containers/buildah/pkg/parse"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
const (
@ -120,7 +120,7 @@ func inspectCmd(c *cobra.Command, args []string, iopts inspectResults) error {
if err = t.Execute(os.Stdout, out); err != nil {
return err
}
if terminal.IsTerminal(int(os.Stdout.Fd())) {
if term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Println()
}
return nil
@ -128,7 +128,7 @@ func inspectCmd(c *cobra.Command, args []string, iopts inspectResults) error {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if terminal.IsTerminal(int(os.Stdout.Fd())) {
if term.IsTerminal(int(os.Stdout.Fd())) {
enc.SetEscapeHTML(false)
}
return enc.Encode(out)

1
go.mod
View File

@ -38,6 +38,7 @@ require (
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
k8s.io/klog v1.0.0 // indirect
)

View File

@ -12,7 +12,7 @@ import (
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
const (
@ -70,7 +70,7 @@ func setJSONFormatEncoder(isTerminal bool, w io.Writer) *json.Encoder {
// Out method for JSON Arrays
func (j JSONStructArray) Out() error {
buf := bytes.NewBuffer(nil)
enc := setJSONFormatEncoder(terminal.IsTerminal(int(os.Stdout.Fd())), buf)
enc := setJSONFormatEncoder(term.IsTerminal(int(os.Stdout.Fd())), buf)
if err := enc.Encode(j.Output); err != nil {
return err
}
@ -161,7 +161,7 @@ func (y YAMLStruct) Out() error {
// humanNewLine prints a new line at the end of the output only if stdout is the terminal
func humanNewLine() {
if terminal.IsTerminal(int(os.Stdout.Fd())) {
if term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Println()
}
}

View File

@ -25,7 +25,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
const (
@ -716,7 +716,7 @@ func AuthConfig(creds string) (*types.DockerAuthConfig, error) {
}
if password == "" {
fmt.Print("Password: ")
termPassword, err := terminal.ReadPassword(0)
termPassword, err := term.ReadPassword(0)
if err != nil {
return nil, errors.Wrapf(err, "could not read password from terminal")
}

View File

@ -48,8 +48,8 @@ import (
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
"golang.org/x/term"
)
// ContainerDevices is an alias for a slice of github.com/opencontainers/runc/libcontainer/configs.Device structures.
@ -719,7 +719,7 @@ func (b *Builder) generateHosts(rdir, hostname string, addHosts []string, chownO
func setupTerminal(g *generate.Generator, terminalPolicy TerminalPolicy, terminalSize *specs.Box) {
switch terminalPolicy {
case DefaultTerminal:
onTerminal := terminal.IsTerminal(unix.Stdin) && terminal.IsTerminal(unix.Stdout) && terminal.IsTerminal(unix.Stderr)
onTerminal := term.IsTerminal(unix.Stdin) && term.IsTerminal(unix.Stdout) && term.IsTerminal(unix.Stderr)
if onTerminal {
logrus.Debugf("stdio is a terminal, defaulting to using a terminal")
} else {
@ -1276,12 +1276,12 @@ func runCopyStdio(logger *logrus.Logger, stdio *sync.WaitGroup, copyPipes bool,
writeDesc[unix.Stdout] = "output"
// Set our terminal's mode to raw, to pass handling of special
// terminal input to the terminal in the container.
if terminal.IsTerminal(unix.Stdin) {
if state, err := terminal.MakeRaw(unix.Stdin); err != nil {
if term.IsTerminal(unix.Stdin) {
if state, err := term.MakeRaw(unix.Stdin); err != nil {
logger.Warnf("error setting terminal state: %v", err)
} else {
defer func() {
if err = terminal.Restore(unix.Stdin, state); err != nil {
if err = term.Restore(unix.Stdin, state); err != nil {
logger.Errorf("unable to restore terminal state: %v", err)
}
}()
@ -1504,7 +1504,7 @@ func runAcceptTerminal(logger *logrus.Logger, consoleListener *net.UnixListener,
winsize.Row = uint16(terminalSize.Height)
winsize.Col = uint16(terminalSize.Width)
} else {
if terminal.IsTerminal(unix.Stdin) {
if term.IsTerminal(unix.Stdin) {
// Use the size of our terminal.
if winsize, err = unix.IoctlGetWinsize(unix.Stdin, unix.TIOCGWINSZ); err != nil {
logger.Warnf("error reading size of controlling terminal: %v", err)

View File

@ -17,8 +17,8 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/syndtr/gocapability/capability"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
"golang.org/x/term"
)
func getVersion(r *types.TestReport) {
@ -35,12 +35,12 @@ func getHostname(r *types.TestReport) error {
}
func getProcessTerminal(r *types.TestReport) error {
r.Spec.Process.Terminal = terminal.IsTerminal(unix.Stdin)
r.Spec.Process.Terminal = term.IsTerminal(unix.Stdin)
return nil
}
func getProcessConsoleSize(r *types.TestReport) error {
if terminal.IsTerminal(unix.Stdin) {
if term.IsTerminal(unix.Stdin) {
winsize, err := unix.IoctlGetWinsize(unix.Stdin, unix.TIOCGWINSZ)
if err != nil {
return errors.Wrapf(err, "error reading size of terminal on stdin")

View File

@ -1,76 +0,0 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package terminal provides support functions for dealing with terminals, as
// commonly found on UNIX systems.
//
// Deprecated: this package moved to golang.org/x/term.
package terminal
import (
"io"
"golang.org/x/term"
)
// EscapeCodes contains escape sequences that can be written to the terminal in
// order to achieve different styles of text.
type EscapeCodes = term.EscapeCodes
// Terminal contains the state for running a VT100 terminal that is capable of
// reading lines of input.
type Terminal = term.Terminal
// NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is
// a local terminal, that terminal must first have been put into raw mode.
// prompt is a string that is written at the start of each input line (i.e.
// "> ").
func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
return term.NewTerminal(c, prompt)
}
// ErrPasteIndicator may be returned from ReadLine as the error, in addition
// to valid line data. It indicates that bracketed paste mode is enabled and
// that the returned line consists only of pasted data. Programs may wish to
// interpret pasted data more literally than typed data.
var ErrPasteIndicator = term.ErrPasteIndicator
// State contains the state of a terminal.
type State = term.State
// IsTerminal returns whether the given file descriptor is a terminal.
func IsTerminal(fd int) bool {
return term.IsTerminal(fd)
}
// ReadPassword reads a line of input from a terminal without local echo. This
// is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) {
return term.ReadPassword(fd)
}
// MakeRaw puts the terminal connected to the given file descriptor into raw
// mode and returns the previous state of the terminal so that it can be
// restored.
func MakeRaw(fd int) (*State, error) {
return term.MakeRaw(fd)
}
// Restore restores the terminal connected to the given file descriptor to a
// previous state.
func Restore(fd int, oldState *State) error {
return term.Restore(fd, oldState)
}
// GetState returns the current state of a terminal which may be useful to
// restore the terminal after a signal.
func GetState(fd int) (*State, error) {
return term.GetState(fd)
}
// GetSize returns the dimensions of the given terminal.
func GetSize(fd int) (width, height int, err error) {
return term.GetSize(fd)
}

1
vendor/modules.txt vendored
View File

@ -509,7 +509,6 @@ golang.org/x/crypto/poly1305
golang.org/x/crypto/ssh
golang.org/x/crypto/ssh/agent
golang.org/x/crypto/ssh/internal/bcrypt_pbkdf
golang.org/x/crypto/ssh/terminal
# golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
golang.org/x/net/context
golang.org/x/net/html