Skip to content

Commit

Permalink
More towards certs
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Sep 15, 2024
1 parent 341bcbe commit 6b38be9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
28 changes: 28 additions & 0 deletions certs/certbot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package certs

import (
"fmt"
"os/exec"
)

// CertbotCreateCertificate runs certbot to create a certificate
func CertbotCreateCertificate(domain, email string) error {
cmd := exec.Command("certbot", "certonly", "--non-interactive", "--agree-tos", "--email", email, "-d", domain)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to create certificate: %s", string(output))
}
fmt.Println("Certificate created successfully:", string(output))
return nil
}

// CertbotRenewCertificate runs certbot to renew a certificate
func CertbotRenewCertificate() error {
cmd := exec.Command("certbot", "renew")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to renew certificate: %s", string(output))
}
fmt.Println("Certificate renewed successfully:", string(output))
return nil
}
98 changes: 66 additions & 32 deletions cmd/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,78 @@ import (
func CertsCommand() *cli.Command {
return &cli.Command{
Name: "certs",
Usage: "Generate self-signed certificates for testing purposes",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cert",
Usage: "Path to save the certificate PEM file",
Value: "cert.pem",
Usage: "Manage SSL certificates",
Subcommands: []*cli.Command{
{
Name: "create",
Usage: "Create a new certificate",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "domain",
Usage: "Domain name for the certificate",
Required: true,
},
&cli.StringFlag{
Name: "email",
Usage: "Email for Let's Encrypt notifications",
Required: true,
},
},
Action: func(c *cli.Context) error {
domain := c.String("domain")
email := c.String("email")
return certs.CertbotCreateCertificate(domain, email)
},
},
&cli.StringFlag{
Name: "key",
Usage: "Path to save the private key PEM file",
Value: "key.pem",
{
Name: "renew",
Usage: "Renew existing certificates",
Action: func(c *cli.Context) error {
return certs.CertbotRenewCertificate()
},
},
},
Action: func(c *cli.Context) error {
cert, privKey, err := certs.GenerateSelfSignedCert() // Use the correct function
if err != nil {
return fmt.Errorf("failed to generate certificate: %w", err)
}
{
Name: "local",
Usage: "Generate self-signed certificates for testing purposes",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cert",
Usage: "Path to save the certificate PEM file",
Value: "cert.pem",
},
&cli.StringFlag{
Name: "key",
Usage: "Path to save the private key PEM file",
Value: "key.pem",
},
},
Action: func(c *cli.Context) error {
cert, privKey, err := certs.GenerateSelfSignedCert() // Use the correct function
if err != nil {
return fmt.Errorf("failed to generate certificate: %w", err)
}

// Export the cert and key to PEM format
certPEM, keyPEM, err := certs.ExportPEM(cert, privKey)
if err != nil {
return fmt.Errorf("failed to export certificate and key: %w", err)
}
// Export the cert and key to PEM format
certPEM, keyPEM, err := certs.ExportPEM(cert, privKey)
if err != nil {
return fmt.Errorf("failed to export certificate and key: %w", err)
}

// Save the PEM files
certOutput := c.String("cert")
keyOutput := c.String("key")
// Save the PEM files
certOutput := c.String("cert")
keyOutput := c.String("key")

if err := os.WriteFile(certOutput, certPEM, 0644); err != nil {
return fmt.Errorf("failed to write certificate file: %w", err)
}
if err := os.WriteFile(keyOutput, keyPEM, 0600); err != nil {
return fmt.Errorf("failed to write private key file: %w", err)
}
if err := os.WriteFile(certOutput, certPEM, 0644); err != nil {
return fmt.Errorf("failed to write certificate file: %w", err)
}
if err := os.WriteFile(keyOutput, keyPEM, 0600); err != nil {
return fmt.Errorf("failed to write private key file: %w", err)
}

fmt.Printf("Certificate and key have been successfully generated and saved to %s and %s\n", certOutput, keyOutput)
return nil
fmt.Printf("Certificate and key have been successfully generated and saved to %s and %s\n", certOutput, keyOutput)
return nil
},
},
},
}
}

0 comments on commit 6b38be9

Please sign in to comment.