From 442c54ca1259c4978ff2ae3657082ec7030a9633 Mon Sep 17 00:00:00 2001 From: sudosammy Date: Mon, 24 Jan 2022 15:57:04 +0800 Subject: [PATCH 1/5] fix le bug where certs were created in / --- VERSION | 2 +- libknary/certbot.go | 15 ++++++++++++--- libknary/lego/cert_storage.go | 14 +++++++++++--- main.go | 8 ++------ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/VERSION b/VERSION index fbcbf73..8cf6caf 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.4.0 \ No newline at end of file +3.4.1 \ No newline at end of file diff --git a/libknary/certbot.go b/libknary/certbot.go index d6001c3..82cb3bd 100644 --- a/libknary/certbot.go +++ b/libknary/certbot.go @@ -5,6 +5,7 @@ import ( "errors" "log" "os" + "path/filepath" "time" "github.com/go-acme/lego/v4/certcrypto" @@ -65,7 +66,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { return nil } -func StartLetsEncrypt() string { +func StartLetsEncrypt() { // check if folder structure is correct cmd.CreateFolderStructure() @@ -136,7 +137,12 @@ func StartLetsEncrypt() string { Printy("TLS private key found: "+certsStorage.GetFileName("*."+GetFirstDomain(), ".key"), 3) Printy("TLS certificate found: "+certsStorage.GetFileName("*."+GetFirstDomain(), ".crt"), 3) } - return cmd.SanitizedDomain("*." + GetFirstDomain()) + + // Set TLS_CRT and TLS_KEY to our LE generated certs + os.Setenv("TLS_CRT", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain("*."+GetFirstDomain())+".crt")) + os.Setenv("TLS_KEY", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain("*."+GetFirstDomain())+".key")) + + return } if os.Getenv("DEBUG") == "true" { @@ -158,7 +164,10 @@ func StartLetsEncrypt() string { } certsStorage.SaveResource(certificates) - return cmd.SanitizedDomain(certificates.Domain) + + // Set TLS_CRT and TLS_KEY to our LE generated certs + os.Setenv("TLS_CRT", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain(certificates.Domain)+".crt")) + os.Setenv("TLS_KEY", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain(certificates.Domain)+".key")) } func renewError(msg string) { diff --git a/libknary/lego/cert_storage.go b/libknary/lego/cert_storage.go index a65f92d..dfe26ef 100644 --- a/libknary/lego/cert_storage.go +++ b/libknary/lego/cert_storage.go @@ -30,21 +30,29 @@ import ( // func GetCertPath() string { var certFolderName string + var certPath string - if !filepath.IsAbs(os.Getenv("TLS_CRT")) { + if os.Getenv("TLS_CRT") == "" || os.Getenv("TLS_KEY") == "" { + // this is the default LE config + certPath = "certs" // put LE certs in ./certs/* dir. if it doesn't exist, it'll be created by StartLetsEncrypt() + } else { + certPath = os.Getenv("TLS_CRT") + } + + if !filepath.IsAbs(certPath) { pwd, err := os.Getwd() if err != nil { log.Fatalf(err.Error()) } - path, err := filepath.Abs(filepath.Join(pwd, os.Getenv("TLS_CRT"))) + path, err := filepath.Abs(filepath.Join(pwd, certPath)) if err != nil { log.Fatalf(err.Error()) } certFolderName = filepath.Dir(path) } else { - certFolderName = filepath.Dir(os.Getenv("TLS_CRT")) + certFolderName = filepath.Dir(certPath) } return certFolderName diff --git a/main.go b/main.go index a7ae197..70fae2d 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( ) const ( - VERSION = "3.4.0" + VERSION = "3.4.1" GITHUB = "https://github.com/sudosammy/knary" GITHUBVERSION = "https://raw.githubusercontent.com/sudosammy/knary/master/VERSION" ) @@ -149,11 +149,7 @@ func main() { // generate a let's encrypt certificate if os.Getenv("LETS_ENCRYPT") != "" && os.Getenv("HTTP") == "true" && os.Getenv("DNS") == "true" && (os.Getenv("TLS_CRT") == "" || os.Getenv("TLS_KEY") == "") { - certName := libknary.StartLetsEncrypt() - // out of this we need to set TLS_CRT and TLS_KEY - // TODO make these not rely on hardcoded paths - os.Setenv("TLS_CRT", "certs/"+certName+".crt") - os.Setenv("TLS_KEY", "certs/"+certName+".key") + libknary.StartLetsEncrypt() libknary.Printy("Let's Encrypt certificate is loaded", 1) } else if os.Getenv("LETS_ENCRYPT") != "" && (os.Getenv("HTTP") != "true" || os.Getenv("DNS") != "true") { From 098f6ac64842d13bcecda9b4e935a9b5db82ab9c Mon Sep 17 00:00:00 2001 From: sudosammy Date: Mon, 24 Jan 2022 16:42:51 +0800 Subject: [PATCH 2/5] cert filepath fix --- libknary/lego/cert_storage.go | 2 +- libknary/lego/storage.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libknary/lego/cert_storage.go b/libknary/lego/cert_storage.go index dfe26ef..4d6ad0b 100644 --- a/libknary/lego/cert_storage.go +++ b/libknary/lego/cert_storage.go @@ -50,7 +50,7 @@ func GetCertPath() string { log.Fatalf(err.Error()) } - certFolderName = filepath.Dir(path) + certFolderName = path } else { certFolderName = filepath.Dir(certPath) } diff --git a/libknary/lego/storage.go b/libknary/lego/storage.go index 4c51eb7..613ff72 100644 --- a/libknary/lego/storage.go +++ b/libknary/lego/storage.go @@ -7,7 +7,7 @@ import ( ) func CreateFolderStructure() { - folder := filepath.Join(GetCertPath() + "archives") + folder := filepath.Join(GetCertPath(), "archives") err := os.MkdirAll(folder, os.ModePerm) if err != nil { log.Fatal(err) From 73c7dd67fa6fa2553ca23bc98ae96d527cffc0a4 Mon Sep 17 00:00:00 2001 From: sudosammy Date: Mon, 24 Jan 2022 16:45:34 +0800 Subject: [PATCH 3/5] + and , are similar lol --- libknary/certbot.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libknary/certbot.go b/libknary/certbot.go index 82cb3bd..997958b 100644 --- a/libknary/certbot.go +++ b/libknary/certbot.go @@ -139,8 +139,8 @@ func StartLetsEncrypt() { } // Set TLS_CRT and TLS_KEY to our LE generated certs - os.Setenv("TLS_CRT", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain("*."+GetFirstDomain())+".crt")) - os.Setenv("TLS_KEY", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain("*."+GetFirstDomain())+".key")) + os.Setenv("TLS_CRT", filepath.Join(cmd.GetCertPath(), cmd.SanitizedDomain("*."+GetFirstDomain())+".crt")) + os.Setenv("TLS_KEY", filepath.Join(cmd.GetCertPath(), cmd.SanitizedDomain("*."+GetFirstDomain())+".key")) return } @@ -166,8 +166,8 @@ func StartLetsEncrypt() { certsStorage.SaveResource(certificates) // Set TLS_CRT and TLS_KEY to our LE generated certs - os.Setenv("TLS_CRT", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain(certificates.Domain)+".crt")) - os.Setenv("TLS_KEY", filepath.Join(cmd.GetCertPath()+cmd.SanitizedDomain(certificates.Domain)+".key")) + os.Setenv("TLS_CRT", filepath.Join(cmd.GetCertPath(), cmd.SanitizedDomain(certificates.Domain)+".crt")) + os.Setenv("TLS_KEY", filepath.Join(cmd.GetCertPath(), cmd.SanitizedDomain(certificates.Domain)+".key")) } func renewError(msg string) { From b2a71b5065b1a40a92e9d3aaf3e265afd27e81f6 Mon Sep 17 00:00:00 2001 From: sudosammy Date: Mon, 24 Jan 2022 17:09:33 +0800 Subject: [PATCH 4/5] create cert dirs when needed --- libknary/lego/cert_storage.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libknary/lego/cert_storage.go b/libknary/lego/cert_storage.go index 4d6ad0b..caa7155 100644 --- a/libknary/lego/cert_storage.go +++ b/libknary/lego/cert_storage.go @@ -34,9 +34,9 @@ func GetCertPath() string { if os.Getenv("TLS_CRT") == "" || os.Getenv("TLS_KEY") == "" { // this is the default LE config - certPath = "certs" // put LE certs in ./certs/* dir. if it doesn't exist, it'll be created by StartLetsEncrypt() + certPath = "./certs" // put LE certs in ./certs/* dir. if it doesn't exist, it'll be created by StartLetsEncrypt() } else { - certPath = os.Getenv("TLS_CRT") + certPath = filepath.Dir(os.Getenv("TLS_CRT")) } if !filepath.IsAbs(certPath) { @@ -52,7 +52,7 @@ func GetCertPath() string { certFolderName = path } else { - certFolderName = filepath.Dir(certPath) + certFolderName = certPath } return certFolderName From 78a48e3ad141418f7f3a5a192180c663c5d35348 Mon Sep 17 00:00:00 2001 From: sudosammy Date: Mon, 24 Jan 2022 17:26:45 +0800 Subject: [PATCH 5/5] these were annoying me --- libknary/lego/accounts_storage.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libknary/lego/accounts_storage.go b/libknary/lego/accounts_storage.go index 4ff7025..76102a7 100644 --- a/libknary/lego/accounts_storage.go +++ b/libknary/lego/accounts_storage.go @@ -92,14 +92,14 @@ func (s *AccountsStorage) GetPrivateKey(keyType certcrypto.KeyType) crypto.Priva accKeyPath := s.keyFilePath if _, err := os.Stat(accKeyPath); os.IsNotExist(err) { - log.Printf("No key found for account %s. Generating a %s key.", s.userID, keyType) + // log.Printf("No key found for account %s. Generating a %s key.", s.userID, keyType) privateKey, err := generatePrivateKey(accKeyPath, keyType) if err != nil { log.Fatalf("Could not generate RSA private account key for account %s: %v", s.userID, err) } - log.Printf("Saved key to %s", accKeyPath) + // log.Printf("Saved key to %s", accKeyPath) return privateKey }