lots o progress

This commit is contained in:
2023-09-11 05:08:12 -04:00
parent 323b70eb9f
commit ac98a90222
8 changed files with 322 additions and 115 deletions

132
main.go
View File

@@ -5,21 +5,20 @@ import (
"code.gitea.io/sdk/gitea"
"fmt"
"git.nevets.tech/Steven/ezconf"
"github.com/go-git/go-git/v5/plumbing/object"
"io"
"os/exec"
"strings"
"time"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v55/github"
"io"
"os"
"os/exec"
"strings"
)
var config *ezconf.Configuration
var githubClient *github.Client
var giteaClient *gitea.Client
var domain string
var legoBaseArgs []string
@@ -31,19 +30,20 @@ var creds *http.BasicAuth
var repo *git.Repository
//TODO create logic for domain based configs
//TODO create logic for gh vs gt repos
func main() {
makeDirs()
createConfig()
}
func maindis() {
config = ezconf.NewConfiguration("/etc/certman/certman.conf")
var err error
args := os.Args
// -c
hasConfig, configIndex := contains(args, "-c")
if hasConfig {
config = ezconf.NewConfiguration(args[configIndex+1])
} else {
fmt.Printf("Error, no config passed. Please add '-c /path/to/config.ini' to the command\n")
os.Exit(1)
}
// -d
hasDomain, domainIndex := contains(args, "-d")
if hasDomain {
@@ -82,11 +82,7 @@ func main() {
Username: config.GetAsString("Git.username"),
Password: config.GetAsString("Git.api_token"),
}
giteaClient, err = gitea.NewClient(config.GetAsString("Git.server"), gitea.SetToken(config.GetAsString("Git.api_token")))
if err != nil {
fmt.Printf("Error connecting to gitea instance: %v\n", err)
os.Exit(1)
}
giteaClient = createGiteaClient()
storage = memory.NewStorage()
fs = memfs.New()
@@ -96,13 +92,13 @@ func main() {
case "gen":
{
url := createGiteaRepo()
cloneRepo(url)
repo, workTree = cloneRepo(url)
fixUpdateSh()
cmd = exec.Command("lego", legoNewSiteArgs...)
}
case "renew":
{
cloneRepo(config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name") + "/" + domain + "-certificates.git")
repo, workTree = cloneRepo(config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name") + "/" + domain + "-certificates.git")
cmd = exec.Command("lego", legoRenewSiteArgs...)
}
case "gen-cert-only":
@@ -116,7 +112,7 @@ func main() {
case "git":
{
url := createGiteaRepo()
cloneRepo(url)
repo, workTree = cloneRepo(url)
fixUpdateSh()
addAndPushCerts()
os.Exit(0)
@@ -159,42 +155,6 @@ func main() {
addAndPushCerts()
}
func createGiteaRepo() string {
options := gitea.CreateRepoFromTemplateOption{
Avatar: true,
Description: "Certificates storage for " + domain,
GitContent: true,
GitHooks: true,
Labels: true,
Name: domain + "-certificates",
Owner: config.GetAsString("Git.org_name"),
Private: true,
Topics: true,
Webhooks: true,
}
giteaRepo, _, err := giteaClient.CreateRepoFromTemplate(config.GetAsString("Git.org_name"), config.GetAsString("Git.template_name"), options)
if err != nil {
fmt.Printf("Error creating repo: %v\n", err)
os.Exit(1)
}
return giteaRepo.CloneURL
}
func cloneRepo(url string) {
var err error
repo, err = git.Clone(storage, fs, &git.CloneOptions{URL: url, Auth: creds})
if err != nil {
fmt.Printf("Error clone git repo: %v\n", err)
os.Exit(1)
}
workTree, err = repo.Worktree()
if err != nil {
fmt.Printf("Error getting worktree from repo: %v\n", err)
os.Exit(1)
}
}
func fixUpdateSh() {
oldUpdateSh, err := fs.Open("update.sh")
if err != nil {
@@ -222,60 +182,6 @@ func fixUpdateSh() {
}
}
func addAndPushCerts() {
//TODO integrate SOPS api when stable release
certs, err := os.ReadDir(config.GetAsString("Certificates.certs_path") + "/certificates")
if err != nil {
fmt.Printf("Error reading from directory: %v\n", err)
os.Exit(1)
}
for _, cert := range certs {
if strings.HasPrefix(cert.Name(), domain) {
file, err := fs.Create(cert.Name())
if err != nil {
fmt.Printf("Error copying cert to memfs: %v\n", err)
os.Exit(1)
}
certFile, err := os.ReadFile(config.GetAsString("Certificates.certs_path") + "/certificates/" + cert.Name())
_, err = file.Write(certFile)
err = file.Close()
if err != nil {
fmt.Printf("Error writing to memfs: %v\n", err)
os.Exit(1)
}
_, err = workTree.Add(cert.Name())
if err != nil {
fmt.Printf("Error adding certificate %v: %v", cert.Name(), err)
os.Exit(1)
}
}
}
status, err := workTree.Status()
if err != nil {
fmt.Printf("Error getting repo status: %v\n", err)
os.Exit(1)
}
fmt.Println("Work Tree Status:\n" + status.String())
signature := &object.Signature{
Name: "Cert Manager",
Email: "certs@nevets.tech",
When: time.Now(),
}
_, err = workTree.Commit("Update "+domain+" @ "+time.Now().Format("Mon Jan _2 2006 15:04:05 MST"), &git.CommitOptions{Author: signature, Committer: signature})
if err != nil {
fmt.Printf("Error committing certs: %v\n", err)
os.Exit(1)
}
err = repo.Push(&git.PushOptions{Auth: creds, Force: true, RemoteName: "origin"})
if err != nil {
fmt.Printf("Error pushing to origin: %v\n", err)
os.Exit(1)
}
fmt.Println("Successfully uploaded to " + config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name") + "/" + domain + "-certificates.git")
}
func contains(slice []string, value string) (sliceHas bool, index int) {
for i, entry := range slice {
if entry == value {