2017-02-24 19:23:15 +00:00
|
|
|
package http
|
2015-09-03 14:57:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2015-09-03 14:57:17 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthTokenCreate(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
2017-02-24 19:23:15 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
2015-09-03 14:57:17 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
config := api.DefaultConfig()
|
2015-09-03 14:57:17 +00:00
|
|
|
config.Address = addr
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
client, err := api.NewClient(config)
|
2015-09-03 14:57:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
2015-09-03 14:57:17 +00:00
|
|
|
Lease: "1h",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-08 15:14:30 +00:00
|
|
|
if secret.Auth.LeaseDuration != 3600 {
|
|
|
|
t.Errorf("expected 1h, got %q", secret.Auth.LeaseDuration)
|
|
|
|
}
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
renewCreateRequest := &api.TokenCreateRequest{
|
2016-06-08 15:14:30 +00:00
|
|
|
TTL: "1h",
|
|
|
|
Renewable: new(bool),
|
|
|
|
}
|
2015-09-03 14:57:17 +00:00
|
|
|
|
2016-06-08 15:14:30 +00:00
|
|
|
secret, err = client.Auth().Token().Create(renewCreateRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-03 14:57:17 +00:00
|
|
|
if secret.Auth.LeaseDuration != 3600 {
|
|
|
|
t.Errorf("expected 1h, got %q", secret.Auth.LeaseDuration)
|
|
|
|
}
|
2016-06-08 15:14:30 +00:00
|
|
|
if secret.Auth.Renewable {
|
|
|
|
t.Errorf("expected non-renewable token")
|
|
|
|
}
|
|
|
|
|
|
|
|
*renewCreateRequest.Renewable = true
|
|
|
|
secret, err = client.Auth().Token().Create(renewCreateRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret.Auth.LeaseDuration != 3600 {
|
|
|
|
t.Errorf("expected 1h, got %q", secret.Auth.LeaseDuration)
|
|
|
|
}
|
|
|
|
if !secret.Auth.Renewable {
|
|
|
|
t.Errorf("expected renewable token")
|
|
|
|
}
|
2016-06-08 18:49:48 +00:00
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
explicitMaxCreateRequest := &api.TokenCreateRequest{
|
2016-06-08 18:49:48 +00:00
|
|
|
TTL: "1h",
|
|
|
|
ExplicitMaxTTL: "1800s",
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err = client.Auth().Token().Create(explicitMaxCreateRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret.Auth.LeaseDuration != 1800 {
|
2018-04-03 16:20:20 +00:00
|
|
|
t.Errorf("expected 1800 seconds, got %d", secret.Auth.LeaseDuration)
|
2016-06-08 18:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
explicitMaxCreateRequest.ExplicitMaxTTL = "2h"
|
|
|
|
secret, err = client.Auth().Token().Create(explicitMaxCreateRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if secret.Auth.LeaseDuration != 3600 {
|
|
|
|
t.Errorf("expected 3600 seconds, got %q", secret.Auth.LeaseDuration)
|
|
|
|
}
|
2015-09-03 14:57:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 20:18:59 +00:00
|
|
|
func TestAuthTokenLookup(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
2017-02-24 19:23:15 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
2015-12-29 20:18:59 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
config := api.DefaultConfig()
|
2015-12-29 20:18:59 +00:00
|
|
|
config.Address = addr
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
client, err := api.NewClient(config)
|
2015-12-29 20:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
|
|
|
// Create a new token ...
|
2017-02-24 19:23:15 +00:00
|
|
|
secret2, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
2015-12-29 20:18:59 +00:00
|
|
|
Lease: "1h",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup details of this token
|
|
|
|
secret, err := client.Auth().Token().Lookup(secret2.Auth.ClientToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup details of token, err = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Data["id"] != secret2.Auth.ClientToken {
|
|
|
|
t.Errorf("Did not get back details about our provided token, id returned=%s", secret.Data["id"])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:05:15 +00:00
|
|
|
func TestAuthTokenLookupSelf(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
2017-02-24 19:23:15 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
2015-12-28 00:05:15 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
config := api.DefaultConfig()
|
2015-12-28 00:05:15 +00:00
|
|
|
config.Address = addr
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
client, err := api.NewClient(config)
|
2015-12-28 00:05:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
|
|
|
// you should be able to lookup your own token
|
|
|
|
secret, err := client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should be allowed to lookup self, err = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Data["id"] != token {
|
|
|
|
t.Errorf("Did not get back details about our own (self) token, id returned=%s", secret.Data["id"])
|
|
|
|
}
|
|
|
|
if secret.Data["display_name"] != "root" {
|
|
|
|
t.Errorf("Did not get back details about our own (self) token, display_name returned=%s", secret.Data["display_name"])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-03 14:57:17 +00:00
|
|
|
func TestAuthTokenRenew(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
2017-02-24 19:23:15 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
2015-09-03 14:57:17 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
config := api.DefaultConfig()
|
2015-09-03 14:57:17 +00:00
|
|
|
config.Address = addr
|
|
|
|
|
2017-02-24 19:23:15 +00:00
|
|
|
client, err := api.NewClient(config)
|
2015-09-03 14:57:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
|
|
|
// The default root token is not renewable, so this should not work
|
|
|
|
_, err = client.Auth().Token().Renew(token, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should not be allowed to renew root token")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "lease is not renewable") {
|
2016-03-01 17:33:35 +00:00
|
|
|
t.Fatalf("wrong error; got %v", err)
|
2015-09-03 14:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new token that should be renewable
|
2017-02-24 19:23:15 +00:00
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
2015-09-03 14:57:17 +00:00
|
|
|
Lease: "1h",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
client.SetToken(secret.Auth.ClientToken)
|
|
|
|
|
|
|
|
// Now attempt a renew with the new token
|
2016-01-30 00:36:56 +00:00
|
|
|
secret, err = client.Auth().Token().Renew(secret.Auth.ClientToken, 3600)
|
2015-09-03 14:57:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Auth.LeaseDuration != 3600 {
|
2016-01-30 00:36:56 +00:00
|
|
|
t.Errorf("expected 1h, got %v", secret.Auth.LeaseDuration)
|
2015-09-03 14:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Auth.Renewable != true {
|
|
|
|
t.Error("expected lease to be renewable")
|
|
|
|
}
|
2015-10-30 21:27:33 +00:00
|
|
|
|
|
|
|
// Do the same thing with the self variant
|
2016-01-30 00:36:56 +00:00
|
|
|
secret, err = client.Auth().Token().RenewSelf(3600)
|
2015-10-30 21:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Auth.LeaseDuration != 3600 {
|
2016-01-30 00:36:56 +00:00
|
|
|
t.Errorf("expected 1h, got %v", secret.Auth.LeaseDuration)
|
2015-10-30 21:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Auth.Renewable != true {
|
|
|
|
t.Error("expected lease to be renewable")
|
|
|
|
}
|
2015-09-03 14:57:17 +00:00
|
|
|
}
|