diff --git a/pkg/git/ssh_config.go b/pkg/git/ssh_config.go index 636575ffc4..cc2a18369a 100644 --- a/pkg/git/ssh_config.go +++ b/pkg/git/ssh_config.go @@ -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() @@ -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 + } + 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) @@ -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) })