Skip to content

Commit 66eaf29

Browse files
committed
remove temp sshKeyFile after use(pipe-cd#2215)
Signed-off-by: hiep-tk <[email protected]>
1 parent 1c0920b commit 66eaf29

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

pkg/app/piped/cmd/piped/piped.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ func (p *piped) run(ctx context.Context, input cli.Input) (runErr error) {
175175

176176
// Configure SSH config if needed.
177177
if cfg.Git.ShouldConfigureSSHConfig() {
178-
if err := git.AddSSHConfig(cfg.Git); err != nil {
178+
tempFile, err := git.AddSSHConfig(cfg.Git)
179+
if len(tempFile) > 0 {
180+
defer os.Remove(tempFile)
181+
}
182+
if err != nil {
179183
input.Logger.Error("failed to configure ssh-config", zap.Error(err))
180184
return err
181185
}

pkg/git/ssh_config.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,51 @@ type sshConfig struct {
4848
IdentityFile string
4949
}
5050

51-
func AddSSHConfig(cfg config.PipedGit) error {
51+
func AddSSHConfig(cfg config.PipedGit) (string, error) {
5252
cfgPath := cfg.SSHConfigFilePath
5353
if cfgPath == "" {
5454
home, err := os.UserHomeDir()
5555
if err != nil {
56-
return fmt.Errorf("failed to detect the current user's home directory: %w", err)
56+
return "", fmt.Errorf("failed to detect the current user's home directory: %w", err)
5757
}
5858
cfgPath = path.Join(home, ".ssh", "config")
5959
}
6060
sshDir := filepath.Dir(cfgPath)
6161

6262
if err := os.MkdirAll(sshDir, 0700); err != nil {
63-
return fmt.Errorf("failed to create a directory %s: %v", sshDir, err)
63+
return "", fmt.Errorf("failed to create a directory %s: %v", sshDir, err)
6464
}
6565

6666
sshKey, err := cfg.LoadSSHKey()
6767
if err != nil {
68-
return err
68+
return "", err
6969
}
7070

7171
sshKeyFile, err := os.CreateTemp(sshDir, "piped-ssh-key-*")
7272
if err != nil {
73-
return err
73+
return "", err
7474
}
7575

76-
// TODO: Remove this key file when Piped terminating.
7776
if _, err := sshKeyFile.Write(sshKey); err != nil {
78-
return err
77+
return sshKeyFile.Name(), err
7978
}
8079

8180
configData, err := generateSSHConfig(cfg, sshKeyFile.Name())
8281
if err != nil {
83-
return err
82+
return sshKeyFile.Name(), err
8483
}
8584

8685
f, err := os.OpenFile(cfgPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
8786
if err != nil {
88-
return fmt.Errorf("could not create/append to %s: %v", cfgPath, err)
87+
return sshKeyFile.Name(), fmt.Errorf("could not create/append to %s: %v", cfgPath, err)
8988
}
9089
defer f.Close()
9190

9291
if _, err := f.Write([]byte(configData)); err != nil {
93-
return fmt.Errorf("failed to write sshConfig to %s: %v", cfgPath, err)
92+
return sshKeyFile.Name(), fmt.Errorf("failed to write sshConfig to %s: %v", cfgPath, err)
9493
}
9594

96-
return nil
95+
return sshKeyFile.Name(), nil
9796
}
9897

9998
func generateSSHConfig(cfg config.PipedGit, sshKeyFile string) (string, error) {

0 commit comments

Comments
 (0)