chore: fix runssh include -sid flag and ssh key parse

This commit is contained in:
Jesús Pérez Lorenzo 2021-08-31 14:08:23 +01:00
parent a8b8152d98
commit cf5947fc19
4 changed files with 25 additions and 10 deletions

View File

@ -127,7 +127,9 @@ Usage of upclapi:
-kdr string -kdr string
use coder use coder
-o string -o string
output format (json|yaml) output format (attached for infofloatip)
-sid string
ssh id for ssh commands (or defaul id_rsa)
-t string -t string
target item for command target item for command
``` ```
@ -168,7 +170,7 @@ Usage of upclapi:
(yaml-config-file has to include *ssh** config section) (yaml-config-file has to include *ssh** config section)
```bash ```bash
./build/upclapi -c runssh -f [yaml-config-file] -ssh remote-command-to-execute ./build/upclapi -c runssh -f [yaml-config-file] -ssh remote-command-to-execute [-sid ssh/key_filename (or id_pub in HOME by default)]
``` ```
- Create Servers [Ansible Inventory](https://docs.ansible.com/ansible/latest/index.html) - Create Servers [Ansible Inventory](https://docs.ansible.com/ansible/latest/index.html)

View File

@ -45,6 +45,7 @@ type RunFlags struct {
encdr string encdr string
out string out string
target string target string
sid string
} }
type CoderConfig struct { type CoderConfig struct {
cmd string cmd string
@ -496,6 +497,7 @@ func ParseFlags() (RunFlags,error) {
encdr: os.Getenv("UPCLAPI_ENCODER"), encdr: os.Getenv("UPCLAPI_ENCODER"),
out: os.Getenv("UPCLAPI_OUT"), out: os.Getenv("UPCLAPI_OUT"),
target: os.Getenv("UPCLAPI_TARGET"), target: os.Getenv("UPCLAPI_TARGET"),
sid: os.Getenv("UPCLAPI_SID"),
} }
if runFlags.command == "" { if runFlags.command == "" {
runFlags.command = DFLT_COMMAND runFlags.command = DFLT_COMMAND
@ -523,8 +525,9 @@ func ParseFlags() (RunFlags,error) {
flag.StringVar(&runFlags.id, "id", runFlags.id, "resource name or uuid") flag.StringVar(&runFlags.id, "id", runFlags.id, "resource name or uuid")
flag.StringVar(&runFlags.runCmd, "cmd", runFlags.runCmd, "run [ssh] command") flag.StringVar(&runFlags.runCmd, "cmd", runFlags.runCmd, "run [ssh] command")
flag.StringVar(&runFlags.encdr, "kdr", runFlags.encdr, "use coder ") flag.StringVar(&runFlags.encdr, "kdr", runFlags.encdr, "use coder ")
flag.StringVar(&runFlags.out, "o", runFlags.out, "output format ") flag.StringVar(&runFlags.out, "o", runFlags.out, "output format (attached for infofloatip)")
flag.StringVar(&runFlags.target, "t", runFlags.out, "target item for command ") flag.StringVar(&runFlags.target, "t", runFlags.target, "target item for command ")
flag.StringVar(&runFlags.sid, "sid", runFlags.sid, "ssh id for ssh commands (or defaul id_rsa)")
// Actually parse the flags // Actually parse the flags
flag.Parse() flag.Parse()

2
run.go
View File

@ -432,7 +432,7 @@ func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *D
} }
case "runssh": case "runssh":
if sshAccess.Host != "" { if sshAccess.Host != "" {
output, err := runSSH(sshAccess, runCommand) output, err := runSSH(runFlags, sshAccess, runCommand)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

20
ssh.go
View File

@ -15,7 +15,7 @@ import (
// "github.com/davecgh/go-spew/spew" // "github.com/davecgh/go-spew/spew"
) )
func publicKeyAuthFunc(kPath string) ssh.AuthMethod { func publicKeyAuthGet(kPath string) ssh.AuthMethod {
// keyPath, err := homedir.Expand(kPath) // keyPath, err := homedir.Expand(kPath)
// if err != nil { // if err != nil {
// log.Fatal("find key's home dir failed", err) // log.Fatal("find key's home dir failed", err)
@ -23,16 +23,18 @@ func publicKeyAuthFunc(kPath string) ssh.AuthMethod {
// key, err := ioutil.ReadFile(keyPath ) // key, err := ioutil.ReadFile(keyPath )
key, err := ioutil.ReadFile(kPath ) key, err := ioutil.ReadFile(kPath )
if err != nil { if err != nil {
log.Fatal("ssh key file read failed", err) log.Fatal("ssh key file read failed: " , err)
return nil
} }
// Create the Signer for this private key. // Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key) signer, err := ssh.ParsePrivateKey(key)
if err != nil { if err != nil {
log.Fatal("ssh key signer failed", err) log.Fatal("ssh key signer failed: ", err)
return nil
} }
return ssh.PublicKeys(signer) return ssh.PublicKeys(signer)
} }
func runSSH(cfg SSHAccess, cmds ...string ) ([]byte, error) { func runSSH(runFlags RunFlags, cfg SSHAccess, cmds ...string ) ([]byte, error) {
// fmt.Fprintf(os.Stderr, "SSH: %#v\n", cfg) // fmt.Fprintf(os.Stderr, "SSH: %#v\n", cfg)
fmt.Fprintf(os.Stderr, "%s - running : %s\n\n", cfg.Host, cmds) fmt.Fprintf(os.Stderr, "%s - running : %s\n\n", cfg.Host, cmds)
@ -46,7 +48,15 @@ func runSSH(cfg SSHAccess, cmds ...string ) ([]byte, error) {
if cfg.UType == "password" { if cfg.UType == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password(cfg.Password)} config.Auth = []ssh.AuthMethod{ssh.Password(cfg.Password)}
} else { } else {
config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(cfg.KeyPath)} sshkey := cfg.KeyPath
if len(sshkey) == 0 {
sid := runFlags.sid
if len(sid) == 0 {
sid="id_rsa"
}
sshkey = fmt.Sprintf("%s/.ssh/%s", os.Getenv("HOME"),sid)
}
config.Auth = []ssh.AuthMethod{publicKeyAuthGet(sshkey)}
} }
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
conn, err := ssh.Dial("tcp", addr,config) conn, err := ssh.Dial("tcp", addr,config)