From 2436e6b11f7260f2983abedcebb0f9011cf80db8 Mon Sep 17 00:00:00 2001 From: loweel Date: Tue, 14 Sep 2021 16:11:48 +0200 Subject: [PATCH] First code --- .gitignore | 4 ++ .vscode/settings.json | 3 + go.mod | 3 + zumba.go | 147 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 go.mod create mode 100644 zumba.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc66f21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +acme.json +*.crt +zumba +./vscode diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a460645 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "go.inferGopath": false +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..228e775 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module zumba + +go 1.13 diff --git a/zumba.go b/zumba.go new file mode 100644 index 0000000..f2e3ad3 --- /dev/null +++ b/zumba.go @@ -0,0 +1,147 @@ +package main + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "os" + "strings" + "time" +) + +type AcmeJson struct { + Letsencryptresolver struct { + Account struct { + Email string `json:"Email"` + Registration struct { + Body struct { + Status string `json:"status"` + Contact []string `json:"contact"` + } `json:"body"` + URI string `json:"uri"` + } `json:"Registration"` + PrivateKey string `json:"PrivateKey"` + KeyType string `json:"KeyType"` + } `json:"Account"` + Certificates []struct { + Domain struct { + Main string `json:"main"` + } `json:"domain"` + Certificate string `json:"certificate"` + Key string `json:"key"` + Store string `json:"Store"` + } `json:"Certificates"` + } `json:"letsencryptresolver"` +} + +// Squelch +var MyAcme AcmeJson +var AcmeFile, CertPath string + +func init() { + + AcmeFile = os.Getenv("ACME_FILE") + CertPath = os.Getenv("CERT_PATH") + + if AcmeFile == "" { + log.Println("Missing ENV VAR ACME_FILE") + os.Exit(2) + } + + if CertPath == "" { + log.Println("Missing ENV VAR CERT_PATH") + os.Exit(2) + } + +} + +func loadAcme() { + file, err := ioutil.ReadFile(AcmeFile) + + if err != nil { + log.Println("Cannot open config file", err.Error()) + os.Exit(1) + } else { + log.Println("Json File open") + } + + err = json.Unmarshal([]byte(file), &MyAcme) + + if err != nil { + log.Println("Cannot marshal json: ", err.Error()) + os.Exit(1) + } else { + log.Println("Json Syntax OK") + } + + log.Println("Acme file Loaded") + +} + +func main() { + + ticker := time.NewTicker(12 * time.Hour) + + loadAcme() + + for range ticker.C { + + for _, k := range MyAcme.Letsencryptresolver.Certificates { + + // Decode + decoded, err := base64.StdEncoding.DecodeString(k.Certificate) + if err != nil { + fmt.Println("Unable to decode certificate", k.Domain.Main) + continue + } else { + fmt.Println("Decoded certificate ok: ", k.Domain.Main) + } + + // Write chain + name := fmt.Sprintf("%s/%s.chain.crt", CertPath, k.Domain.Main) + fmt.Println("Writing file", name) + err = ioutil.WriteFile(name, decoded, 0644) + if err != nil { + fmt.Println("Error writing file", name) + } else { + fmt.Println("Decoded chain written at: ", name) + } + + // Write cert + name = fmt.Sprintf("%s/%s.crt", CertPath, k.Domain.Main) + fmt.Println("Writing file", name) + parts := strings.Split(string(decoded), "\n\n") + err = ioutil.WriteFile(name, []byte(parts[0]), 0644) + if err != nil { + fmt.Println("Error writing file", name) + } else { + fmt.Println("Decoded cert written at: ", name) + } + + // Decode key + decoded, err = base64.StdEncoding.DecodeString(k.Key) + if err != nil { + fmt.Println("Unable to decode Key", k.Domain.Main) + continue + } else { + fmt.Println("Decoded key for ", k.Domain.Main) + } + + // Write key + name = fmt.Sprintf("%s/%s.key", CertPath, k.Domain.Main) + fmt.Println("Writing key file", name) + err = ioutil.WriteFile(name, []byte(decoded), 0644) + if err != nil { + fmt.Println("Error writing file", name) + } else { + fmt.Println("Decoded key written at: ", name) + } + } + + } + + os.Exit(0) + +}