2019-11-02 21:32:58 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2019-11-25 03:27:59 +07:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-11-02 21:32:58 +07:00
|
|
|
"testing"
|
2019-11-25 03:27:59 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2019-11-02 21:32:58 +07:00
|
|
|
)
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
type HighAvailabilityWebDriverSuite struct {
|
2019-11-02 21:32:58 +07:00
|
|
|
*SeleniumSuite
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
func NewHighAvailabilityWebDriverSuite() *HighAvailabilityWebDriverSuite {
|
|
|
|
return &HighAvailabilityWebDriverSuite{SeleniumSuite: new(SeleniumSuite)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilityWebDriverSuite) SetupSuite() {
|
|
|
|
wds, err := StartWebDriver()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.WebDriverSession = wds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilityWebDriverSuite) TearDownSuite() {
|
|
|
|
err := s.WebDriverSession.Stop()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
func (s *HighAvailabilityWebDriverSuite) SetupTest() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
feat(session): add redis sentinel provider (#1768)
* feat(session): add redis sentinel provider
* refactor(session): use int for ports as per go standards
* refactor(configuration): adjust tests and validation
* refactor(configuration): add err format consts
* refactor(configuration): explicitly map redis structs
* refactor(session): merge redis/redis sentinel providers
* refactor(session): add additional checks to redis providers
* feat(session): add redis cluster provider
* fix: update config for new values
* fix: provide nil certpool to affected tests/mocks
* test: add additional tests to cover uncovered code
* docs: expand explanation of host and nodes relation for redis
* ci: add redis-sentinel to suite highavailability, add redis-sentinel quorum
* fix(session): sentinel password
* test: use redis alpine library image for redis sentinel, use expose instead of ports, use redis ip, adjust redis ip range, adjust redis config
* test: make entrypoint.sh executable, fix entrypoint.sh if/elif
* test: add redis failover tests
* test: defer docker start, adjust sleep, attempt logout before login, attempt visit before login and tune timeouts, add additional logging
* test: add sentinel integration test
* test: add secondary node failure to tests, fix password usage, bump test timeout, add sleep
* feat: use sentinel failover cluster
* fix: renamed addrs to sentineladdrs upstream
* test(session): sentinel failover
* test: add redis standard back into testing
* test: move redis standalone test to traefik2
* fix/docs: apply suggestions from code review
2021-03-10 06:03:05 +07:00
|
|
|
func (s *HighAvailabilityWebDriverSuite) TestShouldKeepUserSessionActive() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secret := s.doRegisterThenLogout(ctx, s.T(), "john", "password")
|
|
|
|
|
|
|
|
err := haDockerEnvironment.Restart("redis-node-0")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, "")
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilityWebDriverSuite) TestShouldKeepUserSessionActiveWithPrimaryRedisNodeFailure() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secret := s.doRegisterThenLogout(ctx, s.T(), "john", "password")
|
|
|
|
|
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, "")
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
|
|
|
|
err := haDockerEnvironment.Stop("redis-node-0")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = haDockerEnvironment.Start("redis-node-0")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Allow fail over to occur.
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
|
|
|
|
// Verify the user is still authenticated
|
|
|
|
s.doVisit(s.T(), GetLoginBaseURL())
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
|
|
|
|
// Then logout and login again to check we can see the secret.
|
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
s.verifyIsFirstFactorPage(ctx, s.T())
|
|
|
|
|
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, fmt.Sprintf("%s/secret.html", SecureBaseURL))
|
|
|
|
s.verifySecretAuthorized(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilityWebDriverSuite) TestShouldKeepUserSessionActiveWithPrimaryRedisSentinelFailureAndSecondaryRedisNodeFailure() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secret := s.doRegisterThenLogout(ctx, s.T(), "john", "password")
|
|
|
|
|
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, "")
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
|
|
|
|
err := haDockerEnvironment.Stop("redis-sentinel-0")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = haDockerEnvironment.Start("redis-sentinel-0")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = haDockerEnvironment.Stop("redis-node-2")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = haDockerEnvironment.Start("redis-node-2")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Allow fail over to occur.
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
|
|
|
|
// Verify the user is still authenticated
|
|
|
|
s.doVisit(s.T(), GetLoginBaseURL())
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
func (s *HighAvailabilityWebDriverSuite) TestShouldKeepUserDataInDB() {
|
2019-12-07 23:40:42 +07:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
2019-11-25 03:27:59 +07:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secret := s.doRegisterThenLogout(ctx, s.T(), "john", "password")
|
|
|
|
|
|
|
|
err := haDockerEnvironment.Restart("mariadb")
|
2019-12-07 23:40:42 +07:00
|
|
|
s.Require().NoError(err)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2019-12-07 23:40:42 +07:00
|
|
|
time.Sleep(20 * time.Second)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, "")
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilityWebDriverSuite) TestShouldKeepSessionAfterAutheliaRestart() {
|
2019-12-08 19:16:13 +07:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
|
2019-11-25 03:27:59 +07:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secret := s.doRegisterAndLogin2FA(ctx, s.T(), "john", "password", false, "")
|
2019-12-08 19:16:13 +07:00
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
err := haDockerEnvironment.Restart("authelia-backend")
|
2019-12-08 19:16:13 +07:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = waitUntilAutheliaBackendIsReady(haDockerEnvironment)
|
|
|
|
s.Require().NoError(err)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
|
|
|
|
// Verify the user is still authenticated
|
2020-05-27 18:55:44 +07:00
|
|
|
s.doVisit(s.T(), GetLoginBaseURL())
|
2019-11-25 03:27:59 +07:00
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
|
|
|
|
// Then logout and login again to check the secret is still there
|
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
s.verifyIsFirstFactorPage(ctx, s.T())
|
|
|
|
|
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, fmt.Sprintf("%s/secret.html", SecureBaseURL))
|
|
|
|
s.verifySecretAuthorized(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
var UserJohn = "john"
|
|
|
|
var UserBob = "bob"
|
|
|
|
var UserHarry = "harry"
|
|
|
|
|
|
|
|
var Users = []string{UserJohn, UserBob, UserHarry}
|
|
|
|
|
|
|
|
var expectedAuthorizations = map[string](map[string]bool){
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/secret.html", PublicBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: true, UserHarry: true,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/secret.html", SecureBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: true, UserHarry: true,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/secret.html", AdminBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: false, UserHarry: false,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/secret.html", SingleFactorBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: true, UserHarry: true,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/secret.html", MX1MailBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: true, UserHarry: false,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/secret.html", MX2MailBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: false, UserBob: true, UserHarry: false,
|
|
|
|
},
|
|
|
|
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/groups/admin/secret.html", DevBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: false, UserHarry: false,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/groups/dev/secret.html", DevBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: true, UserHarry: false,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/users/john/secret.html", DevBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: false, UserHarry: false,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/users/harry/secret.html", DevBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: false, UserHarry: true,
|
|
|
|
},
|
2021-03-14 14:08:26 +07:00
|
|
|
fmt.Sprintf("%s/users/bob/secret.html", DevBaseURL): {
|
2019-11-25 03:27:59 +07:00
|
|
|
UserJohn: true, UserBob: true, UserHarry: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilityWebDriverSuite) TestShouldVerifyAccessControl() {
|
2020-04-09 08:05:17 +07:00
|
|
|
verifyUserIsAuthorized := func(ctx context.Context, t *testing.T, username, targetURL string, authorized bool) { //nolint:unparam
|
2019-11-25 03:27:59 +07:00
|
|
|
s.doVisit(t, targetURL)
|
|
|
|
s.verifyURLIs(ctx, t, targetURL)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
if authorized {
|
|
|
|
s.verifySecretAuthorized(ctx, t)
|
|
|
|
} else {
|
|
|
|
s.verifyBodyContains(ctx, t, "403 Forbidden")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyAuthorization := func(username string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2019-11-30 23:49:52 +07:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
2019-11-25 03:27:59 +07:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.doRegisterAndLogin2FA(ctx, t, username, "password", false, "")
|
|
|
|
|
|
|
|
for url, authorizations := range expectedAuthorizations {
|
2019-11-30 23:49:52 +07:00
|
|
|
t.Run(url, func(t *testing.T) {
|
|
|
|
verifyUserIsAuthorized(ctx, t, username, url, authorizations[username])
|
|
|
|
})
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
s.doLogout(ctx, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:05:17 +07:00
|
|
|
for _, user := range Users {
|
|
|
|
s.T().Run(user, verifyAuthorization(user))
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HighAvailabilitySuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
func NewHighAvailabilitySuite() *HighAvailabilitySuite {
|
2019-11-25 03:27:59 +07:00
|
|
|
return &HighAvailabilitySuite{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DoGetWithAuth(t *testing.T, username, password string) int {
|
|
|
|
client := NewHTTPClient()
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/secret.html", SingleFactorBaseURL), nil)
|
|
|
|
assert.NoError(t, err)
|
2021-03-14 14:08:26 +07:00
|
|
|
req.SetBasicAuth(username, password)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
|
assert.NoError(t, err)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
return res.StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilitySuite) TestBasicAuth() {
|
|
|
|
s.Assert().Equal(DoGetWithAuth(s.T(), "john", "password"), 200)
|
|
|
|
s.Assert().Equal(DoGetWithAuth(s.T(), "john", "bad-password"), 302)
|
|
|
|
s.Assert().Equal(DoGetWithAuth(s.T(), "dontexist", "password"), 302)
|
2019-12-06 04:35:03 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilitySuite) TestOneFactorScenario() {
|
|
|
|
suite.Run(s.T(), NewOneFactorScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilitySuite) TestTwoFactorScenario() {
|
|
|
|
suite.Run(s.T(), NewTwoFactorScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilitySuite) TestRegulationScenario() {
|
|
|
|
suite.Run(s.T(), NewRegulationScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilitySuite) TestCustomHeadersScenario() {
|
|
|
|
suite.Run(s.T(), NewCustomHeadersScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HighAvailabilitySuite) TestRedirectionCheckScenario() {
|
|
|
|
suite.Run(s.T(), NewRedirectionCheckScenario())
|
|
|
|
}
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2019-12-06 04:35:03 +07:00
|
|
|
func (s *HighAvailabilitySuite) TestHighAvailabilityWebDriverSuite() {
|
|
|
|
suite.Run(s.T(), NewHighAvailabilityWebDriverSuite())
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:40:42 +07:00
|
|
|
func TestHighAvailabilityWebDriverSuite(t *testing.T) {
|
2021-03-14 14:08:26 +07:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:40:42 +07:00
|
|
|
suite.Run(t, NewHighAvailabilityWebDriverSuite())
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
func TestHighAvailabilitySuite(t *testing.T) {
|
2021-03-14 14:08:26 +07:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
suite.Run(t, NewHighAvailabilitySuite())
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|