waveterm/wavesrv/pkg/scpacket/scpacket.go
Sylvie Crowe 903b26bfca
Use ssh library: add user input (#281)
* feat: create backend for user input requests

This is the first part of a change that allows the backend to request
user input from the frontend. Essentially, the backend will send a
request for the user to answer some query, and the frontend will send
that answer back. It is blocking, so it needs to be used within a
goroutine.

There is some placeholder code in the frontend that will be updated in
future commits. Similarly, there is some debug code in the backend
remote.go file.

* feat: create frontend for user input requests

This is part of a change to allow the backend to request user input from
the frontend. This adds a component specifically for handling this
logic. It is only a starting point, and does not work perfectly yet.

* refactor: update user input backend/interface

This updates the user input backend to fix a few potential bugs. It also
refactors the user input request and response types to better handle
markdown and errors while making it more convenient to work with.

A couple frontend changes were made to keep everything compatible.

* fix: add props to user input request modal

There was a second place that the modals were created that I previously
missed. This fixes that second casel

* feat: complete user input modal

This rounds out the most immediate concerns for the new user input
modal. The frontend now includes a timer to show how much time is left
and will close itself once it reaches zero. Css
formatting has been cleaned up to be more reasonable.

There is still some test code present on the back end. This will be
removed once actuall examples of the new modal are in place.

* feat: create first pass known_hosts detection

Manually integrating with golang's ssh library means that the code must
authenticate known_hosts on its own. This is a first pass at creating a
system that parses the known hosts files and denys a connection if there
is a mismatch. This needs to be updated with a means to add keys to the
known-hosts file if the user requests it.

* feat: allow writing to known_hosts first pass

As a follow-up to the previous change, we now allow the user to respond
to interactive queries in order to determine if an unknown known hosts
key can be added to a known_hosts file if it is missing. This needs to
be refined further, but it gets the basic functionality there.

* feat: add user input for kbd-interactive auth

This adds a modal so the user can respond to prompts provided using the
keyboard interactive authentication method.

* feat: add interactive password authentication

This makes the ssh password authentication interactive with its own user
input modal. Unfortunately, this method does not allow trying a default
first. This will need to be expanded in the future to accomodate that.

* fix: allow automatic and interactive auth together

Previously, it was impossible to use to separate methods of the same
type to try ssh authentication. This made it impossible to make an auto
attempt before a manual one. This change restricts that by combining
them into one method where the auto attempt is tried once first and
cannot be tried again. Following that, interactive authentication can be
tried separately.

It also lowers the time limit on kbd interactive authentication to 15
seconds due to limitations on the library we are using.

* fix: set number of retries to one in ssh

Number of retries means number of attempts after the fact, not number of
total attempts. It has been adjusted from 2 to 1 to reflect this.

* refactor: change argument order in GetUserInput

This is a simple change to move the context to the first argument of
GetUserInput to match the convention used elsewhere in the code.

* fix: set number of retries to two again

I was wrong in my previous analysis. The number given is the total
number of tries. This is confusing when keyboard authentication and
password authentication are both available which usually doesn't happen.

* feat: create naive ui for ssh key passphrases

This isn't quite as reactive as the other methods, but it does attempt
to use publickey without a passphrase, then attempt to use the password
as the passphrase, and finally prompting the user for a passphrase. The
problem with this approach is that if multiple keys are used and they
all have passphrases, they need to all be checked up front. In practice,
this will not happen often, but it is something to be aware of.

* fix: add the userinput.tsx changes

These were missed in the previous commit. Adding them now.
2024-02-09 00:16:56 -03:00

219 lines
6.0 KiB
Go

// Copyright 2023, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package scpacket
import (
"fmt"
"reflect"
"regexp"
"strings"
"github.com/alessio/shellescape"
"github.com/google/uuid"
"github.com/wavetermdev/waveterm/waveshell/pkg/base"
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
"github.com/wavetermdev/waveterm/waveshell/pkg/utilfn"
)
var RemoteNameRe = regexp.MustCompile("^\\*?[a-zA-Z0-9_-]+$")
type RemotePtrType struct {
OwnerId string `json:"ownerid"`
RemoteId string `json:"remoteid"`
Name string `json:"name"`
}
func (r RemotePtrType) IsSessionScope() bool {
return strings.HasPrefix(r.Name, "*")
}
func (rptr *RemotePtrType) GetDisplayName(baseDisplayName string) string {
name := baseDisplayName
if rptr == nil {
return name
}
if rptr.Name != "" {
name = name + ":" + rptr.Name
}
if rptr.OwnerId != "" {
name = "@" + rptr.OwnerId + ":" + name
}
return name
}
func (r RemotePtrType) Validate() error {
if r.OwnerId != "" {
if _, err := uuid.Parse(r.OwnerId); err != nil {
return fmt.Errorf("invalid ownerid format: %v", err)
}
}
if r.RemoteId != "" {
if _, err := uuid.Parse(r.RemoteId); err != nil {
return fmt.Errorf("invalid remoteid format: %v", err)
}
}
if r.Name != "" {
ok := RemoteNameRe.MatchString(r.Name)
if !ok {
return fmt.Errorf("invalid remote name")
}
}
return nil
}
func (r RemotePtrType) MakeFullRemoteRef() string {
if r.RemoteId == "" {
return ""
}
if r.OwnerId == "" && r.Name == "" {
return r.RemoteId
}
if r.OwnerId != "" && r.Name == "" {
return fmt.Sprintf("@%s:%s", r.OwnerId, r.RemoteId)
}
if r.OwnerId == "" && r.Name != "" {
return fmt.Sprintf("%s:%s", r.RemoteId, r.Name)
}
return fmt.Sprintf("@%s:%s:%s", r.OwnerId, r.RemoteId, r.Name)
}
const FeCommandPacketStr = "fecmd"
const WatchScreenPacketStr = "watchscreen"
const FeInputPacketStr = "feinput"
const RemoteInputPacketStr = "remoteinput"
const CmdInputTextPacketStr = "cmdinputtext"
const UserInputResponsePacketStr = "userinputresp"
type FeCommandPacketType struct {
Type string `json:"type"`
MetaCmd string `json:"metacmd"`
MetaSubCmd string `json:"metasubcmd,omitempty"`
Args []string `json:"args,omitempty"`
Kwargs map[string]string `json:"kwargs,omitempty"`
RawStr string `json:"rawstr,omitempty"`
UIContext *UIContextType `json:"uicontext,omitempty"`
Interactive bool `json:"interactive"`
}
func (pk *FeCommandPacketType) GetRawStr() string {
if pk.RawStr != "" {
return pk.RawStr
}
cmd := "/" + pk.MetaCmd
if pk.MetaSubCmd != "" {
cmd = cmd + ":" + pk.MetaSubCmd
}
var args []string
for k, v := range pk.Kwargs {
argStr := fmt.Sprintf("%s=%s", shellescape.Quote(k), shellescape.Quote(v))
args = append(args, argStr)
}
for _, arg := range pk.Args {
args = append(args, shellescape.Quote(arg))
}
if len(args) == 0 {
return cmd
}
return cmd + " " + strings.Join(args, " ")
}
type UIContextType struct {
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
Remote *RemotePtrType `json:"remote,omitempty"`
WinSize *packet.WinSize `json:"winsize,omitempty"`
Build string `json:"build,omitempty"`
}
type FeInputPacketType struct {
Type string `json:"type"`
CK base.CommandKey `json:"ck"`
Remote RemotePtrType `json:"remote"`
InputData64 string `json:"inputdata64"`
SigName string `json:"signame,omitempty"`
WinSize *packet.WinSize `json:"winsize,omitempty"`
}
type RemoteInputPacketType struct {
Type string `json:"type"`
RemoteId string `json:"remoteid"`
InputData64 string `json:"inputdata64"`
}
type WatchScreenPacketType struct {
Type string `json:"type"`
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
Connect bool `json:"connect"`
AuthKey string `json:"authkey"`
}
type CmdInputTextPacketType struct {
Type string `json:"type"`
SeqNum int `json:"seqnum"`
ScreenId string `json:"screenid"`
Text utilfn.StrWithPos `json:"text"`
}
type UserInputResponsePacketType struct {
Type string `json:"type"`
RequestId string `json:"requestid"`
Text string `json:"text,omitempty"`
Confirm bool `json:"confirm,omitempty"`
ErrorMsg string `json:"errormsg,omitempty"`
}
func init() {
packet.RegisterPacketType(FeCommandPacketStr, reflect.TypeOf(FeCommandPacketType{}))
packet.RegisterPacketType(WatchScreenPacketStr, reflect.TypeOf(WatchScreenPacketType{}))
packet.RegisterPacketType(FeInputPacketStr, reflect.TypeOf(FeInputPacketType{}))
packet.RegisterPacketType(RemoteInputPacketStr, reflect.TypeOf(RemoteInputPacketType{}))
packet.RegisterPacketType(CmdInputTextPacketStr, reflect.TypeOf(CmdInputTextPacketType{}))
packet.RegisterPacketType(UserInputResponsePacketStr, reflect.TypeOf(UserInputResponsePacketType{}))
}
func (*CmdInputTextPacketType) GetType() string {
return CmdInputTextPacketStr
}
func MakeCmdInputTextPacket(screenId string) *CmdInputTextPacketType {
return &CmdInputTextPacketType{Type: CmdInputTextPacketStr, ScreenId: screenId}
}
func (*FeCommandPacketType) GetType() string {
return FeCommandPacketStr
}
func MakeFeCommandPacket() *FeCommandPacketType {
return &FeCommandPacketType{Type: FeCommandPacketStr}
}
func (*FeInputPacketType) GetType() string {
return FeInputPacketStr
}
func MakeFeInputPacket() *FeInputPacketType {
return &FeInputPacketType{Type: FeInputPacketStr}
}
func (*WatchScreenPacketType) GetType() string {
return WatchScreenPacketStr
}
func MakeWatchScreenPacket() *WatchScreenPacketType {
return &WatchScreenPacketType{Type: WatchScreenPacketStr}
}
func MakeRemoteInputPacket() *RemoteInputPacketType {
return &RemoteInputPacketType{Type: RemoteInputPacketStr}
}
func (*RemoteInputPacketType) GetType() string {
return RemoteInputPacketStr
}
func (*UserInputResponsePacketType) GetType() string {
return UserInputResponsePacketStr
}