Add ability to specify SSH key via env var

This commit is contained in:
Bill Thiede 2024-12-24 14:50:25 -08:00
parent fba785f313
commit 39cdeb06c8

View File

@ -78,11 +78,15 @@ func init() {
func newPublicKey() ([]ssh.AuthMethod, error) { func newPublicKey() ([]ssh.AuthMethod, error) {
var signers []ssh.AuthMethod var signers []ssh.AuthMethod
possiblePaths := []string{
for _, path := range []string{
filepath.Join(os.Getenv("HOME"), ".ssh", "id_dsa"), filepath.Join(os.Getenv("HOME"), ".ssh", "id_dsa"),
filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa"), filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa"),
} { }
if p := os.Getenv("SSH_PRIVATE_KEY"); p != "" {
possiblePaths = append([]string{p}, possiblePaths...)
}
for _, path := range possiblePaths {
// A public key may be used to authenticate against the remote // A public key may be used to authenticate against the remote
// server by using an unencrypted PEM-encoded private key file. // server by using an unencrypted PEM-encoded private key file.
@ -97,6 +101,7 @@ func newPublicKey() ([]ssh.AuthMethod, error) {
return nil, fmt.Errorf("unable to read private key %q: %v", path, err) return nil, fmt.Errorf("unable to read private key %q: %v", path, err)
} }
glog.Infof("Using private key %q", path)
// Create the Signer for this private key. // Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key) signer, err := ssh.ParsePrivateKey(key)
if err != nil { if err != nil {