Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[management] Add integration test for the setup-keys API endpoints #2936

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
832e168
refactor jwtValidator and geo db to interfaces + first component test…
pascal-fischer Nov 22, 2024
72157b5
unexport event store and add peerUpdateManager
pascal-fischer Nov 22, 2024
bc6428c
wait if network map update is received and allow validating network m…
pascal-fischer Nov 22, 2024
0928382
add testdata
pascal-fischer Nov 22, 2024
5485b68
add more key creation tests
pascal-fischer Nov 22, 2024
bcec9b4
Merge branch 'refs/heads/main' into test/add-api-component-test
pascal-fischer Nov 22, 2024
b3f0959
add all setup keys tests
pascal-fischer Nov 22, 2024
dcbadd4
fix linter
pascal-fischer Nov 22, 2024
f2280ed
unexport impl structs
pascal-fischer Nov 22, 2024
426b38a
unexport impl structs
pascal-fischer Nov 22, 2024
63b000c
add different users
pascal-fischer Nov 25, 2024
4a1b179
rename geo mock
pascal-fischer Nov 25, 2024
ada2dd5
add benchmark tests for setup keys
pascal-fischer Nov 28, 2024
dfc429d
remove build tags
pascal-fischer Nov 28, 2024
7c3e4a1
fix get test expectation
pascal-fischer Nov 28, 2024
1e0ce80
fix error handling in metrics dummy
pascal-fischer Nov 28, 2024
e4bdb1e
fix time location
pascal-fischer Nov 28, 2024
0149e75
fix time location
pascal-fischer Nov 28, 2024
f389150
fix time location
pascal-fischer Nov 28, 2024
e47c9ed
Merge branch 'main' into test/add-api-component-test
pascal-fischer Nov 29, 2024
95be512
update expected benchmark timings
pascal-fischer Nov 29, 2024
74853be
Merge branch 'main' into test/add-api-component-test
pascal-fischer Nov 29, 2024
83d2af3
update expected benchmark timings
pascal-fischer Nov 29, 2024
49037a7
update expected benchmark timings
pascal-fischer Nov 29, 2024
a931fd7
update expected benchmark timings
pascal-fischer Nov 29, 2024
9c783e5
update expected benchmark timings
pascal-fischer Nov 29, 2024
06d18c0
add additional cases for other resources with high counts
pascal-fischer Nov 29, 2024
39f8eb7
update expected benchmark timings
pascal-fischer Nov 29, 2024
6e2471d
update expected benchmark timings
pascal-fischer Nov 29, 2024
0abb8ef
mark helper
pascal-fischer Nov 29, 2024
c6f12be
update expected benchmark timings
pascal-fischer Nov 29, 2024
65c99ee
update expected benchmark timings
pascal-fischer Nov 29, 2024
2dfe7ca
add dummy keys to delete
pascal-fischer Nov 29, 2024
430092c
fix operation
pascal-fischer Nov 29, 2024
ccff4b4
fix create test group
pascal-fischer Nov 29, 2024
798bddd
update expected benchmark timings
pascal-fischer Dec 1, 2024
86a9bc7
update expected benchmark timings
pascal-fischer Dec 1, 2024
b338429
update expected benchmark timings
pascal-fischer Dec 1, 2024
384bf51
update expected benchmark timings
pascal-fischer Dec 1, 2024
437d3da
update expected benchmark timings
pascal-fischer Dec 2, 2024
bb4696f
update expected benchmark timings
pascal-fischer Dec 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion management/cmd/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ var (
KeysLocation: config.HttpConfig.AuthKeysLocation,
}

