package client import ( "fmt" "path/filepath" "git.nevets.tech/Keys/certman/app/shared" "git.nevets.tech/Keys/certman/client" "git.nevets.tech/Keys/certman/common" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5/storage/memory" "github.com/spf13/cobra" ) var ( renewCertSubCmd = &cobra.Command{ Use: "renew", Short: "Renews a domains certificate", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return renewCert(args[0]) }, } updateCertLinkSubCmd = &cobra.Command{ Use: "update-link", Short: "Update linked certificates", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return updateLinks(args[0]) }, } decryptCertsSubCmd = &cobra.Command{ Use: "decrypt [certPath] [cryptoKey]", Short: "Decrypt certificates", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { return client.DecryptCertificates(args[0], args[1]) }, } ) func init() { renewCertSubCmd.AddCommand(updateCertLinkSubCmd, decryptCertsSubCmd) shared.CertCmd.AddCommand(renewCertSubCmd) } func renewCert(domain string) error { gitWorkspace := &common.GitWorkspace{ Domain: domain, Storage: memory.NewStorage(), FS: memfs.New(), } config := shared.Config() domainConfig, exists := shared.DomainStore().Get(domain) if !exists { return shared.ErrConfigNotFound } if err := client.PullCerts(config, domainConfig, gitWorkspace); err != nil { return err } certsDir := common.CertsDir(config, domainConfig) return client.DecryptAndWriteCertificates(certsDir, config, domainConfig, gitWorkspace) } func updateLinks(domain string) error { domainConfig, exists := shared.DomainStore().Get(domain) if !exists { return fmt.Errorf("domain %s does not exist", domain) } certsDir := shared.DomainCertsDirWConf(domain, domainConfig) certLinks := domainConfig.Certificates.CertSymlinks for _, certLink := range certLinks { err := common.LinkFile(filepath.Join(certsDir, domain+".crt"), certLink, domain, ".crt") if err != nil { fmt.Printf("Error linking cert %s to %s: %v", certLink, domain, err) continue } } keyLinks := domainConfig.Certificates.KeySymlinks for _, keyLink := range keyLinks { err := common.LinkFile(filepath.Join(certsDir, domain+".crt"), keyLink, domain, ".key") if err != nil { fmt.Printf("Error linking cert %s to %s: %v", keyLink, domain, err) continue } } return nil }