mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-11 18:15:28 +00:00
81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
|
// Copyright 2020 Mohammed El Bahja. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT license.
|
||
|
|
||
|
package goph
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net"
|
||
|
"os"
|
||
|
|
||
|
"golang.org/x/crypto/ssh"
|
||
|
"golang.org/x/crypto/ssh/agent"
|
||
|
)
|
||
|
|
||
|
// Auth represents ssh auth methods.
|
||
|
type Auth []ssh.AuthMethod
|
||
|
|
||
|
// Password returns password auth method.
|
||
|
func Password(pass string) Auth {
|
||
|
return Auth{
|
||
|
ssh.Password(pass),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Key returns auth method from private key with or without passphrase.
|
||
|
func Key(prvFile string, passphrase string) (Auth, error) {
|
||
|
|
||
|
signer, err := GetSigner(prvFile, passphrase)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return Auth{
|
||
|
ssh.PublicKeys(signer),
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// HasAgent checks if ssh agent exists.
|
||
|
func HasAgent() bool {
|
||
|
return os.Getenv("SSH_AUTH_SOCK") != ""
|
||
|
}
|
||
|
|
||
|
// UseAgent auth via ssh agent, (Unix systems only)
|
||
|
func UseAgent() (Auth, error) {
|
||
|
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not find ssh agent: %w", err)
|
||
|
}
|
||
|
return Auth{
|
||
|
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// GetSigner returns ssh signer from private key file.
|
||
|
func GetSigner(prvFile string, passphrase string) (ssh.Signer, error) {
|
||
|
|
||
|
var (
|
||
|
err error
|
||
|
signer ssh.Signer
|
||
|
)
|
||
|
|
||
|
privateKey, err := ioutil.ReadFile(prvFile)
|
||
|
|
||
|
if err != nil {
|
||
|
|
||
|
return nil, err
|
||
|
|
||
|
} else if passphrase != "" {
|
||
|
|
||
|
signer, err = ssh.ParsePrivateKeyWithPassphrase(privateKey, []byte(passphrase))
|
||
|
|
||
|
} else {
|
||
|
|
||
|
signer, err = ssh.ParsePrivateKey(privateKey)
|
||
|
}
|
||
|
|
||
|
return signer, err
|
||
|
}
|