Compare commits

..

No commits in common. "07fe16b72acdb29f70971da728f6815f898a6662" and "329d93a759180cb5ff446835a5740b6a7ffb816a" have entirely different histories.

7 changed files with 24 additions and 55 deletions

View File

@ -18,12 +18,11 @@ func TestServer(t *testing.T) {
server := ports.New(store) server := ports.New(store)
server.IP = "127.0.0.1" server.IP = "127.0.0.1"
server.AddTCPPort("2555") server.AddTCPPort("25")
go server.Start(ctx) go server.Start(ctx)
time.Sleep(1 * time.Second)
rAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(server.IP, "2555")) rAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(server.IP, "25"))
if err != nil { if err != nil {
t.Fatalf("Error resolving remote address: %s", err) t.Fatalf("Error resolving remote address: %s", err)
} }

View File

@ -2,8 +2,6 @@ package store
import "git.t-juice.club/torjus/apiary/models" import "git.t-juice.club/torjus/apiary/models"
var _ LoginAttemptStore = &CachingStore{}
type CachingStore struct { type CachingStore struct {
backend LoginAttemptStore backend LoginAttemptStore
@ -35,16 +33,14 @@ func NewCachingStore(backend LoginAttemptStore) *CachingStore {
//TODO: Handle better maybe? //TODO: Handle better maybe?
panic(err) panic(err)
} }
var loginCount int
for attempt := range all { cs.totalLoginsCount = len(all)
for _, attempt := range all {
cs.uniqueUsernames[attempt.Username] = struct{}{} cs.uniqueUsernames[attempt.Username] = struct{}{}
cs.uniquePasswords[attempt.Password] = struct{}{} cs.uniquePasswords[attempt.Password] = struct{}{}
cs.uniqueIPs[attempt.RemoteIP.String()] = struct{}{} cs.uniqueIPs[attempt.RemoteIP.String()] = struct{}{}
cs.uniqueCountries[attempt.Country] = struct{}{} cs.uniqueCountries[attempt.Country] = struct{}{}
loginCount++
} }
cs.totalLoginsCount = loginCount
return cs return cs
} }
@ -62,7 +58,7 @@ func (s *CachingStore) AddAttempt(l *models.LoginAttempt) error {
return s.backend.AddAttempt(l) return s.backend.AddAttempt(l)
} }
func (s *CachingStore) All() (<-chan models.LoginAttempt, error) { func (s *CachingStore) All() ([]models.LoginAttempt, error) {
return s.backend.All() return s.backend.All()
} }

View File

