// This shows an example of how to generate a SSH RSA Private/Public key pair and save it locally

package helper

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"io/ioutil"
	"log"

	"github.com/sirupsen/logrus"
	"golang.org/x/crypto/ssh"
)

// GenerateSSHKeys create SSH keys (private and public) into a given directory
func GenerateSSHKeys(dir string) {
	savePrivateFileTo := dir + "/id_rsa"
	savePublicFileTo := dir + "/id_rsa.pub"
	bitSize := 4096

	privateKey, err := GeneratePrivateSSHKey(bitSize)
	if err != nil {
		log.Fatal(err.Error())
	}

	publicKeyBytes, err := GeneratePublicSSHKey(&privateKey.PublicKey)
	if err != nil {
		log.Fatal(err.Error())
	}

	privateKeyBytes := EncodePrivateSSHKeyToPEM(privateKey)

	err = WriteSSHKeyToFile(privateKeyBytes, savePrivateFileTo)
	if err != nil {
		log.Fatal(err.Error())
	}

	err = WriteSSHKeyToFile([]byte(publicKeyBytes), savePublicFileTo)
	if err != nil {
		log.Fatal(err.Error())
	}
}

// GeneratePrivateSSHKey creates a RSA Private Key of specified byte size
func GeneratePrivateSSHKey(bitSize int) (*rsa.PrivateKey, error) {
	// Private Key generation
	privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
	if err != nil {
		return nil, err
	}

	// Validate Private Key
	err = privateKey.Validate()
	if err != nil {
		return nil, err
	}

	logrus.Traceln("Private Key generated")
	return privateKey, nil
}

// EncodePrivateSSHKeyToPEM encodes Private Key from RSA to PEM format
func EncodePrivateSSHKeyToPEM(privateKey *rsa.PrivateKey) []byte {
	// Get ASN.1 DER format
	privDER := x509.MarshalPKCS1PrivateKey(privateKey)

	// pem.Block
	privBlock := pem.Block{
		Type:    "RSA PRIVATE KEY",
		Headers: nil,
		Bytes:   privDER,
	}

	// Private key in PEM format
	privatePEM := pem.EncodeToMemory(&privBlock)

	return privatePEM
}

// GeneratePublicSSHKey take a rsa.PublicKey and return bytes suitable for writing to .pub file
// returns in the format "ssh-rsa ..."
func GeneratePublicSSHKey(privatekey *rsa.PublicKey) ([]byte, error) {
	publicRsaKey, err := ssh.NewPublicKey(privatekey)
	if err != nil {
		return nil, err
	}

	pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)

	logrus.Traceln("Public key generated")
	return pubKeyBytes, nil
}

// WriteSSHKeyToFile writes keys to a file
func WriteSSHKeyToFile(keyBytes []byte, saveFileTo string) error {
	err := ioutil.WriteFile(saveFileTo, keyBytes, 0600)
	if err != nil {
		return err
	}

	log.Printf("Key saved to: %s", saveFileTo)
	return nil
}
