2018-10-30 16:17:19 +00:00
|
|
|
package approle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-10-30 18:09:04 +00:00
|
|
|
"os"
|
2018-10-30 16:17:19 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-10-30 18:09:04 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-01-09 00:48:57 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-10-30 16:17:19 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/command/agent/auth"
|
2018-10-30 18:09:04 +00:00
|
|
|
"github.com/hashicorp/vault/helper/parseutil"
|
2018-10-30 16:17:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type approleMethod struct {
|
|
|
|
logger hclog.Logger
|
|
|
|
mountPath string
|
|
|
|
|
2018-10-30 18:09:04 +00:00
|
|
|
roleIDFilePath string
|
|
|
|
secretIDFilePath string
|
|
|
|
cachedRoleID string
|
|
|
|
cachedSecretID string
|
|
|
|
removeSecretIDFileAfterReading bool
|
2018-10-31 00:53:49 +00:00
|
|
|
secretIDResponseWrappingPath string
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewApproleAuthMethod(conf *auth.AuthConfig) (auth.AuthMethod, error) {
|
|
|
|
if conf == nil {
|
|
|
|
return nil, errors.New("empty config")
|
|
|
|
}
|
|
|
|
if conf.Config == nil {
|
|
|
|
return nil, errors.New("empty config data")
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &approleMethod{
|
2018-10-30 18:09:04 +00:00
|
|
|
logger: conf.Logger,
|
|
|
|
mountPath: conf.MountPath,
|
|
|
|
removeSecretIDFileAfterReading: true,
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:09:04 +00:00
|
|
|
roleIDFilePathRaw, ok := conf.Config["role_id_file_path"]
|
2018-10-30 16:17:19 +00:00
|
|
|
if !ok {
|
2018-10-30 18:09:04 +00:00
|
|
|
return nil, errors.New("missing 'role_id_file_path' value")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
2018-10-30 18:09:04 +00:00
|
|
|
a.roleIDFilePath, ok = roleIDFilePathRaw.(string)
|
2018-10-30 16:17:19 +00:00
|
|
|
if !ok {
|
2018-10-30 18:09:04 +00:00
|
|
|
return nil, errors.New("could not convert 'role_id_file_path' config value to string")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
2018-10-30 18:09:04 +00:00
|
|
|
if a.roleIDFilePath == "" {
|
|
|
|
return nil, errors.New("'role_id_file_path' value is empty")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:09:04 +00:00
|
|
|
secretIDFilePathRaw, ok := conf.Config["secret_id_file_path"]
|
2018-10-30 16:17:19 +00:00
|
|
|
if !ok {
|
2018-10-30 18:09:04 +00:00
|
|
|
return nil, errors.New("missing 'secret_id_file_path' value")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
2018-10-30 18:09:04 +00:00
|
|
|
a.secretIDFilePath, ok = secretIDFilePathRaw.(string)
|
2018-10-30 16:17:19 +00:00
|
|
|
if !ok {
|
2018-10-30 18:09:04 +00:00
|
|
|
return nil, errors.New("could not convert 'secret_id_file_path' config value to string")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
2018-10-30 18:09:04 +00:00
|
|
|
if a.secretIDFilePath == "" {
|
|
|
|
return nil, errors.New("'secret_id_file_path' value is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
removeSecretIDFileAfterReadingRaw, ok := conf.Config["remove_secret_id_file_after_reading"]
|
|
|
|
if ok {
|
|
|
|
removeSecretIDFileAfterReading, err := parseutil.ParseBool(removeSecretIDFileAfterReadingRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("error parsing 'remove_secret_id_file_after_reading' value: {{err}}", err)
|
|
|
|
}
|
|
|
|
a.removeSecretIDFileAfterReading = removeSecretIDFileAfterReading
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 00:53:49 +00:00
|
|
|
secretIDResponseWrappingPathRaw, ok := conf.Config["secret_id_response_wrapping_path"]
|
|
|
|
if ok {
|
|
|
|
a.secretIDResponseWrappingPath, ok = secretIDResponseWrappingPathRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not convert 'secret_id_response_wrapping_path' config value to string")
|
|
|
|
}
|
|
|
|
if a.secretIDResponseWrappingPath == "" {
|
|
|
|
return nil, errors.New("'secret_id_response_wrapping_path' value is empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:17:19 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *approleMethod) Authenticate(ctx context.Context, client *api.Client) (string, map[string]interface{}, error) {
|
2018-10-30 18:09:04 +00:00
|
|
|
if _, err := os.Stat(a.roleIDFilePath); err == nil {
|
|
|
|
roleID, err := ioutil.ReadFile(a.roleIDFilePath)
|
|
|
|
if err != nil {
|
|
|
|
if a.cachedRoleID == "" {
|
|
|
|
return "", nil, errwrap.Wrapf("error reading role ID file and no cached role ID known: {{err}}", err)
|
|
|
|
}
|
|
|
|
a.logger.Warn("error reading role ID file", "error", err)
|
|
|
|
}
|
|
|
|
if len(roleID) == 0 {
|
|
|
|
if a.cachedRoleID == "" {
|
|
|
|
return "", nil, errors.New("role ID file empty and no cached role ID known")
|
|
|
|
}
|
|
|
|
a.logger.Warn("role ID file exists but read empty value, re-using cached value")
|
|
|
|
} else {
|
|
|
|
a.cachedRoleID = strings.TrimSpace(string(roleID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(a.secretIDFilePath); err == nil {
|
|
|
|
secretID, err := ioutil.ReadFile(a.secretIDFilePath)
|
|
|
|
if err != nil {
|
|
|
|
if a.cachedSecretID == "" {
|
|
|
|
return "", nil, errwrap.Wrapf("error reading secret ID file and no cached secret ID known: {{err}}", err)
|
|
|
|
}
|
|
|
|
a.logger.Warn("error reading secret ID file", "error", err)
|
|
|
|
}
|
|
|
|
if len(secretID) == 0 {
|
|
|
|
if a.cachedSecretID == "" {
|
|
|
|
return "", nil, errors.New("secret ID file empty and no cached secret ID known")
|
|
|
|
}
|
|
|
|
a.logger.Warn("secret ID file exists but read empty value, re-using cached value")
|
|
|
|
} else {
|
2018-10-31 00:53:49 +00:00
|
|
|
stringSecretID := strings.TrimSpace(string(secretID))
|
|
|
|
if a.secretIDResponseWrappingPath != "" {
|
|
|
|
clonedClient, err := client.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, errwrap.Wrapf("error cloning client to unwrap secret ID: {{err}}", err)
|
|
|
|
}
|
|
|
|
clonedClient.SetToken(stringSecretID)
|
|
|
|
// Validate the creation path
|
|
|
|
resp, err := clonedClient.Logical().Read("sys/wrapping/lookup")
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, errwrap.Wrapf("error looking up wrapped secret ID: {{err}}", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
return "", nil, errors.New("response nil when looking up wrapped secret ID")
|
|
|
|
}
|
|
|
|
if resp.Data == nil {
|
|
|
|
return "", nil, errors.New("data in repsonse nil when looking up wrapped secret ID")
|
|
|
|
}
|
|
|
|
creationPathRaw, ok := resp.Data["creation_path"]
|
|
|
|
if !ok {
|
|
|
|
return "", nil, errors.New("creation_path in repsonse nil when looking up wrapped secret ID")
|
|
|
|
}
|
|
|
|
creationPath, ok := creationPathRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return "", nil, errors.New("creation_path in repsonse could not be parsed as string when looking up wrapped secret ID")
|
|
|
|
}
|
|
|
|
if creationPath != a.secretIDResponseWrappingPath {
|
|
|
|
a.logger.Error("SECURITY: unable to validate wrapping token creation path", "expected", a.secretIDResponseWrappingPath, "found", creationPath)
|
|
|
|
return "", nil, errors.New("unable to validate wrapping token creation path")
|
|
|
|
}
|
|
|
|
// Now get the secret ID
|
|
|
|
resp, err = clonedClient.Logical().Unwrap("")
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, errwrap.Wrapf("error unwrapping secret ID: {{err}}", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
return "", nil, errors.New("response nil when unwrapping secret ID")
|
|
|
|
}
|
|
|
|
if resp.Data == nil {
|
|
|
|
return "", nil, errors.New("data in repsonse nil when unwrapping secret ID")
|
|
|
|
}
|
|
|
|
secretIDRaw, ok := resp.Data["secret_id"]
|
|
|
|
if !ok {
|
|
|
|
return "", nil, errors.New("secret_id in repsonse nil when unwrapping secret ID")
|
|
|
|
}
|
|
|
|
secretID, ok := secretIDRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return "", nil, errors.New("secret_id in repsonse could not be parsed as string when unwrapping secret ID")
|
|
|
|
}
|
|
|
|
stringSecretID = secretID
|
|
|
|
}
|
|
|
|
a.cachedSecretID = stringSecretID
|
2018-10-30 18:09:04 +00:00
|
|
|
if a.removeSecretIDFileAfterReading {
|
|
|
|
if err := os.Remove(a.secretIDFilePath); err != nil {
|
|
|
|
a.logger.Error("error removing secret ID file after reading", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.cachedRoleID == "" {
|
|
|
|
return "", nil, errors.New("no known role ID")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
2018-10-30 18:09:04 +00:00
|
|
|
if a.cachedSecretID == "" {
|
|
|
|
return "", nil, errors.New("no known secret ID")
|
2018-10-30 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/login", a.mountPath), map[string]interface{}{
|
2018-10-30 18:09:04 +00:00
|
|
|
"role_id": a.cachedRoleID,
|
|
|
|
"secret_id": a.cachedSecretID,
|
2018-10-30 16:17:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *approleMethod) NewCreds() chan struct{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *approleMethod) CredSuccess() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *approleMethod) Shutdown() {
|
|
|
|
}
|