2015-09-21 06:31:12 -04:00
|
|
|
/*
|
|
|
|
Copyright 2015 Home Office All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-17 12:00:15 -04:00
|
|
|
"os"
|
2015-09-21 06:31:12 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// the userpass authentication plugin
|
2015-09-23 17:39:50 -04:00
|
|
|
type authUserPassPlugin struct {
|
2015-09-21 06:31:12 -04:00
|
|
|
client *api.Client
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:39:50 -04:00
|
|
|
type userPassLogin struct {
|
2015-09-21 06:31:12 -04:00
|
|
|
// the password for the account
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// NewUserPassPlugin creates a new User Pass plugin
|
2015-09-23 17:39:50 -04:00
|
|
|
func NewUserPassPlugin(client *api.Client) AuthInterface {
|
|
|
|
return &authUserPassPlugin{
|
2015-09-21 06:31:12 -04:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// Create a userpass plugin with the username and password provide in the file
|
2017-07-25 03:08:15 -04:00
|
|
|
func (r authUserPassPlugin) Create(cfg *vaultAuthOptions) (string, error) {
|
2015-09-23 17:39:50 -04:00
|
|
|
// step: extract the options
|
2017-07-25 03:08:15 -04:00
|
|
|
if cfg.Username == "" {
|
|
|
|
cfg.Username = os.Getenv("VAULT_SIDEKICK_USERNAME")
|
2016-09-17 12:00:15 -04:00
|
|
|
}
|
2017-07-25 03:08:15 -04:00
|
|
|
if cfg.Password == "" {
|
|
|
|
cfg.Password = os.Getenv("VAULT_SIDEKICK_PASSWORD")
|
2016-09-17 12:00:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:31:12 -04:00
|
|
|
// step: create the token request
|
2017-07-25 03:08:15 -04:00
|
|
|
request := r.client.NewRequest("POST", fmt.Sprintf("/v1/auth/userpass/login/%s", cfg.Username))
|
|
|
|
if err := request.SetJSONBody(userPassLogin{Password: cfg.Password}); err != nil {
|
2015-09-23 17:39:50 -04:00
|
|
|
return "", err
|
2015-09-21 06:31:12 -04:00
|
|
|
}
|
|
|
|
// step: make the request
|
2015-09-23 17:39:50 -04:00
|
|
|
resp, err := r.client.RawRequest(request)
|
2015-09-21 06:31:12 -04:00
|
|
|
if err != nil {
|
2015-09-23 17:39:50 -04:00
|
|
|
return "", err
|
2015-09-21 06:31:12 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2015-09-23 17:39:50 -04:00
|
|
|
|
2015-09-21 06:31:12 -04:00
|
|
|
// step: parse and return auth
|
2015-09-23 17:39:50 -04:00
|
|
|
secret, err := api.ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return secret.Auth.ClientToken, nil
|
2015-09-21 06:31:12 -04:00
|
|
|
}
|