feat(env): pass secret to remote server. (#91)

* feat(env): pass secret to remote server.

* add testing

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-08-01 12:01:37 +08:00 committed by GitHub
parent e6d4fa77d1
commit ecfaecd46d
3 changed files with 50 additions and 0 deletions

12
main.go
View File

@ -115,6 +115,16 @@ func main() {
Usage: "proxy connection timeout", Usage: "proxy connection timeout",
EnvVar: "PLUGIN_PROXY_TIMEOUT,PROXY_SSH_TIMEOUT", EnvVar: "PLUGIN_PROXY_TIMEOUT,PROXY_SSH_TIMEOUT",
}, },
cli.StringSliceFlag{
Name: "secrets",
Usage: "plugin secret",
EnvVar: "PLUGIN_SECRETS",
},
cli.StringSliceFlag{
Name: "envs",
Usage: "Pass envs",
EnvVar: "PLUGIN_ENVS",
},
} }
// Override a template // Override a template
@ -169,6 +179,8 @@ func run(c *cli.Context) error {
Timeout: c.Duration("timeout"), Timeout: c.Duration("timeout"),
CommandTimeout: c.Int("command.timeout"), CommandTimeout: c.Int("command.timeout"),
Script: c.StringSlice("script"), Script: c.StringSlice("script"),
Secrets: c.StringSlice("secrets"),
Envs: c.StringSlice("envs"),
Proxy: easyssh.DefaultConfig{ Proxy: easyssh.DefaultConfig{
Key: c.String("proxy.ssh-key"), Key: c.String("proxy.ssh-key"),
KeyPath: c.String("proxy.key-path"), KeyPath: c.String("proxy.key-path"),

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -29,6 +30,8 @@ type (
Timeout time.Duration Timeout time.Duration
CommandTimeout int CommandTimeout int
Script []string Script []string
Secrets []string
Envs []string
Proxy easyssh.DefaultConfig Proxy easyssh.DefaultConfig
} }
@ -86,6 +89,15 @@ func (p Plugin) Exec() error {
}, },
} }
env := []string{}
for _, key := range p.Config.Envs {
val := os.Getenv(key)
val = strings.Replace(val, " ", "", -1)
env = append(env, key+"="+val)
}
p.Config.Script = append(env, p.Config.Script...)
p.log(host, "commands: ", strings.Join(p.Config.Script, "\n")) p.log(host, "commands: ", strings.Join(p.Config.Script, "\n"))
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout) stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout)
if err != nil { if err != nil {

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"os"
"testing" "testing"
"github.com/appleboy/easyssh-proxy" "github.com/appleboy/easyssh-proxy"
@ -229,3 +230,28 @@ func TestSSHCommandExitCodeError(t *testing.T) {
err := plugin.Exec() err := plugin.Exec()
assert.NotNil(t, err) assert.NotNil(t, err)
} }
func TestSetENV(t *testing.T) {
os.Setenv("FOO", "1")
plugin := Plugin{
Config: Config{
Host: []string{"localhost"},
UserName: "drone-scp",
Port: 22,
KeyPath: "./tests/.ssh/id_rsa",
Secrets: []string{"FOO"},
Envs: []string{"FOO"},
Script: []string{"whoami; echo $FOO"},
CommandTimeout: 1,
Proxy: easyssh.DefaultConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
},
}
err := plugin.Exec()
assert.Nil(t, err)
}