redis_store.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package redis3
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "net"
  6. "os"
  7. "github.com/go-redsync/redsync/v4"
  8. "github.com/go-redsync/redsync/v4/redis/goredis/v9"
  9. "github.com/redis/go-redis/v9"
  10. "github.com/seaweedfs/seaweedfs/weed/filer"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/util"
  13. )
  14. func init() {
  15. filer.Stores = append(filer.Stores, &Redis3Store{})
  16. }
  17. type Redis3Store struct {
  18. UniversalRedis3Store
  19. }
  20. func (store *Redis3Store) GetName() string {
  21. return "redis3"
  22. }
  23. func (store *Redis3Store) Initialize(configuration util.Configuration, prefix string) (err error) {
  24. return store.initialize(
  25. configuration.GetString(prefix+"address"),
  26. configuration.GetString(prefix+"password"),
  27. configuration.GetInt(prefix+"database"),
  28. configuration.GetBool(prefix+"enable_mtls"),
  29. configuration.GetString(prefix+"ca_cert_path"),
  30. configuration.GetString(prefix+"client_cert_path"),
  31. configuration.GetString(prefix+"client_key_path"),
  32. )
  33. }
  34. func (store *Redis3Store) initialize(hostPort string, password string, database int, enableMtls bool, caCertPath string, clientCertPath string, clientKeyPath string) (err error) {
  35. if enableMtls {
  36. clientCert, err := tls.LoadX509KeyPair(clientCertPath, clientKeyPath)
  37. if err != nil {
  38. glog.Fatalf("Error loading client certificate and key pair: %v", err)
  39. }
  40. caCertBytes, err := os.ReadFile(caCertPath)
  41. if err != nil {
  42. glog.Fatalf("Error reading CA certificate file: %v", err)
  43. }
  44. caCertPool := x509.NewCertPool()
  45. if ok := caCertPool.AppendCertsFromPEM(caCertBytes); !ok {
  46. glog.Fatalf("Error appending CA certificate to pool")
  47. }
  48. redisHost, _, err := net.SplitHostPort(hostPort)
  49. if err != nil {
  50. glog.Fatalf("Error parsing redis host and port from %s: %v", hostPort, err)
  51. }
  52. tlsConfig := &tls.Config{
  53. Certificates: []tls.Certificate{clientCert},
  54. RootCAs: caCertPool,
  55. ServerName: redisHost,
  56. MinVersion: tls.VersionTLS12,
  57. }
  58. store.Client = redis.NewClient(&redis.Options{
  59. Addr: hostPort,
  60. Password: password,
  61. DB: database,
  62. TLSConfig: tlsConfig,
  63. })
  64. } else {
  65. store.Client = redis.NewClient(&redis.Options{
  66. Addr: hostPort,
  67. Password: password,
  68. DB: database,
  69. })
  70. }
  71. store.redsync = redsync.New(goredis.NewPool(store.Client))
  72. return
  73. }