forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
270 lines (217 loc) · 6.93 KB
/
users.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package api
import (
"errors"
"sort"
"github.com/Velocidex/ordereddict"
context "golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"www.velocidex.com/golang/velociraptor/acls"
acl_proto "www.velocidex.com/golang/velociraptor/acls/proto"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/services"
)
// This is only used to set the user's own password which is always
// allowed for any user.
func (self *ApiServer) SetPassword(
ctx context.Context,
in *api_proto.SetPasswordRequest) (*emptypb.Empty, error) {
// Enforce a minimum length password
if len(in.Password) < 4 {
return nil, InvalidStatus("Password is not set or too short")
}
user_manager := services.GetUserManager()
user_record, _, err := user_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
principal := user_record.Name
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, Status(self.verbose, err)
}
org_config_obj, err := org_manager.GetOrgConfig(services.ROOT_ORG_ID)
if err != nil {
return nil, Status(self.verbose, err)
}
// The user we change the password for.
target := in.Username
if target == "" {
target = principal
}
err = user_manager.SetUserPassword(
ctx, org_config_obj, principal, target, in.Password, "")
if err != nil {
return nil, Status(self.verbose, err)
}
return &emptypb.Empty{}, nil
}
func (self *ApiServer) GetUsers(
ctx context.Context,
in *emptypb.Empty) (*api_proto.Users, error) {
user_manager := services.GetUserManager()
user_record, org_config_obj, err := user_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
principal := user_record.Name
// Only show users in the current org
users, err := user_manager.ListUsers(ctx, principal, []string{org_config_obj.OrgId})
if err != nil {
return nil, Status(self.verbose, err)
}
sort.Slice(users, func(i, j int) bool { return users[i].Name < users[j].Name })
return &api_proto.Users{Users: users}, nil
}
func (self *ApiServer) GetGlobalUsers(
ctx context.Context,
in *emptypb.Empty) (*api_proto.Users, error) {
user_manager := services.GetUserManager()
user_record, _, err := user_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
principal := user_record.Name
// Show all users visible to us
users, err := user_manager.ListUsers(ctx, principal, []string{})
if err != nil {
return nil, Status(self.verbose, err)
}
sort.Slice(users, func(i, j int) bool { return users[i].Name < users[j].Name })
return &api_proto.Users{Users: users}, nil
}
// Create a new user in the specified orgs.
func (self *ApiServer) CreateUser(ctx context.Context,
in *api_proto.UpdateUserRequest) (*emptypb.Empty, error) {
users_manager := services.GetUserManager()
user_record, org_config_obj, err := users_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
principal := user_record.Name
// Prepare an ACL object from the incoming request.
acl := &acl_proto.ApiClientACL{
Roles: in.Roles,
}
mode := services.UseExistingUser
if in.AddNewUser {
mode = services.AddNewUser
}
err = users_manager.AddUserToOrg(ctx, mode, principal, in.Name, in.Orgs, acl)
if err == nil {
services.LogAudit(ctx,
org_config_obj, principal, "user_create",
ordereddict.NewDict().
Set("username", in.Name).
Set("acl", acl).
Set("org_ids", in.Orgs))
}
return &emptypb.Empty{}, err
}
func (self *ApiServer) GetUser(
ctx context.Context, in *api_proto.UserRequest) (*api_proto.VelociraptorUser, error) {
users_manager := services.GetUserManager()
user_record, _, err := users_manager.GetUserFromContext(ctx)
if err != nil {
return nil, err
}
user, err := users_manager.GetUser(ctx, user_record.Name, in.Name)
if err != nil {
if errors.Is(err, acls.PermissionDenied) {
return nil, status.Error(codes.PermissionDenied,
"User is not allowed to view requested user.")
}
return nil, err
}
return user, nil
}
func (self *ApiServer) GetUserFavorites(
ctx context.Context,
in *api_proto.Favorite) (*api_proto.Favorites, error) {
// No special permission requires to view a user's own favorites.
users_manager := services.GetUserManager()
user_record, org_config_obj, err := users_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
principal := user_record.Name
return users_manager.GetFavorites(ctx, org_config_obj, principal, in.Type)
}
func (self *ApiServer) GetUserRoles(
ctx context.Context,
in *api_proto.UserRequest) (*api_proto.UserRoles, error) {
users_manager := services.GetUserManager()
_, _, err := users_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
// Allow the user to ask about other orgs.
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, Status(self.verbose, err)
}
org_config_obj, err := org_manager.GetOrgConfig(in.Org)
if err != nil {
return nil, Status(self.verbose, err)
}
acl_manager, err := services.GetACLManager(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
policy, err := acl_manager.GetPolicy(org_config_obj, in.Name)
if err != nil {
policy = &acl_proto.ApiClientACL{}
}
user_roles := &api_proto.UserRoles{
Name: in.Name,
Org: in.Org,
OrgName: org_config_obj.OrgName,
Roles: policy.Roles,
Permissions: acls.DescribePermissions(policy),
AllRoles: acls.ALL_ROLES,
AllPermissions: acls.ALL_PERMISSIONS,
}
// Expand the policy's permissions
err = acls.GetRolePermissions(org_config_obj, policy.Roles, policy)
if err != nil {
return nil, Status(self.verbose, err)
}
user_roles.EffectivePermissions = acls.DescribePermissions(policy)
return user_roles, nil
}
func (self *ApiServer) SetUserRoles(
ctx context.Context,
in *api_proto.UserRoles) (*emptypb.Empty, error) {
users_manager := services.GetUserManager()
user_record, org_config_obj, err := users_manager.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
principal := user_record.Name
// Prepare an ACL object from the incoming request.
acl := &acl_proto.ApiClientACL{}
for _, r := range in.Roles {
if acls.ValidateRole(r) {
acl.Roles = append(acl.Roles, r)
}
}
// Add any special permissions
err = acls.SetTokenPermission(acl, in.Permissions...)
if err != nil {
return nil, Status(self.verbose, err)
}
// Now attempt to set the ACL - permission checks are done by
// users.AddUserToOrg
err = users_manager.AddUserToOrg(ctx, services.UseExistingUser,
principal, in.Name, []string{in.Org}, acl)
if err == nil {
services.LogAudit(ctx,
org_config_obj, principal, "user_grant",
ordereddict.NewDict().
Set("username", in.Name).
Set("acl", acl).
Set("org_ids", []string{in.Org}))
}
return &emptypb.Empty{}, err
}