command/policy-write

This commit is contained in:
Mitchell Hashimoto 2015-04-01 22:58:37 -07:00
parent d5403d6673
commit 20d6fdf83f
4 changed files with 141 additions and 0 deletions

101
command/policy_write.go Normal file
View File

@ -0,0 +1,101 @@
package command
import (
"bytes"
"fmt"
"io"
"os"
"strings"
)
// PolicyWriteCommand is a Command that enables a new endpoint.
type PolicyWriteCommand struct {
Meta
}
func (c *PolicyWriteCommand) Run(args []string) int {
flags := c.Meta.FlagSet("policy-write", FlagSetDefault)
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) != 2 {
flags.Usage()
c.Ui.Error(fmt.Sprintf(
"\npolicy-write expects exactly two arguments"))
return 1
}
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
name := args[0]
path := args[1]
// Read the policy
var f io.Reader = os.Stdin
if path != "-" {
file, err := os.Open(path)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error opening file: %s", err))
return 1
}
defer file.Close()
f = file
}
var buf bytes.Buffer
if _, err := io.Copy(&buf, f); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error reading file: %s", err))
return 1
}
rules := buf.String()
if err := client.Sys().PutPolicy(name, rules); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Policy '%s' written.", name))
return 0
}
func (c *PolicyWriteCommand) Synopsis() string {
return "Write a policy to the server"
}
func (c *PolicyWriteCommand) Help() string {
helpText := `
Usage: vault policy-write [options] name path
Write a policy with the given name from the contents of a file or stdin.
If the path is "-", the policy is read from stdin. Otherwise, it is
loaded from the file at the given path.
General Options:
-address=TODO The address of the Vault server.
-ca-cert=path Path to a PEM encoded CA cert file to use to
verify the Vault server SSL certificate.
-ca-path=path Path to a directory of PEM encoded CA cert files
to verify the Vault server SSL certificate. If both
-ca-cert and -ca-path are specified, -ca-path is used.
-insecure Do not verify TLS certificate. This is highly
not recommended. This is especially not recommended
for unsealing a vault.
`
return strings.TrimSpace(helpText)
}

View File

@ -0,0 +1,32 @@
package command
import (
"testing"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
"github.com/mitchellh/cli"
)
func TestPolicyWrite(t *testing.T) {
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := http.TestServer(t, core)
defer ln.Close()
ui := new(cli.MockUi)
c := &PolicyWriteCommand{
Meta: Meta{
ClientToken: token,
Ui: ui,
},
}
args := []string{
"-address", addr,
"foo",
"./test-fixtures/config.hcl",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}

View File

@ -49,6 +49,12 @@ func init() {
}, nil
},
"policy-write": func() (cli.Command, error) {
return &command.PolicyWriteCommand{
Meta: meta,
}, nil
},
"read": func() (cli.Command, error) {
return &command.ReadCommand{
Meta: meta,

View File

@ -39,6 +39,8 @@ func handleSysPolicy(core *vault.Core) http.Handler {
switch r.Method {
case "GET":
handleSysReadPolicy(core, w, r)
case "PUT":
fallthrough
case "POST":
handleSysWritePolicy(core, w, r)
case "DELETE":