From d5da23849f6fc6f89a5ce6cb6f79c3e56fb76dfe Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 21 Jul 2025 22:16:54 +0700 Subject: [PATCH] refactor --- api/Certs.go | 4 +-- api/CreateCert.go | 40 +++------------------- api/RootCert.go | 4 +-- {utils => crt}/CreateRootKeyCertificate.go | 6 ++-- {utils => crt}/CreateServerCert.go | 2 +- {utils => crt}/LoadRootCertAndKey.go | 2 +- {utils => crt}/ParseCert.go | 2 +- {utils => crt}/SavePEMFile.go | 2 +- 8 files changed, 16 insertions(+), 46 deletions(-) rename {utils => crt}/CreateRootKeyCertificate.go (94%) rename {utils => crt}/CreateServerCert.go (98%) rename {utils => crt}/LoadRootCertAndKey.go (98%) rename {utils => crt}/ParseCert.go (98%) rename {utils => crt}/SavePEMFile.go (95%) diff --git a/api/Certs.go b/api/Certs.go index a8f9b19..b06fa90 100644 --- a/api/Certs.go +++ b/api/Certs.go @@ -5,7 +5,7 @@ import ( "fmt" "net/http" "os" - "scm/utils" + "scm/crt" "github.com/google/uuid" ) @@ -47,7 +47,7 @@ func Certs(w http.ResponseWriter, r *http.Request) { new_cert["href"] = fmt.Sprintf("https://%s", entry.Name()) new_cert["cert"] = string(certdata) new_cert["key"] = string(keydata) - new_cert["certinfo"] = utils.ParseCert(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name())) + new_cert["certinfo"] = crt.ParseCert(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name())) certs_info = append(certs_info, new_cert) } } diff --git a/api/CreateCert.go b/api/CreateCert.go index 89aaabc..339d54a 100644 --- a/api/CreateCert.go +++ b/api/CreateCert.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "os" + "scm/crt" "scm/model" "scm/utils" ) @@ -22,7 +23,7 @@ func CreateCert(w http.ResponseWriter, r *http.Request) { http.Error(w, "invalid JSON format", http.StatusBadRequest) return } - caCert, caKey, err := utils.LoadRootCertAndKey( + caCert, caKey, err := crt.LoadRootCertAndKey( fmt.Sprintf( "%s/%s/%s.crt", os.Getenv("CERTS_PATH"), @@ -49,7 +50,7 @@ func CreateCert(w http.ResponseWriter, r *http.Request) { fmt.Println("error generating server key:", err) return } - serverCert, err := utils.CreateServerCert(caCert, caKey, serverKey, requestData) + serverCert, err := crt.CreateServerCert(caCert, caKey, serverKey, requestData) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) fmt.Println("error creating server certificate:", err) @@ -60,7 +61,7 @@ func CreateCert(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - err = utils.SavePEMFile( + err = crt.SavePEMFile( fmt.Sprintf( "%s/%s/%s.crt", os.Getenv("CERTS_PATH"), @@ -75,7 +76,7 @@ func CreateCert(w http.ResponseWriter, r *http.Request) { fmt.Println("error saving certificate:", err) return } - err = utils.SavePEMFile( + err = crt.SavePEMFile( fmt.Sprintf( "%s/%s/%s.key", os.Getenv("CERTS_PATH"), @@ -92,34 +93,3 @@ func CreateCert(w http.ResponseWriter, r *http.Request) { } fmt.Fprintf(w, "%s", `{"status": "success", "message": "Certificate created successfully"}`) } - -// func createServerCert( -// caCert *x509.Certificate, -// caKey *rsa.PrivateKey, -// serverKey *rsa.PrivateKey, -// certdata model.CreateCertificateRequest, -// ) ([]byte, error) { -// // Шаблон серверного сертификата -// template := x509.Certificate{ -// SerialNumber: big.NewInt(time.Now().Unix()), -// Subject: pkix.Name{ -// Organization: certdata.OrganizationName, -// CommonName: certdata.CommonName, -// OrganizationalUnit: certdata.OrganizationUnit, -// }, -// NotBefore: time.Now(), -// NotAfter: time.Now().AddDate(1, 0, 0), // 1 год -// KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, -// ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, -// DNSNames: certdata.DNSNames, -// } - -// // Создаем сертификат, подписанный CA -// return x509.CreateCertificate( -// rand.Reader, -// &template, -// caCert, -// &serverKey.PublicKey, -// caKey, -// ) -// } diff --git a/api/RootCert.go b/api/RootCert.go index 16ac286..5324c28 100644 --- a/api/RootCert.go +++ b/api/RootCert.go @@ -5,8 +5,8 @@ import ( "fmt" "net/http" "os" + "scm/crt" "scm/model" - "scm/utils" ) func RootCert(w http.ResponseWriter, r *http.Request) { @@ -25,7 +25,7 @@ func RootCert(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - err = utils.CreateRootKeyCertificate(rootCertRequest) + err = crt.CreateRootKeyCertificate(rootCertRequest) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/utils/CreateRootKeyCertificate.go b/crt/CreateRootKeyCertificate.go similarity index 94% rename from utils/CreateRootKeyCertificate.go rename to crt/CreateRootKeyCertificate.go index a54a7de..310da55 100644 --- a/utils/CreateRootKeyCertificate.go +++ b/crt/CreateRootKeyCertificate.go @@ -1,4 +1,4 @@ -package utils +package crt import ( "crypto/rand" @@ -9,6 +9,7 @@ import ( "math/big" "os" "scm/model" + "scm/utils" "time" "github.com/youmark/pkcs8" @@ -62,7 +63,7 @@ func CreateRootKeyCertificate(data model.CreateRootCertRequest) error { return err } - err = CheckExistsOrCreateDir("root") + err = utils.CheckExistsOrCreateDir("root") if err != nil { return err } @@ -87,6 +88,5 @@ func CreateRootKeyCertificate(data model.CreateRootCertRequest) error { if err != nil { return err } - // Implementation of CreateRootCertificate function return nil } diff --git a/utils/CreateServerCert.go b/crt/CreateServerCert.go similarity index 98% rename from utils/CreateServerCert.go rename to crt/CreateServerCert.go index ef2cbf9..bf37fdb 100644 --- a/utils/CreateServerCert.go +++ b/crt/CreateServerCert.go @@ -1,4 +1,4 @@ -package utils +package crt import ( "crypto/rand" diff --git a/utils/LoadRootCertAndKey.go b/crt/LoadRootCertAndKey.go similarity index 98% rename from utils/LoadRootCertAndKey.go rename to crt/LoadRootCertAndKey.go index 5bf3aaa..68db8b6 100644 --- a/utils/LoadRootCertAndKey.go +++ b/crt/LoadRootCertAndKey.go @@ -1,4 +1,4 @@ -package utils +package crt import ( "crypto/rsa" diff --git a/utils/ParseCert.go b/crt/ParseCert.go similarity index 98% rename from utils/ParseCert.go rename to crt/ParseCert.go index 64d7eda..76a0949 100644 --- a/utils/ParseCert.go +++ b/crt/ParseCert.go @@ -1,4 +1,4 @@ -package utils +package crt import ( "crypto/x509" diff --git a/utils/SavePEMFile.go b/crt/SavePEMFile.go similarity index 95% rename from utils/SavePEMFile.go rename to crt/SavePEMFile.go index 957f76e..dae7b6a 100644 --- a/utils/SavePEMFile.go +++ b/crt/SavePEMFile.go @@ -1,4 +1,4 @@ -package utils +package crt import ( "encoding/pem"