-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_roles.go
More file actions
175 lines (154 loc) · 4.95 KB
/
path_roles.go
File metadata and controls
175 lines (154 loc) · 4.95 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
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
package solacevaultplugin
import (
"context"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathRoles(b *solaceBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the role.",
Required: true,
},
"broker": {
Type: framework.TypeString,
Description: "Name of the broker configuration to use.",
Required: true,
},
"cli_username": {
Type: framework.TypeString,
Description: "CLI username on the Solace broker.",
Required: true,
},
"rotation_period": {
Type: framework.TypeDurationSecond,
Description: "How often to rotate the password, in seconds. 0 disables automatic rotation.",
Default: 0,
},
"password_length": {
Type: framework.TypeInt,
Description: "Length of generated passwords. Must be between 16 and 128. Default: 25.",
Default: 25,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathRolesWrite,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRolesWrite,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathRolesRead,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathRolesDelete,
},
},
ExistenceCheck: b.pathRolesExistenceCheck,
HelpSynopsis: "Manage roles that map to Solace CLI users.",
HelpDescription: "Create, read, update, or delete a role that maps to a CLI user account on a Solace broker.",
},
{
Pattern: "roles/?$",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathRolesList,
},
},
HelpSynopsis: "List configured roles.",
HelpDescription: "List the names of all configured roles.",
},
}
}
func (b *solaceBackend) pathRolesExistenceCheck(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
name := d.Get("name").(string)
role, err := getRole(ctx, req.Storage, name)
if err != nil {
return false, err
}
return role != nil, nil
}
func (b *solaceBackend) pathRolesWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
broker := d.Get("broker").(string)
cliUsername := d.Get("cli_username").(string)
rotationPeriodSec := d.Get("rotation_period").(int)
passwordLength := d.Get("password_length").(int)
if broker == "" {
return logical.ErrorResponse("broker is required"), nil
}
if cliUsername == "" {
return logical.ErrorResponse("cli_username is required"), nil
}
if passwordLength < 16 || passwordLength > 128 {
return logical.ErrorResponse(fmt.Sprintf("password_length must be between 16 and 128, got %d", passwordLength)), nil
}
// Verify the referenced broker exists
brokerConfig, err := getBroker(ctx, req.Storage, broker)
if err != nil {
return nil, err
}
if brokerConfig == nil {
return logical.ErrorResponse("broker %q not found", broker), nil
}
// Preserve existing password and last_rotated if updating
existing, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
role := &RoleEntry{
Broker: broker,
CLIUsername: cliUsername,
RotationPeriod: time.Duration(rotationPeriodSec) * time.Second,
PasswordLength: passwordLength,
}
if existing != nil {
role.Password = existing.Password
role.LastRotated = existing.LastRotated
}
if err := putRole(ctx, req.Storage, name, role); err != nil {
return nil, err
}
return nil, nil
}
func (b *solaceBackend) pathRolesRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
role, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
return nil, nil
}
data := map[string]interface{}{
"broker": role.Broker,
"cli_username": role.CLIUsername,
"rotation_period": int(role.RotationPeriod.Seconds()),
"password_length": role.PasswordLength,
}
if !role.LastRotated.IsZero() {
data["last_rotated"] = role.LastRotated.Format(time.RFC3339)
}
return &logical.Response{Data: data}, nil
}
func (b *solaceBackend) pathRolesDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if err := deleteRole(ctx, req.Storage, name); err != nil {
return nil, err
}
return nil, nil
}
func (b *solaceBackend) pathRolesList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roles, err := listRoles(ctx, req.Storage)
if err != nil {
return nil, err
}
return logical.ListResponse(roles), nil
}