-
Notifications
You must be signed in to change notification settings - Fork 351
/
model.go
439 lines (379 loc) · 11.6 KB
/
model.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package model
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/go-openapi/swag"
"github.com/google/uuid"
"github.com/treeverse/lakefs/pkg/auth/crypt"
"github.com/treeverse/lakefs/pkg/kv"
"golang.org/x/crypto/bcrypt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
const (
StatementEffectAllow = "allow"
StatementEffectDeny = "deny"
PartitionKey = "auth"
PackageName = "auth"
groupsPrefix = "groups"
groupsUsersPrefix = "gUsers"
groupsPoliciesPrefix = "gPolicies"
usersPrefix = "users"
policiesPrefix = "policies"
usersPoliciesPrefix = "uPolicies"
usersCredentialsPrefix = "uCredentials" // #nosec G101 -- False positive: this is only a kv key prefix
credentialsPrefix = "credentials"
expiredTokensPrefix = "expiredTokens"
metadataPrefix = "installation_metadata"
)
//nolint:gochecknoinits
func init() {
kv.MustRegisterType("auth", "users", (&UserData{}).ProtoReflect().Type())
kv.MustRegisterType("auth", "policies", (&PolicyData{}).ProtoReflect().Type())
kv.MustRegisterType("auth", "groups", (&GroupData{}).ProtoReflect().Type())
kv.MustRegisterType("auth", kv.FormatPath("uCredentials", "*", "credentials"), (&CredentialData{}).ProtoReflect().Type())
kv.MustRegisterType("auth", kv.FormatPath("gUsers", "*", "users"), (&kv.SecondaryIndex{}).ProtoReflect().Type())
kv.MustRegisterType("auth", kv.FormatPath("gPolicies", "*", "policies"), (&kv.SecondaryIndex{}).ProtoReflect().Type())
kv.MustRegisterType("auth", kv.FormatPath("uPolicies", "*", "policies"), (&kv.SecondaryIndex{}).ProtoReflect().Type())
kv.MustRegisterType("auth", "expiredTokens", (&TokenData{}).ProtoReflect().Type())
kv.MustRegisterType("auth", "installation_metadata", nil)
}
func UserPath(userName string) []byte {
return []byte(kv.FormatPath(usersPrefix, userName))
}
func PolicyPath(displayName string) []byte {
return []byte(kv.FormatPath(policiesPrefix, displayName))
}
func GroupPath(displayName string) []byte {
return []byte(kv.FormatPath(groupsPrefix, displayName))
}
func CredentialPath(userName string, accessKeyID string) []byte {
return []byte(kv.FormatPath(usersCredentialsPrefix, userName, credentialsPrefix, accessKeyID))
}
func GroupUserPath(groupDisplayName string, userName string) []byte {
return []byte(kv.FormatPath(groupsUsersPrefix, groupDisplayName, usersPrefix, userName))
}
func UserPolicyPath(userName string, policyDisplayName string) []byte {
return []byte(kv.FormatPath(usersPoliciesPrefix, userName, policiesPrefix, policyDisplayName))
}
func GroupPolicyPath(groupDisplayName string, policyDisplayName string) []byte {
return []byte(kv.FormatPath(groupsPoliciesPrefix, groupDisplayName, policiesPrefix, policyDisplayName))
}
func ExpiredTokenPath(tokenID string) []byte {
return []byte(kv.FormatPath(expiredTokensPrefix, tokenID))
}
func ExpiredTokensPath() []byte {
return ExpiredTokenPath("")
}
func MetadataKeyPath(key string) string {
return kv.FormatPath(metadataPrefix, key)
}
var ErrInvalidStatementSrcFormat = errors.New("invalid statements src format")
type PaginationParams struct {
Prefix string
After string
Amount int
}
// Paginator describes the parameters of a slice of data from a database.
type Paginator struct {
Amount int
NextPageToken string
}
// SuperuserConfiguration requests a particular configuration for a superuser.
type SuperuserConfiguration struct {
User
AccessKeyID string
SecretAccessKey string
}
type User struct {
CreatedAt time.Time `db:"created_at"`
// Username is a unique identifier for the user. In password-based authentication, it is the email.
Username string `db:"display_name" json:"display_name"`
// FriendlyName, if set, is a shorter name for the user than
// Username. Unlike Username it does not identify the user (it
// might not be unique); use it in the user's GUI rather than in
// backend code.
FriendlyName *string `db:"friendly_name" json:"friendly_name"`
Email *string `db:"email" json:"email"`
EncryptedPassword []byte `db:"encrypted_password" json:"encrypted_password"`
Source string `db:"source" json:"source"`
ExternalID *string `db:"external_id" json:"external_id"`
}
type DBUser struct {
ID int64 `db:"id"`
User
}
func ConvertDBID(id int64) string {
const base = 10
return strconv.FormatInt(id, base)
}
type Group struct {
CreatedAt time.Time `db:"created_at"`
DisplayName string `db:"display_name" json:"display_name"`
}
type DBGroup struct {
ID int `db:"id"`
Group
}
type ACLPermission string
type ACL struct {
Permission ACLPermission `json:"permission"`
}
type Policy struct {
CreatedAt time.Time `db:"created_at"`
DisplayName string `db:"display_name" json:"display_name"`
Statement Statements `db:"statement"`
ACL ACL `db:"acl" json:"acl,omitempty"`
}
type DBPolicy struct {
ID int `db:"id"`
Policy
}
type Statement struct {
Effect string `json:"Effect"`
Action []string `json:"Action"`
Resource string `json:"Resource"`
}
type Statements []Statement
type BaseCredential struct {
AccessKeyID string `db:"access_key_id"`
SecretAccessKey string `db:"-" json:"-"`
SecretAccessKeyEncryptedBytes []byte `db:"secret_access_key" json:"-"`
IssuedDate time.Time `db:"issued_date"`
}
type Credential struct {
Username string
BaseCredential
}
type DBCredential struct {
UserID int64 `db:"user_id"`
BaseCredential
}
// CredentialKeys - For JSON serialization:
type CredentialKeys struct {
AccessKeyID string `json:"access_key_id"`
SecretAccessKey string `json:"secret_access_key"`
}
func (u *User) UpdatePassword(password string) error {
pw, err := HashPassword(password)
if err != nil {
return err
}
u.EncryptedPassword = pw
return nil
}
// HashPassword generates a hashed password from a plaintext string
func HashPassword(password string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
}
// Authenticate a user from a password Returns nil on success, or an error on failure.
func (u *User) Authenticate(password string) error {
return bcrypt.CompareHashAndPassword(u.EncryptedPassword, []byte(password))
}
func (s Statements) Value() (driver.Value, error) {
if s == nil {
return json.Marshal([]struct{}{})
}
return json.Marshal(s)
}
func (s *Statements) Scan(src interface{}) error {
if src == nil {
return nil
}
data, ok := src.([]byte)
if !ok {
return ErrInvalidStatementSrcFormat
}
return json.Unmarshal(data, s)
}
func UserFromProto(pb *UserData) *User {
return &User{
CreatedAt: pb.CreatedAt.AsTime(),
Username: pb.Username,
FriendlyName: &pb.FriendlyName,
Email: &pb.Email,
EncryptedPassword: pb.EncryptedPassword,
Source: pb.Source,
ExternalID: &pb.ExternalId,
}
}
func ProtoFromUser(u *User) *UserData {
return &UserData{
CreatedAt: timestamppb.New(u.CreatedAt),
Username: u.Username,
FriendlyName: swag.StringValue(u.FriendlyName),
Email: swag.StringValue(u.Email),
EncryptedPassword: u.EncryptedPassword,
Source: u.Source,
ExternalId: swag.StringValue(u.ExternalID),
}
}
func GroupFromProto(pb *GroupData) *Group {
return &Group{
CreatedAt: pb.CreatedAt.AsTime(),
DisplayName: pb.DisplayName,
}
}
func ProtoFromGroup(g *Group) *GroupData {
return &GroupData{
CreatedAt: timestamppb.New(g.CreatedAt),
DisplayName: g.DisplayName,
}
}
func PolicyFromProto(pb *PolicyData) *Policy {
policy := &Policy{
CreatedAt: pb.CreatedAt.AsTime(),
DisplayName: pb.DisplayName,
Statement: *statementsFromProto(pb.Statements),
}
if pb.Acl != nil {
policy.ACL = ACL{
Permission: ACLPermission(pb.Acl.Permission),
}
}
return policy
}
func ProtoFromPolicy(p *Policy) *PolicyData {
return &PolicyData{
CreatedAt: timestamppb.New(p.CreatedAt),
DisplayName: p.DisplayName,
Statements: protoFromStatements(&p.Statement),
Acl: &ACLData{
Permission: string(p.ACL.Permission),
},
}
}
func CredentialFromProto(s crypt.SecretStore, pb *CredentialData) (*Credential, error) {
secret, err := DecryptSecret(s, pb.SecretAccessKeyEncryptedBytes)
if err != nil {
return nil, err
}
return &Credential{
Username: string(pb.UserId),
BaseCredential: BaseCredential{
AccessKeyID: pb.AccessKeyId,
SecretAccessKey: secret,
SecretAccessKeyEncryptedBytes: pb.SecretAccessKeyEncryptedBytes,
IssuedDate: pb.IssuedDate.AsTime(),
},
}, nil
}
func ProtoFromCredential(c *Credential) *CredentialData {
return &CredentialData{
AccessKeyId: c.AccessKeyID,
SecretAccessKeyEncryptedBytes: c.SecretAccessKeyEncryptedBytes,
IssuedDate: timestamppb.New(c.IssuedDate),
UserId: []byte(c.Username),
}
}
func statementFromProto(pb *StatementData) *Statement {
return &Statement{
Effect: pb.Effect,
Action: pb.Action,
Resource: pb.Resource,
}
}
func protoFromStatement(s *Statement) *StatementData {
return &StatementData{
Effect: s.Effect,
Action: s.Action,
Resource: s.Resource,
}
}
func statementsFromProto(pb []*StatementData) *Statements {
statements := make(Statements, len(pb))
for i := range pb {
statements[i] = *statementFromProto(pb[i])
}
return &statements
}
func protoFromStatements(s *Statements) []*StatementData {
statements := make([]*StatementData, len(*s))
x := *s
for i := range *s {
statements[i] = protoFromStatement(&x[i])
}
return statements
}
func ConvertUsersList(users []*DBUser) []*User {
kvUsers := make([]*User, 0, len(users))
for _, u := range users {
kvUsers = append(kvUsers, &u.User)
}
return kvUsers
}
func ConvertUsersDataList(users []proto.Message) []*User {
kvUsers := make([]*User, 0, len(users))
for _, u := range users {
a := u.(*UserData)
kvUsers = append(kvUsers, UserFromProto(a))
}
return kvUsers
}
func ConvertCredList(creds []*DBCredential, username string) []*Credential {
res := make([]*Credential, 0, len(creds))
for _, c := range creds {
res = append(res, &Credential{
Username: username,
BaseCredential: c.BaseCredential,
})
}
return res
}
func ConvertGroupList(groups []*DBGroup) []*Group {
res := make([]*Group, 0, len(groups))
for _, g := range groups {
res = append(res, &g.Group)
}
return res
}
func ConvertGroupDataList(group []proto.Message) []*Group {
res := make([]*Group, 0, len(group))
for _, g := range group {
a := g.(*GroupData)
res = append(res, GroupFromProto(a))
}
return res
}
func ConvertPolicyDataList(policies []proto.Message) []*Policy {
res := make([]*Policy, 0, len(policies))
for _, p := range policies {
a := p.(*PolicyData)
res = append(res, PolicyFromProto(a))
}
return res
}
func ConvertCredDataList(s crypt.SecretStore, creds []proto.Message) ([]*Credential, error) {
res := make([]*Credential, 0, len(creds))
for _, c := range creds {
credentialData := c.(*CredentialData)
m, err := CredentialFromProto(s, credentialData)
if err != nil {
return nil, fmt.Errorf("credentials for %s: %w", credentialData.AccessKeyId, err)
}
m.SecretAccessKey = ""
res = append(res, m)
}
return res, nil
}
func DecryptSecret(s crypt.SecretStore, value []byte) (string, error) {
decrypted, err := s.Decrypt(value)
if err != nil {
return "", err
}
return string(decrypted), nil
}
func EncryptSecret(s crypt.SecretStore, secretAccessKey string) ([]byte, error) {
encrypted, err := s.Encrypt([]byte(secretAccessKey))
if err != nil {
return nil, err
}
return encrypted, nil
}
func CreateID() string {
return uuid.New().String()
}