2018-01-18 17:19:18 +00:00
|
|
|
package vault_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/builtin/credential/approle"
|
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-01-18 17:19:18 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExpiration_RenewToken_TestCluster(t *testing.T) {
|
|
|
|
// Use a TestCluster and the approle backend to test renewal
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
CredentialBackends: map[string]logical.Factory{
|
|
|
|
"approle": approle.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
core := cluster.Cores[0].Core
|
|
|
|
vault.TestWaitActive(t, core)
|
|
|
|
client := cluster.Cores[0].Client
|
|
|
|
|
|
|
|
// Mount the auth backend
|
|
|
|
err := client.Sys().EnableAuthWithOptions("approle", &api.EnableAuthOptions{
|
|
|
|
Type: "approle",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tune the mount
|
2022-04-07 19:12:58 +00:00
|
|
|
err = client.Sys().TuneMount("auth/approle", api.MountConfigInput{
|
2018-01-18 17:19:18 +00:00
|
|
|
DefaultLeaseTTL: "5s",
|
|
|
|
MaxLeaseTTL: "5s",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create role
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err := client.Logical().Write("auth/approle/role/role-period", map[string]interface{}{
|
2018-01-18 17:19:18 +00:00
|
|
|
"period": "5s",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get role_id
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Logical().Read("auth/approle/role/role-period/role-id")
|
2018-01-18 17:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for fetching the role-id")
|
|
|
|
}
|
|
|
|
roleID := resp.Data["role_id"]
|
|
|
|
|
|
|
|
// Get secret_id
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Logical().Write("auth/approle/role/role-period/secret-id", map[string]interface{}{})
|
2018-01-18 17:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for fetching the secret-id")
|
|
|
|
}
|
|
|
|
secretID := resp.Data["secret_id"]
|
|
|
|
|
|
|
|
// Login
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Logical().Write("auth/approle/login", map[string]interface{}{
|
2018-01-18 17:19:18 +00:00
|
|
|
"role_id": roleID,
|
|
|
|
"secret_id": secretID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for login")
|
|
|
|
}
|
|
|
|
if resp.Auth == nil {
|
|
|
|
t.Fatal("expected auth object from response")
|
|
|
|
}
|
|
|
|
if resp.Auth.ClientToken == "" {
|
|
|
|
t.Fatal("expected a client token")
|
|
|
|
}
|
|
|
|
|
|
|
|
roleToken := resp.Auth.ClientToken
|
|
|
|
// Wait 3 seconds
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// Renew
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Logical().Write("auth/token/renew", map[string]interface{}{
|
2018-01-18 17:19:18 +00:00
|
|
|
"token": roleToken,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for renew")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform token lookup and verify TTL
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Auth().Token().Lookup(roleToken)
|
2018-01-18 17:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for token lookup")
|
|
|
|
}
|
|
|
|
|
|
|
|
ttlRaw, ok := resp.Data["ttl"].(json.Number)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("no ttl value found in data object")
|
|
|
|
}
|
|
|
|
ttlInt, err := ttlRaw.Int64()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to convert ttl to int: %s", err)
|
|
|
|
}
|
|
|
|
ttl := time.Duration(ttlInt) * time.Second
|
|
|
|
if ttl < 4*time.Second {
|
|
|
|
t.Fatal("expected ttl value to be around 5s")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 3 seconds
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// Do a second renewal to ensure that period can be renewed past sys/mount max_ttl
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Logical().Write("auth/token/renew", map[string]interface{}{
|
2018-01-18 17:19:18 +00:00
|
|
|
"token": roleToken,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for renew")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform token lookup and verify TTL
|
2022-04-07 19:12:58 +00:00
|
|
|
resp, err = client.Auth().Token().Lookup(roleToken)
|
2018-01-18 17:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response for token lookup")
|
|
|
|
}
|
|
|
|
|
|
|
|
ttlRaw, ok = resp.Data["ttl"].(json.Number)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("no ttl value found in data object")
|
|
|
|
}
|
|
|
|
ttlInt, err = ttlRaw.Int64()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to convert ttl to int: %s", err)
|
|
|
|
}
|
|
|
|
ttl = time.Duration(ttlInt) * time.Second
|
|
|
|
if ttl < 4*time.Second {
|
|
|
|
t.Fatal("expected ttl value to be around 5s")
|
|
|
|
}
|
|
|
|
}
|