defender: fix GetHost for blocklist entries too

This commit is contained in:
Nicola Murino 2021-06-20 21:57:19 +02:00
parent ac8d8a3da1
commit e09bdd43d4
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 12 additions and 10 deletions

View file

@ -266,10 +266,12 @@ func (d *memoryDefender) GetHost(ip string) (*DefenderEntry, error) {
defer d.RUnlock() defer d.RUnlock()
if banTime, ok := d.banned[ip]; ok { if banTime, ok := d.banned[ip]; ok {
return &DefenderEntry{ if banTime.After(time.Now()) {
IP: ip, return &DefenderEntry{
BanTime: banTime, IP: ip,
}, nil BanTime: banTime,
}, nil
}
} }
if hs, ok := d.hosts[ip]; ok { if hs, ok := d.hosts[ip]; ok {

View file

@ -206,14 +206,14 @@ func TestExpiredHostBans(t *testing.T) {
assert.Len(t, res, 0) assert.Len(t, res, 0)
assert.False(t, defender.IsBanned(testIP)) assert.False(t, defender.IsBanned(testIP))
entry, err := defender.GetHost(testIP) _, err = defender.GetHost(testIP)
assert.NoError(t, err) assert.Error(t, err)
assert.Equal(t, testIP, entry.IP) _, ok := defender.banned[testIP]
assert.NotEmpty(t, entry.GetBanTime()) assert.True(t, ok)
// now add an event for an expired banned ip, it should be removed // now add an event for an expired banned ip, it should be removed
defender.AddEvent(testIP, HostEventLoginFailed) defender.AddEvent(testIP, HostEventLoginFailed)
assert.False(t, defender.IsBanned(testIP)) assert.False(t, defender.IsBanned(testIP))
entry, err = defender.GetHost(testIP) entry, err := defender.GetHost(testIP)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, testIP, entry.IP) assert.Equal(t, testIP, entry.IP)
assert.Empty(t, entry.GetBanTime()) assert.Empty(t, entry.GetBanTime())
@ -248,7 +248,7 @@ func TestExpiredHostBans(t *testing.T) {
assert.Len(t, res, 0) assert.Len(t, res, 0)
_, err = defender.GetHost(testIP) _, err = defender.GetHost(testIP)
assert.Error(t, err) assert.Error(t, err)
_, ok := defender.hosts[testIP] _, ok = defender.hosts[testIP]
assert.True(t, ok) assert.True(t, ok)
} }