@ -9,8 +9,6 @@ import (
"git.t-juice.club/torjus/apiary/models" "git.t-juice.club/torjus/apiary/models"
) )
var _ LoginAttemptStore = &MemoryStore{}
type MemoryStore struct { type MemoryStore struct {
lock sync.RWMutex lock sync.RWMutex
attempts []models.LoginAttempt attempts []models.LoginAttempt
@ -34,17 +32,8 @@ func (ms *MemoryStore) AddAttempt(l *models.LoginAttempt) error {
return nil return nil
} }
func (ms *MemoryStore) All() (<-chan models.LoginAttempt, error) { func (ms *MemoryStore) All() ([]models.LoginAttempt, error) {
ch := make(chan models.LoginAttempt) return ms.attempts, nil
go func() {
ms.lock.RLock()
defer ms.lock.RUnlock()
for _, attempt := range ms.attempts {
ch <- attempt
}
close(ch)
}()
return ch, nil
} }
func (ms *MemoryStore) Stats(statType LoginStats, limit int) ([]StatsResult, error) { func (ms *MemoryStore) Stats(statType LoginStats, limit int) ([]StatsResult, error) {

View File

@ -8,8 +8,6 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
var _ LoginAttemptStore = &MetricsCollectingStore{}
const tickDuration = 5 * time.Second const tickDuration = 5 * time.Second
type MetricsCollectingStore struct { type MetricsCollectingStore struct {
@ -91,7 +89,7 @@ func (s *MetricsCollectingStore) AddAttempt(l *models.LoginAttempt) error {
return err return err
} }
func (s *MetricsCollectingStore) All() (<-chan models.LoginAttempt, error) { func (s *MetricsCollectingStore) All() ([]models.LoginAttempt, error) {
return s.store.All() return s.store.All()
} }

View File

@ -9,8 +9,6 @@ import (
_ "github.com/jackc/pgx/v4/stdlib" _ "github.com/jackc/pgx/v4/stdlib"
) )
var _ LoginAttemptStore = &PostgresStore{}
const DBSchema = ` const DBSchema = `
CREATE TABLE IF NOT EXISTS login_attempts( CREATE TABLE IF NOT EXISTS login_attempts(
id serial PRIMARY KEY, id serial PRIMARY KEY,
@ -63,30 +61,27 @@ RETURNING id;`
return tx.Commit() return tx.Commit()
} }
func (s *PostgresStore) All() (<-chan models.LoginAttempt, error) { func (s *PostgresStore) All() ([]models.LoginAttempt, error) {
stmt := `SELECT date, remote_ip, username, password, client_version, connection_uuid, country FROM login_attempts` stmt := `SELECT date, remote_ip, username, password, client_version, connection_uuid, country FROM login_attempts`
rows, err := s.db.Query(stmt) rows, err := s.db.Query(stmt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
ch := make(chan models.LoginAttempt) var attempts []models.LoginAttempt
go func() { for rows.Next() {
defer rows.Close() var a models.LoginAttempt
for rows.Next() { var ip string
var a models.LoginAttempt if err := rows.Scan(&a.Date, &ip, &a.Username, &a.Password, &a.SSHClientVersion, &a.SSHClientVersion, &a.Country); err != nil {
var ip string return nil, err
if err := rows.Scan(&a.Date, &ip, &a.Username, &a.Password, &a.SSHClientVersion, &a.SSHClientVersion, &a.Country); err != nil {
panic(err)
}
a.RemoteIP = net.ParseIP(ip)
ch <- a
} }
close(ch) a.RemoteIP = net.ParseIP(ip)
}() attempts = append(attempts, a)
}
return ch, nil return attempts, nil
} }
func (s *PostgresStore) Stats(statType LoginStats, limit int) ([]StatsResult, error) { func (s *PostgresStore) Stats(statType LoginStats, limit int) ([]StatsResult, error) {

View File

@ -38,7 +38,7 @@ type AttemptQuery struct {
} }
type LoginAttemptStore interface { type LoginAttemptStore interface {
AddAttempt(l *models.LoginAttempt) error AddAttempt(l *models.LoginAttempt) error
All() (<-chan models.LoginAttempt, error) All() ([]models.LoginAttempt, error)
Stats(statType LoginStats, limit int) ([]StatsResult, error) Stats(statType LoginStats, limit int) ([]StatsResult, error)
Query(query AttemptQuery) ([]models.LoginAttempt, error) Query(query AttemptQuery) ([]models.LoginAttempt, error)
HealthCheker HealthCheker

View File

@ -25,12 +25,8 @@ func testLoginAttemptStore(s store.LoginAttemptStore, t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error getting all attempts: %s", err) t.Fatalf("Error getting all attempts: %s", err)
} }
var count int if len(all) != len(testAttempts) {
for range all { t.Errorf("All returned wrong amount. Got %d want %d", len(all), len(testAttempts))
count++
}
if count != len(testAttempts) {
t.Errorf("All returned wrong amount. Got %d want %d", count, len(testAttempts))
} }
stats, err := s.Stats(store.LoginStatsTotals, 1) stats, err := s.Stats(store.LoginStatsTotals, 1)
if err != nil { if err != nil {
@ -233,11 +229,7 @@ func benchmarkLoginAttemptStore(setupFunc func() store.LoginAttemptStore, b *tes
if err != nil { if err != nil {
b.Fatalf("Error fetchin all: %s", err) b.Fatalf("Error fetchin all: %s", err)
} }
var count int _ = len(all)
for range all {
count++
}
_ = count
} }
}) })
} }