Skip to content

Commit

Permalink
refactor memorystorage, add goroutine for logratation)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Sep 9, 2024
1 parent bdff757 commit 81ce9d5
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 151 deletions.
4 changes: 2 additions & 2 deletions pkg/auth/oidc/store/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/in4it/wireguard-server/pkg/auth/oidc"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGetDiscovery(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestGetDiscovery(t *testing.T) {
}))
defer ts.Close()

store, err := NewStore(&testingmocks.MockMemoryStorage{})
store, err := NewStore(&memorystorage.MockMemoryStorage{})
if err != nil {
t.Fatalf("new store error: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/oidc/store/jwks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/in4it/wireguard-server/pkg/auth/oidc"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGetJwks(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestGetJwks(t *testing.T) {
}))
defer ts.Close()

store, err := NewStore(&testingmocks.MockMemoryStorage{})
store, err := NewStore(&memorystorage.MockMemoryStorage{})
if err != nil {
t.Fatalf("new store error: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/oidc/store/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"testing"

"github.com/in4it/wireguard-server/pkg/auth/oidc"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestSave(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
store, err := NewStore(storage)
if err != nil {
t.Fatalf("error: %s", err)
Expand Down
18 changes: 9 additions & 9 deletions pkg/auth/provisioning/scim/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"path"
"testing"

testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
"github.com/in4it/wireguard-server/pkg/users"
"github.com/in4it/wireguard-server/pkg/wireguard"
)

const USERSTORE_MAX_USERS = 1000

func TestUsersGetCount100EmptyResult(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestUsersGetCount100EmptyResult(t *testing.T) {
}

func TestUsersGetCount10(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store")
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestUsersGetCount10(t *testing.T) {
func TestUsersGetCount10Start5(t *testing.T) {
count := 10
start := 5
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store")
Expand Down Expand Up @@ -138,12 +138,12 @@ func TestUsersGetCount10Start5(t *testing.T) {
}

func TestUsersGetNonExistentUser(t *testing.T) {
userStore, err := users.NewUserStore(&testingmocks.MockMemoryStorage{}, USERSTORE_MAX_USERS)
userStore, err := users.NewUserStore(&memorystorage.MockMemoryStorage{}, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user stoer")
}

s := New(&testingmocks.MockMemoryStorage{}, userStore, "token")
s := New(&memorystorage.MockMemoryStorage{}, userStore, "token")
req := httptest.NewRequest("GET", "http://example.com/api/scim/v2/Users?filter=userName+eq+%22ward%40in4it.io%22&", nil)
w := httptest.NewRecorder()
s.getUsersHandler(w, req)
Expand All @@ -161,7 +161,7 @@ func TestUsersGetNonExistentUser(t *testing.T) {
}

func TestAddUser(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store: %s", err)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestAddUser(t *testing.T) {
}

func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store: %s", err)
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {
}
}
func TestCreateUserConnectionSuspendUserFlow(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/configmanager/start_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ func startStats(storage storage.Iface) {

func startPacketLogger(storage storage.Iface, clientCache *wireguard.ClientCache, vpnConfig *wireguard.VPNConfig) {
go wireguard.RunPacketLogger(storage, clientCache, vpnConfig)
// run cleanup
go wireguard.PacketLoggerLogRotation(storage)
}
2 changes: 2 additions & 0 deletions pkg/configmanager/start_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ func startStats(storage storage.Iface) {
func startPacketLogger(storage storage.Iface, clientCache *wireguard.ClientCache, vpnConfig *wireguard.VPNConfig) {
// run statistics go routine
go wireguard.RunPacketLogger(storage, clientCache, vpnConfig)
// run cleanup
go wireguard.PacketLoggerLogRotation(storage)
}
10 changes: 5 additions & 5 deletions pkg/license/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGuessInfrastructureGCP(t *testing.T) {
Expand Down Expand Up @@ -52,10 +52,10 @@ func TestGetMaxUsersGCPBYOL(t *testing.T) {
licenseURL = ts.URL
metadataIP = strings.Replace(ts.URL, "http://", "", -1)

mockStorage := &testingmocks.MockReadWriter{
Data: map[string][]byte{
"config/license.key": []byte("license-1234556-license"),
},
mockStorage := &memorystorage.MockMemoryStorage{}
err := mockStorage.WriteFile("config/license.key", []byte("license-1234556-license"))
if err != nil {
t.Fatalf("writefile error: %s", err)
}

for _, v := range []int{50} {
Expand Down
33 changes: 16 additions & 17 deletions pkg/license/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/in4it/wireguard-server/pkg/logging"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGetMaxUsersAzure(t *testing.T) {
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestGetMaxUsersAWSMarketplace(t *testing.T) {
"t3.xlarge": 250,
}
for instanceType, v := range testCases {
if getMaxUsers(&testingmocks.MockReadWriter{}, "aws-marketplace", instanceType) != v {
if getMaxUsers(&memorystorage.MockMemoryStorage{}, "aws-marketplace", instanceType) != v {
t.Fatalf("Wrong output: %d vs %d", GetMaxUsersAWS(instanceType), v)
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestGetMaxUsersAWSBYOL(t *testing.T) {
licenseURL = ts.URL
metadataIP = strings.Replace(ts.URL, "http://", "", -1)
for _, v := range testCases {
if v2 := GetMaxUsersAWSBYOL(http.Client{Timeout: 5 * time.Second}, &testingmocks.MockMemoryStorage{}); v2 != v {
if v2 := GetMaxUsersAWSBYOL(http.Client{Timeout: 5 * time.Second}, &memorystorage.MockMemoryStorage{}); v2 != v {
t.Fatalf("Wrong output: %d vs %d", v2, v)
}
}
Expand All @@ -111,7 +111,7 @@ func TestGetMaxUsersAWS(t *testing.T) {
"t3.xlarge": 3,
}
for instanceType, v := range testCases {
if getMaxUsers(&testingmocks.MockReadWriter{}, "aws", instanceType) != v {
if getMaxUsers(&memorystorage.MockMemoryStorage{}, "aws", instanceType) != v {
t.Fatalf("Wrong output: %d vs %d", GetMaxUsersAWS(instanceType), v)
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestGuessInfrastructureAWS(t *testing.T) {
t.Fatalf("wrong infra returned: %s", infra)
}

if getMaxUsers(&testingmocks.MockReadWriter{}, infra, "t3.large") != 3 {
if getMaxUsers(&memorystorage.MockMemoryStorage{}, infra, "t3.large") != 3 {
t.Fatalf("wrong users returned")
}
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestGetAzureInstanceType(t *testing.T) {

usersPerVCPU := 25

users := getMaxUsers(&testingmocks.MockReadWriter{}, "azure", getAzureInstanceType(http.Client{Timeout: 5 * time.Second}))
users := getMaxUsers(&memorystorage.MockMemoryStorage{}, "azure", getAzureInstanceType(http.Client{Timeout: 5 * time.Second}))

if users != usersPerVCPU*2 {
t.Fatalf("Wrong user count returned")
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestGuessInfrastructureDigitalOcean(t *testing.T) {
t.Fatalf("wrong infra returned: %s", infra)
}

if getMaxUsers(&testingmocks.MockReadWriter{}, infra, "t3.large") != 3 {
if getMaxUsers(&memorystorage.MockMemoryStorage{}, infra, "t3.large") != 3 {
t.Fatalf("wrong users returned")
}
}
Expand Down Expand Up @@ -332,12 +332,11 @@ func TestGetMaxUsersDigitalOceanBYOL(t *testing.T) {
licenseURL = ts.URL
metadataIP = strings.Replace(ts.URL, "http://", "", -1)

mockStorage := &testingmocks.MockReadWriter{
Data: map[string][]byte{
"config/license.key": []byte("license-1234556-license"),
},
mockStorage := &memorystorage.MockMemoryStorage{}
err := mockStorage.WriteFile("config/license.key", []byte("license-1234556-license"))
if err != nil {
t.Fatalf("writefile error: %s", err)
}

for _, v := range []int{50} {
if v2 := GetMaxUsersDigitalOceanBYOL(http.Client{Timeout: 5 * time.Second}, mockStorage); v2 != v {
t.Fatalf("Wrong output: %d vs %d", v2, v)
Expand Down Expand Up @@ -387,27 +386,27 @@ func TestGetLicenseKey(t *testing.T) {
metadataIP = strings.Replace(ts.URL, "http://", "", -1)

logging.Loglevel = logging.LOG_DEBUG + logging.LOG_ERROR
key := GetLicenseKey(&testingmocks.MockMemoryStorage{}, "")
key := GetLicenseKey(&memorystorage.MockMemoryStorage{}, "")
if key == "" {
t.Fatalf("key is empty")
}
key = GetLicenseKey(&testingmocks.MockMemoryStorage{}, "aws")
key = GetLicenseKey(&memorystorage.MockMemoryStorage{}, "aws")
if key == "" {
t.Fatalf("aws key is empty")
}
key = GetLicenseKey(&testingmocks.MockMemoryStorage{}, "digitalocean")
key = GetLicenseKey(&memorystorage.MockMemoryStorage{}, "digitalocean")
if key == "" {
t.Fatalf("digitalocean key is empty")
}
key = GetLicenseKey(&testingmocks.MockMemoryStorage{}, "gcp")
key = GetLicenseKey(&memorystorage.MockMemoryStorage{}, "gcp")
if key == "" {
t.Fatalf("gcp key is empty")
}
}
func TestGetLicenseKeyNoCloudProvider(t *testing.T) {

logging.Loglevel = logging.LOG_DEBUG + logging.LOG_ERROR
key := GetLicenseKey(&testingmocks.MockMemoryStorage{}, "")
key := GetLicenseKey(&memorystorage.MockMemoryStorage{}, "")
if key == "" {
t.Fatalf("key is empty")
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/rest/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/in4it/wireguard-server/pkg/auth/saml"
"github.com/in4it/wireguard-server/pkg/logging"
"github.com/in4it/wireguard-server/pkg/rest/login"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
"github.com/in4it/wireguard-server/pkg/users"
"github.com/russellhaering/gosaml2/types"
dsigtypes "github.com/russellhaering/goxmldsig/types"
Expand Down Expand Up @@ -80,7 +80,7 @@ c7tL1QjbfAUHAQYwmHkWgPP+T2wAv0pOt36GgMCM`
}

func TestAuthHandler(t *testing.T) {
c, err := newContext(&testingmocks.MockMemoryStorage{}, SERVER_TYPE_VPN)
c, err := newContext(&memorystorage.MockMemoryStorage{}, SERVER_TYPE_VPN)
if err != nil {
t.Fatalf("Cannot create context: %s", err)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestAuthHandler(t *testing.T) {

func TestNewSAMLConnection(t *testing.T) {
// generate new keypair
kp := saml.NewKeyPair(&testingmocks.MockMemoryStorage{}, "www.idp.inv")
kp := saml.NewKeyPair(&memorystorage.MockMemoryStorage{}, "www.idp.inv")
_, cert, err := kp.GetKeyPair()
if err != nil {
t.Fatalf("Can't generate new keypair: %s", err)
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestNewSAMLConnection(t *testing.T) {
defer l.Close()

// first create a new user
c, err := newContext(&testingmocks.MockMemoryStorage{}, SERVER_TYPE_VPN)
c, err := newContext(&memorystorage.MockMemoryStorage{}, SERVER_TYPE_VPN)
if err != nil {
t.Fatalf("Cannot create context")
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func TestNewSAMLConnection(t *testing.T) {

}
func TestAddModifyDeleteNewSAMLConnection(t *testing.T) {
c, err := newContext(&testingmocks.MockMemoryStorage{}, SERVER_TYPE_VPN)
c, err := newContext(&memorystorage.MockMemoryStorage{}, SERVER_TYPE_VPN)
if err != nil {
t.Fatalf("Cannot create context")
}
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestAddModifyDeleteNewSAMLConnection(t *testing.T) {
}

func TestSAMLCallback(t *testing.T) {
c, err := newContext(&testingmocks.MockMemoryStorage{}, SERVER_TYPE_VPN)
c, err := newContext(&memorystorage.MockMemoryStorage{}, SERVER_TYPE_VPN)
if err != nil {
t.Fatalf("Cannot create context")
}
Expand Down Expand Up @@ -701,7 +701,7 @@ func TestOIDCFlow(t *testing.T) {
}

// first create a new user
c, err := newContext(&testingmocks.MockMemoryStorage{}, SERVER_TYPE_VPN)
c, err := newContext(&memorystorage.MockMemoryStorage{}, SERVER_TYPE_VPN)
if err != nil {
t.Fatalf("Cannot create context")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/rest/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"encoding/pem"
"testing"

testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGetJWTKeys(t *testing.T) {
mockStorage := testingmocks.MockMemoryStorage{}
mockStorage := memorystorage.MockMemoryStorage{}
keys, err := getJWTKeys(&mockStorage)
if err != nil {
t.Fatalf("error: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/rest/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"testing"
"time"

testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
"github.com/in4it/wireguard-server/pkg/wireguard"
)

func TestUserStatsHandler(t *testing.T) {

storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

c, err := newContext(storage, SERVER_TYPE_VPN)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/rest/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"testing"

testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
"github.com/in4it/wireguard-server/pkg/users"
"github.com/in4it/wireguard-server/pkg/wireguard"
)
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {
defer l.Close()

// first create a new user
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

c, err := newContext(storage, SERVER_TYPE_VPN)
if err != nil {
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {

func TestCreateUser(t *testing.T) {
// first create a new user
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

c, err := newContext(storage, SERVER_TYPE_VPN)
if err != nil {
Expand Down
Loading

0 comments on commit 81ce9d5

Please sign in to comment.