httpAPIHandler, err := httpapi.APIHandler(ctx, accountManager, geo, *jwtValidator, appMetrics, httpAPIAuthCfg, integratedPeerValidator)
httpAPIHandler, err := httpapi.APIHandler(ctx, accountManager, geo, jwtValidator, appMetrics, httpAPIAuthCfg, integratedPeerValidator)
if err != nil {
return fmt.Errorf("failed creating HTTP API handler: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type DefaultAccountManager struct {
externalCacheManager ExternalCacheManager
ctx context.Context
eventStore activity.Store
geo *geolocation.Geolocation
geo geolocation.Geolocation

requestBuffer *AccountRequestBuffer

Expand Down Expand Up @@ -1041,7 +1041,7 @@ func BuildManager(
singleAccountModeDomain string,
dnsDomain string,
eventStore activity.Store,
geo *geolocation.Geolocation,
geo geolocation.Geolocation,
userDeleteFromIDPEnabled bool,
integratedPeerValidator integrated_validator.IntegratedValidator,
metrics telemetry.AppMetrics,
Expand Down
48 changes: 3 additions & 45 deletions management/server/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

nbdns "github.com/netbirdio/netbird/dns"
"github.com/netbirdio/netbird/management/server/account"
"github.com/netbirdio/netbird/management/server/activity"
"github.com/netbirdio/netbird/management/server/group"
"github.com/netbirdio/netbird/management/server/jwtclaims"
Expand All @@ -28,47 +27,6 @@ import (
"github.com/netbirdio/netbird/route"
)

type MocIntegratedValidator struct {
ValidatePeerFunc func(_ context.Context, update *nbpeer.Peer, peer *nbpeer.Peer, userID string, accountID string, dnsDomain string, peersGroup []string, extraSettings *account.ExtraSettings) (*nbpeer.Peer, bool, error)
}

func (a MocIntegratedValidator) ValidateExtraSettings(_ context.Context, newExtraSettings *account.ExtraSettings, oldExtraSettings *account.ExtraSettings, peers map[string]*nbpeer.Peer, userID string, accountID string) error {
return nil
}

func (a MocIntegratedValidator) ValidatePeer(_ context.Context, update *nbpeer.Peer, peer *nbpeer.Peer, userID string, accountID string, dnsDomain string, peersGroup []string, extraSettings *account.ExtraSettings) (*nbpeer.Peer, bool, error) {
if a.ValidatePeerFunc != nil {
return a.ValidatePeerFunc(context.Background(), update, peer, userID, accountID, dnsDomain, peersGroup, extraSettings)
}
return update, false, nil
}
func (a MocIntegratedValidator) GetValidatedPeers(accountID string, groups map[string]*group.Group, peers map[string]*nbpeer.Peer, extraSettings *account.ExtraSettings) (map[string]struct{}, error) {
validatedPeers := make(map[string]struct{})
for _, peer := range peers {
validatedPeers[peer.ID] = struct{}{}
}
return validatedPeers, nil
}

func (MocIntegratedValidator) PreparePeer(_ context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *account.ExtraSettings) *nbpeer.Peer {
return peer
}

func (MocIntegratedValidator) IsNotValidPeer(_ context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *account.ExtraSettings) (bool, bool, error) {
return false, false, nil
}

func (MocIntegratedValidator) PeerDeleted(_ context.Context, _, _ string) error {
return nil
}

func (MocIntegratedValidator) SetPeerInvalidationListener(func(accountID string)) {

}

func (MocIntegratedValidator) Stop(_ context.Context) {
}

func verifyCanAddPeerToAccount(t *testing.T, manager AccountManager, account *Account, userID string) {
t.Helper()
peer := &nbpeer.Peer{
Expand Down Expand Up @@ -1038,7 +996,7 @@ func BenchmarkTest_GetAccountWithclaims(b *testing.B) {
}

b.Run("public without account ID", func(b *testing.B) {
//b.ResetTimer()
// b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := am.getAccountIDWithAuthorizationClaims(context.Background(), publicClaims)
if err != nil {
Expand All @@ -1048,7 +1006,7 @@ func BenchmarkTest_GetAccountWithclaims(b *testing.B) {
})

b.Run("private without account ID", func(b *testing.B) {
//b.ResetTimer()
// b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := am.getAccountIDWithAuthorizationClaims(context.Background(), claims)
if err != nil {
Expand All @@ -1059,7 +1017,7 @@ func BenchmarkTest_GetAccountWithclaims(b *testing.B) {

b.Run("private with account ID", func(b *testing.B) {
claims.AccountId = id
//b.ResetTimer()
// b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := am.getAccountIDWithAuthorizationClaims(context.Background(), claims)
if err != nil {
Expand Down
39 changes: 32 additions & 7 deletions management/server/geolocation/geolocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
log "github.com/sirupsen/logrus"
)

type Geolocation struct {
type Geolocation interface {
Lookup(ip net.IP) (*Record, error)
GetAllCountries() ([]Country, error)
GetCitiesByCountry(countryISOCode string) ([]City, error)
Stop() error
}

type geolocationImpl struct {
mmdbPath string
mux sync.RWMutex
db *maxminddb.Reader
Expand Down Expand Up @@ -54,7 +61,7 @@
geonamesdbPattern = "geonames_*.db"
)

func NewGeolocation(ctx context.Context, dataDir string, autoUpdate bool) (*Geolocation, error) {
func NewGeolocation(ctx context.Context, dataDir string, autoUpdate bool) (Geolocation, error) {
mmdbGlobPattern := filepath.Join(dataDir, mmdbPattern)
mmdbFile, err := getDatabaseFilename(ctx, geoLiteCityTarGZURL, mmdbGlobPattern, autoUpdate)
if err != nil {
Expand Down Expand Up @@ -86,7 +93,7 @@
return nil, err
}

geo := &Geolocation{
geo := &geolocationImpl{
mmdbPath: mmdbPath,
mux: sync.RWMutex{},
db: db,
Expand All @@ -113,7 +120,7 @@
return db, nil
}

func (gl *Geolocation) Lookup(ip net.IP) (*Record, error) {
func (gl *geolocationImpl) Lookup(ip net.IP) (*Record, error) {
gl.mux.RLock()
defer gl.mux.RUnlock()

Expand All @@ -127,7 +134,7 @@
}

// GetAllCountries retrieves a list of all countries.
func (gl *Geolocation) GetAllCountries() ([]Country, error) {
func (gl *geolocationImpl) GetAllCountries() ([]Country, error) {
allCountries, err := gl.locationDB.GetAllCountries()
if err != nil {
return nil, err
Expand All @@ -143,7 +150,7 @@
}

// GetCitiesByCountry retrieves a list of cities in a specific country based on the country's ISO code.
func (gl *Geolocation) GetCitiesByCountry(countryISOCode string) ([]City, error) {
func (gl *geolocationImpl) GetCitiesByCountry(countryISOCode string) ([]City, error) {
allCities, err := gl.locationDB.GetCitiesByCountry(countryISOCode)
if err != nil {
return nil, err
Expand All @@ -158,7 +165,7 @@
return cities, nil
}

func (gl *Geolocation) Stop() error {
func (gl *geolocationImpl) Stop() error {
close(gl.stopCh)
if gl.db != nil {
if err := gl.db.Close(); err != nil {
Expand Down Expand Up @@ -259,3 +266,21 @@
}
return nil
}

type GeolocationMock struct{}

Check warning on line 270 in management/server/geolocation/geolocation.go

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

exported: type name will be used as geolocation.GeolocationMock by other packages, and that is repetitive; consider calling this Mock (revive)

func (g *GeolocationMock) Lookup(ip net.IP) (*Record, error) {
return &Record{}, nil
}

func (g *GeolocationMock) GetAllCountries() ([]Country, error) {
return []Country{}, nil
}

func (g *GeolocationMock) GetCitiesByCountry(countryISOCode string) ([]City, error) {
return []City{}, nil
}

func (g *GeolocationMock) Stop() error {
return nil
}
2 changes: 1 addition & 1 deletion management/server/geolocation/geolocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGeoLite_Lookup(t *testing.T) {
db, err := openDB(filename)
assert.NoError(t, err)

geo := &Geolocation{
geo := &geolocationImpl{
mux: sync.RWMutex{},
db: db,
stopCh: make(chan struct{}),
Expand Down
4 changes: 2 additions & 2 deletions management/server/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type GRPCServer struct {
peersUpdateManager *PeersUpdateManager
config *Config
secretsManager SecretsManager
jwtValidator *jwtclaims.JWTValidator
jwtValidator jwtclaims.JWTValidator
jwtClaimsExtractor *jwtclaims.ClaimsExtractor
appMetrics telemetry.AppMetrics
ephemeralManager *EphemeralManager
Expand All @@ -57,7 +57,7 @@ func NewServer(
return nil, err
}

var jwtValidator *jwtclaims.JWTValidator
var jwtValidator jwtclaims.JWTValidator

if config.HttpConfig != nil && config.HttpConfig.AuthIssuer != "" && config.HttpConfig.AuthAudience != "" && validateURL(config.HttpConfig.AuthKeysLocation) {
jwtValidator, err = jwtclaims.NewJWTValidator(
Expand Down
4 changes: 2 additions & 2 deletions management/server/http/geolocations_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var (
// GeolocationsHandler is a handler that returns locations.
type GeolocationsHandler struct {
accountManager server.AccountManager
geolocationManager *geolocation.Geolocation
geolocationManager geolocation.Geolocation
claimsExtractor *jwtclaims.ClaimsExtractor
}

// NewGeolocationsHandlerHandler creates a new Geolocations handler
func NewGeolocationsHandlerHandler(accountManager server.AccountManager, geolocationManager *geolocation.Geolocation, authCfg AuthCfg) *GeolocationsHandler {
func NewGeolocationsHandlerHandler(accountManager server.AccountManager, geolocationManager geolocation.Geolocation, authCfg AuthCfg) *GeolocationsHandler {
return &GeolocationsHandler{
accountManager: accountManager,
geolocationManager: geolocationManager,
Expand Down
4 changes: 2 additions & 2 deletions management/server/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type AuthCfg struct {
type apiHandler struct {
Router *mux.Router
AccountManager s.AccountManager
geolocationManager *geolocation.Geolocation
geolocationManager geolocation.Geolocation
AuthCfg AuthCfg
}

Expand All @@ -40,7 +40,7 @@ type emptyObject struct {
}

// APIHandler creates the Management service HTTP API handler registering all the available endpoints.
func APIHandler(ctx context.Context, accountManager s.AccountManager, LocationManager *geolocation.Geolocation, jwtValidator jwtclaims.JWTValidator, appMetrics telemetry.AppMetrics, authCfg AuthCfg, integratedValidator integrated_validator.IntegratedValidator) (http.Handler, error) {
func APIHandler(ctx context.Context, accountManager s.AccountManager, LocationManager geolocation.Geolocation, jwtValidator jwtclaims.JWTValidator, appMetrics telemetry.AppMetrics, authCfg AuthCfg, integratedValidator integrated_validator.IntegratedValidator) (http.Handler, error) {
claimsExtractor := jwtclaims.NewClaimsExtractor(
jwtclaims.WithAudience(authCfg.Audience),
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
Expand Down
4 changes: 2 additions & 2 deletions management/server/http/posture_checks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
// PostureChecksHandler is a handler that returns posture checks of the account.
type PostureChecksHandler struct {
accountManager server.AccountManager
geolocationManager *geolocation.Geolocation
geolocationManager geolocation.Geolocation
claimsExtractor *jwtclaims.ClaimsExtractor
}

// NewPostureChecksHandler creates a new PostureChecks handler
func NewPostureChecksHandler(accountManager server.AccountManager, geolocationManager *geolocation.Geolocation, authCfg AuthCfg) *PostureChecksHandler {
func NewPostureChecksHandler(accountManager server.AccountManager, geolocationManager geolocation.Geolocation, authCfg AuthCfg) *PostureChecksHandler {
return &PostureChecksHandler{
accountManager: accountManager,
geolocationManager: geolocationManager,
Expand Down
2 changes: 1 addition & 1 deletion management/server/http/posture_checks_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
return claims.AccountId, claims.UserId, nil
},
},
geolocationManager: &geolocation.Geolocation{},
geolocationManager: &geolocation.geolocationImpl{},

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

undefined: geolocation.geolocationImpl

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: geolocation.geolocationImpl (typecheck)

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: geolocation.geolocationImpl (typecheck)

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: geolocation.geolocationImpl

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

undefined: geolocation.geolocationImpl

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / test (386, postgres)

undefined: geolocation.geolocationImpl

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

undefined: geolocation.geolocationImpl

Check failure on line 73 in management/server/http/posture_checks_handler_test.go

View workflow job for this annotation

GitHub Actions / test (amd64, postgres)

undefined: geolocation.geolocationImpl
claimsExtractor: jwtclaims.NewClaimsExtractor(
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
return jwtclaims.AuthorizationClaims{
Expand Down
2 changes: 1 addition & 1 deletion management/server/http/setupkeys_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestSetupKeysHandlers(t *testing.T) {
func assertKeys(t *testing.T, got *api.SetupKey, expected *api.SetupKey) {
t.Helper()
// this comparison is done manually because when converting to JSON dates formatted differently
// assert.Equal(t, got.UpdatedAt, tc.expectedSetupKey.UpdatedAt) //doesn't work
// assert.Equal(t, got.UpdatedAt, tc.expectedResponse.UpdatedAt) //doesn't work
assert.WithinDurationf(t, got.UpdatedAt, expected.UpdatedAt, 0, "")
assert.WithinDurationf(t, got.Expires, expected.Expires, 0, "")
assert.Equal(t, got.Name, expected.Name)
Expand Down
Loading
Loading