feat: Allow user multiple keys with userkey create

And end user may have more than one ssh key, the userkey create
command should be able to accept more than one key so you can do
something like:

   curl https://github.com/USER.keys | ssh sshportal -p 2222 -l admin userkey create USER

The userkey create command also does not work properly from an
interactive shell due to the use of bufio.  This patch adds the
ability to use either the interactive shell or direct ssh command to
input one or more keys.

Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
This commit is contained in:
Jason Wessel 2021-03-05 08:27:19 -08:00
parent 762736d622
commit 28a5fd1846

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"net/url" "net/url"
"os" "os"
"regexp" "regexp"
@ -2017,34 +2018,58 @@ GLOBAL OPTIONS:
return err return err
} }
fmt.Fprintf(s, "Enter key:\n") var reader *bufio.Reader
reader := bufio.NewReader(s) var term *terminal.Terminal
text, _ := reader.ReadString('\n') if len(sshCommand) == 0 { // interactive mode
term = terminal.NewTerminal(s, "Paste your key(s) and end with a blank line> ")
key, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(text)) } else {
if err != nil { fmt.Fprintf(s, "Enter key(s):\n")
return err reader = bufio.NewReader(s)
} }
userkey := dbmodels.UserKey{ for {
User: &user, var text string
Key: key.Marshal(), var errReadline error
Comment: comment, if len(sshCommand) == 0 { // interactive mode
AuthorizedKey: string(gossh.MarshalAuthorizedKey(key)), text, errReadline = term.ReadLine()
} } else {
if c.String("comment") != "" { text, errReadline = reader.ReadString('\n')
userkey.Comment = c.String("comment") }
} if errReadline != nil && errReadline != io.EOF {
return errReadline
}
if text != "" && text != "\n" {
key, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(text))
if err != nil {
return err
}
if _, err := govalidator.ValidateStruct(userkey); err != nil { userkey := dbmodels.UserKey{
return err User: &user,
} Key: key.Marshal(),
Comment: comment,
AuthorizedKey: string(gossh.MarshalAuthorizedKey(key)),
}
if c.String("comment") != "" {
userkey.Comment = c.String("comment")
}
// save the userkey in database if _, err := govalidator.ValidateStruct(userkey); err != nil {
if err := db.Create(&userkey).Error; err != nil { return err
return err }
// save the userkey in database
if err := db.Create(&userkey).Error; err != nil {
return err
}
fmt.Fprintf(s, "%d\n", userkey.ID)
if errReadline == io.EOF {
return nil
}
} else {
break
}
} }
fmt.Fprintf(s, "%d\n", userkey.ID)
return nil return nil
}, },
}, { }, {