Skip to content

Commit 61ed191

Browse files
authored
feat: auto ssl renewal (#424)
1 parent 9cef8d6 commit 61ed191

20 files changed

+150
-165
lines changed

ssl_manager/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func decodePrivateKey(key string) (*rsa.PrivateKey, error) {
2424
return privateKey, nil
2525
}
2626

27-
// Fetch SSL Issuer's Name
2827
func (s Manager) FetchIssuerName() string {
2928
if s.options.IsStaging {
3029
return "Let's Encrypt (Staging)"

swiftwave_service/cmd/auto-service-tls-renew.go

Lines changed: 0 additions & 119 deletions
This file was deleted.

swiftwave_service/cmd/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ var initCmd = &cobra.Command{
110110
newConfig := &local_config.Config{
111111
IsDevelopmentMode: false,
112112
ServiceConfig: local_config.ServiceConfig{
113-
UseTLS: false,
114-
ManagementNodeAddress: domainName,
113+
UseTLS: false,
114+
ManagementNodeAddress: domainName,
115+
AutoRenewManagementNodeCert: false,
115116
},
116117
PostgresqlConfig: local_config.PostgresqlConfig{
117118
Host: defaultString(currentPostgresHost, "127.0.0.1"),

swiftwave_service/cmd/swiftwave-service-tls-renew.service

Lines changed: 0 additions & 10 deletions
This file was deleted.

swiftwave_service/cmd/swiftwave-service-tls-renew.timer

Lines changed: 0 additions & 10 deletions
This file was deleted.

swiftwave_service/cmd/tls.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func init() {
2828
tlsCmd.AddCommand(tlsDisableCmd)
2929
tlsCmd.AddCommand(generateCertificateCommand)
3030
tlsCmd.AddCommand(renewCertificateCommand)
31-
tlsCmd.AddCommand(autoServiceTLSRenewCmd)
3231
}
3332

3433
var tlsCmd = &cobra.Command{
@@ -59,6 +58,7 @@ var tlsEnableCmd = &cobra.Command{
5958
return
6059
}
6160
config.LocalConfig.ServiceConfig.UseTLS = true
61+
config.LocalConfig.ServiceConfig.AutoRenewManagementNodeCert = true
6262
err := local_config.Update(config.LocalConfig)
6363
if err != nil {
6464
printError("Failed to update config")
@@ -83,6 +83,7 @@ var tlsDisableCmd = &cobra.Command{
8383
return
8484
}
8585
lConfig.ServiceConfig.UseTLS = false
86+
config.LocalConfig.ServiceConfig.AutoRenewManagementNodeCert = false
8687
err := local_config.Update(lConfig)
8788
if err != nil {
8889
printError("Failed to update config")
@@ -194,6 +195,7 @@ var generateCertificateCommand = &cobra.Command{
194195
printSuccess("Successfully generated TLS certificate for " + domain)
195196
// Enable TLS for swiftwave service
196197
config.LocalConfig.ServiceConfig.UseTLS = true
198+
config.LocalConfig.ServiceConfig.AutoRenewManagementNodeCert = true
197199
err = local_config.Update(config.LocalConfig)
198200
if err != nil {
199201
printError("Failed to update config")
@@ -220,13 +222,13 @@ var renewCertificateCommand = &cobra.Command{
220222
if _, err := os.Stat(sslCertificatePath); os.IsNotExist(err) {
221223
printError("No TLS certificate found")
222224
printInfo("Use `swiftwave tls generate` to generate a new certificate")
223-
return
225+
os.Exit(1)
224226
}
225227
isRenewalRequired, err := isRenewalImminent(sslCertificatePath)
226228
if err != nil {
227229
printError("Failed to check if renewal is required")
228230
printError(err.Error())
229-
return
231+
os.Exit(1)
230232
}
231233
if isRenewalRequired {
232234
printSuccess("Renewal is required")

swiftwave_service/config/local_config/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Config struct {
1616
type ServiceConfig struct {
1717
UseTLS bool `yaml:"use_tls"`
1818
ManagementNodeAddress string `yaml:"management_node_address"`
19+
AutoRenewManagementNodeCert bool `yaml:"auto_renew_management_node_cert"`
1920
BindAddress string `yaml:"bind_address"`
2021
BindPort int `yaml:"bind_port"`
2122
SocketPathDirectory string `yaml:"-"`

swiftwave_service/core/domain.operations.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package core
22

33
import (
44
"context"
5+
"crypto/x509"
6+
"encoding/pem"
57
"errors"
68
"gorm.io/gorm"
9+
"time"
710
)
811

912
// This file contains the operations for the Domain model.
@@ -12,28 +15,42 @@ import (
1215
// Each function's argument format should be (ctx context.Context, db gorm.DB, ...)
1316
// context used to pass some data to the function e.g. user id, auth info, etc.
1417

15-
func FindAllDomains(ctx context.Context, db gorm.DB) ([]*Domain, error) {
18+
func FindAllDomains(_ context.Context, db gorm.DB) ([]*Domain, error) {
1619
var domains []*Domain
1720
tx := db.Find(&domains)
1821
return domains, tx.Error
1922
}
2023

21-
func (domain *Domain) FindById(ctx context.Context, db gorm.DB, id uint) error {
24+
func FetchDomainsThoseWillExpire(_ context.Context, db gorm.DB, daysToExpire int) ([]*Domain, error) {
25+
var domains []*Domain
26+
tx := db.Where("ssl_expired_at < ?", time.Now().AddDate(0, 0, daysToExpire)).Find(&domains)
27+
return domains, tx.Error
28+
}
29+
30+
func (domain *Domain) FindById(_ context.Context, db gorm.DB, id uint) error {
2231
tx := db.Where("id = ?", id).First(&domain)
2332
return tx.Error
2433
}
2534

26-
func (domain *Domain) Create(ctx context.Context, db gorm.DB) error {
35+
func (domain *Domain) Create(_ context.Context, db gorm.DB) error {
36+
err := domain.fillSSLInfo()
37+
if err != nil {
38+
return err
39+
}
2740
tx := db.Create(&domain)
2841
return tx.Error
2942
}
3043

31-
func (domain *Domain) Update(ctx context.Context, db gorm.DB) error {
44+
func (domain *Domain) Update(_ context.Context, db gorm.DB) error {
45+
err := domain.fillSSLInfo()
46+
if err != nil {
47+
return err
48+
}
3249
tx := db.Save(&domain)
3350
return tx.Error
3451
}
3552

36-
func (domain *Domain) Delete(ctx context.Context, db gorm.DB) error {
53+
func (domain *Domain) Delete(_ context.Context, db gorm.DB) error {
3754
// Make sure there is no ingress rule or redirect rule associated with this domain
3855
isIngressRuleExist := db.Where("domain_id = ?", domain.ID).First(&IngressRule{}).RowsAffected > 0
3956
if isIngressRuleExist {
@@ -47,8 +64,31 @@ func (domain *Domain) Delete(ctx context.Context, db gorm.DB) error {
4764
return tx.Error
4865
}
4966

50-
func (domain *Domain) UpdateSSLStatus(ctx context.Context, db gorm.DB, status DomainSSLStatus) error {
67+
func (domain *Domain) UpdateSSLStatus(_ context.Context, db gorm.DB, status DomainSSLStatus) error {
5168
domain.SSLStatus = status
5269
tx := db.Where("id = ?", domain.ID).Update("ssl_status", status)
5370
return tx.Error
5471
}
72+
73+
func (domain *Domain) fillSSLInfo() error {
74+
if domain == nil || domain.SSLFullChain == "" {
75+
return nil
76+
}
77+
certBytes := []byte(domain.SSLFullChain)
78+
block, _ := pem.Decode(certBytes)
79+
if block == nil {
80+
return errors.New("failed to decode SSL full chain certificate")
81+
}
82+
cert, err := x509.ParseCertificate(block.Bytes)
83+
if err != nil {
84+
return errors.New("failed to parse SSL full chain certificate")
85+
}
86+
domain.SSLIssuedAt = cert.NotBefore
87+
domain.SSLExpiredAt = cert.NotAfter
88+
var sslIssuer = "Unknown Issuer"
89+
if len(cert.Issuer.Organization) > 0 {
90+
sslIssuer = cert.Issuer.Organization[0]
91+
}
92+
domain.SSLIssuer = sslIssuer
93+
return nil
94+
}

swiftwave_service/core/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type Domain struct {
7979
SSLPrivateKey string `json:"ssl_private_key"`
8080
SSLFullChain string `json:"ssl_full_chain"`
8181
SSLIssuedAt time.Time `json:"ssl_issued_at"`
82+
SSLExpiredAt time.Time `json:"ssl_expired_at"`
8283
SSLIssuer string `json:"ssl_issuer"`
8384
SSLAutoRenew bool `json:"ssl_auto_renew" gorm:"default:false"`
8485
IngressRules []IngressRule `json:"ingress_rules" gorm:"foreignKey:DomainID"`

swiftwave_service/cronjob/cleanup_unused_images.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
func (m Manager) CleanupUnusedImages() {
12+
logger.CronJobLogger.Println("Starting cleanup of unused images [cronjob]")
1213
for {
1314
time.Sleep(1 * time.Hour)
1415
// Fetch all servers

0 commit comments

Comments
 (0)