feat: secure service connections and login
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package loginlimit_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/loginlimit"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/security/tlsconfig"
|
||||
)
|
||||
|
||||
func TestLimitIsSharedByInstances(t *testing.T) {
|
||||
rawURL := testCacheURL(t)
|
||||
tlsConfiguration := testCacheTLS(t)
|
||||
config := loginlimit.Config{AccountAttempts: 5, SourceAttempts: 20, Window: time.Minute}
|
||||
first, err := loginlimit.New(rawURL, config, tlsConfiguration)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, first.Close()) })
|
||||
second, err := loginlimit.New(rawURL, config, tlsConfiguration)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, second.Close()) })
|
||||
|
||||
username := "rate_" + uuid.NewString()
|
||||
limiters := []*loginlimit.Limiter{first, second}
|
||||
results := make(chan bool, 16)
|
||||
errors := make(chan error, 16)
|
||||
var group sync.WaitGroup
|
||||
for i := range 16 {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
allowed, retryAfter, err := limiters[i%len(limiters)].Allow(context.Background(), username, "192.0.2.10")
|
||||
if err != nil {
|
||||
errors <- err
|
||||
return
|
||||
}
|
||||
if !allowed && retryAfter <= 0 {
|
||||
errors <- loginlimit.ErrStore
|
||||
return
|
||||
}
|
||||
results <- allowed
|
||||
}()
|
||||
}
|
||||
group.Wait()
|
||||
close(results)
|
||||
close(errors)
|
||||
for err := range errors {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
var allowed int
|
||||
for result := range results {
|
||||
if result {
|
||||
allowed++
|
||||
}
|
||||
}
|
||||
require.Equal(t, int(config.AccountAttempts), allowed)
|
||||
}
|
||||
|
||||
func TestSourceLimitCoversDifferentAccounts(t *testing.T) {
|
||||
limiter, err := loginlimit.New(testCacheURL(t), loginlimit.Config{
|
||||
AccountAttempts: 10,
|
||||
SourceAttempts: 2,
|
||||
Window: time.Minute,
|
||||
}, testCacheTLS(t))
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, limiter.Close()) })
|
||||
|
||||
suffix := uuid.NewString()
|
||||
for i, expected := range []bool{true, true, false} {
|
||||
allowed, retryAfter, err := limiter.Allow(context.Background(), suffix+string(rune('a'+i)), "198.51.100.20")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, allowed)
|
||||
if !allowed {
|
||||
require.Positive(t, retryAfter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCacheTLS(t *testing.T) *tls.Config {
|
||||
t.Helper()
|
||||
configuration, err := tlsconfig.LoadClient(tlsconfig.ClientConfig{
|
||||
CAPath: os.Getenv("TEST_CACHE_TLS_CA_PATH"),
|
||||
CertificatePath: os.Getenv("TEST_CACHE_TLS_CERTIFICATE_PATH"),
|
||||
PrivateKeyPath: os.Getenv("TEST_CACHE_TLS_PRIVATE_KEY_PATH"),
|
||||
ServerName: os.Getenv("TEST_CACHE_TLS_SERVER_NAME"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return configuration
|
||||
}
|
||||
|
||||
func testCacheURL(t *testing.T) string {
|
||||
t.Helper()
|
||||
rawURL := strings.TrimSpace(os.Getenv("TEST_CACHE_URL"))
|
||||
if rawURL != "" {
|
||||
return rawURL
|
||||
}
|
||||
path := strings.TrimSpace(os.Getenv("TEST_CACHE_URL_FILE"))
|
||||
if path == "" {
|
||||
t.Skip("set TEST_CACHE_URL or TEST_CACHE_URL_FILE to run integration tests")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
require.NoError(t, err)
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user