-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.go
126 lines (106 loc) · 2.6 KB
/
app.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 (
"os"
"os/signal"
"syscall"
"time"
"github.com/pkg/errors"
"k8s.io/utils/clock"
)
type ObjectID = string
type Source interface {
GetUsers() ([]SourceUser, error)
GetGroupsWithMembers() ([]SourceGroupWithMembers, error)
CreateUserFromRaw(raw map[string]any) (SourceUser, error)
CreateGroupFromRaw(raw map[string]any) (SourceGroup, error)
}
type App struct {
syncInterval time.Duration
usernameReplaces []ReplacementPair
groupnameReplaces []ReplacementPair
removeLimit int
banDuration time.Duration
ytsaurus *Ytsaurus
source Source
stopCh chan struct{}
sigCh chan os.Signal
logger appLoggerType
}
func NewApp(cfg *Config, logger appLoggerType) (*App, error) {
if (cfg.Azure == nil) == (cfg.Ldap == nil) {
return nil, errors.New("one and only one source should be specified")
}
var err error
var source Source
if cfg.Azure != nil {
source, err = NewAzureReal(cfg.Azure, logger)
if err != nil {
return nil, err
}
}
if cfg.Ldap != nil {
source, err = NewLdap(cfg.Ldap, logger)
if err != nil {
return nil, err
}
}
return NewAppCustomized(cfg, logger, source, clock.RealClock{})
}
// NewAppCustomized used in tests.
func NewAppCustomized(cfg *Config, logger appLoggerType, source Source, clock clock.PassiveClock) (*App, error) {
yt, err := NewYtsaurus(&cfg.Ytsaurus, logger, clock)
if err != nil {
return nil, err
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGUSR1)
return &App{
syncInterval: cfg.App.SyncInterval,
usernameReplaces: cfg.App.UsernameReplacements,
groupnameReplaces: cfg.App.GroupnameReplacements,
removeLimit: cfg.App.RemoveLimit,
banDuration: cfg.App.BanBeforeRemoveDuration,
ytsaurus: yt,
source: source,
stopCh: make(chan struct{}),
sigCh: sigCh,
logger: logger,
}, nil
}
func (a *App) Start() {
a.logger.Info("Starting the application")
if a.syncInterval > 0 {
ticker := time.NewTicker(a.syncInterval)
for {
select {
case <-a.stopCh:
a.logger.Info("Stopping the application")
return
case <-ticker.C:
a.logger.Debug("Received next tick")
a.syncOnce()
case <-a.sigCh:
a.logger.Info("Received SIGUSR1")
a.syncOnce()
}
}
} else {
a.logger.Info(
"app.sync_interval config variable is not specified or is not greater than zero, " +
"auto sync is disabled. Send SIGUSR1 for manual sync.",
)
for {
select {
case <-a.stopCh:
a.logger.Info("Stopping the application")
return
case <-a.sigCh:
a.logger.Info("Received SIGUSR1")
a.syncOnce()
}
}
}
}
func (a *App) Stop() {
close(a.stopCh)
}