feat: trocar configurações de certificado

Agora a emissão e renovação de certificado é automática.
This commit is contained in:
tkinaba
2026-04-09 17:31:10 -03:00
parent b84e0f6def
commit 219dcc78bd
6 changed files with 54 additions and 57 deletions
+1
View File
@@ -1,2 +1,3 @@
dist
config.toml
main
+7 -9
View File
@@ -13,7 +13,7 @@ func GenerateConfigTOML(cv ConfigValues) (string, error) {
// [server]
sb.WriteString("# Server Configuration\n")
sb.WriteString("[server]\n")
sb.WriteString("port = 8080\n")
sb.WriteString(fmt.Sprintf("port = %s\n", cv.Server["port"]))
sb.WriteString(fmt.Sprintf("timeout_seconds = %s\n", cv.Server["timeout"]))
sb.WriteString(fmt.Sprintf("environment = %q\n", cv.Server["environment"]))
sb.WriteString("\n")
@@ -31,18 +31,16 @@ func GenerateConfigTOML(cv ConfigValues) (string, error) {
sb.WriteString("# Certificate Options\n")
sb.WriteString("[certificate]\n")
sb.WriteString(fmt.Sprintf("mapped_dir = %q\n", cv.Cert["cert_dir_path"]))
sb.WriteString(fmt.Sprintf("cert_path = %q\n", "/app/certs/"+cv.Cert["cert_name"]))
sb.WriteString(fmt.Sprintf("key_path = %q\n", "/app/certs/"+cv.Cert["key_name"]))
sb.WriteString(fmt.Sprintf("ca_path = %q\n", "/app/certs/"+cv.Cert["ca_name"]))
sb.WriteString(fmt.Sprintf("server_name = %q\n", cv.Cert["server_name"]))
sb.WriteString("cert_path = \"/app/certs/client.crt\"\n")
sb.WriteString("key_path = \"/app/certs/client.key\"\n")
sb.WriteString("ca_path = \"/app/certs/ca.crt\"\n")
sb.WriteString("\n")
// [application] — hardcoded / pre-defined
sb.WriteString("# Pre-defined options\n")
// [application]
sb.WriteString("[application]\n")
sb.WriteString("erp = \"TOTVS\"\n")
sb.WriteString("central_server_url = \"https://warden:8080\"\n")
sb.WriteString("api_key = \"super secreto\"\n")
sb.WriteString(fmt.Sprintf("central_server_url = %q\n", cv.Application["central_server_url"]))
sb.WriteString(fmt.Sprintf("enrollment_token = %q\n", cv.Application["enrollment_token"]))
sb.WriteString("\n")
// [log] — hardcoded defaults
+31 -46
View File
@@ -3,7 +3,6 @@ package tui
import (
"fmt"
"os"
"path/filepath"
"strconv"
"charm.land/bubbles/v2/spinner"
@@ -31,6 +30,7 @@ type Model struct {
loginForm FormStep
wireguardForm FormStep
appForm FormStep
serverForm FormStep
dbForm FormStep
certForm FormStep
@@ -50,11 +50,12 @@ type DockerLoginData struct {
}
type ConfigValues struct {
Login map[string]string
Wireguard map[string]string
Server map[string]string
Database map[string]string
Cert map[string]string
Login map[string]string
Wireguard map[string]string
Server map[string]string
Database map[string]string
Cert map[string]string
Application map[string]string
}
type AppConfig struct {
@@ -70,12 +71,12 @@ type AppConfig struct {
MinConns int64 `toml:"min_conns"`
} `toml:"database"`
Certificates struct {
DirPath string `toml:"mapped_dir"`
CertName string `toml:"cert_path"`
KeyName string `toml:"key_path"`
CAName string `toml:"ca_path"`
ServerName string `toml:"server_name"`
DirPath string `toml:"mapped_dir"`
} `toml:"certificate"`
Application struct {
CentralServerURL string `toml:"central_server_url"`
EnrollmentToken string `toml:"enrollment_token"`
} `toml:"application"`
}
func loadConfig() AppConfig {
@@ -91,10 +92,8 @@ func loadConfig() AppConfig {
config.Database.MinConns = 2
config.Certificates.DirPath = "/caminho/para/diretorio"
config.Certificates.CertName = "certificado.crt"
config.Certificates.KeyName = "chave.key"
config.Certificates.CAName = "chaveCA.crt"
config.Certificates.ServerName = "client"
config.Application.CentralServerURL = "https://servidor:8443"
_, err := os.Stat("config.toml")
if err == nil {
@@ -103,12 +102,6 @@ func loadConfig() AppConfig {
}
}
if err == nil {
config.Certificates.CertName = filepath.Base(config.Certificates.CertName)
config.Certificates.KeyName = filepath.Base(config.Certificates.KeyName)
config.Certificates.CAName = filepath.Base(config.Certificates.CAName)
}
return config
}
@@ -231,37 +224,29 @@ func InitialModel() Model {
Type: FieldTypeNumber,
},
}),
certForm: NewFormStep("Certificado", []FormField{
appForm: NewFormStep("Aplicação", []FormField{
{
Id: "cert_dir_path",
Label: "Caminho para o diretório dos certificados",
Placeholder: "/caminho/para/diretorio",
Default: cfg.Certificates.DirPath,
Id: "central_server_url",
Label: "URL do Servidor Central",
Placeholder: "https://servidor:8443",
Default: cfg.Application.CentralServerURL,
Type: FieldTypeText,
},
{
Id: "cert_name",
Label: "Nome do arquivo do certificado",
Default: cfg.Certificates.CertName,
Type: FieldTypeText,
Id: "enrollment_token",
Label: "Token de Inscrição",
Placeholder: "token gerado no painel web",
Default: cfg.Application.EnrollmentToken,
Type: FieldTypeText,
},
}),
certForm: NewFormStep("Certificado", []FormField{
{
Id: "key_name",
Label: "Nome do arquivo da chave",
Default: cfg.Certificates.KeyName,
Type: FieldTypeText,
},
{
Id: "ca_name",
Label: "Nome do arquivo da autoridade certificadora",
Default: cfg.Certificates.CAName,
Type: FieldTypeText,
},
{
Id: "server_name",
Label: "Nome do servidor",
Default: cfg.Certificates.ServerName,
Type: FieldTypeText,
Id: "cert_dir_path",
Label: "Diretório para armazenar certificados",
Placeholder: "/caminho/para/diretorio",
Default: cfg.Certificates.DirPath,
Type: FieldTypeText,
},
}),
spinner: s,
+1
View File
@@ -19,6 +19,7 @@ const (
StepRunWireguard
// Docker Config
StepAppConfig
StepServerConfig
StepDatabaseConfig
StepCertConfig
+11 -2
View File
@@ -77,6 +77,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case StepRunWireguard:
return m.updateRunWireguardDocker(msg)
case StepAppConfig:
done, cmd := m.appForm.Update(msg)
if done {
m.configValues.Application = m.appForm.Values()
m.currentStep = StepServerConfig
}
return m, cmd
case StepServerConfig:
done, cmd := m.serverForm.Update(msg)
@@ -192,7 +201,7 @@ func (m Model) updateIPQuestion(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
// Yes
if m.cursor == 0 {
m.currentStep = StepServerConfig
m.currentStep = StepAppConfig
return m, nil
}
@@ -262,7 +271,7 @@ func (m Model) updateRunWireguardDocker(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.finishedDockerRun && m.dockerRunError != nil {
return m, tea.Quit
} else if m.finishedDockerRun && m.dockerRunError == nil {
m.currentStep = StepServerConfig
m.currentStep = StepAppConfig
}
}
+3
View File
@@ -53,6 +53,9 @@ func (m Model) View() tea.View {
}
// App Config Stuff
case StepAppConfig:
body = m.appForm.View()
helpMsg = formMsg
case StepServerConfig:
body = m.serverForm.View()
helpMsg = formMsg