Skip to content
Merged
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
41 changes: 30 additions & 11 deletions pkg/up/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package up

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/samber/lo"

"github.com/pluralsh/plural-cli/pkg/bundle"
"github.com/pluralsh/plural-cli/pkg/config"
"github.com/pluralsh/plural-cli/pkg/manifest"
"github.com/pluralsh/plural-cli/pkg/provider"
Expand Down Expand Up @@ -117,18 +117,15 @@ func backfillConsoleContext(man *manifest.ProjectManifest) error {
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")
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")

files, err := filepath.Glob(filepath.Join(os.Getenv("HOME"), ".ssh", "*"))
if err != nil {
return err
}

var deployKey string
prompt := &survey.Input{
prompt := &survey.Select{
Message: "Select a file containing a read-only deploy key for this repo (use tab to list files in the directory):",
Default: "~/.ssh",
Suggest: func(toComplete string) []string {
path, err := homedir.Expand(toComplete)
if err != nil {
path = toComplete
}
files, _ := filepath.Glob(bundle.CleanPath(path) + "*")
return files
},
Options: files,
}

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

if err := verifySSHKey(contents, url); err != nil {
return fmt.Errorf("ssh key not valid for url %s, error: %w", url, err)
}

console["repo_url"] = url
console["private_key"] = contents
ctx.Configuration["console"] = console
return ctx.Write(path)
}

func verifySSHKey(key, url string) error {
dir, err := os.MkdirTemp("", "repo")
if err != nil {
return err
}
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
return
}
}(dir)
auth, _ := git.SSHAuth("git", key, "")
if _, err := git.Clone(auth, url, dir); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

can we send a better error here, eg something like: "ssh key not valid for url {url}, error: {error}"

Copy link
Member Author

Choose a reason for hiding this comment

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

done

return err
}
return nil
}
Loading