Skip to content

Commit 0ac0d4b

Browse files
committed
pass secret when run remote cmd
1 parent 2831224 commit 0ac0d4b

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

app/job_ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func (m ModJobSshStruct) setupClusterInner(clusterConf clusterconf.ClusterConfYm
358358
}) {
359359
fmt.Println(color.RedString("ssh setup remote pubkey error: %v", output))
360360
logf, _ := os.ReadFile(logfps[0])
361-
fmt.Println(color.RedString("∂∂remote log: %v", string(logf)))
361+
fmt.Println(color.RedString("remote log: %v", string(logf)))
362362
os.Exit(1)
363363
// // debug sshd_config
364364
// util.ModRunCmd.NewBuilder("cat", "/etc/ssh/sshd_config").WithRoot().ShowProgress().BlockRun()

util/adminuser.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
7+
88
"gopkg.in/yaml.v2"
99
)
1010

@@ -21,52 +21,57 @@ func GetCurUserConfigPath() string {
2121
return filepath.Join(configDir, "userconfig_"+username)
2222
}
2323

24+
func GetUserConfigPath(username string) string {
25+
configDir := "/teledeploy_secret/config"
26+
return filepath.Join(configDir, "userconfig_"+username)
27+
}
28+
2429
// ReadCurUserConfig 读取当前进程用户的配置文件
2530
func ReadCurUserConfig() (*AdminUserConfig, error) {
2631
configPath := GetCurUserConfigPath()
27-
32+
2833
// 检查文件是否存在
2934
_, err := os.Stat(configPath)
3035
if os.IsNotExist(err) {
3136
return nil, fmt.Errorf("user config file not found at %s", configPath)
3237
}
33-
38+
3439
// 读取文件内容
3540
data, err := os.ReadFile(configPath)
3641
if err != nil {
3742
return nil, fmt.Errorf("error reading user config: %w", err)
3843
}
39-
44+
4045
// 解析YAML
4146
var config AdminUserConfig
4247
if err := yaml.Unmarshal(data, &config); err != nil {
4348
return nil, fmt.Errorf("error parsing user config: %w", err)
4449
}
45-
50+
4651
return &config, nil
4752
}
4853

4954
// WriteAdminUserConfig 写入管理员用户配置到文件
5055
// 可以指定要写入的用户名,默认写入adminuser
51-
func WriteAdminUserConfig(config *AdminUserConfig, username ...string) error {
56+
func WriteAdminUserConfig(config *AdminUserConfig) error {
5257
configPath := GetCurUserConfigPath()
53-
58+
5459
// 确保目录存在
5560
configDir := filepath.Dir(configPath)
5661
if err := os.MkdirAll(configDir, 0755); err != nil {
5762
return fmt.Errorf("error creating config directory: %w", err)
5863
}
59-
64+
6065
// 序列化配置为YAML
6166
data, err := yaml.Marshal(config)
6267
if err != nil {
6368
return fmt.Errorf("error serializing admin user config: %w", err)
6469
}
65-
70+
6671
// 写入文件
6772
if err := os.WriteFile(configPath, data, 0600); err != nil {
6873
return fmt.Errorf("error writing admin user config: %w", err)
6974
}
70-
75+
7176
return nil
72-
}
77+
}

util/password.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
// GetPassword 尝试从环境变量获取密码,如果失败则提示用户输入
1616
// 返回密码和获取是否成功的标志
1717
func GetPassword(uiPrompt string) (string, bool) {
18+
PrintStep("GetPassword", uiPrompt)
1819
// 1. 先尝试从环境变量获取密码
1920
password, ok := os.LookupEnv("SSH_PW")
2021
if ok {

util/remote_control.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,19 @@ func StartRemoteCmds(hosts []string, remoteCmd string, usePasswd string) ([]stri
352352
}
353353
debugFile.WriteString(fmt.Sprintf("Checking sudo permissions output: %s\n", stdout))
354354

355+
rcloneName := ""
356+
configRcloneOnce := func() {
357+
if rcloneName != "" {
358+
return
359+
}
360+
// 定义需要在多个代码块中共享的变量
361+
rcloneName = base64.RawURLEncoding.EncodeToString([]byte(server))
362+
err = NewRcloneConfiger(RcloneConfigTypeSsh{}, rcloneName, server).
363+
WithUser(user, usePasswd).WithPort(port).DoConfig()
364+
if err != nil {
365+
debugErr("", "", err, "配置rclone失败", true)
366+
}
367+
}
355368
if strings.Contains(stdout, "sudo_need_config") {
356369
// 1. 创建本地临时密码文件
357370
localPasswdFile := "/tmp/sudo_passwd"
@@ -361,13 +374,7 @@ func StartRemoteCmds(hosts []string, remoteCmd string, usePasswd string) ([]stri
361374
}
362375
defer os.Remove(localPasswdFile)
363376

364-
// 定义需要在多个代码块中共享的变量
365-
rcloneName := base64.RawURLEncoding.EncodeToString([]byte(server))
366-
err = NewRcloneConfiger(RcloneConfigTypeSsh{}, rcloneName, server).
367-
WithUser(user, usePasswd).WithPort(port).DoConfig()
368-
if err != nil {
369-
debugErr("", "", err, "配置rclone失败", true)
370-
}
377+
configRcloneOnce()
371378

372379
// 2. 使用 rclone 传输密码文件到远程
373380
remotePasswdFile := fmt.Sprintf("sudo_passwd_%s", user)
@@ -418,6 +425,18 @@ func StartRemoteCmds(hosts []string, remoteCmd string, usePasswd string) ([]stri
418425
// return
419426
}
420427

428+
// cur user config
429+
curUserConfig := GetUserConfigPath(user)
430+
WriteAdminUserConfig(&AdminUserConfig{
431+
Username: user,
432+
Password: usePasswd,
433+
})
434+
configRcloneOnce()
435+
if err := RcloneSyncFileToFile(curUserConfig, fmt.Sprintf("%s:%s", rcloneName, curUserConfig)); err != nil {
436+
debugErr("", "", err, "传输当前用户配置时出错", true)
437+
return
438+
}
439+
421440
// // 3. 配置 rclone
422441
// err = NewRcloneConfiger(RcloneConfigTypeSsh{}, rcloneName, server).
423442
// WithUser(user, usePasswd).

0 commit comments

Comments
 (0)