Skip to content

Commit fdb78db

Browse files
Merge pull request #33 from stackroost/dev
feat(cli): add check-ssl-expiry command to monitor domain certificate…
2 parents 6fbd9ec + 1b20088 commit fdb78db

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"stackroost/internal"
1010
"stackroost/internal/logger"
1111
"strings"
12+
"stackroost/cmd/ssl"
1213
)
1314

1415
var rootCmd = &cobra.Command{
@@ -208,6 +209,8 @@ func init() {
208209
createDomainCmd.Flags().StringP("server", "s", "apache", "Web server type (e.g., apache, nginx, caddy)")
209210
createDomainCmd.Flags().Bool("ssl", false, "Enable Let's Encrypt SSL (Apache/Nginx only)")
210211
createDomainCmd.MarkFlagRequired("name")
212+
rootCmd.AddCommand(ssl.CheckSSLExpiryCmd)
213+
211214
}
212215

213216
func Execute() {

cmd/ssl/check_expiry.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ssl
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"crypto/tls"
7+
"time"
8+
"github.com/spf13/cobra"
9+
"stackroost/internal/logger"
10+
)
11+
12+
var CheckSSLExpiryCmd = &cobra.Command{
13+
Use: "check-ssl-expiry",
14+
Short: "Check the SSL certificate expiry for a domain",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
domain, _ := cmd.Flags().GetString("domain")
17+
if domain == "" {
18+
logger.Error("Please provide a domain using --domain")
19+
os.Exit(1)
20+
}
21+
checkSSLExpiry(domain)
22+
},
23+
}
24+
25+
func init() {
26+
CheckSSLExpiryCmd.Flags().String("domain", "", "Domain to check SSL expiry for")
27+
CheckSSLExpiryCmd.MarkFlagRequired("domain")
28+
}
29+
30+
func checkSSLExpiry(domain string) {
31+
conn, err := tls.Dial("tcp", domain+":443", nil)
32+
if err != nil {
33+
logger.Error(fmt.Sprintf("Failed to connect: %v", err))
34+
os.Exit(1)
35+
}
36+
defer conn.Close()
37+
38+
certs := conn.ConnectionState().PeerCertificates
39+
if len(certs) == 0 {
40+
logger.Error("No SSL certificates found")
41+
os.Exit(1)
42+
}
43+
expiry := certs[0].NotAfter
44+
daysLeft := int(time.Until(expiry).Hours() / 24)
45+
46+
logger.Info(fmt.Sprintf("SSL for %s expires on: %s (%d days left)", domain, expiry.Format(time.RFC1123), daysLeft))
47+
48+
if daysLeft < 15 {
49+
logger.Warn("SSL certificate is expiring soon!")
50+
}
51+
}

0 commit comments

Comments
 (0)