Fix: custon ca use crypto

pull/44/head
zijiren233 3 years ago
parent 5a96b833c3
commit d98c4b2d41

@ -21,12 +21,12 @@ type Etcd struct {
} }
type Backend struct { type Backend struct {
Endpoint string `gorm:"primaryKey" json:"endpoint"` Endpoint string `gorm:"primaryKey" json:"endpoint"`
Comment string `gorm:"type:text" json:"comment"` Comment string `gorm:"type:text" json:"comment"`
Tls bool `gorm:"default:false" json:"tls"` Tls bool `gorm:"default:false" json:"tls"`
JwtSecret string `json:"jwtSecret"` JwtSecret string `json:"jwtSecret"`
CustomCAFile string `json:"customCaFile"` CustomCA string `gorm:"type:text" json:"customCA"`
TimeOut string `gorm:"default:10s" json:"timeOut"` TimeOut string `gorm:"default:10s" json:"timeOut"`
Consul Consul `gorm:"embedded;embeddedPrefix:consul_" json:"consul"` Consul Consul `gorm:"embedded;embeddedPrefix:consul_" json:"consul"`
Etcd Etcd `gorm:"embedded;embeddedPrefix:etcd_" json:"etcd"` Etcd Etcd `gorm:"embedded;embeddedPrefix:etcd_" json:"etcd"`
@ -64,6 +64,11 @@ func (v *VendorBackend) BeforeSave(tx *gorm.DB) error {
return err return err
} }
} }
if v.Backend.CustomCA != "" {
if v.Backend.CustomCA, err = utils.CryptoToBase64([]byte(v.Backend.CustomCA), key); err != nil {
return err
}
}
return nil return nil
} }
@ -94,5 +99,12 @@ func (v *VendorBackend) AfterFind(tx *gorm.DB) error {
v.Backend.Etcd.Password = string(data) v.Backend.Etcd.Password = string(data)
} }
} }
if v.Backend.CustomCA != "" {
if data, err = utils.DecryptoFromBase64(v.Backend.CustomCA, key); err != nil {
return err
} else {
v.Backend.CustomCA = string(data)
}
}
return nil return nil
} }

@ -400,12 +400,8 @@ func NewGrpcClientConn(ctx context.Context, conf *model.Backend) (*grpc.ClientCo
if err != nil { if err != nil {
return nil, err return nil, err
} }
if conf.CustomCAFile != "" { if conf.CustomCA != "" {
b, err := os.ReadFile(conf.CustomCAFile) rootCAs.AppendCertsFromPEM([]byte(conf.CustomCA))
if err != nil {
return nil, err
}
rootCAs.AppendCertsFromPEM(b)
} }
opts = append(opts, ggrpc.WithTLSConfig(&tls.Config{ opts = append(opts, ggrpc.WithTLSConfig(&tls.Config{
RootCAs: rootCAs, RootCAs: rootCAs,
@ -462,8 +458,8 @@ func NewHttpClientConn(ctx context.Context, conf *model.Backend) (*http.Client,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if conf.CustomCAFile != "" { if conf.CustomCA != "" {
b, err := os.ReadFile(conf.CustomCAFile) b, err := os.ReadFile(conf.CustomCA)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save