2015-06-30 03:29:41 +00:00
|
|
|
package cert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CLIHandler struct{}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
|
2015-06-30 03:29:41 +00:00
|
|
|
var data struct {
|
2015-12-08 04:32:49 +00:00
|
|
|
Mount string `mapstructure:"mount"`
|
2017-04-30 15:37:10 +00:00
|
|
|
Name string `mapstructure:"name"`
|
2015-06-30 03:29:41 +00:00
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &data); err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-06-30 03:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Mount == "" {
|
|
|
|
data.Mount = "cert"
|
|
|
|
}
|
|
|
|
|
2017-04-30 15:37:10 +00:00
|
|
|
options := map[string]interface{}{
|
|
|
|
"name": data.Name,
|
|
|
|
}
|
2015-06-30 03:29:41 +00:00
|
|
|
path := fmt.Sprintf("auth/%s/login", data.Mount)
|
2017-04-30 15:37:10 +00:00
|
|
|
secret, err := c.Logical().Write(path, options)
|
2015-06-30 03:29:41 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2015-06-30 03:29:41 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("empty response from credential provider")
|
2015-06-30 03:29:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
return secret, nil
|
2015-06-30 03:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
|
|
|
The "cert" credential provider allows you to authenticate with a
|
|
|
|
client certificate. No other authentication materials are needed.
|
2017-04-30 15:37:10 +00:00
|
|
|
Optionally, you may specify the specific certificate role to
|
|
|
|
authenticate against with the "name" parameter.
|
2015-06-30 03:29:41 +00:00
|
|
|
|
2015-08-07 22:15:53 +00:00
|
|
|
Example: vault auth -method=cert \
|
|
|
|
-client-cert=/path/to/cert.pem \
|
|
|
|
-client-key=/path/to/key.pem
|
2017-04-30 15:37:10 +00:00
|
|
|
name=cert1
|
2015-06-30 03:29:41 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|