Major Refactoring, Client can now be used as a library
Some checks failed
Build (artifact) / build (push) Failing after 1m3s
Some checks failed
Build (artifact) / build (push) Failing after 1m3s
This commit is contained in:
15
app/shared/certs.go
Normal file
15
app/shared/certs.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
CertCmd = &cobra.Command{
|
||||
Use: "cert",
|
||||
Short: "Certificate management",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return cmd.Help()
|
||||
},
|
||||
}
|
||||
)
|
||||
191
app/shared/commands.go
Normal file
191
app/shared/commands.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.nevets.tech/Keys/certman/common"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
VersionCmd = basicCmd("version", "Show version", versionCmd)
|
||||
NewKeyCmd = basicCmd("gen-key", "Generates encryption key", newKeyCmd)
|
||||
DevCmd = basicCmd("dev", "Dev Function", devCmd)
|
||||
|
||||
domainCertDir string
|
||||
NewDomainCmd = &cobra.Command{
|
||||
Use: "new-domain",
|
||||
Short: "Create config and directories for new domain",
|
||||
Args: cobra.ExactArgs(1),
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
dirOverridden := cmd.Flags().Changed("dir")
|
||||
return newDomainCmd(args[0], domainCertDir, dirOverridden)
|
||||
},
|
||||
}
|
||||
|
||||
modeFlag string
|
||||
thinInstallFlag bool
|
||||
InstallCmd = &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "Create certman files and directories",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
switch modeFlag {
|
||||
case "server", "client":
|
||||
return installCmd(thinInstallFlag, modeFlag)
|
||||
default:
|
||||
return fmt.Errorf("invalid --mode %q (must be server or client)", modeFlag)
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
NewDomainCmd.Flags().StringVar(&domainCertDir, "dir", "/var/local/certman/certificates/", "Alternate directory for certificates")
|
||||
|
||||
InstallCmd.Flags().StringVar(&modeFlag, "mode", "client", "CertManager mode [server, client]")
|
||||
InstallCmd.Flags().BoolVarP(&thinInstallFlag, "thin", "t", false, "Thin install (skip creating dirs)")
|
||||
}
|
||||
|
||||
func devCmd(cmd *cobra.Command, args []string) {
|
||||
testDomain := "lunamc.org"
|
||||
err := LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading configuration: %v\n", err)
|
||||
}
|
||||
err = LoadDomainConfigs()
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading configs: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Println(testDomain)
|
||||
}
|
||||
|
||||
func versionCmd(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("CertManager (certman) - Steven Tracey\nVersion: %s build-%s\n",
|
||||
common.Version, common.Build,
|
||||
)
|
||||
}
|
||||
|
||||
func newKeyCmd(cmd *cobra.Command, args []string) {
|
||||
key, err := common.GenerateKey()
|
||||
if err != nil {
|
||||
log.Fatalf("%v", err)
|
||||
}
|
||||
fmt.Printf(key)
|
||||
}
|
||||
|
||||
func newDomainCmd(domain, domainDir string, dirOverridden bool) error {
|
||||
//TODO add config option for "overridden dir"
|
||||
if !common.IsValidFQDN(domain) {
|
||||
return fmt.Errorf("invalid FQDN: %q", domain)
|
||||
}
|
||||
|
||||
err := LoadConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Creating new domain %s\n", domain)
|
||||
err = CreateDomainConfig(domain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
CreateDomainCertsDir(domain, domainDir, dirOverridden)
|
||||
|
||||
certmanUser, err := user.Lookup("certman")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting user certman: %v", err)
|
||||
}
|
||||
uid, err := strconv.Atoi(strings.TrimSpace(certmanUser.Uid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gid, err := strconv.Atoi(strings.TrimSpace(certmanUser.Gid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = common.ChownRecursive("/etc/certman/domains", uid, gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = common.ChownRecursive("/var/local/certman", uid, gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Successfully created domain entry for " + domain + "\nUpdate config file as needed in /etc/certman/domains/" + domain + ".conf\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func installCmd(isThin bool, mode string) error {
|
||||
if !isThin {
|
||||
if os.Geteuid() != 0 {
|
||||
return fmt.Errorf("installation must be run as root")
|
||||
}
|
||||
|
||||
MakeDirs()
|
||||
CreateConfig(mode)
|
||||
|
||||
err := LoadConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.OpenFile("/var/run/certman.pid", os.O_RDONLY|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating pid file: %v", err)
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error closing pid file: %v", err)
|
||||
}
|
||||
|
||||
newUserCmd := exec.Command("useradd", "-d", "/var/local/certman", "-U", "-r", "-s", "/sbin/nologin", "certman")
|
||||
if output, err := newUserCmd.CombinedOutput(); err != nil {
|
||||
if !strings.Contains(err.Error(), "exit status 9") {
|
||||
return fmt.Errorf("error creating user: %v: output %s", err, output)
|
||||
}
|
||||
}
|
||||
newGroupCmd := exec.Command("groupadd", "-r", "-U", "certman", "certsock")
|
||||
if output, err := newGroupCmd.CombinedOutput(); err != nil {
|
||||
if !strings.Contains(err.Error(), "exit status 9") {
|
||||
return fmt.Errorf("error creating group: %v: output %s", err, output)
|
||||
}
|
||||
}
|
||||
certmanUser, err := user.Lookup("certman")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting user certman: %v", err)
|
||||
}
|
||||
uid, err := strconv.Atoi(strings.TrimSpace(certmanUser.Uid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gid, err := strconv.Atoi(strings.TrimSpace(certmanUser.Gid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = common.ChownRecursive("/etc/certman", uid, gid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error changing uid/gid: %v", err)
|
||||
}
|
||||
err = common.ChownRecursive("/var/local/certman", uid, gid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error changing uid/gid: %v", err)
|
||||
}
|
||||
err = os.Chown("/var/run/certman.pid", uid, gid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error changing uid/gid: %v", err)
|
||||
}
|
||||
} else {
|
||||
CreateConfig(mode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
358
app/shared/config.go
Normal file
358
app/shared/config.go
Normal file
@@ -0,0 +1,358 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.nevets.tech/Keys/certman/common"
|
||||
pb "git.nevets.tech/Keys/certman/proto/v1"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrBlankConfigEntry = errors.New("blank config entry")
|
||||
ErrConfigNotFound = errors.New("config file not found")
|
||||
)
|
||||
|
||||
type DomainConfigStore struct {
|
||||
mu sync.RWMutex
|
||||
configs map[string]*common.DomainConfig
|
||||
}
|
||||
|
||||
func NewDomainConfigStore() *DomainConfigStore {
|
||||
return &DomainConfigStore{
|
||||
configs: make(map[string]*common.DomainConfig),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DomainConfigStore) Get(domain string) (*common.DomainConfig, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
v, ok := s.configs[domain]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func (s *DomainConfigStore) Set(domain string, v *common.DomainConfig) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.configs[domain] = v
|
||||
}
|
||||
|
||||
// Swap atomically replaces the entire config map (used during reload).
|
||||
func (s *DomainConfigStore) Swap(newConfigs map[string]*common.DomainConfig) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.configs = newConfigs
|
||||
}
|
||||
|
||||
// Snapshot returns a shallow copy safe to iterate without holding the lock.
|
||||
func (s *DomainConfigStore) Snapshot() map[string]*common.DomainConfig {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
snap := make(map[string]*common.DomainConfig, len(s.configs))
|
||||
for k, v := range s.configs {
|
||||
snap[k] = v
|
||||
}
|
||||
return snap
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global state
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
var (
|
||||
config *common.AppConfig
|
||||
configMu sync.RWMutex
|
||||
domainStore = NewDomainConfigStore()
|
||||
)
|
||||
|
||||
func Config() *common.AppConfig {
|
||||
configMu.RLock()
|
||||
defer configMu.RUnlock()
|
||||
return config
|
||||
}
|
||||
|
||||
func DomainStore() *DomainConfigStore {
|
||||
domainStore.mu.RLock()
|
||||
defer domainStore.mu.RUnlock()
|
||||
return domainStore
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Loading
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// LoadConfig reads the main certman.conf into config.
|
||||
func LoadConfig() error {
|
||||
vConfig := viper.New()
|
||||
vConfig.SetConfigFile("/etc/certman/certman.conf")
|
||||
vConfig.SetConfigType("toml")
|
||||
if err := vConfig.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if vConfig.GetString("App.mode") == "server" {
|
||||
vConfig.SetConfigType("toml")
|
||||
vConfig.SetConfigFile("/etc/certman/server.conf")
|
||||
if err := vConfig.MergeInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := vConfig.Unmarshal(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadDomainConfigs reads every .conf file in the domains directory.
|
||||
func LoadDomainConfigs() error {
|
||||
dir := "/etc/certman/domains/"
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading domain config dir: %w", err)
|
||||
}
|
||||
|
||||
temp := make(map[string]*common.DomainConfig)
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || filepath.Ext(entry.Name()) != ".conf" {
|
||||
continue
|
||||
}
|
||||
|
||||
path := filepath.Join(dir, entry.Name())
|
||||
v := viper.New()
|
||||
v.SetConfigFile(path)
|
||||
v.SetConfigType("toml")
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return fmt.Errorf("loading %s: %w", path, err)
|
||||
}
|
||||
|
||||
domain := v.GetString("domain.domain_name")
|
||||
if domain == "" {
|
||||
return fmt.Errorf("%s: missing domain.domain_name", path)
|
||||
}
|
||||
|
||||
if _, exists := temp[domain]; exists {
|
||||
fmt.Printf("Duplicate domain in %s, skipping...\n", path)
|
||||
continue
|
||||
}
|
||||
cfg := &common.DomainConfig{}
|
||||
if err = v.Unmarshal(cfg); err != nil {
|
||||
return fmt.Errorf("unmarshaling %s: %w", path, err)
|
||||
}
|
||||
temp[domain] = cfg
|
||||
}
|
||||
|
||||
domainStore.Swap(temp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Saving
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func WriteConfig(filePath string, config *common.AppConfig) error {
|
||||
buf, err := toml.Marshal(&config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling config: %w", err)
|
||||
}
|
||||
if err = os.WriteFile(filePath, buf, 0640); err != nil {
|
||||
return fmt.Errorf("write config file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteDomainConfig(config *common.DomainConfig) error {
|
||||
buf, err := toml.Marshal(config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling domain config: %w", err)
|
||||
}
|
||||
configPath := filepath.Join("/etc/certman/domains", config.Domain.DomainName+".conf")
|
||||
if err = os.WriteFile(configPath, buf, 0640); err != nil {
|
||||
return fmt.Errorf("write config file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveDomainConfigs writes every loaded domain config back to disk.
|
||||
func SaveDomainConfigs() error {
|
||||
for _, v := range domainStore.Snapshot() {
|
||||
err := WriteDomainConfig(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Domain Specific Lookups
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func PostPullHooks(domain string) ([]*pb.Hook, error) {
|
||||
var hooks []*pb.Hook
|
||||
if err := viper.UnmarshalKey("Hooks.PostPull", hooks); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return hooks, nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Directory bootstrapping
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func MakeDirs() {
|
||||
dirs := []struct {
|
||||
path string
|
||||
perm os.FileMode
|
||||
}{
|
||||
{"/etc/certman", 0755},
|
||||
{"/etc/certman/domains", 0755},
|
||||
{"/var/local/certman", 0750},
|
||||
}
|
||||
|
||||
for _, d := range dirs {
|
||||
if err := os.MkdirAll(d.path, d.perm); err != nil {
|
||||
log.Fatalf("Unable to create directory %s: %v", d.path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CreateConfig(mode string) {
|
||||
content := strings.NewReplacer(
|
||||
"{mode}", mode,
|
||||
).Replace(defaultConfig)
|
||||
createFile("/etc/certman/certman.conf", 0640, []byte(content))
|
||||
|
||||
switch mode {
|
||||
case "server":
|
||||
content = strings.NewReplacer(
|
||||
"{uuid}", uuid.New().String(),
|
||||
).Replace(defaultServerConfig)
|
||||
createFile("/etc/certman/server.conf", 640, []byte(content))
|
||||
}
|
||||
}
|
||||
|
||||
func CreateDomainConfig(domain string) error {
|
||||
key, err := common.GenerateKey()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to generate key: %v", err)
|
||||
}
|
||||
localConfig := Config()
|
||||
|
||||
var content string
|
||||
switch localConfig.App.Mode {
|
||||
case "server":
|
||||
content = strings.NewReplacer(
|
||||
"{domain}", domain,
|
||||
"{key}", key,
|
||||
).Replace(defaultServerDomainConfig)
|
||||
case "client":
|
||||
content = strings.NewReplacer(
|
||||
"{domain}", domain,
|
||||
"{key}", key,
|
||||
).Replace(defaultClientDomainConfig)
|
||||
default:
|
||||
return fmt.Errorf("unknown certman mode: %v", localConfig.App.Mode)
|
||||
}
|
||||
|
||||
path := filepath.Join("/etc/certman/domains", domain+".conf")
|
||||
createFile(path, 0640, []byte(content))
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateDomainCertsDir(domain string, dir string, dirOverride bool) {
|
||||
var target string
|
||||
if dirOverride {
|
||||
target = filepath.Join(dir, domain)
|
||||
} else {
|
||||
target = filepath.Join("/var/local/certman/certificates", domain)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(target, 0750); err != nil {
|
||||
if os.IsExist(err) {
|
||||
fmt.Println("Directory already exists...")
|
||||
return
|
||||
}
|
||||
log.Fatalf("Error creating certificate directory for %s: %v", domain, err)
|
||||
}
|
||||
}
|
||||
|
||||
const defaultConfig = `[App]
|
||||
mode = '{mode}'
|
||||
tick_rate = 2
|
||||
|
||||
[Git]
|
||||
host = 'gitea'
|
||||
server = 'https://gitea.instance.com'
|
||||
username = 'User'
|
||||
api_token = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
|
||||
org_name = 'org'
|
||||
|
||||
[Certificates]
|
||||
data_root = '/var/local/certman'
|
||||
`
|
||||
|
||||
const defaultServerConfig = `[App]
|
||||
uuid = '{uuid}'
|
||||
|
||||
[Certificates]
|
||||
email = 'User@example.com'
|
||||
ca_dir_url = 'https://acme-v02.api.letsencrypt.org/directory'
|
||||
|
||||
[Cloudflare]
|
||||
cf_email = 'email@example.com'
|
||||
cf_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'`
|
||||
|
||||
const defaultServerDomainConfig = `[Certificates]
|
||||
data_root = ''
|
||||
expiry = 90
|
||||
request_method = 'dns-01'
|
||||
renew_period = 30
|
||||
subdomains = []
|
||||
crypto_key = '{key}'
|
||||
|
||||
[Domain]
|
||||
domain_name = '{domain}'
|
||||
enabled = true
|
||||
dns_server = 'default'
|
||||
|
||||
[Repo]
|
||||
repo_suffix = '-certificates'
|
||||
|
||||
[Internal]
|
||||
last_issued = 0
|
||||
repo_exists = false
|
||||
status = 'clean'
|
||||
`
|
||||
|
||||
const defaultClientDomainConfig = `[Certificates]
|
||||
data_root = ''
|
||||
cert_symlinks = []
|
||||
key_symlinks = []
|
||||
crypto_key = '{key}'
|
||||
|
||||
[Domain]
|
||||
domain_name = '{domain}'
|
||||
enabled = true
|
||||
|
||||
[Hooks.PostPull]
|
||||
command = []
|
||||
cwd = "/dev/null"
|
||||
timeout_seconds = 30
|
||||
env = { "FOO" = "bar" }
|
||||
|
||||
[Repo]
|
||||
repo_suffix = '-certificates'
|
||||
`
|
||||
191
app/shared/daemon.go
Normal file
191
app/shared/daemon.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.nevets.tech/Keys/certman/common"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type Daemon interface {
|
||||
Init()
|
||||
Tick()
|
||||
Reload()
|
||||
Stop()
|
||||
}
|
||||
|
||||
var (
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
|
||||
DaemonCmd = &cobra.Command{
|
||||
Use: "daemon",
|
||||
Short: "Daemon management",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return cmd.Help()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
DaemonCmd.AddCommand(&cobra.Command{
|
||||
Use: "stop",
|
||||
Short: "stop the daemon",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return stopDaemonCmd()
|
||||
},
|
||||
})
|
||||
|
||||
DaemonCmd.AddCommand(&cobra.Command{
|
||||
Use: "reload",
|
||||
Short: "reload daemon configs",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return reloadDaemonCmd()
|
||||
},
|
||||
})
|
||||
|
||||
DaemonCmd.AddCommand(&cobra.Command{
|
||||
Use: "tick",
|
||||
Short: "Manually triggers daemon tick",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return tickDaemonCmd()
|
||||
},
|
||||
})
|
||||
|
||||
DaemonCmd.AddCommand(&cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Show daemon status",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return daemonStatusCmd()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func RunDaemonCmd(daemon Daemon) error {
|
||||
err := common.CreateOrUpdatePIDFile("/var/run/certman.pid")
|
||||
if err != nil {
|
||||
if errors.Is(err, common.ErrorPIDInUse) {
|
||||
return fmt.Errorf("daemon process is already running")
|
||||
}
|
||||
return fmt.Errorf("error creating pidfile: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
|
||||
// Check if main config exists
|
||||
if _, err := os.Stat("/etc/certman/certman.conf"); os.IsNotExist(err) {
|
||||
return fmt.Errorf("main config file not found, please run 'certman --install', then properly configure /etc/certman/certman.conf")
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("error opening /etc/certman/certman.conf: %v", err)
|
||||
}
|
||||
err = LoadConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading configuration: %v", err)
|
||||
}
|
||||
localConfig := Config()
|
||||
|
||||
// Setup SIGINT and SIGTERM listeners
|
||||
sigChannel := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChannel, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer signal.Stop(sigChannel)
|
||||
|
||||
reloadSigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(reloadSigChan, syscall.SIGHUP)
|
||||
defer signal.Stop(reloadSigChan)
|
||||
|
||||
tickSigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(tickSigChan, syscall.SIGUSR1)
|
||||
defer signal.Stop(tickSigChan)
|
||||
|
||||
tickRate := localConfig.App.TickRate
|
||||
ticker := time.NewTicker(time.Duration(tickRate) * time.Hour)
|
||||
defer ticker.Stop()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
daemon.Init()
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
daemon.Stop()
|
||||
return
|
||||
case <-reloadSigChan:
|
||||
daemon.Reload()
|
||||
case <-ticker.C:
|
||||
daemon.Tick()
|
||||
case <-tickSigChan:
|
||||
daemon.Tick()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Cleanup on stop
|
||||
sig := <-sigChannel
|
||||
fmt.Printf("Program terminated with %v\n", sig.String())
|
||||
|
||||
stop()
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func stop() {
|
||||
cancel()
|
||||
common.ClearPIDFile()
|
||||
}
|
||||
|
||||
func stopDaemonCmd() error {
|
||||
proc, err := common.DaemonProcess()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting daemon process: %v", err)
|
||||
}
|
||||
|
||||
err = proc.Signal(syscall.SIGTERM)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending SIGTERM to daemon PID: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func reloadDaemonCmd() error {
|
||||
proc, err := common.DaemonProcess()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting daemon process: %v", err)
|
||||
}
|
||||
|
||||
err = proc.Signal(syscall.SIGHUP)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending SIGHUP to daemon PID: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tickDaemonCmd() error {
|
||||
proc, err := common.DaemonProcess()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting daemon process: %v", err)
|
||||
}
|
||||
|
||||
err = proc.Signal(syscall.SIGUSR1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending SIGUSR1 to daemon PID: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func daemonStatusCmd() error {
|
||||
fmt.Println("Not implemented :/")
|
||||
return nil
|
||||
}
|
||||
1
app/shared/install.go
Normal file
1
app/shared/install.go
Normal file
@@ -0,0 +1 @@
|
||||
package shared
|
||||
72
app/shared/util.go
Normal file
72
app/shared/util.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.nevets.tech/Keys/certman/common"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func createFile(fileName string, filePermission os.FileMode, data []byte) {
|
||||
fileInfo, err := os.Stat(fileName)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
file, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating configuration file: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(data)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to file: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = file.Chmod(filePermission)
|
||||
if err != nil {
|
||||
fmt.Println("Error changing file permission: ", err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Error opening configuration file: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
if fileInfo.Size() == 0 {
|
||||
file, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating configuration file: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(data)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to file:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DomainCertsDirWConf Can return ErrBlankConfigEntry or other errors
|
||||
func DomainCertsDirWConf(domain string, domainConfig *common.DomainConfig) string {
|
||||
var effectiveDataRoot string
|
||||
if domainConfig.Certificates.DataRoot == "" {
|
||||
effectiveDataRoot = config.Certificates.DataRoot
|
||||
} else {
|
||||
effectiveDataRoot = domainConfig.Certificates.DataRoot
|
||||
}
|
||||
|
||||
return filepath.Join(effectiveDataRoot, "certificates", domain)
|
||||
}
|
||||
|
||||
func basicCmd(use, short string, commandFunc func(cmd *cobra.Command, args []string)) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: use,
|
||||
Short: short,
|
||||
Run: commandFunc,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user