Return wrapped errors
This commit is contained in:
@ -4,20 +4,21 @@ import (
|
||||
"fmt"
|
||||
|
||||
"forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func createKey() {
|
||||
passphrase, err := getPassphrase()
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
key, err := crypto.CreateRSAKey(keySize)
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
privatePEM, err := crypto.EncodePrivateKeyToEncryptedPEM(key, passphrase)
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
fmt.Print(string(privatePEM))
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
peering "forge.cadoles.com/Cadoles/go-http-peering"
|
||||
)
|
||||
@ -11,11 +12,11 @@ import (
|
||||
func createToken() {
|
||||
privateKey, err := loadPrivateKey()
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
token, err := crypto.CreateServerToken(privateKey, tokenIssuer, peering.PeerID(tokenPeerID))
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
fmt.Println(token)
|
||||
}
|
||||
|
@ -4,16 +4,17 @@ import (
|
||||
"fmt"
|
||||
|
||||
"forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func getPublicKey() {
|
||||
privateKey, err := loadPrivateKey()
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
publicPEM, err := crypto.EncodePublicKeyToPEM(privateKey.Public())
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
handleError(errors.WithStack(err))
|
||||
}
|
||||
fmt.Print(string(publicPEM))
|
||||
}
|
||||
|
@ -6,13 +6,13 @@ import (
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
@ -29,14 +29,14 @@ func askPassphrase() ([]byte, error) {
|
||||
fmt.Print("Passphrase: ")
|
||||
passphrase, err := terminal.ReadPassword(syscall.Stdin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Print("Confirm passphrase: ")
|
||||
passphraseConfirmation, err := terminal.ReadPassword(syscall.Stdin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
@ -48,7 +48,6 @@ func askPassphrase() ([]byte, error) {
|
||||
}
|
||||
|
||||
func privateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, error) {
|
||||
|
||||
if passphrase == nil {
|
||||
return nil, errors.New("passphrase cannot be empty")
|
||||
}
|
||||
@ -61,7 +60,7 @@ func privateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, e
|
||||
|
||||
block, err := x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, passphrase, x509.PEMCipherAES256)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return pem.EncodeToMemory(block), nil
|
||||
@ -73,24 +72,24 @@ func loadPrivateKey() (*rsa.PrivateKey, error) {
|
||||
}
|
||||
pem, err := ioutil.ReadFile(keyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
passphrase, err := getPassphrase()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
privateKey, err := crypto.DecodePEMEncryptedPrivateKey(pem, passphrase)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
return privateKey, nil
|
||||
}
|
||||
|
||||
func handleError(err error) {
|
||||
if !debug {
|
||||
fmt.Println(err)
|
||||
fmt.Printf("%+v\n", errors.WithStack(err))
|
||||
} else {
|
||||
panic(err)
|
||||
panic(fmt.Sprintf("%+v", errors.WithStack(err)))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user