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
178
package main
import (
"context"
"fmt"
"strings"
"github.com/openbao/openbao/sdk/v2/framework"
"github.com/openbao/openbao/sdk/v2/helper/tokenutil"
"github.com/openbao/openbao/sdk/v2/logical"
)
const rolePrefix = "role/"
type roleEntry struct {
tokenutil.TokenParams
BoundDIDs []string `json:"bound_dids"`
BoundHandles []string `json:"bound_handles"`
}
func (r *roleEntry) allows(did, handle string) error {
for _, b := range r.BoundDIDs {
if b == "*" || b == did {
return nil
}
}
if handle != "" {
for _, b := range r.BoundHandles {
if b == "*" || b == handle {
return nil
}
}
}
if len(r.BoundDIDs) == 0 && handle == "" {
return fmt.Errorf("the role admits handles only; sign in with a handle")
}
return fmt.Errorf("the role does not admit %s", did)
}
func getRole(ctx context.Context, s logical.Storage, name string) (*roleEntry, error) {
raw, err := s.Get(ctx, rolePrefix+name)
if err != nil || raw == nil {
return nil, err
}
var r roleEntry
if err := raw.DecodeJSON(&r); err != nil {
return nil, err
}
return &r, nil
}
func pathRoleList(b *backend) *framework.Path {
return &framework.Path{
Pattern: "role/?$",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{Callback: b.roleList},
},
HelpSynopsis: "Lists the roles.",
}
}
func pathRole(b *backend) *framework.Path {
fields := map[string]*framework.FieldSchema{
"name": {Type: framework.TypeString, Description: "Name of the role.", Required: true},
"bound_dids": {
Type: framework.TypeCommaStringSlice,
Description: "DIDs that may log in with this role; * admits every DID.",
},
"bound_handles": {
Type: framework.TypeCommaStringSlice,
Description: "Handles that may log in with this role, by a handle login; * admits every handle.",
},
}
tokenutil.AddTokenFields(fields)
return &framework.Path{
Pattern: "role/" + framework.GenericNameRegex("name"),
Fields: fields,
ExistenceCheck: b.roleExists,
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{Callback: b.roleWrite},
logical.UpdateOperation: &framework.PathOperation{Callback: b.roleWrite},
logical.ReadOperation: &framework.PathOperation{Callback: b.roleRead},
logical.DeleteOperation: &framework.PathOperation{Callback: b.roleDelete},
},
HelpSynopsis: "Manages a role: who may log in with it and the token parameters of the login.",
}
}
func (b *backend) roleExists(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
r, err := getRole(ctx, req.Storage, d.Get("name").(string))
return r != nil, err
}
func (b *backend) roleList(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
names, err := req.Storage.List(ctx, rolePrefix)
if err != nil {
return nil, err
}
return logical.ListResponse(names), nil
}
func lowerAll(in []string) []string {
out := make([]string, 0, len(in))
for _, s := range in {
if s = strings.ToLower(strings.TrimSpace(s)); s != "" {
out = append(out, s)
}
}
return out
}
func (b *backend) roleWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
b.lock.Lock()
defer b.lock.Unlock()
r, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if r == nil {
r = &roleEntry{}
}
if v, ok := d.GetOk("bound_dids"); ok {
r.BoundDIDs = nil
for _, did := range v.([]string) {
did = strings.TrimSpace(did)
if did != "*" && !didPattern.MatchString(did) {
return logical.ErrorResponse(fmt.Sprintf("bound_dids entry %q is not a DID", did)), nil
}
if did != "" {
r.BoundDIDs = append(r.BoundDIDs, did)
}
}
}
if v, ok := d.GetOk("bound_handles"); ok {
r.BoundHandles = nil
for _, h := range lowerAll(v.([]string)) {
if h != "*" {
var err error
if h, err = normalizeHandle(h); err != nil {
return logical.ErrorResponse("bound_handles entry: " + err.Error()), nil
}
}
r.BoundHandles = append(r.BoundHandles, h)
}
}
if len(r.BoundDIDs) == 0 && len(r.BoundHandles) == 0 {
return logical.ErrorResponse("bound_dids or bound_handles is required (use * to admit everyone)"), nil
}
if err := r.ParseTokenFields(req, d); err != nil {
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
}
entry, err := logical.StorageEntryJSON(rolePrefix+name, r)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) roleRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
r, err := getRole(ctx, req.Storage, d.Get("name").(string))
if err != nil || r == nil {
return nil, err
}
data := map[string]interface{}{"bound_dids": r.BoundDIDs, "bound_handles": r.BoundHandles}
r.PopulateTokenData(data)
return &logical.Response{Data: data}, nil
}
func (b *backend) roleDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
b.lock.Lock()
defer b.lock.Unlock()
return nil, req.Storage.Delete(ctx, rolePrefix+d.Get("name").(string))
}