2017-09-02 22:48:48 +00:00
|
|
|
package api_test
|
2015-03-12 00:46:25 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-02 22:48:48 +00:00
|
|
|
"encoding/json"
|
2015-03-12 00:46:25 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-06-07 20:01:09 +00:00
|
|
|
"time"
|
2017-09-02 22:48:48 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2015-03-12 00:46:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseSecret(t *testing.T) {
|
2017-09-08 01:55:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-03-12 00:46:25 +00:00
|
|
|
raw := strings.TrimSpace(`
|
|
|
|
{
|
2015-04-08 20:35:32 +00:00
|
|
|
"lease_id": "foo",
|
2015-03-12 00:46:25 +00:00
|
|
|
"renewable": true,
|
|
|
|
"lease_duration": 10,
|
2015-03-16 20:29:51 +00:00
|
|
|
"data": {
|
|
|
|
"key": "value"
|
2015-10-07 21:21:41 +00:00
|
|
|
},
|
|
|
|
"warnings": [
|
|
|
|
"a warning!"
|
2016-05-08 01:08:13 +00:00
|
|
|
],
|
|
|
|
"wrap_info": {
|
|
|
|
"token": "token",
|
2017-11-13 20:31:32 +00:00
|
|
|
"accessor": "accessor",
|
2016-06-07 19:00:35 +00:00
|
|
|
"ttl": 60,
|
2016-06-13 23:58:17 +00:00
|
|
|
"creation_time": "2016-06-07T15:52:10-04:00",
|
|
|
|
"wrapped_accessor": "abcd1234"
|
2016-05-08 01:08:13 +00:00
|
|
|
}
|
2015-03-12 00:46:25 +00:00
|
|
|
}`)
|
|
|
|
|
2016-06-07 20:01:09 +00:00
|
|
|
rawTime, _ := time.Parse(time.RFC3339, "2016-06-07T15:52:10-04:00")
|
|
|
|
|
2017-09-02 22:48:48 +00:00
|
|
|
secret, err := api.ParseSecret(strings.NewReader(raw))
|
2015-03-12 00:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-02 22:48:48 +00:00
|
|
|
expected := &api.Secret{
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID: "foo",
|
2015-03-16 20:29:51 +00:00
|
|
|
Renewable: true,
|
|
|
|
LeaseDuration: 10,
|
2015-03-12 00:46:25 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"key": "value",
|
|
|
|
},
|
2015-10-07 21:21:41 +00:00
|
|
|
Warnings: []string{
|
|
|
|
"a warning!",
|
|
|
|
},
|
2017-09-02 22:48:48 +00:00
|
|
|
WrapInfo: &api.SecretWrapInfo{
|
2016-06-13 23:58:17 +00:00
|
|
|
Token: "token",
|
2017-11-13 20:31:32 +00:00
|
|
|
Accessor: "accessor",
|
2016-06-13 23:58:17 +00:00
|
|
|
TTL: 60,
|
|
|
|
CreationTime: rawTime,
|
|
|
|
WrappedAccessor: "abcd1234",
|
2016-05-08 01:08:13 +00:00
|
|
|
},
|
2015-03-12 00:46:25 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(secret, expected) {
|
2016-06-07 20:01:09 +00:00
|
|
|
t.Fatalf("bad:\ngot\n%#v\nexpected\n%#v\n", secret, expected)
|
2015-03-12 00:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
|
|
|
|
func TestSecret_TokenID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp string
|
2017-10-07 22:41:07 +00:00
|
|
|
err bool
|
2017-09-02 22:48:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: nil,
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_auth_client_token",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
ClientToken: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"real_auth_client_token",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
ClientToken: "my-token",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"my-token",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_string",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"id": 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
true,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_string",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"id": "my-token",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"my-token",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenID()
|
|
|
|
if err != nil && !tc.err {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
if act != tc.exp {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": "default",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenID, err := secret.TokenID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenID != token {
|
|
|
|
t.Errorf("expected %q to be %q", tokenID, token)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenID, err := secret.TokenID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenID != token {
|
|
|
|
t.Errorf("expected %q to be %q", tokenID, token)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenID, err := secret.TokenID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenID != token {
|
|
|
|
t.Errorf("expected %q to be %q", tokenID, token)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenID, err := secret.TokenID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenID != token {
|
|
|
|
t.Errorf("expected %q to be %q", tokenID, token)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenID, err := secret.TokenID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenID != token {
|
|
|
|
t.Errorf("expected %q to be %q", tokenID, token)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenID, err := secret.TokenID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenID != token {
|
|
|
|
t.Errorf("expected %q to be %q", tokenID, token)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecret_TokenAccessor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp string
|
2017-10-07 22:41:07 +00:00
|
|
|
err bool
|
2017-09-02 22:48:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: nil,
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_auth_accessor",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Accessor: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"real_auth_accessor",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Accessor: "my-accessor",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"my-accessor",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_string",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"accessor": 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
2017-10-07 22:41:07 +00:00
|
|
|
true,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_string",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"accessor": "my-accessor",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"my-accessor",
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenAccessor()
|
|
|
|
if err != nil && !tc.err {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
if act != tc.exp {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": "default",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, accessor := secret.Auth.ClientToken, secret.Auth.Accessor
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
newAccessor, err := secret.TokenAccessor()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAccessor != accessor {
|
|
|
|
t.Errorf("expected %q to be %q", newAccessor, accessor)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, accessor := secret.Auth.ClientToken, secret.Auth.Accessor
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
newAccessor, err := secret.TokenAccessor()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAccessor != accessor {
|
|
|
|
t.Errorf("expected %q to be %q", newAccessor, accessor)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token, accessor := secret.Auth.ClientToken, secret.Auth.Accessor
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
newAccessor, err := secret.TokenAccessor()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAccessor != accessor {
|
|
|
|
t.Errorf("expected %q to be %q", newAccessor, accessor)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token, accessor := secret.Auth.ClientToken, secret.Auth.Accessor
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
newAccessor, err := secret.TokenAccessor()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAccessor != accessor {
|
|
|
|
t.Errorf("expected %q to be %q", newAccessor, accessor)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token, accessor := secret.Auth.ClientToken, secret.Auth.Accessor
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
newAccessor, err := secret.TokenAccessor()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAccessor != accessor {
|
|
|
|
t.Errorf("expected %q to be %q", newAccessor, accessor)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token, accessor := secret.Auth.ClientToken, secret.Auth.Accessor
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
newAccessor, err := secret.TokenAccessor()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAccessor != accessor {
|
|
|
|
t.Errorf("expected %q to be %q", newAccessor, accessor)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecret_TokenRemainingUses(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
-1,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
2017-10-07 22:41:07 +00:00
|
|
|
-1,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
2017-10-07 22:41:07 +00:00
|
|
|
-1,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_json_number",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"num_uses": 123,
|
|
|
|
},
|
|
|
|
},
|
2017-10-07 22:41:07 +00:00
|
|
|
123,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_json_number",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"num_uses": json.Number("123"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenRemainingUses()
|
|
|
|
if tc.exp != -1 && err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
if act != tc.exp {
|
|
|
|
t.Errorf("expected %d to be %d", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
uses := 5
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": "default",
|
|
|
|
"num_uses": uses,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remaining uses is not returned from this API
|
2017-10-07 22:41:07 +00:00
|
|
|
uses = -1
|
|
|
|
remaining, err := secret.TokenRemainingUses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if remaining != uses {
|
|
|
|
t.Errorf("expected %d to be %d", remaining, uses)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
uses := 5
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
NumUses: uses,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// /auth/token/create does not return the number of uses
|
2017-10-07 22:41:07 +00:00
|
|
|
uses = -1
|
|
|
|
remaining, err := secret.TokenRemainingUses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if remaining != uses {
|
|
|
|
t.Errorf("expected %d to be %d", remaining, uses)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
uses := 5
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
NumUses: uses,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
remaining, err := secret.TokenRemainingUses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if remaining != uses {
|
|
|
|
t.Errorf("expected %d to be %d", remaining, uses)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
uses := 5
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
NumUses: uses,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uses = uses - 1 // we just used it
|
2017-10-07 22:41:07 +00:00
|
|
|
remaining, err := secret.TokenRemainingUses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if remaining != uses {
|
|
|
|
t.Errorf("expected %d to be %d", remaining, uses)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
uses := 5
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
NumUses: uses,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// /auth/token/renew does not return the number of uses
|
2017-10-07 22:41:07 +00:00
|
|
|
uses = -1
|
|
|
|
remaining, err := secret.TokenRemainingUses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if remaining != uses {
|
|
|
|
t.Errorf("expected %d to be %d", remaining, uses)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
uses := 5
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
NumUses: uses,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// /auth/token/renew-self does not return the number of uses
|
2017-10-07 22:41:07 +00:00
|
|
|
uses = -1
|
|
|
|
remaining, err := secret.TokenRemainingUses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if remaining != uses {
|
|
|
|
t.Errorf("expected %d to be %d", remaining, uses)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecret_TokenPolicies(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp []string
|
2017-10-07 22:41:07 +00:00
|
|
|
err bool
|
2017-09-02 22:48:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth_policies",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Policies: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_auth_policies",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Policies: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"real_auth_policies",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Policies: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"foo"},
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_slice",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"policies": 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
true,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_slice",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"policies": []interface{}{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"foo"},
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenPolicies()
|
|
|
|
if err != nil && !tc.err {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %#v to be %#v", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
policies := []string{"bar", "default", "foo"}
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": strings.Join(policies, ","),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tPol, err := secret.TokenPolicies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tPol, policies) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tPol, policies)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
policies := []string{"bar", "default", "foo"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: policies,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tPol, err := secret.TokenPolicies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tPol, policies) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tPol, policies)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
policies := []string{"bar", "default", "foo"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: policies,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tPol, err := secret.TokenPolicies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tPol, policies) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tPol, policies)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
policies := []string{"bar", "default", "foo"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: policies,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tPol, err := secret.TokenPolicies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tPol, policies) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tPol, policies)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
policies := []string{"bar", "default", "foo"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: policies,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tPol, err := secret.TokenPolicies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tPol, policies) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tPol, policies)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
policies := []string{"bar", "default", "foo"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: policies,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tPol, err := secret.TokenPolicies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tPol, policies) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tPol, policies)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-08 01:55:36 +00:00
|
|
|
func TestSecret_TokenMetadata(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp map[string]string
|
2017-10-07 22:41:07 +00:00
|
|
|
err bool
|
2017-09-08 01:55:36 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth_metadata",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Metadata: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_auth_metadata",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Metadata: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"real_auth_metdata",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Metadata: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]string{"foo": "bar"},
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_map",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"metadata": 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
true,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_map",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]string{"foo": "bar"},
|
2017-10-07 22:41:07 +00:00
|
|
|
false,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_map_bad_type",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{"foo": 123},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2017-10-07 22:41:07 +00:00
|
|
|
true,
|
2017-09-08 01:55:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenMetadata()
|
|
|
|
if err != nil && !tc.err {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-08 01:55:36 +00:00
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %#v to be %#v", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
metadata := map[string]string{"username": "test"}
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": "default",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tMeta, err := secret.TokenMetadata()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tMeta, metadata) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tMeta, metadata)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-08 01:55:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
metadata := map[string]string{"username": "test"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Metadata: metadata,
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tMeta, err := secret.TokenMetadata()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tMeta, metadata) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tMeta, metadata)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-08 01:55:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
metadata := map[string]string{"username": "test"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Metadata: metadata,
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tMeta, err := secret.TokenMetadata()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tMeta, metadata) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tMeta, metadata)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-08 01:55:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
metadata := map[string]string{"username": "test"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Metadata: metadata,
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tMeta, err := secret.TokenMetadata()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tMeta, metadata) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tMeta, metadata)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-08 01:55:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
metadata := map[string]string{"username": "test"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Metadata: metadata,
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tMeta, err := secret.TokenMetadata()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tMeta, metadata) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tMeta, metadata)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-08 01:55:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
metadata := map[string]string{"username": "test"}
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Metadata: metadata,
|
|
|
|
Policies: []string{"default"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tMeta, err := secret.TokenMetadata()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tMeta, metadata) {
|
|
|
|
t.Errorf("expected %#v to be %#v", tMeta, metadata)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-02 22:48:48 +00:00
|
|
|
func TestSecret_TokenIsRenewable(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: nil,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"auth_renewable_false",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Renewable: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"auth_renewable_true",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
Renewable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_bool",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"renewable": 123,
|
|
|
|
},
|
|
|
|
},
|
2017-10-07 22:41:07 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_bool_string",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"renewable": "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_bool_true",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"renewable": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_bool_false",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"renewable": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenIsRenewable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
if act != tc.exp {
|
|
|
|
t.Errorf("expected %t to be %t", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
renewable := true
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": "default",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tRenew, err := secret.TokenIsRenewable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tRenew != renewable {
|
|
|
|
t.Errorf("expected %t to be %t", tRenew, renewable)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
renewable := true
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
Renewable: &renewable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tRenew, err := secret.TokenIsRenewable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tRenew != renewable {
|
|
|
|
t.Errorf("expected %t to be %t", tRenew, renewable)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
renewable := true
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
Renewable: &renewable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tRenew, err := secret.TokenIsRenewable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tRenew != renewable {
|
|
|
|
t.Errorf("expected %t to be %t", tRenew, renewable)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
renewable := true
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
Renewable: &renewable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tRenew, err := secret.TokenIsRenewable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tRenew != renewable {
|
|
|
|
t.Errorf("expected %t to be %t", tRenew, renewable)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
renewable := true
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
Renewable: &renewable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tRenew, err := secret.TokenIsRenewable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tRenew != renewable {
|
|
|
|
t.Errorf("expected %t to be %t", tRenew, renewable)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
renewable := true
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
Renewable: &renewable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tRenew, err := secret.TokenIsRenewable()
|
2017-09-08 01:55:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-07 22:41:07 +00:00
|
|
|
if tRenew != renewable {
|
|
|
|
t.Errorf("expected %t to be %t", tRenew, renewable)
|
2017-09-08 01:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-02 22:48:48 +00:00
|
|
|
func TestSecret_TokenTTL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
secret *api.Secret
|
|
|
|
exp time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: nil,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_auth_lease_duration",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
LeaseDuration: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"real_auth_lease_duration",
|
|
|
|
&api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
LeaseDuration: 3600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
1 * time.Hour,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nil_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_data",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_not_json_number",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"ttl": 123,
|
|
|
|
},
|
|
|
|
},
|
2017-10-07 22:41:07 +00:00
|
|
|
123 * time.Second,
|
2017-09-02 22:48:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data_json_number",
|
|
|
|
&api.Secret{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"ttl": json.Number("3600"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
1 * time.Hour,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
act, err := tc.secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-02 22:48:48 +00:00
|
|
|
if act != tc.exp {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("auth", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ttl := 30 * time.Minute
|
|
|
|
|
|
|
|
if err := client.Sys().EnableAuth("userpass", "userpass", ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("auth/userpass/users/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
"policies": "default",
|
|
|
|
"ttl": ttl.String(),
|
|
|
|
"explicit_max_ttl": ttl.String(),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Write("auth/userpass/login/test", map[string]interface{}{
|
|
|
|
"password": "test",
|
|
|
|
})
|
|
|
|
if err != nil || secret == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenTTL, err := secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenTTL == 0 || tokenTTL > ttl {
|
|
|
|
t.Errorf("expected %q to non-zero and less than %q", tokenTTL, ttl)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token create", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ttl := 30 * time.Minute
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
TTL: ttl.String(),
|
|
|
|
ExplicitMaxTTL: ttl.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenTTL, err := secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenTTL == 0 || tokenTTL > ttl {
|
|
|
|
t.Errorf("expected %q to non-zero and less than %q", tokenTTL, ttl)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ttl := 30 * time.Minute
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
TTL: ttl.String(),
|
|
|
|
ExplicitMaxTTL: ttl.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Lookup(token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenTTL, err := secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenTTL == 0 || tokenTTL > ttl {
|
|
|
|
t.Errorf("expected %q to non-zero and less than %q", tokenTTL, ttl)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token lookup-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ttl := 30 * time.Minute
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
TTL: ttl.String(),
|
|
|
|
ExplicitMaxTTL: ttl.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenTTL, err := secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenTTL == 0 || tokenTTL > ttl {
|
|
|
|
t.Errorf("expected %q to non-zero and less than %q", tokenTTL, ttl)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ttl := 30 * time.Minute
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
TTL: ttl.String(),
|
|
|
|
ExplicitMaxTTL: ttl.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenTTL, err := secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenTTL == 0 || tokenTTL > ttl {
|
|
|
|
t.Errorf("expected %q to non-zero and less than %q", tokenTTL, ttl)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-15 20:19:28 +00:00
|
|
|
t.Run("token renew-self", func(t *testing.T) {
|
2017-09-02 22:48:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ttl := 30 * time.Minute
|
|
|
|
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
|
|
Policies: []string{"default"},
|
|
|
|
TTL: ttl.String(),
|
|
|
|
ExplicitMaxTTL: ttl.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
token := secret.Auth.ClientToken
|
|
|
|
|
|
|
|
client.SetToken(token)
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
tokenTTL, err := secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tokenTTL == 0 || tokenTTL > ttl {
|
|
|
|
t.Errorf("expected %q to non-zero and less than %q", tokenTTL, ttl)
|
2017-09-02 22:48:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|