Skip to content

Commit 5daff86

Browse files
authored
fix: existing repo setup doesn't ask for ssh key (#674)
* fix: existing repo setup doesn't ask for ssh key * verify key * improve error msg
1 parent 058793f commit 5daff86

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

pkg/up/context.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package up
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"strings"
78

89
"github.com/AlecAivazis/survey/v2"
910
"github.com/samber/lo"
1011

11-
"github.com/pluralsh/plural-cli/pkg/bundle"
1212
"github.com/pluralsh/plural-cli/pkg/config"
1313
"github.com/pluralsh/plural-cli/pkg/manifest"
1414
"github.com/pluralsh/plural-cli/pkg/provider"
@@ -117,18 +117,15 @@ func backfillConsoleContext(man *manifest.ProjectManifest) error {
117117
utils.Highlight("It looks like you cloned this repo before running plural up, we just need you to generate and give us a deploy key to continue\n")
118118
utils.Highlight("If you want, you can use `plural crypto ssh-keygen` to generate a keypair to use as a deploy key as well\n\n")
119119

120+
files, err := filepath.Glob(filepath.Join(os.Getenv("HOME"), ".ssh", "*"))
121+
if err != nil {
122+
return err
123+
}
124+
120125
var deployKey string
121-
prompt := &survey.Input{
126+
prompt := &survey.Select{
122127
Message: "Select a file containing a read-only deploy key for this repo (use tab to list files in the directory):",
123-
Default: "~/.ssh",
124-
Suggest: func(toComplete string) []string {
125-
path, err := homedir.Expand(toComplete)
126-
if err != nil {
127-
path = toComplete
128-
}
129-
files, _ := filepath.Glob(bundle.CleanPath(path) + "*")
130-
return files
131-
},
128+
Options: files,
132129
}
133130

134131
opts := []survey.AskOpt{survey.WithValidator(survey.Required)}
@@ -155,8 +152,30 @@ func backfillConsoleContext(man *manifest.ProjectManifest) error {
155152
return fmt.Errorf("found non-ssh upstream url %s, please reclone the repo with SSH and retry", url)
156153
}
157154

155+
if err := verifySSHKey(contents, url); err != nil {
156+
return fmt.Errorf("ssh key not valid for url %s, error: %w", url, err)
157+
}
158+
158159
console["repo_url"] = url
159160
console["private_key"] = contents
160161
ctx.Configuration["console"] = console
161162
return ctx.Write(path)
162163
}
164+
165+
func verifySSHKey(key, url string) error {
166+
dir, err := os.MkdirTemp("", "repo")
167+
if err != nil {
168+
return err
169+
}
170+
defer func(path string) {
171+
err := os.RemoveAll(path)
172+
if err != nil {
173+
return
174+
}
175+
}(dir)
176+
auth, _ := git.SSHAuth("git", key, "")
177+
if _, err := git.Clone(auth, url, dir); err != nil {
178+
return err
179+
}
180+
return nil
181+
}

0 commit comments

Comments
 (0)