2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-06 16:53:43 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-21 16:07:46 +00:00
|
|
|
"io"
|
2016-06-09 17:38:46 +00:00
|
|
|
"os"
|
2015-04-06 16:53:43 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/password"
|
2015-04-06 16:53:43 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
2017-09-21 16:07:46 +00:00
|
|
|
type CLIHandler struct {
|
|
|
|
// for tests
|
|
|
|
testStdout io.Writer
|
|
|
|
}
|
2015-04-06 16:53:43 +00:00
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
|
2015-04-06 16:53:43 +00:00
|
|
|
mount, ok := m["mount"]
|
|
|
|
if !ok {
|
|
|
|
mount = "github"
|
|
|
|
}
|
|
|
|
|
2017-09-21 16:07:46 +00:00
|
|
|
// Extract or prompt for token
|
|
|
|
token := m["token"]
|
|
|
|
if token == "" {
|
|
|
|
token = os.Getenv("VAULT_AUTH_GITHUB_TOKEN")
|
|
|
|
}
|
|
|
|
if token == "" {
|
|
|
|
// Override the output
|
|
|
|
stdout := h.testStdout
|
|
|
|
if stdout == nil {
|
2018-01-18 17:14:19 +00:00
|
|
|
stdout = os.Stderr
|
2017-09-21 16:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
fmt.Fprintf(stdout, "GitHub Personal Access Token (will be hidden): ")
|
|
|
|
token, err = password.Read(os.Stdin)
|
|
|
|
fmt.Fprintf(stdout, "\n")
|
|
|
|
if err != nil {
|
|
|
|
if err == password.ErrInterrupted {
|
|
|
|
return nil, fmt.Errorf("user interrupted")
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("An error occurred attempting to "+
|
2017-09-21 16:07:46 +00:00
|
|
|
"ask for a token. The raw error message is shown below, but usually "+
|
|
|
|
"this is because you attempted to pipe a value into the command or "+
|
|
|
|
"you are executing outside of a terminal (tty). If you want to pipe "+
|
|
|
|
"the value, pass \"-\" as the argument to read from stdin. The raw "+
|
2021-04-22 15:20:59 +00:00
|
|
|
"error was: %w", err)
|
2016-06-09 17:38:46 +00:00
|
|
|
}
|
2015-04-06 16:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("auth/%s/login", mount)
|
|
|
|
secret, err := c.Logical().Write(path, map[string]interface{}{
|
2017-09-21 16:07:46 +00:00
|
|
|
"token": strings.TrimSpace(token),
|
2015-04-06 16:53:43 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-04-06 16:53:43 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("empty response from credential provider")
|
2015-04-06 16:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
return secret, nil
|
2015-04-06 16:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
2017-09-06 14:02:15 +00:00
|
|
|
Usage: vault login -method=github [CONFIG K=V...]
|
2015-04-06 16:53:43 +00:00
|
|
|
|
2017-09-13 01:48:52 +00:00
|
|
|
The GitHub auth method allows users to authenticate using a GitHub
|
2017-09-06 14:02:15 +00:00
|
|
|
personal access token. Users can generate a personal access token from the
|
|
|
|
settings page on their GitHub account.
|
2015-04-06 16:53:43 +00:00
|
|
|
|
2017-09-02 22:50:03 +00:00
|
|
|
Authenticate using a GitHub token:
|
2015-06-16 17:05:11 +00:00
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=github token=abcd1234
|
2015-06-16 17:05:11 +00:00
|
|
|
|
2017-09-02 22:50:03 +00:00
|
|
|
Configuration:
|
|
|
|
|
|
|
|
mount=<string>
|
2017-09-06 14:02:15 +00:00
|
|
|
Path where the GitHub credential method is mounted. This is usually
|
|
|
|
provided via the -path flag in the "vault login" command, but it can be
|
|
|
|
specified here as well. If specified here, it takes precedence over the
|
|
|
|
value for -path. The default value is "github".
|
2017-09-02 22:50:03 +00:00
|
|
|
|
|
|
|
token=<string>
|
2017-09-21 16:07:46 +00:00
|
|
|
GitHub personal access token to use for authentication. If not provided,
|
|
|
|
Vault will prompt for the value.
|
2017-09-02 22:50:03 +00:00
|
|
|
`
|
2015-04-06 16:53:43 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|