diff --git a/cmd/open_im_api/main.go b/cmd/open_im_api/main.go index 97aa9f3ef..b9b0e2373 100644 --- a/cmd/open_im_api/main.go +++ b/cmd/open_im_api/main.go @@ -222,7 +222,7 @@ func main() { initGroup.POST("/set_client_config", clientInit.SetClientInitConfig) initGroup.POST("/get_client_config", clientInit.GetClientInitConfig) } - + go config.RegisterConf() go apiThird.MinioInit() defaultPorts := config.Config.Api.GinPort ginPort := flag.Int("port", defaultPorts[0], "get ginServerPort from cmd,default 10002 as port") diff --git a/config/config.yaml b/config/config.yaml index 3e1f54bdc..a25b39fd8 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -9,6 +9,7 @@ etcd: etcdAddr: [ 127.0.0.1:2379 ] #单机部署时,默认即可 userName: password: + secret: mysql: dbMysqlAddress: [ 127.0.0.1:13306 ] #mysql地址 目前仅支持单机,默认即可 diff --git a/config/usualConfig.yaml b/config/usualConfig.yaml index e2c720d2b..ab4483211 100644 --- a/config/usualConfig.yaml +++ b/config/usualConfig.yaml @@ -1,6 +1,7 @@ etcd: userName: password: + secret: # etcd 配置密钥 mysql: dbMysqlUserName: root #mysql用户名,建议修改 diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 3d290e0d6..64ca6d727 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -1,6 +1,8 @@ package config import ( + "Open_IM/pkg/grpc-etcdv3/getcdv3" + "Open_IM/pkg/utils" "fmt" "io/ioutil" "os" @@ -16,6 +18,8 @@ var ( Root = filepath.Join(filepath.Dir(b), "../../..") ) +const confName = "openIMConf" + var Config config type callBackConfig struct { @@ -157,6 +161,7 @@ type config struct { EtcdAddr []string `yaml:"etcdAddr"` UserName string `yaml:"userName"` Password string `yaml:"password"` + Secret string `yaml:"secret"` } Log struct { StorageLocation string `yaml:"storageLocation"` @@ -563,6 +568,7 @@ type usualConfig struct { Etcd struct { UserName string `yaml:"userName"` Password string `yaml:"password"` + Secret string `yaml:"secret"` } `yaml:"etcd"` Mysql struct { DBUserName string `yaml:"dbMysqlUserName"` @@ -647,13 +653,15 @@ func unmarshalConfig(config interface{}, configName string) { func init() { unmarshalConfig(&Config, "config.yaml") unmarshalConfig(&UsualConfig, "usualConfig.yaml") - fmt.Println(UsualConfig) if Config.Etcd.UserName == "" { Config.Etcd.UserName = UsualConfig.Etcd.UserName } if Config.Etcd.Password == "" { Config.Etcd.Password = UsualConfig.Etcd.Password } + if Config.Etcd.Secret == "" { + Config.Etcd.Secret = UsualConfig.Etcd.Secret + } if Config.Mysql.DBUserName == "" { Config.Mysql.DBUserName = UsualConfig.Mysql.DBUserName @@ -720,5 +728,17 @@ func init() { if Config.TokenPolicy.AccessSecret == "" { Config.TokenPolicy.AccessSecret = UsualConfig.Tokenpolicy.AccessSecret } - +} + +func RegisterConf() { + bytes, err := yaml.Marshal(Config) + if err != nil { + panic(err.Error()) + } + secretMD5 := utils.Md5(Config.Etcd.Secret) + confBytes, err := utils.AesEncrypt(bytes, []byte(secretMD5[0:16])) + if err != nil { + panic(err.Error()) + } + getcdv3.RegisterConf(getcdv3.GetPrefix(Config.Etcd.EtcdSchema, confName), string(confBytes)) } diff --git a/pkg/grpc-etcdv3/getcdv3/register.go b/pkg/grpc-etcdv3/getcdv3/register.go index aa3ba3072..6884dc465 100644 --- a/pkg/grpc-etcdv3/getcdv3/register.go +++ b/pkg/grpc-etcdv3/getcdv3/register.go @@ -1,6 +1,7 @@ package getcdv3 import ( + "Open_IM/pkg/common/config" "Open_IM/pkg/common/log" "Open_IM/pkg/utils" "context" @@ -127,3 +128,25 @@ func UnRegisterEtcd() { rEtcd.cancel() rEtcd.cli.Delete(rEtcd.ctx, rEtcd.key) } + +func RegisterConf(key, conf string) { + etcdAddr := strings.Join(config.Config.Etcd.EtcdAddr, ",") + cli, err := clientv3.New(clientv3.Config{ + Endpoints: strings.Split(etcdAddr, ","), DialTimeout: 5 * time.Second}) + + if err != nil { + panic(err.Error()) + } + //lease + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + resp, err := cli.Grant(ctx, 10) + if err != nil { + panic(err.Error()) + } + if _, err := cli.Put(ctx, key, conf, clientv3.WithLease(resp.ID)); err != nil { + fmt.Println("panic, params: ") + panic(err.Error()) + } + +} diff --git a/pkg/utils/encryption .go b/pkg/utils/encryption.go similarity index 100% rename from pkg/utils/encryption .go rename to pkg/utils/encryption.go diff --git a/script/init_pwd.sh b/script/init_pwd.sh new file mode 100644 index 000000000..c0b222a87 --- /dev/null +++ b/script/init_pwd.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +source ../.env + +nameList=(dbMysqlUserName, dbUserName, dbUserName, accessKeyID) +pwdList=(dbMysqlPassword, dbPassword, dbPassWord, secretAccessKey) + +for i in ${nameList[*]}; do + sed -i 's/{$i}: [a-z]/{$i}: {$USER}/g' ../config/usualConfig.yaml +done + +for i in ${pwdList[*]}; do + sed -i 's/{$i}: [a-z]/{$i}: {$PASSWORD}/g' ../config/usualConfig.yaml +done \ No newline at end of file