This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
vault-sidekick/auth_approle.go
Rohith aeb3cb34bf File Permissions
* Added a mode option to the resource specification enabling secrets to set the file permissions
 * Fixed a bug in the renewal time, when a resource does not have a custom update and the lease time is 0s
 * Cleaned up some of the vetting issue
2017-05-24 13:25:00 +01:00

76 lines
1.9 KiB
Go

/*
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 (
"os"
"github.com/hashicorp/vault/api"
)
// the userpass authentication plugin
type authAppRolePlugin struct {
client *api.Client
}
type appRoleLogin struct {
RoleID string `json:"role_id,omitempty"`
SecretID string `json:"secret_id,omitempty"`
}
// NewAppRolePlugin creates a new App Role plugin
func NewAppRolePlugin(client *api.Client) AuthInterface {
return &authAppRolePlugin{
client: client,
}
}
// Create a approle plugin with the secret id and role id provided in the file
func (r authAppRolePlugin) Create(cfg map[string]string) (string, error) {
// step: extract the options
roleID, _ := cfg["role_id"]
secretID, _ := cfg["secret_id"]
if roleID == "" {
roleID = os.Getenv("VAULT_SIDEKICK_ROLE_ID")
}
if secretID == "" {
secretID = os.Getenv("VAULT_SIDEKICK_SECRET_ID")
}
// step: create the token request
request := r.client.NewRequest("POST", "/v1/auth/approle/login")
login := appRoleLogin{SecretID: secretID, RoleID: roleID}
if err := request.SetJSONBody(login); err != nil {
return "", err
}
// step: make the request
resp, err := r.client.RawRequest(request)
if err != nil {
return "", err
}
defer resp.Body.Close()
// step: parse and return auth
secret, err := api.ParseSecret(resp.Body)
if err != nil {
return "", err
}
return secret.Auth.ClientToken, nil
}