2019-04-25 04:52:08 +07:00
|
|
|
package regulation_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/mocks"
|
|
|
|
"github.com/authelia/authelia/internal/models"
|
|
|
|
"github.com/authelia/authelia/internal/regulation"
|
|
|
|
"github.com/authelia/authelia/internal/storage"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type RegulatorSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
ctrl *gomock.Controller
|
2019-11-17 08:05:46 +07:00
|
|
|
storageMock *storage.MockProvider
|
2019-04-25 04:52:08 +07:00
|
|
|
configuration schema.RegulationConfiguration
|
2019-11-30 21:33:45 +07:00
|
|
|
clock mocks.TestingClock
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegulatorSuite) SetupTest() {
|
|
|
|
s.ctrl = gomock.NewController(s.T())
|
2019-11-17 08:05:46 +07:00
|
|
|
s.storageMock = storage.NewMockProvider(s.ctrl)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
s.configuration = schema.RegulationConfiguration{
|
|
|
|
MaxRetries: 3,
|
2020-04-05 19:37:21 +07:00
|
|
|
BanTime: "180",
|
|
|
|
FindTime: "30",
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2019-11-30 21:33:45 +07:00
|
|
|
s.clock.Set(time.Now())
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegulatorSuite) TearDownTest() {
|
|
|
|
s.ctrl.Finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegulatorSuite) TestShouldNotThrowWhenUserIsLegitimate() {
|
2020-01-27 04:42:05 +07:00
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-4 * time.Minute),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
2020-01-27 04:42:05 +07:00
|
|
|
Return(attemptsInDB, nil)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks the case in which a user failed to authenticate many times but always
|
|
|
|
// with a certain amount of time larger than FindTime. Meaning the user should not be banned.
|
|
|
|
func (s *RegulatorSuite) TestShouldNotThrowWhenFailedAuthenticationNotInFindTime() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-1 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-90 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-180 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks the case in which a user failed to authenticate many times only a few
|
|
|
|
// seconds ago (meaning we are checking from now back to now-FindTime).
|
|
|
|
func (s *RegulatorSuite) TestShouldBanUserIfLatestAttemptsAreWithinFinTime() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-1 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-4 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-6 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-180 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.Equal(s.T(), regulation.ErrUserIsBanned, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks the case in which a user failed to authenticate many times only a few
|
|
|
|
// seconds ago (meaning we are checking from now-FindTime+X back to now-2FindTime+X knowing that
|
|
|
|
// we are within now and now-BanTime). It means the user has been banned some time ago and is still
|
|
|
|
// banned right now.
|
|
|
|
func (s *RegulatorSuite) TestShouldCheckUserIsStillBanned() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-31 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-34 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-36 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.Equal(s.T(), regulation.ErrUserIsBanned, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegulatorSuite) TestShouldCheckUserIsNotYetBanned() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-34 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-36 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegulatorSuite) TestShouldCheckUserWasAboutToBeBanned() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-14 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
// more than 30 seconds elapsed between this auth and the preceding one.
|
|
|
|
// In that case we don't need to regulate the user even though the number
|
|
|
|
// of retrieved attempts is 3.
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-94 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-96 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegulatorSuite) TestShouldCheckRegulationHasBeenResetOnSuccessfulAttempt() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-90 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-93 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
// The user was almost banned but he did a successful attempt. Therefore, even if the next
|
2020-01-06 06:03:16 +07:00
|
|
|
// failure happens within FindTime, he should not be banned.
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-94 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2019-04-25 04:52:08 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.clock.Now().Add(-96 * time.Second),
|
2019-04-25 04:52:08 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
regulator := regulation.NewRegulator(&s.configuration, s.storageMock, &s.clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunRegulatorSuite(t *testing.T) {
|
|
|
|
s := new(RegulatorSuite)
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
2020-01-27 04:42:05 +07:00
|
|
|
|
|
|
|
// This test checks that the regulator is disabled when configuration is set to 0.
|
|
|
|
func (s *RegulatorSuite) TestShouldHaveRegulatorDisabled() {
|
|
|
|
attemptsInDB := []models.AuthenticationAttempt{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-01-27 04:42:05 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
|
|
|
Time: s.clock.Now().Add(-31 * time.Second),
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-01-27 04:42:05 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
|
|
|
Time: s.clock.Now().Add(-34 * time.Second),
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-01-27 04:42:05 +07:00
|
|
|
Username: "john",
|
|
|
|
Successful: false,
|
|
|
|
Time: s.clock.Now().Add(-36 * time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storageMock.EXPECT().
|
|
|
|
LoadLatestAuthenticationLogs(gomock.Eq("john"), gomock.Any()).
|
|
|
|
Return(attemptsInDB, nil)
|
|
|
|
|
|
|
|
// Check Disabled Functionality
|
|
|
|
configuration := schema.RegulationConfiguration{
|
|
|
|
MaxRetries: 0,
|
2020-04-05 19:37:21 +07:00
|
|
|
FindTime: "180",
|
|
|
|
BanTime: "180",
|
2020-01-27 04:42:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
regulator := regulation.NewRegulator(&configuration, s.storageMock, &s.clock)
|
|
|
|
_, err := regulator.Regulate("john")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
|
|
|
|
// Check Enabled Functionality
|
|
|
|
configuration = schema.RegulationConfiguration{
|
|
|
|
MaxRetries: 1,
|
2020-04-05 19:37:21 +07:00
|
|
|
FindTime: "180",
|
|
|
|
BanTime: "180",
|
2020-01-27 04:42:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
regulator = regulation.NewRegulator(&configuration, s.storageMock, &s.clock)
|
|
|
|
_, err = regulator.Regulate("john")
|
|
|
|
assert.Equal(s.T(), regulation.ErrUserIsBanned, err)
|
|
|
|
}
|