forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters_test.go
177 lines (150 loc) · 4.51 KB
/
counters_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package vault
import (
"testing"
"time"
"github.com/go-test/deep"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
"github.com/hashicorp/vault/sdk/logical"
)
// noinspection SpellCheckingInspection
func testParseTime(t *testing.T, format, timeval string) time.Time {
t.Helper()
tm, err := time.Parse(format, timeval)
if err != nil {
t.Fatalf("Error parsing time %q: %v", timeval, err)
}
return tm
}
func testCountActiveTokens(t *testing.T, c *Core, root string) int {
t.Helper()
rootCtx := namespace.RootContext(nil)
req := &logical.Request{
ClientToken: root,
Operation: logical.ReadOperation,
Path: "sys/internal/counters/tokens",
}
resp, err := c.HandleRequest(rootCtx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
}
schema.ValidateResponse(
t,
// we remove the `sys/` prefix b/c Core removes it before routing to the 'sys' backend
schema.GetResponseSchema(t, c.systemBackend.Route("internal/counters/tokens"), req.Operation),
resp,
true,
)
activeTokens := resp.Data["counters"].(*ActiveTokens)
return activeTokens.ServiceTokens.Total
}
func TestTokenStore_CountActiveTokens(t *testing.T) {
c, _, root := TestCoreUnsealed(t)
rootCtx := namespace.RootContext(nil)
// Count the root token
count := testCountActiveTokens(t, c, root)
if count != 1 {
t.Fatalf("expected %d tokens, not %d", 1, count)
}
tokens := make([]string, 10)
for i := 0; i < 10; i++ {
// Create some service tokens
req := &logical.Request{
ClientToken: root,
Operation: logical.UpdateOperation,
Path: "auth/token/create",
Data: map[string]interface{}{
"ttl": "1h",
},
}
resp, err := c.HandleRequest(rootCtx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
}
tokens[i] = resp.Auth.ClientToken
count = testCountActiveTokens(t, c, root)
if count != i+2 {
t.Fatalf("expected %d tokens, not %d", i+2, count)
}
}
// Revoke the service tokens
for i := 0; i < 10; i++ {
req := &logical.Request{
ClientToken: root,
Operation: logical.UpdateOperation,
Path: "auth/token/revoke",
Data: map[string]interface{}{
"token": tokens[i],
},
}
resp, err := c.HandleRequest(rootCtx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
}
}
// We should now have only 1 token (the root token). However, because
// token deletion works by setting the TTL of the token to 0 and waiting
// for it to get cleaned up by the expiration manager, occasionally we will
// have to wait briefly for all the tokens to actually get deleted.
for i := 0; i < 10; i++ {
count = testCountActiveTokens(t, c, root)
if count == 1 {
return
}
time.Sleep(time.Second)
}
t.Fatalf("expected %d tokens, not %d", 1, count)
}
func testCountActiveEntities(t *testing.T, c *Core, root string, expectedEntities int) {
t.Helper()
rootCtx := namespace.RootContext(nil)
resp, err := c.HandleRequest(rootCtx, &logical.Request{
ClientToken: root,
Operation: logical.ReadOperation,
Path: "sys/internal/counters/entities",
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
}
if diff := deep.Equal(resp.Data, map[string]interface{}{
"counters": &ActiveEntities{
Entities: EntityCounter{
Total: expectedEntities,
},
},
}); diff != nil {
t.Fatal(diff)
}
}
func TestIdentityStore_CountActiveEntities(t *testing.T) {
c, _, root := TestCoreUnsealed(t)
rootCtx := namespace.RootContext(nil)
// Count the root token
testCountActiveEntities(t, c, root, 0)
// Create some entities
req := &logical.Request{
ClientToken: root,
Operation: logical.UpdateOperation,
Path: "entity",
}
ids := make([]string, 10)
for i := 0; i < 10; i++ {
resp, err := c.identityStore.HandleRequest(rootCtx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
}
ids[i] = resp.Data["id"].(string)
testCountActiveEntities(t, c, root, i+1)
}
req.Operation = logical.DeleteOperation
for i := 0; i < 10; i++ {
req.Path = "entity/id/" + ids[i]
resp, err := c.identityStore.HandleRequest(rootCtx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
}
testCountActiveEntities(t, c, root, 9-i)
}
}