2018-02-06 20:44:48 +00:00
|
|
|
package approle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-24 21:00:53 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2020-03-03 21:43:24 +00:00
|
|
|
"sync/atomic"
|
2018-02-06 20:44:48 +00:00
|
|
|
"testing"
|
2018-06-16 22:21:33 +00:00
|
|
|
"time"
|
2018-02-06 20:44:48 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-02-06 20:44:48 +00:00
|
|
|
)
|
|
|
|
|
2018-07-24 21:00:53 +00:00
|
|
|
func TestAppRole_TidyDanglingAccessors_Normal(t *testing.T) {
|
2018-02-06 20:44:48 +00:00
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
b, storage := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Create a role
|
|
|
|
createRole(t, b, storage, "role1", "a,b,c")
|
|
|
|
|
|
|
|
// Create a secret-id
|
|
|
|
roleSecretIDReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "role/role1/secret-id",
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), roleSecretIDReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
accessorHashes, err := storage.List(context.Background(), "accessor/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(accessorHashes) != 1 {
|
|
|
|
t.Fatalf("bad: len(accessorHashes); expect 1, got %d", len(accessorHashes))
|
|
|
|
}
|
|
|
|
|
|
|
|
entry1, err := logical.StorageEntryJSON(
|
|
|
|
"accessor/invalid1",
|
|
|
|
&secretIDAccessorStorageEntry{
|
|
|
|
SecretIDHMAC: "samplesecretidhmac",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-05 15:00:12 +00:00
|
|
|
if err := storage.Put(context.Background(), entry1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-02-06 20:44:48 +00:00
|
|
|
entry2, err := logical.StorageEntryJSON(
|
|
|
|
"accessor/invalid2",
|
|
|
|
&secretIDAccessorStorageEntry{
|
|
|
|
SecretIDHMAC: "samplesecretidhmac2",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-05 15:00:12 +00:00
|
|
|
if err := storage.Put(context.Background(), entry2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-02-06 20:44:48 +00:00
|
|
|
|
|
|
|
accessorHashes, err = storage.List(context.Background(), "accessor/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(accessorHashes) != 3 {
|
|
|
|
t.Fatalf("bad: len(accessorHashes); expect 3, got %d", len(accessorHashes))
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:29:04 +00:00
|
|
|
_, err = b.tidySecretID(context.Background(), &logical.Request{
|
|
|
|
Storage: storage,
|
|
|
|
})
|
2018-02-06 20:44:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
// It runs async so we give it a bit of time to run
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
2018-02-06 20:44:48 +00:00
|
|
|
accessorHashes, err = storage.List(context.Background(), "accessor/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(accessorHashes) != 1 {
|
|
|
|
t.Fatalf("bad: len(accessorHashes); expect 1, got %d", len(accessorHashes))
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 21:00:53 +00:00
|
|
|
|
|
|
|
func TestAppRole_TidyDanglingAccessors_RaceTest(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
b, storage := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Create a role
|
|
|
|
createRole(t, b, storage, "role1", "a,b,c")
|
|
|
|
|
|
|
|
// Create an initial entry
|
|
|
|
roleSecretIDReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "role/role1/secret-id",
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), roleSecretIDReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
count := 1
|
|
|
|
|
2018-07-25 07:37:24 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
2020-03-03 21:43:24 +00:00
|
|
|
start := time.Now()
|
|
|
|
for time.Now().Sub(start) < 10*time.Second {
|
|
|
|
if time.Now().Sub(start) > 100*time.Millisecond && atomic.LoadUint32(b.tidySecretIDCASGuard) == 0 {
|
2018-07-24 21:00:53 +00:00
|
|
|
_, err = b.tidySecretID(context.Background(), &logical.Request{
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 00:36:13 +00:00
|
|
|
wg.Add(1)
|
2018-07-24 21:00:53 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
roleSecretIDReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "role/role1/secret-id",
|
|
|
|
Storage: storage,
|
|
|
|
}
|
2018-07-25 04:18:26 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), roleSecretIDReq)
|
2018-07-24 21:00:53 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
}()
|
2020-03-03 21:43:24 +00:00
|
|
|
|
|
|
|
entry, err := logical.StorageEntryJSON(
|
|
|
|
fmt.Sprintf("accessor/invalid%d", count),
|
|
|
|
&secretIDAccessorStorageEntry{
|
|
|
|
SecretIDHMAC: "samplesecretidhmac",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-05 15:00:12 +00:00
|
|
|
if err := storage.Put(context.Background(), entry); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:00:53 +00:00
|
|
|
count++
|
2020-03-03 21:43:24 +00:00
|
|
|
time.Sleep(100 * time.Microsecond)
|
2018-07-24 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
logger := b.Logger().Named(t.Name())
|
|
|
|
logger.Info("wrote entries", "count", count)
|
2018-07-24 21:00:53 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
// Let tidy finish
|
2020-03-03 21:43:24 +00:00
|
|
|
for atomic.LoadUint32(b.tidySecretIDCASGuard) != 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("running tidy again")
|
2018-07-24 21:00:53 +00:00
|
|
|
|
|
|
|
// Run tidy again
|
2020-03-03 21:43:24 +00:00
|
|
|
secret, err := b.tidySecretID(context.Background(), &logical.Request{
|
2018-07-24 21:00:53 +00:00
|
|
|
Storage: storage,
|
|
|
|
})
|
2020-03-03 21:43:24 +00:00
|
|
|
if err != nil || len(secret.Warnings) > 0 {
|
|
|
|
t.Fatal(err, secret.Warnings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for tidy to start
|
|
|
|
for atomic.LoadUint32(b.tidySecretIDCASGuard) == 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let tidy finish
|
|
|
|
for atomic.LoadUint32(b.tidySecretIDCASGuard) != 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2018-07-24 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
accessorHashes, err := storage.List(context.Background(), "accessor/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(accessorHashes) != count {
|
|
|
|
t.Fatalf("bad: len(accessorHashes); expect %d, got %d", count, len(accessorHashes))
|
|
|
|
}
|
|
|
|
|
|
|
|
roleHMACs, err := storage.List(context.Background(), secretIDPrefix)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
secretIDs, err := storage.List(context.Background(), fmt.Sprintf("%s%s", secretIDPrefix, roleHMACs[0]))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(secretIDs) != count {
|
|
|
|
t.Fatalf("bad: len(secretIDs); expect %d, got %d", count, len(secretIDs))
|
|
|
|
}
|
|
|
|
}
|