Skip to content
Snippets Groups Projects
Commit 135d7556 authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Added IdentityFile support for SSH executor

parent af913976
No related branches found
No related tags found
No related merge requests found
...@@ -173,10 +173,11 @@ This defines the SSH connection parameters. ...@@ -173,10 +173,11 @@ This defines the SSH connection parameters.
| Parameter | Explanation | | Parameter | Explanation |
| ---------- | ----------- | | ---------- | ----------- |
| `host` | where to connect (overriden when using `docker-ssh`) | | `host` | where to connect (overridden when using `docker-ssh`) |
| `port` | specify port, default: 22 | | `port` | specify port, default: 22 |
| `user` | specify user | | `user` | specify user |
| `password` | specify password | | `password` | specify password |
| `identity_file` | specify file path to SSH private key (id_rsa, id_dsa or id_edcsa). The file needs to be stored unencrypted |
Example: Example:
...@@ -186,6 +187,7 @@ Example: ...@@ -186,6 +187,7 @@ Example:
port = "22" port = "22"
user = "root" user = "root"
password = "production-server-password" password = "production-server-password"
identity_file = "
``` ```
### Note ### Note
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"code.google.com/p/go.crypto/ssh" "code.google.com/p/go.crypto/ssh"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"io/ioutil"
) )
type Command struct { type Command struct {
...@@ -25,14 +26,31 @@ type Command struct { ...@@ -25,14 +26,31 @@ type Command struct {
client *ssh.Client client *ssh.Client
} }
func (s *Command) getSSHAuthMethods() []ssh.AuthMethod { func (s *Command) getSSHKey(identityFile string) (key ssh.Signer, err error) {
buf, err := ioutil.ReadFile(identityFile)
if err != nil {
return nil, err
}
key, err = ssh.ParsePrivateKey(buf)
return key, err
}
func (s *Command) getSSHAuthMethods() ([]ssh.AuthMethod, error) {
var methods []ssh.AuthMethod var methods []ssh.AuthMethod
if s.Password != nil { if s.Password != nil {
methods = append(methods, ssh.Password(*s.Password)) methods = append(methods, ssh.Password(*s.Password))
} }
return methods if s.IdentityFile != nil {
key, err := s.getSSHKey(*s.IdentityFile)
if err != nil {
return nil, err
}
methods = append(methods, ssh.PublicKeys(key))
}
return methods, nil
} }
func (s *Command) Connect() error { func (s *Command) Connect() error {
...@@ -40,9 +58,14 @@ func (s *Command) Connect() error { ...@@ -40,9 +58,14 @@ func (s *Command) Connect() error {
user := helpers.StringOrDefault(s.User, "root") user := helpers.StringOrDefault(s.User, "root")
port := helpers.StringOrDefault(s.Port, "22") port := helpers.StringOrDefault(s.Port, "22")
methods, err := s.getSSHAuthMethods()
if err != nil {
return err
}
config := &ssh.ClientConfig{ config := &ssh.ClientConfig{
User: user, User: user,
Auth: s.getSSHAuthMethods(), Auth: methods,
} }
connectRetries := s.ConnectRetries connectRetries := s.ConnectRetries
......
package ssh package ssh
type Config struct { type Config struct {
User *string `toml:"user" json:"user"` User *string `toml:"user" json:"user"`
Password *string `toml:"password" json:"password"` Password *string `toml:"password" json:"password"`
Host *string `toml:"host" json:"host"` Host *string `toml:"host" json:"host"`
Port *string `toml:"port" json:"port"` Port *string `toml:"port" json:"port"`
IdentityFile *string `toml:"identity_file" json:"identity_file"`
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment