mirror of
https://github.com/appleboy/drone-ssh.git
synced 2025-07-02 21:52:51 +08:00
refactor: upgrade easyssh to support DefaultConfig
This commit is contained in:
parent
d447bbd595
commit
41fbf73407
3
main.go
3
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/appleboy/easyssh-proxy"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
_ "github.com/joho/godotenv/autoload"
|
_ "github.com/joho/godotenv/autoload"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@ -168,7 +169,7 @@ 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"),
|
||||||
Proxy: 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"),
|
||||||
User: c.String("proxy.username"),
|
User: c.String("proxy.username"),
|
||||||
|
14
plugin.go
14
plugin.go
@ -20,16 +20,6 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
defaultConfig struct {
|
|
||||||
User string
|
|
||||||
Server string
|
|
||||||
Key string
|
|
||||||
KeyPath string
|
|
||||||
Port string
|
|
||||||
Password string
|
|
||||||
Timeout time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config for the plugin.
|
// Config for the plugin.
|
||||||
Config struct {
|
Config struct {
|
||||||
Key string
|
Key string
|
||||||
@ -41,7 +31,7 @@ type (
|
|||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
CommandTimeout int
|
CommandTimeout int
|
||||||
Script []string
|
Script []string
|
||||||
Proxy defaultConfig
|
Proxy easyssh.DefaultConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin structure
|
// Plugin structure
|
||||||
@ -78,7 +68,7 @@ func (p Plugin) Exec() error {
|
|||||||
Key: p.Config.Key,
|
Key: p.Config.Key,
|
||||||
KeyPath: p.Config.KeyPath,
|
KeyPath: p.Config.KeyPath,
|
||||||
Timeout: p.Config.Timeout,
|
Timeout: p.Config.Timeout,
|
||||||
Proxy: defaultConfig{
|
Proxy: easyssh.DefaultConfig{
|
||||||
Server: p.Config.Proxy.Server,
|
Server: p.Config.Proxy.Server,
|
||||||
User: p.Config.Proxy.User,
|
User: p.Config.Proxy.User,
|
||||||
Password: p.Config.Proxy.Password,
|
Password: p.Config.Proxy.Password,
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/appleboy/easyssh-proxy"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -129,7 +130,7 @@ func TestProxyCommand(t *testing.T) {
|
|||||||
KeyPath: "./tests/.ssh/id_rsa",
|
KeyPath: "./tests/.ssh/id_rsa",
|
||||||
Script: []string{"whoami"},
|
Script: []string{"whoami"},
|
||||||
CommandTimeout: 1,
|
CommandTimeout: 1,
|
||||||
Proxy: defaultConfig{
|
Proxy: easyssh.DefaultConfig{
|
||||||
Server: "localhost",
|
Server: "localhost",
|
||||||
User: "drone-scp",
|
User: "drone-scp",
|
||||||
Port: "22",
|
Port: "22",
|
||||||
|
8
vendor/github.com/appleboy/easyssh-proxy/Makefile
generated
vendored
8
vendor/github.com/appleboy/easyssh-proxy/Makefile
generated
vendored
@ -1,4 +1,4 @@
|
|||||||
.PHONY: test drone-ssh build fmt vet errcheck lint install update release-dirs release-build release-copy release-check release coverage
|
.PHONY: test drone-ssh build fmt vet errcheck lint install update release-dirs release-build release-copy release-check release coverage embedmd
|
||||||
|
|
||||||
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
|
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
|
||||||
|
|
||||||
@ -28,6 +28,12 @@ unconvert:
|
|||||||
fi
|
fi
|
||||||
for PKG in $(PACKAGES); do unconvert -v $$PKG || exit 1; done;
|
for PKG in $(PACKAGES); do unconvert -v $$PKG || exit 1; done;
|
||||||
|
|
||||||
|
embedmd:
|
||||||
|
@hash embedmd > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
go get -u github.com/campoy/embedmd; \
|
||||||
|
fi
|
||||||
|
embedmd -d *.md
|
||||||
|
|
||||||
test:
|
test:
|
||||||
for PKG in $(PACKAGES); do go test -v -cover -coverprofile $$GOPATH/src/$$PKG/coverage.txt $$PKG || exit 1; done;
|
for PKG in $(PACKAGES); do go test -v -cover -coverprofile $$GOPATH/src/$$PKG/coverage.txt $$PKG || exit 1; done;
|
||||||
|
|
||||||
|
124
vendor/github.com/appleboy/easyssh-proxy/README.md
generated
vendored
124
vendor/github.com/appleboy/easyssh-proxy/README.md
generated
vendored
@ -1,5 +1,125 @@
|
|||||||
# easyssh-proxy
|
# easyssh-proxy
|
||||||
|
|
||||||
[](https://godoc.org/github.com/appleboy/easyssh-proxy) [](http://drone.wu-boy.com/appleboy/easyssh-proxy) [](https://codecov.io/gh/appleboy/easyssh-proxy) [](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy)
|
[](https://godoc.org/github.com/appleboy/easyssh-proxy) [](http://drone.wu-boy.com/appleboy/easyssh-proxy) [](https://codecov.io/gh/appleboy/easyssh-proxy) [](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy) [](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge)
|
||||||
|
|
||||||
easyssh-proxy provides a simple implementation of some SSH protocol features in Go
|
easyssh-proxy provides a simple implementation of some SSH protocol features in Go.
|
||||||
|
|
||||||
|
## Feature
|
||||||
|
|
||||||
|
This project is forked from [easyssh](https://github.com/hypersleep/easyssh) but add some features as the following.
|
||||||
|
|
||||||
|
* [x] Support plain text of user private key.
|
||||||
|
* [x] Support key path of user private key.
|
||||||
|
* [x] Support Timeout for the TCP connection to establish.
|
||||||
|
* [x] Support SSH ProxyCommand.
|
||||||
|
|
||||||
|
```
|
||||||
|
+--------+ +----------+ +-----------+
|
||||||
|
| Laptop | <--> | Jumphost | <--> | FooServer |
|
||||||
|
+--------+ +----------+ +-----------+
|
||||||
|
|
||||||
|
OR
|
||||||
|
|
||||||
|
+--------+ +----------+ +-----------+
|
||||||
|
| Laptop | <--> | Firewall | <--> | FooServer |
|
||||||
|
+--------+ +----------+ +-----------+
|
||||||
|
192.168.1.5 121.1.2.3 10.10.29.68
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
You can see `ssh`, `scp`, `ProxyCommand` on `examples` folder.
|
||||||
|
|
||||||
|
### ssh
|
||||||
|
|
||||||
|
See [example/ssh/ssh.go](./example/ssh/ssh.go)
|
||||||
|
|
||||||
|
[embedmd]:# (example/ssh/ssh.go go)
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/appleboy/easyssh-proxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Create MakeConfig instance with remote username, server address and path to private key.
|
||||||
|
ssh := &easyssh.MakeConfig{
|
||||||
|
User: "appleboy",
|
||||||
|
Server: "example.com",
|
||||||
|
// Optional key or Password without either we try to contact your agent SOCKET
|
||||||
|
//Password: "password",
|
||||||
|
Key: "/.ssh/id_rsa",
|
||||||
|
Port: "22",
|
||||||
|
Timeout: 60,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call Run method with command you want to run on remote server.
|
||||||
|
stdout, stderr, done, err := ssh.Run("ls -al", 60)
|
||||||
|
// Handle errors
|
||||||
|
if err != nil {
|
||||||
|
panic("Can't run remote command: " + err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### scp
|
||||||
|
|
||||||
|
See [example/scp/scp.go](./example/scp/scp.go)
|
||||||
|
|
||||||
|
[embedmd]:# (example/scp/scp.go go)
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/appleboy/easyssh-proxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Create MakeConfig instance with remote username, server address and path to private key.
|
||||||
|
ssh := &easyssh.MakeConfig{
|
||||||
|
User: "appleboy",
|
||||||
|
Server: "example.com",
|
||||||
|
Password: "123qwe",
|
||||||
|
Port: "22",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call Scp method with file you want to upload to remote server.
|
||||||
|
// Please make sure the `tmp` floder exists.
|
||||||
|
err := ssh.Scp("/root/source.csv", "/tmp/target.csv")
|
||||||
|
|
||||||
|
// Handle errors
|
||||||
|
if err != nil {
|
||||||
|
panic("Can't run remote command: " + err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println("success")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### SSH ProxyCommand
|
||||||
|
|
||||||
|
See [example/proxy/proxy.go](./example/proxy/proxy.go)
|
||||||
|
|
||||||
|
[embedmd]:# (example/proxy/proxy.go go /\tssh :=/ /\t}$/)
|
||||||
|
```go
|
||||||
|
ssh := &easyssh.MakeConfig{
|
||||||
|
User: "drone-scp",
|
||||||
|
Server: "localhost",
|
||||||
|
Port: "22",
|
||||||
|
KeyPath: "./tests/.ssh/id_rsa",
|
||||||
|
Proxy: easyssh.DefaultConfig{
|
||||||
|
User: "drone-scp",
|
||||||
|
Server: "localhost",
|
||||||
|
Port: "22",
|
||||||
|
KeyPath: "./tests/.ssh/id_rsa",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
45
vendor/github.com/appleboy/easyssh-proxy/easyssh.go
generated
vendored
45
vendor/github.com/appleboy/easyssh-proxy/easyssh.go
generated
vendored
@ -18,24 +18,14 @@ import (
|
|||||||
"golang.org/x/crypto/ssh/agent"
|
"golang.org/x/crypto/ssh/agent"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MakeConfig Contains main authority information.
|
|
||||||
// User field should be a name of user on remote server (ex. john in ssh john@example.com).
|
|
||||||
// Server field should be a remote machine address (ex. example.com in ssh john@example.com)
|
|
||||||
// Key is a path to private key on your local machine.
|
|
||||||
// Port is SSH server port on remote machine.
|
|
||||||
// Note: easyssh looking for private key in user's home directory (ex. /home/john + Key).
|
|
||||||
// Then ensure your Key begins from '/' (ex. /.ssh/id_rsa)
|
|
||||||
type (
|
type (
|
||||||
defaultConfig struct {
|
// MakeConfig Contains main authority information.
|
||||||
User string
|
// User field should be a name of user on remote server (ex. john in ssh john@example.com).
|
||||||
Server string
|
// Server field should be a remote machine address (ex. example.com in ssh john@example.com)
|
||||||
Key string
|
// Key is a path to private key on your local machine.
|
||||||
KeyPath string
|
// Port is SSH server port on remote machine.
|
||||||
Port string
|
// Note: easyssh looking for private key in user's home directory (ex. /home/john + Key).
|
||||||
Password string
|
// Then ensure your Key begins from '/' (ex. /.ssh/id_rsa)
|
||||||
Timeout time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
MakeConfig struct {
|
MakeConfig struct {
|
||||||
User string
|
User string
|
||||||
Server string
|
Server string
|
||||||
@ -44,21 +34,16 @@ type (
|
|||||||
Port string
|
Port string
|
||||||
Password string
|
Password string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Proxy struct {
|
Proxy DefaultConfig
|
||||||
User string
|
|
||||||
Server string
|
|
||||||
Key string
|
|
||||||
KeyPath string
|
|
||||||
Port string
|
|
||||||
Password string
|
|
||||||
Timeout time.Duration
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sshConfig struct {
|
// DefaultConfig for ssh proxy config
|
||||||
|
DefaultConfig struct {
|
||||||
User string
|
User string
|
||||||
|
Server string
|
||||||
Key string
|
Key string
|
||||||
KeyPath string
|
KeyPath string
|
||||||
|
Port string
|
||||||
Password string
|
Password string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
@ -80,7 +65,7 @@ func getKeyFile(keypath string) (ssh.Signer, error) {
|
|||||||
return pubkey, nil
|
return pubkey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSSHConfig(config sshConfig) *ssh.ClientConfig {
|
func getSSHConfig(config DefaultConfig) *ssh.ClientConfig {
|
||||||
// auths holds the detected ssh auth methods
|
// auths holds the detected ssh auth methods
|
||||||
auths := []ssh.AuthMethod{}
|
auths := []ssh.AuthMethod{}
|
||||||
|
|
||||||
@ -117,7 +102,7 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
|
|||||||
var client *ssh.Client
|
var client *ssh.Client
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
targetConfig := getSSHConfig(sshConfig{
|
targetConfig := getSSHConfig(DefaultConfig{
|
||||||
User: ssh_conf.User,
|
User: ssh_conf.User,
|
||||||
Key: ssh_conf.Key,
|
Key: ssh_conf.Key,
|
||||||
KeyPath: ssh_conf.KeyPath,
|
KeyPath: ssh_conf.KeyPath,
|
||||||
@ -127,7 +112,7 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
|
|||||||
|
|
||||||
// Enable proxy command
|
// Enable proxy command
|
||||||
if ssh_conf.Proxy.Server != "" {
|
if ssh_conf.Proxy.Server != "" {
|
||||||
proxyConfig := getSSHConfig(sshConfig{
|
proxyConfig := getSSHConfig(DefaultConfig{
|
||||||
User: ssh_conf.Proxy.User,
|
User: ssh_conf.Proxy.User,
|
||||||
Key: ssh_conf.Proxy.Key,
|
Key: ssh_conf.Proxy.Key,
|
||||||
KeyPath: ssh_conf.Proxy.KeyPath,
|
KeyPath: ssh_conf.Proxy.KeyPath,
|
||||||
|
10
vendor/vendor.json
vendored
10
vendor/vendor.json
vendored
@ -3,12 +3,12 @@
|
|||||||
"ignore": "test",
|
"ignore": "test",
|
||||||
"package": [
|
"package": [
|
||||||
{
|
{
|
||||||
"checksumSHA1": "knoaYH97GvBuW65HyXhR0CQwEJ8=",
|
"checksumSHA1": "KzIglkvAb68+jcmwajNkQLwa1R0=",
|
||||||
"path": "github.com/appleboy/easyssh-proxy",
|
"path": "github.com/appleboy/easyssh-proxy",
|
||||||
"revision": "438121ffb50f6f6de791bceb7046e74c1d818c3d",
|
"revision": "b5f58761a17ac54f2b0008fb8dcc883c73bf289d",
|
||||||
"revisionTime": "2017-03-04T08:24:35Z",
|
"revisionTime": "2017-03-14T07:47:33Z",
|
||||||
"version": "=1.1.0",
|
"version": "1.1.1",
|
||||||
"versionExact": "1.1.0"
|
"versionExact": "1.1.1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",
|
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",
|
||||||
|
Loading…
Reference in New Issue
Block a user