51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
func CheckOrCreateRootKeyCertificate() error {
|
|
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
|
if err == nil {
|
|
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.key"))
|
|
if err == nil {
|
|
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.crt"))
|
|
if err == nil {
|
|
return fmt.Errorf("%s", "key already exists")
|
|
} else if os.IsNotExist(err) {
|
|
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
|
err = CreateRootKeyCertificate()
|
|
if err != nil {
|
|
fmt.Println("error creating root key certificate:", err)
|
|
return fmt.Errorf("%s", "error creating root key certificate")
|
|
}
|
|
} else {
|
|
fmt.Println("error checking certificate directory:", err)
|
|
return fmt.Errorf("%s", "error checking certificate directory")
|
|
}
|
|
} else if os.IsNotExist(err) {
|
|
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
|
err = CreateRootKeyCertificate()
|
|
if err != nil {
|
|
fmt.Println("error creating root key certificate:", err)
|
|
return fmt.Errorf("%s", "error creating root key certificate")
|
|
}
|
|
} else {
|
|
fmt.Println("error checking certificate directory:", err)
|
|
return fmt.Errorf("%s", "error checking certificate directory")
|
|
}
|
|
} else if os.IsNotExist(err) {
|
|
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
|
err = CreateRootKeyCertificate()
|
|
if err != nil {
|
|
fmt.Println("error creating root key certificate:", err)
|
|
return fmt.Errorf("%s", "error creating root key certificate")
|
|
}
|
|
} else {
|
|
fmt.Println("error checking certificate directory:", err)
|
|
return fmt.Errorf("%s", "error checking certificate directory")
|
|
}
|
|
return nil
|
|
}
|