Check folders and store CA certs in ~/.config/cert400

This commit is contained in:
raul 2024-05-22 14:57:24 +02:00
parent 5b5ad6ee2f
commit e209f85cb0
2 changed files with 55 additions and 4 deletions

View File

@ -19,7 +19,7 @@ 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()
generateCA()
},
}

View File

@ -7,13 +7,18 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"os"
"time"
)
func generateCert() {
func generateCA() {
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Error happened looking up user home directory: %v\n", err)
}
ca := &x509.Certificate{
SerialNumber: big.NewInt(2024),
Subject: pkix.Name{
@ -41,13 +46,38 @@ func generateCert() {
}
//////
key, err := os.OpenFile("./server.key", os.O_WRONLY|os.O_CREATE, 0600)
//fmt.Printf("Checking %v/.config/\n", home)
_, err = os.Stat(home + "/.config")
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("$HOME/.config doesn't exist, creating...\n")
os.Mkdir(home+"/.config", 0700)
} else {
log.Fatalf("Error happened accessing .config: %v", err)
}
}
//fmt.Printf("Checking %v/.config/cert400\n", home)
_, err = os.Stat(home + "/.config/cert400")
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("$HOME/.config/cert400 doesn't exist, creating...\n")
os.Mkdir(home+"/.config/cert400", 0700)
} else {
log.Fatalf("Error happened accessing cert400: %v\n", err)
}
}
fmt.Printf("Generating %v/.config/cert400/server.key...\n", home)
// TODO: Check if keys already exist and warn the user about it
key, err := os.OpenFile(home+"/.config/cert400/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)
fmt.Printf("Generating %v/.config/cert400/server.cert...\n", home)
cert, err := os.OpenFile(home+"/.config/cert400/server.crt", os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatalf("Error happened opening file: %v\n", err)
}
@ -78,3 +108,24 @@ func generateCert() {
key.WriteString(string(caPrivKeyPEM.Bytes()))
}
func generateCert() {
// cert := &x509.Certificate{
// SerialNumber: big.NewInt(1658),
// Subject: pkix.Name{
// Organization: []string{"LOL Company"},
// Country: []string{"US"},
// Province: []string{""},
// Locality: []string{"San Francisco"},
// StreetAddress: []string{"Golden Gate Bridge"},
// PostalCode: []string{"94016"},
// },
// PermittedDNSDomains: []string{"test.bulgariu.xyz"},
// NotBefore: time.Now(),
// NotAfter: time.Now().AddDate(10, 0, 0),
// SubjectKeyId: []byte{1, 2, 3, 4, 6},
// ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
// KeyUsage: x509.KeyUsageDigitalSignature,
// }
}