Skip to content

Remove temp sshKeyFile after use#5769

Merged
khanhtc1202 merged 1 commit into
pipe-cd:masterfrom
hiep-tk:master
Apr 22, 2025
Merged

Remove temp sshKeyFile after use#5769
khanhtc1202 merged 1 commit into
pipe-cd:masterfrom
hiep-tk:master

Conversation

@hiep-tk
Copy link
Copy Markdown
Contributor

@hiep-tk hiep-tk commented Apr 22, 2025

What this PR does: remove the temporary ssh key file after using it for the config

Why we need it: clean up the ssh key folder

Which issue(s) this PR fixes:

Fixes #2215

Does this PR introduce a user-facing change?:

  • How are users affected by this change:
  • Is this breaking change:
  • How to migrate (if breaking change):

if cfg.Git.ShouldConfigureSSHConfig() {
if err := git.AddSSHConfig(cfg.Git); err != nil {
tempFile, err := git.AddSSHConfig(cfg.Git)
if err != nil {
Copy link
Copy Markdown
Member

@khanhtc1202 khanhtc1202 Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still leaks in some cases: if the error is not nil, the AddSSHConfig function returns tempFile, but this block will immediately close this run function, thus defer at L184 is not triggered 👀

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @khanhtc1202 , the code might be like this:

tempFile, err := git.AddSSHConfig(cfg.Git)
if len(tempFile) > 0 { // HERE
	defer os.Remove(tempFile)
}
if err != nil {
...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as suggested. @khanhtc1202 @t-kikuc

Comment thread pkg/git/ssh_config.go Outdated
// TODO: Remove this key file when Piped terminating.
if _, err := sshKeyFile.Write(sshKey); err != nil {
return err
return sshKeyFile.Name(), err
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about closing this file in this function if err is not nil, then we can have convention like: return "", err if err occurred, while the file is only returned in case there is no issue, and the caller only need to defer close file in case of no error (after err check)? @hiep-tk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated for consistent return format @khanhtc1202

Signed-off-by: hiep-tk <hiep.trinhkhanh0466@gmail.com>
Copy link
Copy Markdown
Member

@khanhtc1202 khanhtc1202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LNeatTM 👍

@khanhtc1202 khanhtc1202 enabled auto-merge (squash) April 22, 2025 07:41
Copy link
Copy Markdown
Member

@Warashi Warashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@khanhtc1202 khanhtc1202 merged commit bbc6ea5 into pipe-cd:master Apr 22, 2025
16 checks passed
@t-kikuc t-kikuc added cherry-pick v0.51.3 kind/bug Something isn't working as expected labels Apr 22, 2025
@khanhtc1202 khanhtc1202 changed the title remove temp sshKeyFile after use(#2215) Remove temp sshKeyFile after use(#2215) Apr 22, 2025
@khanhtc1202 khanhtc1202 changed the title Remove temp sshKeyFile after use(#2215) Remove temp sshKeyFile after use Apr 22, 2025
github-actions Bot pushed a commit that referenced this pull request Apr 22, 2025
Signed-off-by: hiep-tk <hiep.trinhkhanh0466@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>
Warashi added a commit that referenced this pull request Apr 22, 2025
* Do treeless clone for git clone to improve performance (#5722)

Signed-off-by: Shinnosuke Sawada-Dazai <shin@warashi.dev>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

* remove temp sshKeyFile after use(#2215) (#5769)

Signed-off-by: hiep-tk <hiep.trinhkhanh0466@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <shin@warashi.dev>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>
Signed-off-by: hiep-tk <hiep.trinhkhanh0466@gmail.com>
Co-authored-by: Shinnosuke Sawada-Dazai <shin@warashi.dev>
Co-authored-by: Hiep Trinh <hiep.trinhkhanh0466@gmail.com>
@github-actions github-actions Bot mentioned this pull request May 15, 2025
@github-actions github-actions Bot mentioned this pull request May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/go cherry-pick kind/bug Something isn't working as expected v0.51.3-rc0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove a temp sshkey file when terminating Piped

4 participants