-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_creds_test.go
More file actions
126 lines (112 loc) · 3.03 KB
/
Copy pathpath_creds_test.go
File metadata and controls
126 lines (112 loc) · 3.03 KB
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
package solacevaultplugin
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/hashicorp/vault/sdk/logical"
)
func TestPathCreds_ReadAfterRotation(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
w.Write([]byte(`<rpc-reply><execute-result code="ok"/></rpc-reply>`))
}))
defer server.Close()
b, storage := getTestBackend(t)
ctx := context.Background()
// Setup broker and role
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "config/brokers/test-broker",
Storage: storage,
Data: map[string]interface{}{
"semp_url": server.URL,
"admin_username": "admin",
"admin_password": "secret",
},
}
b.HandleRequest(ctx, req)
req = &logical.Request{
Operation: logical.CreateOperation,
Path: "roles/test-role",
Storage: storage,
Data: map[string]interface{}{
"broker": "test-broker",
"cli_username": "monitor",
},
}
b.HandleRequest(ctx, req)
// Rotate first
req = &logical.Request{
Operation: logical.CreateOperation,
Path: "rotate-role/test-role",
Storage: storage,
}
b.HandleRequest(ctx, req)
// Read creds
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/test-role",
Storage: storage,
}
resp, err := b.HandleRequest(ctx, req)
if err != nil || resp == nil {
t.Fatalf("read creds: err=%v, resp=%v", err, resp)
}
if resp.Data["cli_username"] != "monitor" {
t.Errorf("cli_username = %v, want monitor", resp.Data["cli_username"])
}
if resp.Data["password"] == nil || resp.Data["password"] == "" {
t.Error("password should be set")
}
if resp.Data["broker"] != "test-broker" {
t.Errorf("broker = %v, want test-broker", resp.Data["broker"])
}
if resp.Data["last_rotated"] == nil {
t.Error("last_rotated should be set")
}
}
func TestPathCreds_NoPasswordYet(t *testing.T) {
b, storage := getTestBackend(t)
ctx := context.Background()
writeBroker(t, b, storage, "test-broker")
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "roles/test-role",
Storage: storage,
Data: map[string]interface{}{
"broker": "test-broker",
"cli_username": "monitor",
},
}
b.HandleRequest(ctx, req)
// Read creds before rotation
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/test-role",
Storage: storage,
}
resp, err := b.HandleRequest(ctx, req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil || !resp.IsError() {
t.Error("expected error when password has not been rotated yet")
}
}
func TestPathCreds_RoleNotFound(t *testing.T) {
b, storage := getTestBackend(t)
ctx := context.Background()
req := &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/nonexistent",
Storage: storage,
}
resp, err := b.HandleRequest(ctx, req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil || !resp.IsError() {
t.Error("expected error for nonexistent role")
}
}