feat(dir2config): defaults
This commit is contained in:
322
vendor/gopkg.in/src-d/go-git.v4/plumbing/transport/ssh/auth_method.go
generated
vendored
Normal file
322
vendor/gopkg.in/src-d/go-git.v4/plumbing/transport/ssh/auth_method.go
generated
vendored
Normal file
@ -0,0 +1,322 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/xanzy/ssh-agent"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
const DefaultUsername = "git"
|
||||
|
||||
// AuthMethod is the interface all auth methods for the ssh client
|
||||
// must implement. The clientConfig method returns the ssh client
|
||||
// configuration needed to establish an ssh connection.
|
||||
type AuthMethod interface {
|
||||
transport.AuthMethod
|
||||
// ClientConfig should return a valid ssh.ClientConfig to be used to create
|
||||
// a connection to the SSH server.
|
||||
ClientConfig() (*ssh.ClientConfig, error)
|
||||
}
|
||||
|
||||
// The names of the AuthMethod implementations. To be returned by the
|
||||
// Name() method. Most git servers only allow PublicKeysName and
|
||||
// PublicKeysCallbackName.
|
||||
const (
|
||||
KeyboardInteractiveName = "ssh-keyboard-interactive"
|
||||
PasswordName = "ssh-password"
|
||||
PasswordCallbackName = "ssh-password-callback"
|
||||
PublicKeysName = "ssh-public-keys"
|
||||
PublicKeysCallbackName = "ssh-public-key-callback"
|
||||
)
|
||||
|
||||
// KeyboardInteractive implements AuthMethod by using a
|
||||
// prompt/response sequence controlled by the server.
|
||||
type KeyboardInteractive struct {
|
||||
User string
|
||||
Challenge ssh.KeyboardInteractiveChallenge
|
||||
HostKeyCallbackHelper
|
||||
}
|
||||
|
||||
func (a *KeyboardInteractive) Name() string {
|
||||
return KeyboardInteractiveName
|
||||
}
|
||||
|
||||
func (a *KeyboardInteractive) String() string {
|
||||
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
|
||||
}
|
||||
|
||||
func (a *KeyboardInteractive) ClientConfig() (*ssh.ClientConfig, error) {
|
||||
return a.SetHostKeyCallback(&ssh.ClientConfig{
|
||||
User: a.User,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.KeyboardInteractiveChallenge(a.Challenge),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Password implements AuthMethod by using the given password.
|
||||
type Password struct {
|
||||
User string
|
||||
Password string
|
||||
HostKeyCallbackHelper
|
||||
}
|
||||
|
||||
func (a *Password) Name() string {
|
||||
return PasswordName
|
||||
}
|
||||
|
||||
func (a *Password) String() string {
|
||||
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
|
||||
}
|
||||
|
||||
func (a *Password) ClientConfig() (*ssh.ClientConfig, error) {
|
||||
return a.SetHostKeyCallback(&ssh.ClientConfig{
|
||||
User: a.User,
|
||||
Auth: []ssh.AuthMethod{ssh.Password(a.Password)},
|
||||
})
|
||||
}
|
||||
|
||||
// PasswordCallback implements AuthMethod by using a callback
|
||||
// to fetch the password.
|
||||
type PasswordCallback struct {
|
||||
User string
|
||||
Callback func() (pass string, err error)
|
||||
HostKeyCallbackHelper
|
||||
}
|
||||
|
||||
func (a *PasswordCallback) Name() string {
|
||||
return PasswordCallbackName
|
||||
}
|
||||
|
||||
func (a *PasswordCallback) String() string {
|
||||
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
|
||||
}
|
||||
|
||||
func (a *PasswordCallback) ClientConfig() (*ssh.ClientConfig, error) {
|
||||
return a.SetHostKeyCallback(&ssh.ClientConfig{
|
||||
User: a.User,
|
||||
Auth: []ssh.AuthMethod{ssh.PasswordCallback(a.Callback)},
|
||||
})
|
||||
}
|
||||
|
||||
// PublicKeys implements AuthMethod by using the given key pairs.
|
||||
type PublicKeys struct {
|
||||
User string
|
||||
Signer ssh.Signer
|
||||
HostKeyCallbackHelper
|
||||
}
|
||||
|
||||
// NewPublicKeys returns a PublicKeys from a PEM encoded private key. An
|
||||
// encryption password should be given if the pemBytes contains a password
|
||||
// encrypted PEM block otherwise password should be empty. It supports RSA
|
||||
// (PKCS#1), DSA (OpenSSL), and ECDSA private keys.
|
||||
func NewPublicKeys(user string, pemBytes []byte, password string) (*PublicKeys, error) {
|
||||
block, _ := pem.Decode(pemBytes)
|
||||
if block == nil {
|
||||
return nil, errors.New("invalid PEM data")
|
||||
}
|
||||
if x509.IsEncryptedPEMBlock(block) {
|
||||
key, err := x509.DecryptPEMBlock(block, []byte(password))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block = &pem.Block{Type: block.Type, Bytes: key}
|
||||
pemBytes = pem.EncodeToMemory(block)
|
||||
}
|
||||
|
||||
signer, err := ssh.ParsePrivateKey(pemBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PublicKeys{User: user, Signer: signer}, nil
|
||||
}
|
||||
|
||||
// NewPublicKeysFromFile returns a PublicKeys from a file containing a PEM
|
||||
// encoded private key. An encryption password should be given if the pemBytes
|
||||
// contains a password encrypted PEM block otherwise password should be empty.
|
||||
func NewPublicKeysFromFile(user, pemFile, password string) (*PublicKeys, error) {
|
||||
bytes, err := ioutil.ReadFile(pemFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewPublicKeys(user, bytes, password)
|
||||
}
|
||||
|
||||
func (a *PublicKeys) Name() string {
|
||||
return PublicKeysName
|
||||
}
|
||||
|
||||
func (a *PublicKeys) String() string {
|
||||
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
|
||||
}
|
||||
|
||||
func (a *PublicKeys) ClientConfig() (*ssh.ClientConfig, error) {
|
||||
return a.SetHostKeyCallback(&ssh.ClientConfig{
|
||||
User: a.User,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(a.Signer)},
|
||||
})
|
||||
}
|
||||
|
||||
func username() (string, error) {
|
||||
var username string
|
||||
if user, err := user.Current(); err == nil {
|
||||
username = user.Username
|
||||
} else {
|
||||
username = os.Getenv("USER")
|
||||
}
|
||||
|
||||
if username == "" {
|
||||
return "", errors.New("failed to get username")
|
||||
}
|
||||
|
||||
return username, nil
|
||||
}
|
||||
|
||||
// PublicKeysCallback implements AuthMethod by asking a
|
||||
// ssh.agent.Agent to act as a signer.
|
||||
type PublicKeysCallback struct {
|
||||
User string
|
||||
Callback func() (signers []ssh.Signer, err error)
|
||||
HostKeyCallbackHelper
|
||||
}
|
||||
|
||||
// NewSSHAgentAuth returns a PublicKeysCallback based on a SSH agent, it opens
|
||||
// a pipe with the SSH agent and uses the pipe as the implementer of the public
|
||||
// key callback function.
|
||||
func NewSSHAgentAuth(u string) (*PublicKeysCallback, error) {
|
||||
var err error
|
||||
if u == "" {
|
||||
u, err = username()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
a, _, err := sshagent.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating SSH agent: %q", err)
|
||||
}
|
||||
|
||||
return &PublicKeysCallback{
|
||||
User: u,
|
||||
Callback: a.Signers,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *PublicKeysCallback) Name() string {
|
||||
return PublicKeysCallbackName
|
||||
}
|
||||
|
||||
func (a *PublicKeysCallback) String() string {
|
||||
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
|
||||
}
|
||||
|
||||
func (a *PublicKeysCallback) ClientConfig() (*ssh.ClientConfig, error) {
|
||||
return a.SetHostKeyCallback(&ssh.ClientConfig{
|
||||
User: a.User,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(a.Callback)},
|
||||
})
|
||||
}
|
||||
|
||||
// NewKnownHostsCallback returns ssh.HostKeyCallback based on a file based on a
|
||||
// known_hosts file. http://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT
|
||||
//
|
||||
// If list of files is empty, then it will be read from the SSH_KNOWN_HOSTS
|
||||
// environment variable, example:
|
||||
// /home/foo/custom_known_hosts_file:/etc/custom_known/hosts_file
|
||||
//
|
||||
// If SSH_KNOWN_HOSTS is not set the following file locations will be used:
|
||||
// ~/.ssh/known_hosts
|
||||
// /etc/ssh/ssh_known_hosts
|
||||
func NewKnownHostsCallback(files ...string) (ssh.HostKeyCallback, error) {
|
||||
var err error
|
||||
|
||||
if len(files) == 0 {
|
||||
if files, err = getDefaultKnownHostsFiles(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if files, err = filterKnownHostsFiles(files...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return knownhosts.New(files...)
|
||||
}
|
||||
|
||||
func getDefaultKnownHostsFiles() ([]string, error) {
|
||||
files := filepath.SplitList(os.Getenv("SSH_KNOWN_HOSTS"))
|
||||
if len(files) != 0 {
|
||||
return files, nil
|
||||
}
|
||||
|
||||
homeDirPath, err := homedir.Dir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []string{
|
||||
filepath.Join(homeDirPath, "/.ssh/known_hosts"),
|
||||
"/etc/ssh/ssh_known_hosts",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func filterKnownHostsFiles(files ...string) ([]string, error) {
|
||||
var out []string
|
||||
for _, file := range files {
|
||||
_, err := os.Stat(file)
|
||||
if err == nil {
|
||||
out = append(out, file)
|
||||
continue
|
||||
}
|
||||
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("unable to find any valid known_hosts file, set SSH_KNOWN_HOSTS env variable")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// HostKeyCallbackHelper is a helper that provides common functionality to
|
||||
// configure HostKeyCallback into a ssh.ClientConfig.
|
||||
type HostKeyCallbackHelper struct {
|
||||
// HostKeyCallback is the function type used for verifying server keys.
|
||||
// If nil default callback will be create using NewKnownHostsCallback
|
||||
// without argument.
|
||||
HostKeyCallback ssh.HostKeyCallback
|
||||
}
|
||||
|
||||
// SetHostKeyCallback sets the field HostKeyCallback in the given cfg. If
|
||||
// HostKeyCallback is empty a default callback is created using
|
||||
// NewKnownHostsCallback.
|
||||
func (m *HostKeyCallbackHelper) SetHostKeyCallback(cfg *ssh.ClientConfig) (*ssh.ClientConfig, error) {
|
||||
var err error
|
||||
if m.HostKeyCallback == nil {
|
||||
if m.HostKeyCallback, err = NewKnownHostsCallback(); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
}
|
||||
|
||||
cfg.HostKeyCallback = m.HostKeyCallback
|
||||
return cfg, nil
|
||||
}
|
203
vendor/gopkg.in/src-d/go-git.v4/plumbing/transport/ssh/common.go
generated
vendored
Normal file
203
vendor/gopkg.in/src-d/go-git.v4/plumbing/transport/ssh/common.go
generated
vendored
Normal file
@ -0,0 +1,203 @@
|
||||
// Package ssh implements the SSH transport protocol.
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport/internal/common"
|
||||
|
||||
"github.com/kevinburke/ssh_config"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// DefaultClient is the default SSH client.
|
||||
var DefaultClient = NewClient(nil)
|
||||
|
||||
// DefaultSSHConfig is the reader used to access parameters stored in the
|
||||
// system's ssh_config files. If nil all the ssh_config are ignored.
|
||||
var DefaultSSHConfig sshConfig = ssh_config.DefaultUserSettings
|
||||
|
||||
type sshConfig interface {
|
||||
Get(alias, key string) string
|
||||
}
|
||||
|
||||
// NewClient creates a new SSH client with an optional *ssh.ClientConfig.
|
||||
func NewClient(config *ssh.ClientConfig) transport.Transport {
|
||||
return common.NewClient(&runner{config: config})
|
||||
}
|
||||
|
||||
// DefaultAuthBuilder is the function used to create a default AuthMethod, when
|
||||
// the user doesn't provide any.
|
||||
var DefaultAuthBuilder = func(user string) (AuthMethod, error) {
|
||||
return NewSSHAgentAuth(user)
|
||||
}
|
||||
|
||||
const DefaultPort = 22
|
||||
|
||||
type runner struct {
|
||||
config *ssh.ClientConfig
|
||||
}
|
||||
|
||||
func (r *runner) Command(cmd string, ep *transport.Endpoint, auth transport.AuthMethod) (common.Command, error) {
|
||||
c := &command{command: cmd, endpoint: ep, config: r.config}
|
||||
if auth != nil {
|
||||
c.setAuth(auth)
|
||||
}
|
||||
|
||||
if err := c.connect(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type command struct {
|
||||
*ssh.Session
|
||||
connected bool
|
||||
command string
|
||||
endpoint *transport.Endpoint
|
||||
client *ssh.Client
|
||||
auth AuthMethod
|
||||
config *ssh.ClientConfig
|
||||
}
|
||||
|
||||
func (c *command) setAuth(auth transport.AuthMethod) error {
|
||||
a, ok := auth.(AuthMethod)
|
||||
if !ok {
|
||||
return transport.ErrInvalidAuthMethod
|
||||
}
|
||||
|
||||
c.auth = a
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *command) Start() error {
|
||||
return c.Session.Start(endpointToCommand(c.command, c.endpoint))
|
||||
}
|
||||
|
||||
// Close closes the SSH session and connection.
|
||||
func (c *command) Close() error {
|
||||
if !c.connected {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.connected = false
|
||||
|
||||
//XXX: If did read the full packfile, then the session might be already
|
||||
// closed.
|
||||
_ = c.Session.Close()
|
||||
|
||||
return c.client.Close()
|
||||
}
|
||||
|
||||
// connect connects to the SSH server, unless a AuthMethod was set with
|
||||
// SetAuth method, by default uses an auth method based on PublicKeysCallback,
|
||||
// it connects to a SSH agent, using the address stored in the SSH_AUTH_SOCK
|
||||
// environment var.
|
||||
func (c *command) connect() error {
|
||||
if c.connected {
|
||||
return transport.ErrAlreadyConnected
|
||||
}
|
||||
|
||||
if c.auth == nil {
|
||||
if err := c.setAuthFromEndpoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
config, err := c.auth.ClientConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
overrideConfig(c.config, config)
|
||||
|
||||
c.client, err = ssh.Dial("tcp", c.getHostWithPort(), config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Session, err = c.client.NewSession()
|
||||
if err != nil {
|
||||
_ = c.client.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
c.connected = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *command) getHostWithPort() string {
|
||||
if addr, found := c.doGetHostWithPortFromSSHConfig(); found {
|
||||
return addr
|
||||
}
|
||||
|
||||
host := c.endpoint.Host
|
||||
port := c.endpoint.Port
|
||||
if port <= 0 {
|
||||
port = DefaultPort
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s:%d", host, port)
|
||||
}
|
||||
|
||||
func (c *command) doGetHostWithPortFromSSHConfig() (addr string, found bool) {
|
||||
if DefaultSSHConfig == nil {
|
||||
return
|
||||
}
|
||||
|
||||
host := c.endpoint.Host
|
||||
port := c.endpoint.Port
|
||||
|
||||
configHost := DefaultSSHConfig.Get(c.endpoint.Host, "Hostname")
|
||||
if configHost != "" {
|
||||
host = configHost
|
||||
found = true
|
||||
}
|
||||
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
configPort := DefaultSSHConfig.Get(c.endpoint.Host, "Port")
|
||||
if configPort != "" {
|
||||
if i, err := strconv.Atoi(configPort); err == nil {
|
||||
port = i
|
||||
}
|
||||
}
|
||||
|
||||
addr = fmt.Sprintf("%s:%d", host, port)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *command) setAuthFromEndpoint() error {
|
||||
var err error
|
||||
c.auth, err = DefaultAuthBuilder(c.endpoint.User)
|
||||
return err
|
||||
}
|
||||
|
||||
func endpointToCommand(cmd string, ep *transport.Endpoint) string {
|
||||
return fmt.Sprintf("%s '%s'", cmd, ep.Path)
|
||||
}
|
||||
|
||||
func overrideConfig(overrides *ssh.ClientConfig, c *ssh.ClientConfig) {
|
||||
if overrides == nil {
|
||||
return
|
||||
}
|
||||
|
||||
t := reflect.TypeOf(*c)
|
||||
vc := reflect.ValueOf(c).Elem()
|
||||
vo := reflect.ValueOf(overrides).Elem()
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
vcf := vc.FieldByName(f.Name)
|
||||
vof := vo.FieldByName(f.Name)
|
||||
vcf.Set(vof)
|
||||
}
|
||||
|
||||
*c = vc.Interface().(ssh.ClientConfig)
|
||||
}
|
Reference in New Issue
Block a user