2019-02-22 17:35:49 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rsa"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2024-01-05 11:12:27 +01:00
|
|
|
"strings"
|
2019-02-22 17:35:49 +01:00
|
|
|
"syscall"
|
|
|
|
|
2019-10-16 11:09:29 +02:00
|
|
|
"forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
2022-09-12 17:46:59 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-02-22 17:35:49 +01:00
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getPassphrase() ([]byte, error) {
|
2019-03-29 15:15:38 +01:00
|
|
|
passphrase, exists := os.LookupEnv("KEY_PASSPHRASE")
|
|
|
|
if exists {
|
2019-02-22 17:35:49 +01:00
|
|
|
return []byte(passphrase), nil
|
|
|
|
}
|
|
|
|
return askPassphrase()
|
|
|
|
}
|
|
|
|
|
|
|
|
func askPassphrase() ([]byte, error) {
|
|
|
|
fmt.Print("Passphrase: ")
|
|
|
|
passphrase, err := terminal.ReadPassword(syscall.Stdin)
|
|
|
|
if err != nil {
|
2022-09-12 17:46:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-22 17:35:49 +01:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Print("Confirm passphrase: ")
|
|
|
|
passphraseConfirmation, err := terminal.ReadPassword(syscall.Stdin)
|
|
|
|
if err != nil {
|
2022-09-12 17:46:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-22 17:35:49 +01:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if !bytes.Equal(passphrase, passphraseConfirmation) {
|
|
|
|
return nil, errors.New("passphrases does not match")
|
|
|
|
}
|
|
|
|
|
|
|
|
return passphrase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPrivateKey() (*rsa.PrivateKey, error) {
|
|
|
|
if keyFile == "" {
|
|
|
|
return nil, errors.New("you must specify a key file to load")
|
|
|
|
}
|
|
|
|
pem, err := ioutil.ReadFile(keyFile)
|
|
|
|
if err != nil {
|
2022-09-12 17:46:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-22 17:35:49 +01:00
|
|
|
}
|
|
|
|
passphrase, err := getPassphrase()
|
|
|
|
if err != nil {
|
2022-09-12 17:46:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-22 17:35:49 +01:00
|
|
|
}
|
|
|
|
privateKey, err := crypto.DecodePEMEncryptedPrivateKey(pem, passphrase)
|
|
|
|
if err != nil {
|
2022-09-12 17:46:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-22 17:35:49 +01:00
|
|
|
}
|
|
|
|
return privateKey, nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 11:12:27 +01:00
|
|
|
func loadToken() (string, error) {
|
2024-01-04 16:21:40 +01:00
|
|
|
if tokenFile == "" {
|
2024-01-05 11:12:27 +01:00
|
|
|
return "", errors.New("you must specify a token file to load")
|
2024-01-04 16:21:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
token, err := os.ReadFile(tokenFile)
|
|
|
|
if err != nil {
|
2024-01-05 11:12:27 +01:00
|
|
|
return "", errors.WithStack(err)
|
2024-01-04 16:21:40 +01:00
|
|
|
}
|
|
|
|
|
2024-01-05 11:12:27 +01:00
|
|
|
return strings.TrimSpace(string(token)), nil
|
2024-01-04 16:21:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:35:49 +01:00
|
|
|
func handleError(err error) {
|
|
|
|
if !debug {
|
2022-09-12 17:46:59 +02:00
|
|
|
fmt.Printf("%+v\n", errors.WithStack(err))
|
2019-02-22 17:35:49 +01:00
|
|
|
} else {
|
2022-09-12 17:46:59 +02:00
|
|
|
panic(fmt.Sprintf("%+v", errors.WithStack(err)))
|
2019-02-22 17:35:49 +01:00
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|