Add generate subcommand

This commit is contained in:
raul 2024-05-22 10:41:45 +02:00
parent 6389a914bd
commit b06ba4edf1
3 changed files with 119 additions and 1 deletions

38
cmd/generate.go Normal file
View File

@ -0,0 +1,38 @@
/*
Copyright © 2024 raul
*/
package cmd
import (
"github.com/spf13/cobra"
)
// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
generateCert()
},
}
func init() {
rootCmd.AddCommand(generateCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// generateCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// generateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

80
cmd/generateFunc.go Normal file
View File

@ -0,0 +1,80 @@
package cmd
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"log"
"math/big"
"os"
"time"
)
func generateCert() {
ca := &x509.Certificate{
SerialNumber: big.NewInt(2024),
Subject: pkix.Name{
Organization: []string{"Company, INC."},
Country: []string{"US"},
Province: []string{""},
Locality: []string{"San Francisco sucks"},
StreetAddress: []string{"Golden Gate Bridge"},
PostalCode: []string{"94016"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
caPrivKey, err := rsa.GenerateKey(rand.Reader, 3072)
if err != nil {
log.Fatalf("Error happened generating private key: %v\n", err)
}
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
log.Fatalf("Error happened creating certificate: %v\n", err)
}
//////
key, err := os.OpenFile("./server.key", os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatalf("Error happened opening file: %v\n", err)
}
defer key.Close()
cert, err := os.OpenFile("./server.crt", os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatalf("Error happened opening file: %v\n", err)
}
defer cert.Close()
//////
caPEM := new(bytes.Buffer)
pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
caPrivKeyPEM := new(bytes.Buffer)
pem.Encode(caPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey),
})
// readCert, err := io.ReadAll(caPEM)
// if err != nil {
// log.Fatalf("Error happened preparing to write cert: %v\n", err)
// }
cert.WriteString(string(caPEM.Bytes()))
// readKey, err := io.ReadAll(caPrivKeyPEM)
// if err != nil {
// log.Fatalf("Error happened preparing to write key: %v\n", err)
// }
key.WriteString(string(caPrivKeyPEM.Bytes()))
}

View File

@ -1,7 +1,7 @@
/*
Copyright © 2024 raul
*/
package cmd
import (