2015-03-16 03:35:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-03-16 03:40:12 +00:00
|
|
|
"io"
|
2015-04-01 00:16:12 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-04-27 22:08:03 +00:00
|
|
|
"strings"
|
2015-03-16 03:35:33 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/http"
|
2016-04-01 17:16:05 +00:00
|
|
|
"github.com/hashicorp/vault/meta"
|
2015-03-16 03:35:33 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrite(t *testing.T) {
|
2015-03-29 23:20:34 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
2015-03-16 03:35:33 +00:00
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-03-31 06:30:30 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
2015-03-16 03:35:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"secret/foo",
|
2015-04-01 00:16:12 +00:00
|
|
|
"value=bar",
|
2015-03-16 03:35:33 +00:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Logical().Read("secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:16:12 +00:00
|
|
|
if resp.Data["value"] != "bar" {
|
2015-03-16 03:35:33 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 03:40:12 +00:00
|
|
|
|
|
|
|
func TestWrite_arbitrary(t *testing.T) {
|
2015-03-29 23:20:34 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
2015-03-16 03:40:12 +00:00
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
stdinR, stdinW := io.Pipe()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-03-31 06:30:30 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
2015-03-16 03:40:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testStdin: stdinR,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.Write([]byte(`{"foo":"bar"}`))
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"secret/foo",
|
|
|
|
"-",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Logical().Read("secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["foo"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 00:16:12 +00:00
|
|
|
|
|
|
|
func TestWrite_escaped(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-04-01 00:16:12 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"secret/foo",
|
|
|
|
"value=\\@bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Logical().Read("secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["value"] != "@bar" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_file(t *testing.T) {
|
|
|
|
tf, err := ioutil.TempFile("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tf.Write([]byte(`{"foo":"bar"}`))
|
|
|
|
tf.Close()
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-04-01 00:16:12 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"secret/foo",
|
|
|
|
"@" + tf.Name(),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Logical().Read("secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["foo"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_fileValue(t *testing.T) {
|
|
|
|
tf, err := ioutil.TempFile("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tf.Write([]byte("foo"))
|
|
|
|
tf.Close()
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-04-01 00:16:12 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"secret/foo",
|
|
|
|
"value=@" + tf.Name(),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Logical().Read("secret/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["value"] != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 22:08:03 +00:00
|
|
|
|
|
|
|
func TestWrite_Output(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-04-27 22:08:03 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"auth/token/create",
|
|
|
|
"display_name=foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2017-05-25 13:00:49 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "Key") {
|
|
|
|
t.Fatalf("bad: %s", ui.OutputWriter.String())
|
2015-04-27 22:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-18 20:51:06 +00:00
|
|
|
|
|
|
|
func TestWrite_force(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &WriteCommand{
|
2016-04-01 17:16:05 +00:00
|
|
|
Meta: meta.Meta{
|
2015-06-18 20:51:06 +00:00
|
|
|
ClientToken: token,
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"-force",
|
|
|
|
"sys/rotate",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|