Skip to content

Commit 4961736

Browse files
fix(CustomSSL): validator and ssl proxy updater fixed (#1080) (#1081)
* fix(CustomSSL): ssl key and cert validator * feat(SSLProxyUpdate): add ssl proxy update queue to system * fix(domain): update ssl status function (cherry picked from commit b915425) Co-authored-by: Tanmoy Sarkar <57363826+tanmoysrt@users.noreply.github.com>
1 parent 21ad155 commit 4961736

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

swiftwave_service/core/domain.operations.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"crypto/x509"
66
"encoding/pem"
77
"errors"
8-
"gorm.io/gorm"
8+
"strings"
99
"time"
10+
11+
"gorm.io/gorm"
1012
)
1113

1214
// This file contains the operations for the Domain model.
@@ -33,7 +35,7 @@ func (domain *Domain) FindById(_ context.Context, db gorm.DB, id uint) error {
3335
}
3436

3537
func (domain *Domain) Create(_ context.Context, db gorm.DB) error {
36-
err := domain.fillSSLInfo()
38+
err := domain.validateAndFillSSLInfo()
3739
if err != nil {
3840
return err
3941
}
@@ -42,7 +44,7 @@ func (domain *Domain) Create(_ context.Context, db gorm.DB) error {
4244
}
4345

4446
func (domain *Domain) Update(_ context.Context, db gorm.DB) error {
45-
err := domain.fillSSLInfo()
47+
err := domain.validateAndFillSSLInfo()
4648
if err != nil {
4749
return err
4850
}
@@ -66,16 +68,58 @@ func (domain *Domain) Delete(_ context.Context, db gorm.DB) error {
6668

6769
func (domain *Domain) UpdateSSLStatus(_ context.Context, db gorm.DB, status DomainSSLStatus) error {
6870
domain.SSLStatus = status
69-
tx := db.Where("id = ?", domain.ID).Update("ssl_status", status)
71+
tx := db.Model(&domain).Where("id = ?", domain.ID).Update("ssl_status", status)
7072
return tx.Error
7173
}
7274

73-
func (domain *Domain) fillSSLInfo() error {
75+
func (domain *Domain) validateAndFillSSLInfo() error {
7476
if domain == nil || domain.SSLFullChain == "" {
7577
return nil
7678
}
79+
80+
// if ssl full chain or private key is missing \n at the end , add it
81+
if !strings.HasSuffix(domain.SSLFullChain, "\n") {
82+
domain.SSLFullChain = domain.SSLFullChain + "\n"
83+
}
84+
if !strings.HasSuffix(domain.SSLPrivateKey, "\n") {
85+
domain.SSLPrivateKey = domain.SSLPrivateKey + "\n"
86+
}
87+
88+
// validate private key
89+
keyBytes := []byte(domain.SSLPrivateKey)
90+
block, _ := pem.Decode(keyBytes)
91+
if block == nil {
92+
return errors.New("failed to decode SSL private key")
93+
}
94+
// Attempt parsing the key as any supported private key format
95+
isValidated := false
96+
_, err := x509.ParsePKCS8PrivateKey(block.Bytes)
97+
if err == nil {
98+
isValidated = true // Key is valid PKCS8
99+
}
100+
101+
if !isValidated {
102+
103+
_, err = x509.ParsePKCS1PrivateKey(block.Bytes)
104+
if err == nil {
105+
isValidated = true // Key is valid PKCS1
106+
}
107+
}
108+
109+
if !isValidated {
110+
_, err = x509.ParseECPrivateKey(block.Bytes)
111+
if err == nil {
112+
isValidated = true // Key is valid EC
113+
}
114+
}
115+
116+
if !isValidated {
117+
return errors.New("provided private keys is not a valid private key (RSA, PKCS8, PKCS1, or EC)")
118+
}
119+
120+
// validate full chain certificate
77121
certBytes := []byte(domain.SSLFullChain)
78-
block, _ := pem.Decode(certBytes)
122+
block, _ = pem.Decode(certBytes)
79123
if block == nil {
80124
return errors.New("failed to decode SSL full chain certificate")
81125
}

swiftwave_service/graphql/domain.resolvers.go

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swiftwave_service/graphql/helpers.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package graphql
22

33
import (
44
"context"
5-
"encoding/pem"
65
"errors"
76
"fmt"
87
"log"
@@ -18,7 +17,6 @@ import (
1817
"github.com/swiftwave-org/swiftwave/swiftwave_service/graphql/model"
1918
"github.com/swiftwave-org/swiftwave/swiftwave_service/logger"
2019
"github.com/swiftwave-org/swiftwave/swiftwave_service/manager"
21-
"golang.org/x/crypto/ssh"
2220
"gorm.io/gorm"
2321
)
2422

@@ -55,37 +53,6 @@ func sanitizeFileName(fileName string) string {
5553
return fileName
5654
}
5755

58-
func ValidateSSLFullChainCertificate(certString string) error {
59-
// Parse the SSL public key (including certificates)
60-
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(certString))
61-
if err != nil {
62-
return fmt.Errorf("failed to parse SSL public key: %v", err)
63-
}
64-
65-
// Check if it's an SSH certificate
66-
_, ok := pubKey.(*ssh.Certificate)
67-
if !ok {
68-
return fmt.Errorf("provided file is not an SSL certificate")
69-
}
70-
71-
return nil
72-
}
73-
74-
func ValidateSSLPrivateKey(privateKeyString string) error {
75-
// Decode the PEM block
76-
block, _ := pem.Decode([]byte(privateKeyString))
77-
if block == nil {
78-
return fmt.Errorf("failed to decode PEM block from private key")
79-
}
80-
81-
// Try to parse the key as an SSH private key
82-
_, err := ssh.ParseRawPrivateKey(block.Bytes)
83-
if err != nil {
84-
return fmt.Errorf("failed to parse SSL private key: %v", err)
85-
}
86-
return nil
87-
}
88-
8956
func FetchDockerManager(ctx context.Context, db *gorm.DB) (*containermanger.Manager, error) {
9057
// Fetch a random swarm manager
9158
swarmManagerServer, err := core.FetchSwarmManager(db)

swiftwave_service/worker/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func Queues() []string {
6262
redirectRuleApplyQueueName,
6363
redirectRuleDeleteQueueName,
6464
sslGenerateQueueName,
65+
sslProxyUpdateQueueName,
6566
deletePersistentVolumeQueueName,
6667
persistentVolumeBackupQueueName,
6768
persistentVolumeRestoreQueueName,

0 commit comments

Comments
 (0)