Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions pkg/git/ssh_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ type sshConfig struct {
}

func AddSSHConfig(cfg config.PipedGit) error {
// Check the existence of the specified private SSH key file.
if _, err := os.Stat(cfg.SSHKeyFile); os.IsNotExist(err) {
return fmt.Errorf("the specified private SSH key at %s was not found", cfg.SSHKeyFile)
}

configData, err := generateSSHConfig(cfg)
if err != nil {
return err
}

cfgPath := cfg.SSHConfigFilePath
if cfgPath == "" {
home, err := os.UserHomeDir()
Expand All @@ -67,10 +57,32 @@ func AddSSHConfig(cfg config.PipedGit) error {
}
cfgPath = path.Join(home, ".ssh", "config")
}
dir := filepath.Dir(cfgPath)
sshDir := filepath.Dir(cfgPath)

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

if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("failed to create a directory %s: %v", dir, err)
var sshKeyFile string
if cfg.SSHKeyFile != "" {
f, err := os.CreateTemp(sshDir, "piped-ssh-key-*")
if err != nil {
return err
}
Comment on lines +68 to +71
Copy link
Member

Choose a reason for hiding this comment

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

Across the docs looks like we should close/remove the opened temp file by ourselves 👀 Should we?

It is the caller's responsibility to remove the file when it is no longer needed.

ref: docs

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right. Let me add a TODO to remove the added file.

Copy link
Member

Choose a reason for hiding this comment

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

🙆‍♀️

Copy link
Member Author

Choose a reason for hiding this comment

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

@khanhtc1202 Added.

key, err := os.ReadFile(cfg.SSHKeyFile)
if err != nil {
return err
}
// TODO: Remove this key file when Piped terminating.
if _, err := f.Write(key); err != nil {
return err
}
sshKeyFile = f.Name()
}

configData, err := generateSSHConfig(cfg, sshKeyFile)
if err != nil {
return err
}

f, err := os.OpenFile(cfgPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Expand All @@ -86,12 +98,12 @@ func AddSSHConfig(cfg config.PipedGit) error {
return nil
}

func generateSSHConfig(cfg config.PipedGit) (string, error) {
func generateSSHConfig(cfg config.PipedGit, sshKeyFile string) (string, error) {
var (
buffer bytes.Buffer
data = sshConfig{
Host: defaultHost,
IdentityFile: cfg.SSHKeyFile,
IdentityFile: sshKeyFile,
}
)

Expand Down
9 changes: 5 additions & 4 deletions pkg/git/ssh_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGenerateSSHConfig(t *testing.T) {
{
name: "default",
cfg: config.PipedGit{
SSHKeyFile: "/etc/piped-secret/ssh-key",
SSHKeyFile: "/tmp/piped-secret/ssh-key",
},
expected: `
Host github.com
Expand All @@ -48,7 +48,7 @@ Host github.com
name: "host is configured",
cfg: config.PipedGit{
Host: "gitlab.com",
SSHKeyFile: "/etc/piped-secret/ssh-key",
SSHKeyFile: "/tmp/piped-secret/ssh-key",
},
expected: `
Host gitlab.com
Expand All @@ -65,7 +65,7 @@ Host gitlab.com
cfg: config.PipedGit{
Host: "gitlab.com",
HostName: "gitlab.com",
SSHKeyFile: "/etc/piped-secret/ssh-key",
SSHKeyFile: "/tmp/piped-secret/ssh-key",
},
expected: `
Host gitlab.com
Expand All @@ -81,7 +81,8 @@ Host gitlab.com

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
got, err := generateSSHConfig(tc.cfg)
sshKeyFile := "/etc/piped-secret/ssh-key"
got, err := generateSSHConfig(tc.cfg, sshKeyFile)
assert.Equal(t, tc.expected, got)
assert.Equal(t, tc.expectedErr, err)
})
Expand Down