From 4e7f581a6e7dd70cc97fac0810b2536b37af93f0 Mon Sep 17 00:00:00 2001 From: nghialv Date: Mon, 12 Jul 2021 14:29:17 +0900 Subject: [PATCH 1/3] Copy SSH key into ssh config dir to use instead of using the given one directly --- pkg/git/ssh_config.go | 42 ++++++++++++++++++++++++-------------- pkg/git/ssh_config_test.go | 9 ++++---- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/pkg/git/ssh_config.go b/pkg/git/ssh_config.go index 636575ffc4..1253011f50 100644 --- a/pkg/git/ssh_config.go +++ b/pkg/git/ssh_config.go @@ -17,6 +17,7 @@ package git import ( "bytes" "fmt" + "io/ioutil" "os" "path" "path/filepath" @@ -49,16 +50,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() @@ -67,10 +58,31 @@ 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 := ioutil.TempFile(sshDir, "piped-ssh-key-*") + if err != nil { + return err + } + key, err := os.ReadFile(cfg.SSHKeyFile) + if err != nil { + return err + } + 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) @@ -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, } ) diff --git a/pkg/git/ssh_config_test.go b/pkg/git/ssh_config_test.go index 0a09da6853..dd09d7c7ab 100644 --- a/pkg/git/ssh_config_test.go +++ b/pkg/git/ssh_config_test.go @@ -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 @@ -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 @@ -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 @@ -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) }) From aa317887d8d4309fda11b734c03342acf46b3592 Mon Sep 17 00:00:00 2001 From: nghialv Date: Mon, 12 Jul 2021 14:50:05 +0900 Subject: [PATCH 2/3] Do not use ioutil --- pkg/git/ssh_config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/git/ssh_config.go b/pkg/git/ssh_config.go index 1253011f50..033636b575 100644 --- a/pkg/git/ssh_config.go +++ b/pkg/git/ssh_config.go @@ -17,7 +17,6 @@ package git import ( "bytes" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -66,7 +65,7 @@ func AddSSHConfig(cfg config.PipedGit) error { var sshKeyFile string if cfg.SSHKeyFile != "" { - f, err := ioutil.TempFile(sshDir, "piped-ssh-key-*") + f, err := os.CreateTemp(sshDir, "piped-ssh-key-*") if err != nil { return err } From 3ab99b8cf5056061a90f5e4759b4f46eb38c5184 Mon Sep 17 00:00:00 2001 From: nghialv Date: Mon, 12 Jul 2021 15:01:30 +0900 Subject: [PATCH 3/3] Add todo --- pkg/git/ssh_config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/git/ssh_config.go b/pkg/git/ssh_config.go index 033636b575..cc2a18369a 100644 --- a/pkg/git/ssh_config.go +++ b/pkg/git/ssh_config.go @@ -73,6 +73,7 @@ func AddSSHConfig(cfg config.PipedGit) error { if err != nil { return err } + // TODO: Remove this key file when Piped terminating. if _, err := f.Write(key); err != nil { return err }