-
Notifications
You must be signed in to change notification settings - Fork 9
/
repo.go
126 lines (104 loc) · 3.44 KB
/
repo.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
package main
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/jmoiron/sqlx"
"github.com/openstandia/goldap/message"
)
var maxRetry = 10
// For generic filter
type StmtCache struct {
sm sync.Map
}
func (m *StmtCache) Get(key string) (*sqlx.NamedStmt, bool) {
val, ok := m.sm.Load(key)
if !ok {
return nil, false
}
return val.(*sqlx.NamedStmt), true
}
func (m *StmtCache) Put(key string, value *sqlx.NamedStmt) {
m.sm.Store(key, value)
}
type DBRepository struct {
server *Server
db *sqlx.DB
}
func NewRepository(server *Server) (Repository, error) {
// Init DB Connection
db, err := sqlx.Connect("postgres", fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable search_path=%s",
server.config.DBHostName, server.config.DBPort, server.config.DBUser, server.config.DBName,
server.config.DBPassword, server.config.DBSchema))
if err != nil {
log.Fatalf("alert: Connect error. host=%s, port=%d, user=%s, dbname=%s, error=%s",
server.config.DBHostName, server.config.DBPort, server.config.DBUser, server.config.DBName, err)
}
db.SetMaxOpenConns(server.config.DBMaxOpenConns)
db.SetMaxIdleConns(server.config.DBMaxIdleConns)
// db.SetConnMaxLifetime(time.Hour)
// TODO: Enable to switch another implementation
repo := &HybridRepository{
DBRepository: &DBRepository{
server: server,
db: db,
},
translator: &HybridDBFilterTranslator{},
}
err = repo.Init()
if err != nil {
return nil, err
}
return repo, nil
}
type Repository interface {
// Init is called when initializing repository implementation.
Init() error
// Bind fetches the current bind entry by specified DN. Then execute callback with the entry.
// The callback is expected checking the credential, account lock status and so on.
// This is used for BIND operation.
Bind(ctx context.Context, dn *DN, callback func(current *FetchedCredential) error) error
// FindPPolicyByDN returns the password policy entry by specified DN.
// This is used for password policy process.
FindPPolicyByDN(ctx context.Context, dn *DN) (*PPolicy, error)
// Search handles search request by filter.
// This is used for SEARCH operation.
Search(ctx context.Context, baseDN *DN, option *SearchOption, handler func(entry *SearchEntry) error) (int32, int64, error)
// Update modifies the entry by specified change data.
// This is used for MOD operation.
Update(ctx context.Context, dn *DN, callback func(current *ModifyEntry) error) error
// UpdateDN modifies the entry DN by specified change data.
// This is used for MODRDN operation.
UpdateDN(ctx context.Context, oldDN, newDN *DN, oldRDN *RelativeDN) error
// Insert creates the entry by specified entry data.
Insert(ctx context.Context, entry *AddEntry) (int64, error)
// DeleteByDN deletes the entry by specified DN.
DeleteByDN(ctx context.Context, dn *DN) error
}
type SearchOption struct {
Scope int
Filter message.Filter
PageSize int32
Cursor *int64
RequestedAssocation []string
IsMemberOfRequested bool
IsHasSubordinatesRequested bool
}
type FetchedDNOrig struct {
ID int64 `db:"id"`
DNOrig string `db:"dn_orig"`
}
type FetchedCredential struct {
ID int64
// Credential
Credential []string
// DN of the MemberOf
MemberOf []*DN
// PPolicy related to this entry
PPolicy *PPolicy
PwdAccountLockedTime *time.Time
LastPwdFailureTime *time.Time
PwdFailureCount int
}