mirror of
https://github.com/netbirdio/netbird.git
synced 2025-08-19 11:20:18 +02:00
[management] expect specific error types on registration with setup key (#4094)
This commit is contained in:
@@ -488,7 +488,7 @@ func (am *DefaultAccountManager) AddPeer(ctx context.Context, setupKey, userID s
|
|||||||
if addedByUser {
|
if addedByUser {
|
||||||
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthNone, userID)
|
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthNone, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, fmt.Errorf("failed to get user groups: %w", err)
|
return nil, nil, nil, status.Errorf(status.NotFound, "failed adding new peer: user not found")
|
||||||
}
|
}
|
||||||
groupsToAdd = user.AutoGroups
|
groupsToAdd = user.AutoGroups
|
||||||
opEvent.InitiatorID = userID
|
opEvent.InitiatorID = userID
|
||||||
@@ -498,12 +498,12 @@ func (am *DefaultAccountManager) AddPeer(ctx context.Context, setupKey, userID s
|
|||||||
// Validate the setup key
|
// Validate the setup key
|
||||||
sk, err := am.Store.GetSetupKeyBySecret(ctx, store.LockingStrengthNone, encodedHashedKey)
|
sk, err := am.Store.GetSetupKeyBySecret(ctx, store.LockingStrengthNone, encodedHashedKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, fmt.Errorf("failed to get setup key: %w", err)
|
return nil, nil, nil, status.Errorf(status.NotFound, "couldn't add peer: setup key is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
// we will check key twice for early return
|
// we will check key twice for early return
|
||||||
if !sk.IsValid() {
|
if !sk.IsValid() {
|
||||||
return nil, nil, nil, status.Errorf(status.PreconditionFailed, "couldn't add peer: setup key is invalid")
|
return nil, nil, nil, status.Errorf(status.NotFound, "couldn't add peer: setup key is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
opEvent.InitiatorID = sk.Id
|
opEvent.InitiatorID = sk.Id
|
||||||
|
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/netbirdio/netbird/management/server/integrations/port_forwarding"
|
"github.com/netbirdio/netbird/management/server/integrations/port_forwarding"
|
||||||
"github.com/netbirdio/netbird/management/server/permissions"
|
"github.com/netbirdio/netbird/management/server/permissions"
|
||||||
"github.com/netbirdio/netbird/management/server/settings"
|
"github.com/netbirdio/netbird/management/server/settings"
|
||||||
|
"github.com/netbirdio/netbird/management/server/status"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/management/server/util"
|
"github.com/netbirdio/netbird/management/server/util"
|
||||||
|
|
||||||
@@ -1376,6 +1377,7 @@ func Test_RegisterPeerBySetupKey(t *testing.T) {
|
|||||||
existingSetupKeyID string
|
existingSetupKeyID string
|
||||||
expectedGroupIDsInAccount []string
|
expectedGroupIDsInAccount []string
|
||||||
expectAddPeerError bool
|
expectAddPeerError bool
|
||||||
|
errorType status.Type
|
||||||
expectedErrorMsgSubstring string
|
expectedErrorMsgSubstring string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -1388,13 +1390,15 @@ func Test_RegisterPeerBySetupKey(t *testing.T) {
|
|||||||
name: "Failed registration with setup key not allowing extra DNS labels",
|
name: "Failed registration with setup key not allowing extra DNS labels",
|
||||||
existingSetupKeyID: "A2C8E62B-38F5-4553-B31E-DD66C696CEBB",
|
existingSetupKeyID: "A2C8E62B-38F5-4553-B31E-DD66C696CEBB",
|
||||||
expectAddPeerError: true,
|
expectAddPeerError: true,
|
||||||
|
errorType: status.PreconditionFailed,
|
||||||
expectedErrorMsgSubstring: "setup key doesn't allow extra DNS labels",
|
expectedErrorMsgSubstring: "setup key doesn't allow extra DNS labels",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Absent setup key",
|
name: "Absent setup key",
|
||||||
existingSetupKeyID: "AAAAAAAA-38F5-4553-B31E-DD66C696CEBB",
|
existingSetupKeyID: "AAAAAAAA-38F5-4553-B31E-DD66C696CEBB",
|
||||||
expectAddPeerError: true,
|
expectAddPeerError: true,
|
||||||
expectedErrorMsgSubstring: "failed to get setup key: setup key not found",
|
errorType: status.NotFound,
|
||||||
|
expectedErrorMsgSubstring: "couldn't add peer: setup key is invalid",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1419,6 +1423,11 @@ func Test_RegisterPeerBySetupKey(t *testing.T) {
|
|||||||
if tc.expectAddPeerError {
|
if tc.expectAddPeerError {
|
||||||
require.Error(t, err, "Expected an error when adding peer with setup key: %s", tc.existingSetupKeyID)
|
require.Error(t, err, "Expected an error when adding peer with setup key: %s", tc.existingSetupKeyID)
|
||||||
assert.Contains(t, err.Error(), tc.expectedErrorMsgSubstring, "Error message mismatch")
|
assert.Contains(t, err.Error(), tc.expectedErrorMsgSubstring, "Error message mismatch")
|
||||||
|
e, ok := status.FromError(err)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("Failed to map error")
|
||||||
|
}
|
||||||
|
assert.Equal(t, e.Type(), tc.errorType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user