Skip to content

Commit

Permalink
feat(backend): 密码随机化合并 #1773
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 committed Nov 17, 2023
2 parents ed5e4dd + 6e2bfab commit 845b405
Show file tree
Hide file tree
Showing 31 changed files with 258 additions and 173 deletions.
10 changes: 5 additions & 5 deletions dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func GetRedisLoccalConfFile(port int) (confFile string, err error) {
return
}

// GetPasswordFromLocalConfFile (从配置文件中)获取本地redis实例密码
func GetPasswordFromLocalConfFile(port int) (password string, err error) {
// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码
func GetRedisPasswdFromConfFile(port int) (password string, err error) {
confFile, err := GetRedisLoccalConfFile(port)
if err != nil {
err = fmt.Errorf("get redis local config file failed,err:%v,port:%d", err, port)
Expand All @@ -55,8 +55,8 @@ func GetProxyPasswdFromConfFlie(port int, role string) (password string, err err
grepCmd = fmt.Sprintf(`grep -w "password" %s/twemproxy*/%d/nutcracker.%d.yml|grep -vE "#"|awk '{print $NF}'`,
consts.DataPath, port, port)
} else if role == consts.MetaRolePredixy {
grepCmd = fmt.Sprintf(`grep -iw "auth" %s/predixy/%d/predixy.conf|awk '{print $2}'`,
consts.Data1Path, port)
grepCmd = fmt.Sprintf(`grep -Pi -B 2 "Mode\s*?write" %s/predixy/%d/predixy.conf|grep -iw "auth"|awk '{print $2}'`,
consts.GetRedisDataDir(), port)
}
password, err = util.RunBashCmd(grepCmd, "", nil, 10*time.Second)
if err != nil {
Expand Down Expand Up @@ -90,7 +90,7 @@ func LocalRedisConnectTest(ip string, ports []int, password string) (err error)
l01 := make([]*connTestItem, 0, len(ports))
for _, port := range ports {
if password == "" {
password, err = GetPasswordFromLocalConfFile(port)
password, err = GetRedisPasswdFromConfFile(port)
if err != nil {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (job *RedisBackup) Run() (err error) {
}
bakTasks := make([]*BackupTask, 0, len(job.params.Ports))
for _, port := range job.params.Ports {
password, err = myredis.GetPasswordFromLocalConfFile(port)
password, err = myredis.GetRedisPasswdFromConfFile(port)
if err != nil {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (task *RedisDataRecover) Init(m *jobruntime.JobGenericRuntime) error {
return err
}

//传入的源端口数应该等于临时节点端口数
// 传入的源端口数应该等于临时节点端口数
if len(task.params.SourcePorts) != len(task.params.NewTempPorts) {
err = fmt.Errorf("RedisDataRecover SourcePorts(%d) != NewTempPorts(%d) , is invalid ",
task.params.SourcePorts, task.params.NewTempPorts)
Expand Down Expand Up @@ -152,7 +152,7 @@ func (task *RedisDataRecover) Run() (err error) {

} else {
// 数据构造时从本地获取密码信息
task.password, err = myredis.GetPasswordFromLocalConfFile(newTmpPort)
task.password, err = myredis.GetRedisPasswdFromConfFile(newTmpPort)
if err != nil {
return err
}
Expand Down Expand Up @@ -266,6 +266,7 @@ func (task *RedisDataRecover) CheckRecoverDir() (err error) {
return nil
}

// stopBkDbmon TODO
// // StopBkDbmon 停 bk-dbmon
func (task *RedisDataRecover) stopBkDbmon() (err error) {

Expand Down Expand Up @@ -301,7 +302,7 @@ func (task *RedisDataRecover) startBkDbmon() (err error) {
task.runtime.Logger.Error(err.Error())
return err
}
//用mysql权限
// 用mysql权限
startCmd := fmt.Sprintf("su %s -c 'nohup %s &'", consts.MysqlAaccount, "sh "+startScript)
task.runtime.Logger.Info(startCmd)
_, err = util.RunLocalCmd("su", []string{consts.MysqlAaccount, "-c", "nohup sh " + startScript + " &"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type RedisDataStructureParams struct {
IsIncludeSlave bool `json:"is_include_slave" `
TendisType string `json:"tendis_type" validate:"required"`
DestDir string `json:"dest_dir"` // 备份下载/存放目录
FullFileList []datastructure.FileDetail `json:"full_file_list" validate:"required"` //全备文件列表
BinlogFileList []datastructure.FileDetail `json:"binlog_file_list" ` //binlog文件列表
FullFileList []datastructure.FileDetail `json:"full_file_list" validate:"required"` // 全备文件列表
BinlogFileList []datastructure.FileDetail `json:"binlog_file_list" ` // binlog文件列表
}

// RedisDataStructure redis 数据构造
Expand Down Expand Up @@ -80,7 +80,7 @@ func (task *RedisDataStructure) Init(m *jobruntime.JobGenericRuntime) error {
return err
}

//传入的源端口数应该等于临时节点端口数
// 传入的源端口数应该等于临时节点端口数
if len(task.params.SourcePorts) != len(task.params.NewTempPorts) {
err = fmt.Errorf("RedisDataStructure SourcePorts(%d) != NewTempPorts(%d) , is invalid ",
task.params.SourcePorts, task.params.NewTempPorts)
Expand Down Expand Up @@ -126,7 +126,7 @@ func (task *RedisDataStructure) Run() (err error) {
task.TendisType = task.params.TendisType
task.password = ""
// 数据构造时从本地获取密码信息
task.password, err = myredis.GetPasswordFromLocalConfFile(newTmpPort)
task.password, err = myredis.GetRedisPasswdFromConfFile(newTmpPort)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,28 @@ func (job *RedisDtsOnlineSwitch) BackupSrcProxyConfFile() (err error) {
return nil
}

func (job *RedisDtsOnlineSwitch) getDstPredixyLogRootDir() string {
lines := strings.Split(job.params.DstProxyConfigContent, "\n")
var logLine string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, "Log ") {
logLine = line
break
}
}
parts := strings.Fields(logLine)
path := parts[1]

pathParts := strings.Split(path, "/")
firstPart := pathParts[1]

return "/" + firstPart
}

// NewProxyConfigFileForSameType (类型不变时)生成新的proxy配置文件
func (job *RedisDtsOnlineSwitch) NewProxyConfigFileForSameType() (err error) {
dstConfContent := job.params.DstProxyConfigContent
Expand All @@ -326,6 +348,10 @@ func (job *RedisDtsOnlineSwitch) NewProxyConfigFileForSameType() (err error) {
} else if consts.IsPredixyClusterType(job.params.SrcClusterType) {
re := regexp.MustCompile(`Auth\s*"` + job.params.DstProxyPassword + `"`)
dstConfContent = re.ReplaceAllString(dstConfContent, `Auth "`+job.params.SrcProxyPassword+`"`)
logRootDir := job.getDstPredixyLogRootDir()
if logRootDir != consts.GetRedisDataDir() {
dstConfContent = strings.ReplaceAll(dstConfContent, logRootDir+"/", consts.GetRedisDataDir()+"/")
}
}

newFile := fmt.Sprintf("dts_new_config.billid_%d.%s_%d.%s", job.params.DtsBillID, job.params.SrcProxyIP,
Expand Down Expand Up @@ -379,6 +405,10 @@ func (job *RedisDtsOnlineSwitch) NewProxyConfigFileForDiffType() (err error) {
} else if consts.IsPredixyClusterType(job.params.DstClusterType) {
re := regexp.MustCompile(`Auth\s*"` + job.params.DstProxyPassword + `"`)
dstConfContent = re.ReplaceAllString(dstConfContent, `Auth "`+job.params.SrcProxyPassword+`"`)
logRootDir := job.getDstPredixyLogRootDir()
if logRootDir != consts.GetRedisDataDir() {
dstConfContent = strings.ReplaceAll(dstConfContent, logRootDir+"/", consts.GetRedisDataDir()+"/")
}
}

proxyFileForDstPort := job.getProxyFile(job.params.DstClusterType, job.params.DstProxyPort)
Expand Down Expand Up @@ -721,7 +751,8 @@ func (job *RedisDtsOnlineSwitch) UntarDstProxyMedia() (err error) {
util.LocalDirChownMysql(proxySoftLink + string(filepath.Separator))

if consts.IsPredixyClusterType(job.params.DstClusterType) {
sedCmd := fmt.Sprintf("sed -i 's#/data#%s#g' %s", consts.GetRedisDataDir(),
// 替换 start_predixy.sh 中的 /data
sedCmd := fmt.Sprintf("sed -i 's#/data\"#%s\"#g' %s", consts.GetRedisDataDir(),
filepath.Join(proxySoftLink, "bin", "start_predixy.sh"))
job.runtime.Logger.Info(sedCmd)
_, err = util.RunBashCmd(sedCmd, "", nil, 30*time.Second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (job *TendisKeysPattern) Run() (err error) {

keyTasks := make([]*RedisInsKeyPatternTask, 0, len(job.params.Ports))
for _, port := range job.params.Ports {
password, err := myredis.GetPasswordFromLocalConfFile(port)
password, err := myredis.GetRedisPasswdFromConfFile(port)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (job *TendisPlusMigrateSlots) Run() error {
return nil
}

//这一步放到flow来做会更好些,扩容的时候
// 这一步放到flow来做会更好些,扩容的时候
// job.dstClusterMeetSrc()
// if job.Err != nil {
// return job.Err
Expand Down Expand Up @@ -185,7 +185,7 @@ func (job *TendisPlusMigrateSlots) dstNodeAddr() string {
// dstClusterMeetSrc 新建节点加入源集群
func (job *TendisPlusMigrateSlots) dstClusterMeetSrc() {
var err error
nodePasswordOnMachine, err := myredis.GetPasswordFromLocalConfFile(job.params.SrcNode.Port)
nodePasswordOnMachine, err := myredis.GetRedisPasswdFromConfFile(job.params.SrcNode.Port)
if err != nil {
job.Err = fmt.Errorf("SrcNode GetPassword GetPasswordFromLocalConfFile filed: %+v", err)
job.runtime.Logger.Error(job.Err.Error())
Expand Down Expand Up @@ -620,7 +620,8 @@ func (job *TendisPlusMigrateSlots) MigrateSpecificSlots(srcAddr,
job.runtime.Logger.Info(msg)
importRet, err = dstCli.DoCommand(cmd, 0)
if err != nil && strings.Contains(err.Error(), "slot in deleting") == true {
msg = fmt.Sprintf(`slot in deleting : MigrateSpecificSlots execute cluster setslot importing fail,err:%v,srcAddr:%s,dstAddr:%s,cmd: cluster
msg = fmt.Sprintf(
`slot in deleting : MigrateSpecificSlots execute cluster setslot importing fail,err:%v,srcAddr:%s,dstAddr:%s,cmd: cluster
setslot importing %s %s`, err, srcAddr, dstAddr, srcNodeInfo.NodeID, myredis.ConvertSlotToShellFormat(slots))
job.runtime.Logger.Warn(msg)
time.Sleep(1 * time.Minute)
Expand All @@ -629,14 +630,16 @@ func (job *TendisPlusMigrateSlots) MigrateSpecificSlots(srcAddr,
} else if err != nil && strings.Contains(err.Error(), "slot not empty") == true {
dstCli.ClusterClear()
srcCli.ClusterClear()
msg = fmt.Sprintf(`slot not empty : MigrateSpecificSlots execute cluster setslot importing fail,err:%v,srcAddr:%s,dstAddr:%s,cmd: cluster
msg = fmt.Sprintf(
`slot not empty : MigrateSpecificSlots execute cluster setslot importing fail,err:%v,srcAddr:%s,dstAddr:%s,cmd: cluster
setslot importing %s %s`, err, srcAddr, dstAddr, srcNodeInfo.NodeID, myredis.ConvertSlotToShellFormat(slots))
job.runtime.Logger.Warn(msg)
time.Sleep(1 * time.Minute)
deleteSlotErrRetryTimes++
continue
} else if err != nil {
err = fmt.Errorf(`MigrateSpecificSlots execute cluster setslot importing fail,err:%v,srcAddr:%s,dstAddr:%s,cmd: cluster
err = fmt.Errorf(
`MigrateSpecificSlots execute cluster setslot importing fail,err:%v,srcAddr:%s,dstAddr:%s,cmd: cluster
setslot importing %s %s`, err, srcAddr, dstAddr, srcNodeInfo.NodeID, myredis.ConvertSlotToShellFormat(slots))
job.runtime.Logger.Warn(err.Error())
time.Sleep(1 * time.Minute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (job *RedisKillDeadConn) Run() (err error) {

for _, storage := range job.params.Instances {
addr := fmt.Sprintf("%s:%d", storage.IP, storage.Port)
pwd, err := myredis.GetPasswordFromLocalConfFile(storage.Port)
pwd, err := myredis.GetRedisPasswdFromConfFile(storage.Port)
if err != nil {
job.runtime.Logger.Error("get redis pass from local failed,err %s:%v", addr, err)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (job *RedisPramsSync) Run() (err error) {
job.runtime.Logger.Info("redisparamsync start; params:%+v", job.params)
for _, pair := range job.params.Instances {
addr1 := fmt.Sprintf("%s:%d", pair.MasterInfo.IP, pair.MasterInfo.Port)
pwd, err := myredis.GetPasswordFromLocalConfFile(pair.MasterInfo.Port)
pwd, err := myredis.GetRedisPasswdFromConfFile(pair.MasterInfo.Port)
if err != nil {
job.runtime.Logger.Error("get redis pass from local failed,err %s:%v", addr1, err)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (job *RedisSyncCheck) Rollback() error {
// checkReplication TODO
func (job *RedisSyncCheck) checkReplication(ins InstanceParam) error {
addr := fmt.Sprintf("%s:%d", ins.IP, ins.Port)
pwd, err := myredis.GetPasswordFromLocalConfFile(ins.Port)
pwd, err := myredis.GetRedisPasswdFromConfFile(ins.Port)
if err != nil {
job.runtime.Logger.Error("get redis pass from local failed,err %s:%v", addr, err)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (job *RedisShutdown) Shutdown(port int) {
var err error
stopScript := filepath.Join(job.RedisBinDir, "stop-redis.sh")
job.runtime.Logger.Info("get port[%d] pwd begin.", port)
pwd, err := myredis.GetPasswordFromLocalConfFile(port)
pwd, err := myredis.GetRedisPasswdFromConfFile(port)
if err != nil {
job.errChan <- fmt.Errorf("get redis port[%d] password failed err[%s]", port, err.Error())
return
Expand Down
Loading

0 comments on commit 845b405

Please sign in to comment.