-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_scope.go
161 lines (144 loc) · 4.31 KB
/
path_scope.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
package kmipengine
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"strings"
"sync"
"time"
)
func pathScope(b *KmipBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "scope/?$",
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: "kmip",
},
TakesArbitraryInput: true,
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.handleScopeList(),
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "read",
},
},
},
ExistenceCheck: b.handleListScopeExistenceCheck(),
HelpSynopsis: strings.TrimSpace(KmipHelpSynopsis),
HelpDescription: strings.TrimSpace(KmipHelpDescription),
},
{
Pattern: "scope/(?P<scope>[^/]+)$",
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: "kmip",
},
TakesArbitraryInput: true,
Fields: map[string]*framework.FieldSchema{
"scope": {
Type: framework.TypeString,
Description: "The action of the scope\n\n",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.handleScopeCreate(),
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "write",
},
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.handleScopeDelete(),
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "write",
},
},
},
ExistenceCheck: b.handleScopeExistenceCheck(),
HelpSynopsis: strings.TrimSpace(KmipHelpSynopsis),
HelpDescription: strings.TrimSpace(KmipHelpDescription),
},
}
}
func (kb *KmipBackend) handleListScopeExistenceCheck() framework.ExistenceFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
return true, nil
}
}
func (kb *KmipBackend) handleScopeExistenceCheck() framework.ExistenceFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
key := data.Get("scope").(string)
out, err := req.Storage.Get(ctx, key)
if err != nil {
return false, fmt.Errorf("existence check failed: %w", err)
}
return out != nil, nil
}
}
func (kb *KmipBackend) handleScopeList() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// List the keys at the prefix given by the request
keys, err := listStorage(ctx, req.Storage, "scope")
if err != nil {
return nil, err
}
// Generate the response
return logical.ListResponse(keys), nil
}
}
func (kb *KmipBackend) handleScopeCreate() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
scope := data.Get("scope").(string)
kb.scopeLock[scope] = new(sync.RWMutex)
key := "scope/" + scope
if key == "" {
return logical.ErrorResponse("missing path"), nil
}
out, _ := req.Storage.Get(ctx, key)
if out != nil {
return nil, fmt.Errorf("existence check failed: the path existed")
}
// JSON encode the data
d := map[string]interface{}{
"create_time": time.Now().String(),
}
if err := writeStorage(ctx, req.Storage, key, d); err != nil {
return nil, fmt.Errorf("failed to write: %w", err)
}
return nil, nil
}
}
func (kb *KmipBackend) handleScopeDelete() framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
scopeName := data.Get("scope").(string)
scopePath := "scope/" + scopeName
flag := true // Allow deletion
roles, err := listStorage(ctx, req.Storage, scopePath+"/role")
if err != nil {
return nil, err
}
if len(roles) > 0 {
flag = false
// judge force parameter
if d, ok := req.Data["force"]; ok {
if d == "true" {
flag = true
}
}
}
if !flag {
return nil, fmt.Errorf(errNeedForceParam)
}
for _, roleName := range roles {
if err := kb.deleteRole(ctx, req, scopeName, roleName); err != nil {
return nil, err
}
}
// Delete the key at the request path
if err := req.Storage.Delete(ctx, scopePath); err != nil {
return nil, err
}
delete(kb.scopeLock, scopeName)
return nil, nil
}
}