aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/redis2/redis_store.go
blob: f9322be4218af8714266aed22ef123a20d4f23fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package redis2

import (
	"crypto/tls"
	"crypto/x509"
	"net"
	"os"

	"github.com/redis/go-redis/v9"
	"github.com/seaweedfs/seaweedfs/weed/filer"
	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/util"
)

func init() {
	filer.Stores = append(filer.Stores, &Redis2Store{})
}

type Redis2Store struct {
	UniversalRedis2Store
}

func (store *Redis2Store) GetName() string {
	return "redis2"
}

func (store *Redis2Store) Initialize(configuration util.Configuration, prefix string) (err error) {
	return store.initialize(
		configuration.GetString(prefix+"address"),
		configuration.GetString(prefix+"password"),
		configuration.GetInt(prefix+"database"),
		configuration.GetStringSlice(prefix+"superLargeDirectories"),
		configuration.GetBool(prefix+"enable_mtls"),
		configuration.GetString(prefix+"ca_cert_path"),
		configuration.GetString(prefix+"client_cert_path"),
		configuration.GetString(prefix+"client_key_path"),
	)
}

func (store *Redis2Store) initialize(hostPort string, password string, database int, superLargeDirectories []string, enableMtls bool, caCertPath string, clientCertPath string, clientKeyPath string) (err error) {
	if enableMtls {
		clientCert, err := tls.LoadX509KeyPair(clientCertPath, clientKeyPath)
		if err != nil {
			glog.Fatalf("Error loading client certificate and key pair: %v", err)
		}

		caCertBytes, err := os.ReadFile(caCertPath)
		if err != nil {
			glog.Fatalf("Error reading CA certificate file: %v", err)
		}

		caCertPool := x509.NewCertPool()
		if ok := caCertPool.AppendCertsFromPEM(caCertBytes); !ok {
			glog.Fatalf("Error appending CA certificate to pool")
		}

		redisHost, _, err := net.SplitHostPort(hostPort)
		if err != nil {
			glog.Fatalf("Error parsing redis host and port from %s: %v", hostPort, err)
		}

		tlsConfig := &tls.Config{
			Certificates: []tls.Certificate{clientCert},
			RootCAs:      caCertPool,
			ServerName:   redisHost,
			MinVersion:   tls.VersionTLS12,
		}
		store.Client = redis.NewClient(&redis.Options{
			Addr:      hostPort,
			Password:  password,
			DB:        database,
			TLSConfig: tlsConfig,
		})
	} else {
		store.Client = redis.NewClient(&redis.Options{
			Addr:     hostPort,
			Password: password,
			DB:       database,
		})
	}
	store.loadSuperLargeDirectories(superLargeDirectories)
	return
}