Skip to content

Commit 605841f

Browse files
author
Dmytro Bondar
authored
fix: LDAP sync interval (#304)
Configurable LDAP sync interval for each LDAP provider
1 parent a46dabc commit 605841f

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ The following configuration options are available:
6666
| log_level | advanced | warn | The loglevel, can be one of: trace, debug, info, warn, error. |
6767
| log_pretty | advanced | false | Uses pretty, colorized log messages. |
6868
| log_json | advanced | false | Logs in JSON format. |
69-
| ldap_sync_interval | advanced | 15m | The time interval after which users will be synchronized from LDAP. |
7069
| start_listen_port | advanced | 51820 | The first port number that will be used as listening port for new interfaces. |
7170
| start_cidr_v4 | advanced | 10.11.12.0/24 | The first IPv4 subnet that will be used for new interfaces. |
7271
| start_cidr_v6 | advanced | fdfd:d3ad:c0de:1234::0/64 | The first IPv6 subnet that will be used for new interfaces. |
@@ -127,9 +126,9 @@ The following configuration options are available:
127126
| field_map | auth/ldap | | Mapping of user fields. Internal fields: user_identifier, email, firstname, lastname, phone, department and memberof. |
128127
| login_filter | auth/ldap | | LDAP filters for users that should be allowed to log in. {{login_identifier}} will be replaced with the login username. |
129128
| admin_group | auth/ldap | | Users in this group are marked as administrators. |
130-
| synchronize | auth/ldap | | Periodically synchronize users (name, department, phone, status, ...) to the WireGuard Portal database. |
131129
| disable_missing | auth/ldap | | If synchronization is enabled, missing LDAP users will be disabled in WireGuard Portal. |
132130
| sync_filter | auth/ldap | | LDAP filters for users that should be synchronized to WireGuard Portal. |
131+
| sync_interval | auth/ldap | | The time interval after which users will be synchronized from LDAP. Empty value or `0` disables synchronization. |
133132
| registration_enabled | auth/ldap | | If registration is enabled, new user accounts will created in WireGuard Portal. |
134133
| debug | database | false | Debug database statements (log each statement). |
135134
| slow_query_threshold | database | | A threshold for slow database queries. If the threshold is exceeded, a warning message will be logged. |

internal/app/users/user_manager.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,17 @@ type Manager struct {
2626
cfg *config.Config
2727
bus evbus.MessageBus
2828

29-
syncInterval time.Duration
30-
users UserDatabaseRepo
31-
peers PeerDatabaseRepo
29+
users UserDatabaseRepo
30+
peers PeerDatabaseRepo
3231
}
3332

3433
func NewUserManager(cfg *config.Config, bus evbus.MessageBus, users UserDatabaseRepo, peers PeerDatabaseRepo) (*Manager, error) {
3534
m := &Manager{
3635
cfg: cfg,
3736
bus: bus,
3837

39-
syncInterval: 10 * time.Second,
40-
users: users,
41-
peers: peers,
38+
users: users,
39+
peers: peers,
4240
}
4341
return m, nil
4442
}
@@ -311,26 +309,29 @@ func (m Manager) validateDeletion(ctx context.Context, del *domain.User) error {
311309
}
312310

313311
func (m Manager) runLdapSynchronizationService(ctx context.Context) {
314-
running := true
315-
for running {
316-
select {
317-
case <-ctx.Done():
318-
running = false
319-
continue
320-
case <-time.After(m.syncInterval):
321-
// select blocks until one of the cases evaluate to true
322-
}
323-
324-
for _, ldapCfg := range m.cfg.Auth.Ldap { // LDAP Auth providers
325-
if !ldapCfg.Synchronize {
326-
continue // sync disabled
312+
for _, ldapCfg := range m.cfg.Auth.Ldap { // LDAP Auth providers
313+
go func(cfg config.LdapProvider) {
314+
syncInterval := cfg.SyncInterval
315+
if syncInterval == 0 {
316+
logrus.Debugf("sync disabled for LDAP server: %s", cfg.ProviderName)
317+
return
327318
}
328-
//logrus.Tracef(&ldapCfg)
329-
err := m.synchronizeLdapUsers(ctx, &ldapCfg)
330-
if err != nil {
331-
logrus.Errorf("failed to synchronize LDAP users for %s: %v", ldapCfg.ProviderName, err)
319+
running := true
320+
for running {
321+
select {
322+
case <-ctx.Done():
323+
running = false
324+
continue
325+
case <-time.After(syncInterval * time.Second):
326+
// select blocks until one of the cases evaluate to true
327+
}
328+
329+
err := m.synchronizeLdapUsers(ctx, &cfg)
330+
if err != nil {
331+
logrus.Errorf("failed to synchronize LDAP users for %s: %v", cfg.ProviderName, err)
332+
}
332333
}
333-
}
334+
}(ldapCfg)
334335
}
335336
}
336337

@@ -388,7 +389,7 @@ func (m Manager) updateLdapUsers(ctx context.Context, providerName string, rawUs
388389
tctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
389390
defer cancel()
390391
tctx = domain.SetUserInfo(tctx, domain.SystemAdminContextUserInfo())
391-
392+
392393
if existingUser == nil {
393394
err := m.NewUser(tctx, user)
394395
if err != nil {
@@ -397,7 +398,7 @@ func (m Manager) updateLdapUsers(ctx context.Context, providerName string, rawUs
397398
}
398399

399400
if existingUser != nil && existingUser.Source == domain.UserSourceLdap && userChangedInLdap(existingUser, user) {
400-
401+
401402
err := m.users.SaveUser(tctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
402403
u.UpdatedAt = time.Now()
403404
u.UpdatedBy = "ldap_sync"

internal/config/auth.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"time"
5+
46
"github.com/go-ldap/ldap/v3"
57
)
68

@@ -50,10 +52,10 @@ type LdapProvider struct {
5052
AdminGroupDN string `yaml:"admin_group"` // Members of this group receive admin rights in WG-Portal
5153
ParsedAdminGroupDN *ldap.DN `yaml:"-"`
5254

53-
Synchronize bool `yaml:"synchronize"`
5455
// If DisableMissing is true, missing users will be deactivated
55-
DisableMissing bool `yaml:"disable_missing"`
56-
SyncFilter string `yaml:"sync_filter"`
56+
DisableMissing bool `yaml:"disable_missing"`
57+
SyncFilter string `yaml:"sync_filter"`
58+
SyncInterval time.Duration `yaml:"sync_interval"`
5759

5860
// If RegistrationEnabled is set to true, wg-portal will create new users that do not exist in the database.
5961
RegistrationEnabled bool `yaml:"registration_enabled"`

internal/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type Config struct {
2727
LogLevel string `yaml:"log_level"`
2828
LogPretty bool `yaml:"log_pretty"`
2929
LogJson bool `yaml:"log_json"`
30-
LdapSyncInterval time.Duration `yaml:"ldap_sync_interval"`
3130
StartListenPort int `yaml:"start_listen_port"`
3231
StartCidrV4 string `yaml:"start_cidr_v4"`
3332
StartCidrV6 string `yaml:"start_cidr_v6"`

0 commit comments

Comments
 (0)