2022-09-11 23:16:40 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
existingSetupKeyID = "existingSetupKeyID"
|
|
|
|
newSetupKeyName = "New Setup Key"
|
|
|
|
updatedSetupKeyName = "KKKey"
|
|
|
|
notFoundSetupKeyID = "notFoundSetupKeyID"
|
|
|
|
)
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
func initSetupKeysTestMetaData(defaultKey *server.SetupKey, newKey *server.SetupKey, updatedSetupKey *server.SetupKey,
|
2023-02-03 21:47:20 +01:00
|
|
|
user *server.User,
|
2023-02-28 15:01:24 +01:00
|
|
|
) *SetupKeysHandler {
|
|
|
|
return &SetupKeysHandler{
|
2022-09-11 23:16:40 +02:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2022-11-11 20:36:45 +01:00
|
|
|
GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) {
|
2022-09-11 23:16:40 +02:00
|
|
|
return &server.Account{
|
|
|
|
Id: testAccountID,
|
|
|
|
Domain: "hotmail.com",
|
2022-11-05 10:24:50 +01:00
|
|
|
Users: map[string]*server.User{
|
|
|
|
user.Id: user,
|
|
|
|
},
|
2022-09-11 23:16:40 +02:00
|
|
|
SetupKeys: map[string]*server.SetupKey{
|
|
|
|
defaultKey.Key: defaultKey,
|
|
|
|
},
|
|
|
|
Groups: map[string]*server.Group{
|
|
|
|
"group-1": {ID: "group-1", Peers: []string{"A", "B"}},
|
2023-02-03 21:47:20 +01:00
|
|
|
"id-all": {ID: "id-all", Name: "All"},
|
|
|
|
},
|
2022-11-11 20:36:45 +01:00
|
|
|
}, user, nil
|
2022-09-11 23:16:40 +02:00
|
|
|
},
|
2022-12-05 13:09:59 +01:00
|
|
|
CreateSetupKeyFunc: func(_ string, keyName string, typ server.SetupKeyType, _ time.Duration, _ []string,
|
2023-02-03 21:47:20 +01:00
|
|
|
_ int, _ string,
|
|
|
|
) (*server.SetupKey, error) {
|
2022-09-11 23:16:40 +02:00
|
|
|
if keyName == newKey.Name || typ != newKey.Type {
|
|
|
|
return newKey, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("failed creating setup key")
|
|
|
|
},
|
2022-11-05 10:24:50 +01:00
|
|
|
GetSetupKeyFunc: func(accountID, userID, keyID string) (*server.SetupKey, error) {
|
2022-09-11 23:16:40 +02:00
|
|
|
switch keyID {
|
|
|
|
case defaultKey.Id:
|
|
|
|
return defaultKey, nil
|
|
|
|
case newKey.Id:
|
|
|
|
return newKey, nil
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "key %s not found", keyID)
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
SaveSetupKeyFunc: func(accountID string, key *server.SetupKey, _ string) (*server.SetupKey, error) {
|
2022-09-11 23:16:40 +02:00
|
|
|
if key.Id == updatedSetupKey.Id {
|
|
|
|
return updatedSetupKey, nil
|
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "key %s not found", key.Id)
|
2022-09-11 23:16:40 +02:00
|
|
|
},
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
ListSetupKeysFunc: func(accountID, userID string) ([]*server.SetupKey, error) {
|
2022-09-11 23:16:40 +02:00
|
|
|
return []*server.SetupKey{defaultKey}, nil
|
|
|
|
},
|
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2022-09-11 23:16:40 +02:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
2022-11-05 10:24:50 +01:00
|
|
|
UserId: user.Id,
|
2022-09-11 23:16:40 +02:00
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: testAccountID,
|
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupKeysHandlers(t *testing.T) {
|
|
|
|
defaultSetupKey := server.GenerateDefaultSetupKey()
|
|
|
|
defaultSetupKey.Id = existingSetupKeyID
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
adminUser := server.NewAdminUser("test_user")
|
|
|
|
|
2022-12-05 13:09:59 +01:00
|
|
|
newSetupKey := server.GenerateSetupKey(newSetupKeyName, server.SetupKeyReusable, 0, []string{"group-1"},
|
|
|
|
server.SetupKeyUnlimitedUsage)
|
2022-09-11 23:16:40 +02:00
|
|
|
updatedDefaultSetupKey := defaultSetupKey.Copy()
|
|
|
|
updatedDefaultSetupKey.AutoGroups = []string{"group-1"}
|
|
|
|
updatedDefaultSetupKey.Name = updatedSetupKeyName
|
|
|
|
updatedDefaultSetupKey.Revoked = true
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
expectedSetupKey *api.SetupKey
|
|
|
|
expectedSetupKeys []*api.SetupKey
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Get Setup Keys",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/setup-keys",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedSetupKeys: []*api.SetupKey{toResponseBody(defaultSetupKey)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get Existing Setup Key",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/setup-keys/" + existingSetupKeyID,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedSetupKey: toResponseBody(defaultSetupKey),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get Not Existing Setup Key",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/setup-keys/" + notFoundSetupKeyID,
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Setup Key",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/setup-keys",
|
|
|
|
requestBody: bytes.NewBuffer(
|
2023-08-04 23:54:51 +02:00
|
|
|
[]byte(fmt.Sprintf("{\"name\":\"%s\",\"type\":\"%s\",\"expires_in\":86400}", newSetupKey.Name, newSetupKey.Type))),
|
2022-09-11 23:16:40 +02:00
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedSetupKey: toResponseBody(newSetupKey),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Setup Key",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/setup-keys/" + defaultSetupKey.Id,
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(fmt.Sprintf("{\"name\":\"%s\",\"auto_groups\":[\"%s\"], \"revoked\":%v}",
|
|
|
|
updatedDefaultSetupKey.Type,
|
|
|
|
updatedDefaultSetupKey.AutoGroups[0],
|
|
|
|
updatedDefaultSetupKey.Revoked,
|
|
|
|
))),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedSetupKey: toResponseBody(updatedDefaultSetupKey),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
handler := initSetupKeysTestMetaData(defaultSetupKey, newSetupKey, updatedDefaultSetupKey, adminUser)
|
2022-09-11 23:16:40 +02:00
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2023-02-28 15:01:24 +01:00
|
|
|
router.HandleFunc("/api/setup-keys", handler.GetAllSetupKeys).Methods("GET", "OPTIONS")
|
|
|
|
router.HandleFunc("/api/setup-keys", handler.CreateSetupKey).Methods("POST", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
router.HandleFunc("/api/setup-keys/{keyId}", handler.GetSetupKey).Methods("GET", "OPTIONS")
|
|
|
|
router.HandleFunc("/api/setup-keys/{keyId}", handler.UpdateSetupKey).Methods("PUT", "OPTIONS")
|
2022-09-11 23:16:40 +02:00
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v, content: %s",
|
|
|
|
status, tc.expectedStatus, string(content))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.expectedSetupKey != nil {
|
|
|
|
got := &api.SetupKey{}
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
assertKeys(t, got, tc.expectedSetupKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tc.expectedSetupKeys) > 0 {
|
|
|
|
var got []*api.SetupKey
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
assertKeys(t, got[0], tc.expectedSetupKeys[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertKeys(t *testing.T, got *api.SetupKey, expected *api.SetupKey) {
|
|
|
|
// 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.WithinDurationf(t, got.UpdatedAt, expected.UpdatedAt, 0, "")
|
|
|
|
assert.WithinDurationf(t, got.Expires, expected.Expires, 0, "")
|
|
|
|
assert.Equal(t, got.Name, expected.Name)
|
|
|
|
assert.Equal(t, got.Id, expected.Id)
|
|
|
|
assert.Equal(t, got.Key, expected.Key)
|
|
|
|
assert.Equal(t, got.Type, expected.Type)
|
|
|
|
assert.Equal(t, got.UsedTimes, expected.UsedTimes)
|
|
|
|
assert.Equal(t, got.Revoked, expected.Revoked)
|
|
|
|
assert.ElementsMatch(t, got.AutoGroups, expected.AutoGroups)
|
|
|
|
}
|