Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/database: simplify flush agents/bouncers
Browse files Browse the repository at this point in the history
mmetc committed May 31, 2024
1 parent 16bfab8 commit 9379c33
Showing 1 changed file with 34 additions and 63 deletions.
97 changes: 34 additions & 63 deletions pkg/database/flush.go
Original file line number Diff line number Diff line change
@@ -114,89 +114,60 @@ func (c *Client) FlushOrphans() {
}
}

func (c *Client) flushBouncers(bouncersCfg *csconfig.AuthGCCfg) {
if bouncersCfg == nil {
func (c *Client) flushBouncers(authType string, duration *time.Duration) {
if duration == nil {

Check warning on line 118 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L117-L118

Added lines #L117 - L118 were not covered by tests
return
}

if bouncersCfg.ApiDuration != nil {
log.Debug("trying to delete old bouncers from api")
count, err := c.Ent.Bouncer.Delete().Where(
bouncer.LastPullLTE(time.Now().UTC().Add(-*duration)),
).Where(
bouncer.AuthTypeEQ(authType),
).Exec(c.CTX)

Check warning on line 126 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L122-L126

Added lines #L122 - L126 were not covered by tests

deletionCount, err := c.Ent.Bouncer.Delete().Where(
bouncer.LastPullLTE(time.Now().UTC().Add(-*bouncersCfg.ApiDuration)),
).Where(
bouncer.AuthTypeEQ(types.ApiKeyAuthType),
).Exec(c.CTX)
if err != nil {
c.Log.Errorf("while auto-deleting expired bouncers (api key): %s", err)
} else if deletionCount > 0 {
c.Log.Infof("deleted %d expired bouncers (api auth)", deletionCount)
}
if err != nil {
c.Log.Errorf("while auto-deleting expired bouncers (%s): %s", authType, err)
return

Check warning on line 130 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L128-L130

Added lines #L128 - L130 were not covered by tests
}

if bouncersCfg.CertDuration != nil {
log.Debug("trying to delete old bouncers from cert")

deletionCount, err := c.Ent.Bouncer.Delete().Where(
bouncer.LastPullLTE(time.Now().UTC().Add(-*bouncersCfg.CertDuration)),
).Where(
bouncer.AuthTypeEQ(types.TlsAuthType),
).Exec(c.CTX)
if err != nil {
c.Log.Errorf("while auto-deleting expired bouncers (api key): %s", err)
} else if deletionCount > 0 {
c.Log.Infof("deleted %d expired bouncers (api auth)", deletionCount)
}
if count > 0 {
c.Log.Infof("deleted %d expired bouncers (%s)", count, authType)

Check warning on line 134 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L133-L134

Added lines #L133 - L134 were not covered by tests
}
}

func (c *Client) flushAgents(agentsCfg *csconfig.AuthGCCfg) {
if agentsCfg == nil {
func (c *Client) flushAgents(authType string, duration *time.Duration) {
if duration == nil {

Check warning on line 139 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L138-L139

Added lines #L138 - L139 were not covered by tests
return
}

if agentsCfg.CertDuration != nil {
log.Debug("trying to delete old agents from cert")

deletionCount, err := c.Ent.Machine.Delete().Where(
machine.LastHeartbeatLTE(time.Now().UTC().Add(-*agentsCfg.CertDuration)),
).Where(
machine.Not(machine.HasAlerts()),
).Where(
machine.AuthTypeEQ(types.TlsAuthType),
).Exec(c.CTX)
log.Debugf("deleted %d entries", deletionCount)
if err != nil {
c.Log.Errorf("while auto-deleting expired machine (cert): %s", err)
} else if deletionCount > 0 {
c.Log.Infof("deleted %d expired machine (cert auth)", deletionCount)
}
count, err := c.Ent.Machine.Delete().Where(
machine.LastHeartbeatLTE(time.Now().UTC().Add(-*duration)),
machine.Not(machine.HasAlerts()),
machine.AuthTypeEQ(authType),
).Exec(c.CTX)

if err != nil {
c.Log.Errorf("while auto-deleting expired machines (%s): %s", authType, err)
return

Check warning on line 151 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L143-L151

Added lines #L143 - L151 were not covered by tests
}

if agentsCfg.LoginPasswordDuration != nil {
log.Debug("trying to delete old agents from password")

deletionCount, err := c.Ent.Machine.Delete().Where(
machine.LastHeartbeatLTE(time.Now().UTC().Add(-*agentsCfg.LoginPasswordDuration)),
).Where(
machine.Not(machine.HasAlerts()),
).Where(
machine.AuthTypeEQ(types.PasswordAuthType),
).Exec(c.CTX)
log.Debugf("deleted %d entries", deletionCount)
if err != nil {
c.Log.Errorf("while auto-deleting expired machine (password): %s", err)
} else if deletionCount > 0 {
c.Log.Infof("deleted %d expired machine (password auth)", deletionCount)
}
if count > 0 {
c.Log.Infof("deleted %d expired machines (%s auth)", count, authType)

Check warning on line 155 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}
}

func (c *Client) FlushAgentsAndBouncers(agentsCfg *csconfig.AuthGCCfg, bouncersCfg *csconfig.AuthGCCfg) error {
log.Debug("starting FlushAgentsAndBouncers")

c.flushBouncers(bouncersCfg)
c.flushAgents(agentsCfg)
if agentsCfg != nil {
c.flushAgents(types.TlsAuthType, agentsCfg.CertDuration)
c.flushAgents(types.PasswordAuthType, agentsCfg.LoginPasswordDuration)
}

Check warning on line 165 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L163-L165

Added lines #L163 - L165 were not covered by tests

if bouncersCfg != nil {
c.flushBouncers(types.TlsAuthType, bouncersCfg.CertDuration)
c.flushBouncers(types.ApiKeyAuthType, bouncersCfg.ApiDuration)
}

Check warning on line 170 in pkg/database/flush.go

Codecov / codecov/patch

pkg/database/flush.go#L168-L170

Added lines #L168 - L170 were not covered by tests

return nil
}

0 comments on commit 9379c33

Please sign in to comment.