Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 'gp credential-helper' told us to quit #8416

Merged
merged 1 commit into from
Feb 23, 2022
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
46 changes: 21 additions & 25 deletions components/gitpod-cli/cmd/credential-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,29 @@ var credentialHelper = &cobra.Command{
return
}

result, err := parseFromStdin()
host := result["host"]
if err != nil || host == "" {
log.WithError(err).Print("error parsing 'host' from stdin")
return
}

var user, token string
defer func() {
// Credentials not found, return `quit=true` so no further helpers will be consulted, nor will the user be prompted.
// From https://git-scm.com/docs/gitcredentials#_custom_helpers
if token == "" {
fmt.Print("quit=true\n")
return
}
// Server could return only the token and not the username, so we fallback to hardcoded `oauth2` username.
// See https://github.com/gitpod-io/gitpod/pull/7889#discussion_r801670957
if user == "" {
if token != "" && user == "" {
user = "oauth2"
}
fmt.Printf("username=%s\npassword=%s\n", user, token)
if token != "" {
result["username"] = user
result["password"] = token
}
for k, v := range result {
fmt.Printf("%s=%s\n", k, v)
}
}()

host, err := parseHostFromStdin()
if err != nil {
log.WithError(err).Print("error parsing 'host' from stdin")
return
}

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

Expand Down Expand Up @@ -146,27 +147,22 @@ var credentialHelper = &cobra.Command{
},
}

func parseHostFromStdin() (host string, err error) {
func parseFromStdin() (map[string]string, error) {
result := make(map[string]string)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) > 0 {
tuple := strings.Split(line, "=")
if len(tuple) == 2 {
if strings.TrimSpace(tuple[0]) == "host" {
host = strings.TrimSpace(tuple[1])
}
result[tuple[0]] = strings.TrimSpace(tuple[1])
}
}
}

err = scanner.Err()
if err != nil {
err = fmt.Errorf("parseHostFromStdin error: %v", err)
} else if host == "" {
err = fmt.Errorf("parseHostFromStdin error 'host' is missing")
if err := scanner.Err(); err != nil {
return nil, err
}
return
return result, nil
}

type gitCommandInfo struct {
Expand Down