First code

master
Uriel Fanelli 2021-09-14 16:11:48 +02:00
parent 314bae6c3a
commit 2436e6b11f
4 changed files with 157 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
acme.json
*.crt
zumba
./vscode

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"go.inferGopath": false
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module zumba
go 1.13

147
zumba.go Normal file
View File

@ -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)
}