Skip to content

Commit 37323d9

Browse files
committed
feat(api): add TLS support for database, Redis, and RabbitMQ
1 parent b99862f commit 37323d9

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

src/server/api/go/configs/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ database:
1616
maxOpen: 20
1717
maxIdle: 10
1818
autoMigrate: true
19+
enableTLS: ${DATABASE_ENABLE_TLS}
1920

2021
redis:
2122
addr: "${REDIS_HOST}:${REDIS_EXPORT_PORT}"
2223
password: "${REDIS_PASSWORD}"
2324
db: 0
2425
poolSize: 10
26+
enableTLS: ${REDIS_ENABLE_TLS}
2527

2628
rabbitmq:
2729
url: "amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@${RABBITMQ_HOST}:${RABBITMQ_EXPORT_PORT}/${RABBITMQ_VHOST_ENCODED}"
2830
prefetch: 10
31+
enableTLS: ${RABBITMQ_ENABLE_TLS}
2932

3033
s3:
3134
endpoint: "${S3_ENDPOINT}"

src/server/api/go/internal/bootstrap/container.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package bootstrap
22

33
import (
44
"context"
5+
"crypto/tls"
6+
"strings"
57
"time"
68

79
"github.com/memodb-io/Acontext/internal/config"
@@ -80,6 +82,23 @@ func BuildContainer() *do.Injector {
8082
// RabbitMQ Connection
8183
do.Provide(inj, func(i *do.Injector) (*amqp.Connection, error) {
8284
cfg := do.MustInvoke[*config.Config](i)
85+
86+
// Check if TLS is enabled via config or URL protocol
87+
useTLS := cfg.RabbitMQ.EnableTLS || strings.HasPrefix(cfg.RabbitMQ.URL, "amqps://")
88+
89+
if useTLS {
90+
// Use TLS configuration with minimum TLS 1.2
91+
tlsConfig := &tls.Config{
92+
MinVersion: tls.VersionTLS12,
93+
}
94+
// Convert amqp:// to amqps:// if needed
95+
url := cfg.RabbitMQ.URL
96+
if strings.HasPrefix(url, "amqp://") {
97+
url = strings.Replace(url, "amqp://", "amqps://", 1)
98+
}
99+
return amqp.DialTLS(url, tlsConfig)
100+
}
101+
83102
return amqp.Dial(cfg.RabbitMQ.URL)
84103
})
85104

src/server/api/go/internal/config/config.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ type DBCfg struct {
3232
MaxOpen int
3333
MaxIdle int
3434
AutoMigrate bool
35+
EnableTLS bool
3536
}
3637

3738
type RedisCfg struct {
38-
Addr string
39-
Password string
40-
DB int
41-
PoolSize int
39+
Addr string
40+
Password string
41+
DB int
42+
PoolSize int
43+
EnableTLS bool
4244
}
4345

4446
type MQExchangeName struct {
@@ -52,6 +54,7 @@ type MQCfg struct {
5254
URL string
5355
Queue string
5456
Prefetch int
57+
EnableTLS bool
5558
ExchangeName MQExchangeName
5659
RoutingKey MQRoutingKey
5760
}
@@ -96,17 +99,20 @@ func setDefaults(v *viper.Viper) {
9699
v.SetDefault("root.apiBearerToken", "your-root-api-bearer-token")
97100
v.SetDefault("root.projectBearerTokenPrefix", "sk-ac-")
98101
v.SetDefault("database.dsn", "host=127.0.0.1 user=acontext password=helloworld dbname=acontext port=15432 sslmode=disable TimeZone=UTC")
102+
v.SetDefault("database.enableTLS", false)
99103
v.SetDefault("redis.addr", "127.0.0.1:16379")
100104
v.SetDefault("redis.password", "helloworld")
101105
v.SetDefault("redis.db", 0)
102106
v.SetDefault("redis.poolSize", 10)
107+
v.SetDefault("redis.enableTLS", false)
103108
v.SetDefault("s3.endpoint", "http://127.0.0.1:19000")
104109
v.SetDefault("s3.internalEndpoint", "http://127.0.0.1:19000")
105110
v.SetDefault("s3.region", "auto")
106111
v.SetDefault("s3.accessKey", "acontext")
107112
v.SetDefault("s3.secretKey", "helloworld")
108113
v.SetDefault("s3.bucket", "acontext-assets")
109114
v.SetDefault("rabbitmq.url", "amqp://acontext:[email protected]:15672/%2F")
115+
v.SetDefault("rabbitmq.enableTLS", false)
110116
v.SetDefault("rabbitmq.exchangeName.sessionMessage", "session.message")
111117
v.SetDefault("rabbitmq.routingKey.sessionMessageInsert", "session.message.insert")
112118
v.SetDefault("core.baseURL", "http://127.0.0.1:8019")

src/server/api/go/internal/infra/cache/redis.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@ package cache
22

33
import (
44
"context"
5+
"crypto/tls"
56

67
"github.com/memodb-io/Acontext/internal/config"
78
"github.com/redis/go-redis/extra/redisotel/v9"
89
"github.com/redis/go-redis/v9"
910
)
1011

1112
func New(cfg *config.Config) (*redis.Client, error) {
12-
rdb := redis.NewClient(&redis.Options{
13+
opts := &redis.Options{
1314
Addr: cfg.Redis.Addr,
1415
Password: cfg.Redis.Password,
1516
DB: cfg.Redis.DB,
1617
PoolSize: cfg.Redis.PoolSize,
17-
})
18+
}
19+
20+
// Enable TLS if configured
21+
if cfg.Redis.EnableTLS {
22+
opts.TLSConfig = &tls.Config{
23+
MinVersion: tls.VersionTLS12,
24+
}
25+
}
26+
27+
rdb := redis.NewClient(opts)
1828

1929
if err := rdb.Ping(context.Background()).Err(); err != nil {
2030
return nil, err

src/server/api/go/internal/infra/db/gorm.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package db
22

33
import (
4+
"regexp"
5+
"strings"
46
"time"
57

68
"github.com/memodb-io/Acontext/internal/config"
@@ -14,7 +16,26 @@ func New(cfg *config.Config) (*gorm.DB, error) {
1416
gcfg := &gorm.Config{
1517
Logger: logger.Default.LogMode(logger.Warn),
1618
}
17-
db, err := gorm.Open(postgres.Open(cfg.Database.DSN), gcfg)
19+
20+
// Adjust DSN sslmode based on EnableTLS configuration
21+
dsn := cfg.Database.DSN
22+
if cfg.Database.EnableTLS {
23+
// Replace sslmode=disable with sslmode=require when TLS is enabled
24+
// Use regex to handle various formats (sslmode=disable, sslmode=disable, etc.)
25+
sslmodeRegex := regexp.MustCompile(`(?i)\bsslmode\s*=\s*\w+`)
26+
if sslmodeRegex.MatchString(dsn) {
27+
// Replace existing sslmode
28+
dsn = sslmodeRegex.ReplaceAllString(dsn, "sslmode=require")
29+
} else {
30+
// Append sslmode if not present
31+
if !strings.HasSuffix(dsn, " ") {
32+
dsn += " "
33+
}
34+
dsn += "sslmode=require"
35+
}
36+
}
37+
38+
db, err := gorm.Open(postgres.Open(dsn), gcfg)
1839
if err != nil {
1940
return nil, err
2041
}

0 commit comments

Comments
 (0)