2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-03 16:02:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-05-03 16:02:51 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// Groups is a handler that returns groups of the account
|
|
|
|
type Groups struct {
|
|
|
|
jwtExtractor jwtclaims.ClaimsExtractor
|
|
|
|
accountManager server.AccountManager
|
|
|
|
authAudience string
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
func NewGroups(accountManager server.AccountManager, authAudience string) *Groups {
|
|
|
|
return &Groups{
|
|
|
|
accountManager: accountManager,
|
|
|
|
authAudience: authAudience,
|
|
|
|
jwtExtractor: *jwtclaims.NewClaimsExtractor(nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllGroupsHandler list for the account
|
|
|
|
func (h *Groups) GetAllGroupsHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
var groups []*api.Group
|
2022-05-21 15:21:39 +02:00
|
|
|
for _, g := range account.Groups {
|
|
|
|
groups = append(groups, toGroupResponse(account, g))
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, groups)
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
// UpdateGroupHandler handles update to a group identified by a given ID
|
|
|
|
func (h *Groups) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
groupID, ok := vars["id"]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "group ID field is missing"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(groupID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "group ID can't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = account.Groups[groupID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "couldn't find group with ID %s", groupID), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allGroup, err := account.GetGroupAll()
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if allGroup.ID == groupID {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "updating group ALL is not allowed"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req api.PutApiGroupsIdJSONRequestBody
|
2022-11-11 20:36:45 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if *req.Name == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "group name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
group := server.Group{
|
|
|
|
ID: groupID,
|
|
|
|
Name: *req.Name,
|
|
|
|
Peers: peerIPsToKeys(account, req.Peers),
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
if err := h.accountManager.SaveGroup(account.Id, user.Id, &group); err != nil {
|
2022-06-14 10:32:54 +02:00
|
|
|
log.Errorf("failed updating group %s under account %s %v", groupID, account.Id, err)
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, toGroupResponse(account, &group))
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchGroupHandler handles patch updates to a group identified by a given ID
|
|
|
|
func (h *Groups) PatchGroupHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
groupID := vars["id"]
|
|
|
|
if len(groupID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid group ID"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := account.Groups[groupID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "couldn't find group ID %s", groupID), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allGroup, err := account.GetGroupAll()
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if allGroup.ID == groupID {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "updating group ALL is not allowed"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req api.PatchApiGroupsIdJSONRequestBody
|
2022-11-11 20:36:45 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "no patch instruction received"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var operations []server.GroupUpdateOperation
|
|
|
|
|
|
|
|
for _, patch := range req {
|
|
|
|
switch patch.Path {
|
|
|
|
case api.GroupPatchOperationPathName:
|
|
|
|
if patch.Op != api.GroupPatchOperationOpReplace {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"name field only accepts replace operation, got %s", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(patch.Value) == 0 || patch.Value[0] == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "group name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
operations = append(operations, server.GroupUpdateOperation{
|
|
|
|
Type: server.UpdateGroupName,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.GroupPatchOperationPathPeers:
|
|
|
|
switch patch.Op {
|
|
|
|
case api.GroupPatchOperationOpReplace:
|
|
|
|
peerKeys := peerIPsToKeys(account, &patch.Value)
|
|
|
|
operations = append(operations, server.GroupUpdateOperation{
|
|
|
|
Type: server.UpdateGroupPeers,
|
|
|
|
Values: peerKeys,
|
|
|
|
})
|
|
|
|
case api.GroupPatchOperationOpRemove:
|
|
|
|
peerKeys := peerIPsToKeys(account, &patch.Value)
|
|
|
|
operations = append(operations, server.GroupUpdateOperation{
|
|
|
|
Type: server.RemovePeersFromGroup,
|
|
|
|
Values: peerKeys,
|
|
|
|
})
|
|
|
|
case api.GroupPatchOperationOpAdd:
|
|
|
|
peerKeys := peerIPsToKeys(account, &patch.Value)
|
|
|
|
operations = append(operations, server.GroupUpdateOperation{
|
|
|
|
Type: server.InsertPeersToGroup,
|
|
|
|
Values: peerKeys,
|
|
|
|
})
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"invalid operation, \"%v\", for Peers field", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid patch path"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := h.accountManager.UpdateGroup(account.Id, groupID, operations)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, toGroupResponse(account, group))
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateGroupHandler handles group creation request
|
|
|
|
func (h *Groups) CreateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
var req api.PostApiGroupsJSONRequestBody
|
2022-11-11 20:36:45 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
if req.Name == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "group name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
group := server.Group{
|
2022-06-14 10:32:54 +02:00
|
|
|
ID: xid.New().String(),
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: req.Name,
|
2022-06-14 10:32:54 +02:00
|
|
|
Peers: peerIPsToKeys(account, req.Peers),
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
err = h.accountManager.SaveGroup(account.Id, user.Id, &group)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, toGroupResponse(account, &group))
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
// DeleteGroupHandler handles group deletion request
|
2022-05-03 16:02:51 +02:00
|
|
|
func (h *Groups) DeleteGroupHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
aID := account.Id
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
groupID := mux.Vars(r)["id"]
|
|
|
|
if len(groupID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid group ID"), w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
allGroup, err := account.GetGroupAll()
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if allGroup.ID == groupID {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "deleting group ALL is not allowed"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
err = h.accountManager.DeleteGroup(aID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, "")
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
// GetGroupHandler returns a group
|
2022-05-03 16:02:51 +02:00
|
|
|
func (h *Groups) GetGroupHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
groupID := mux.Vars(r)["id"]
|
|
|
|
if len(groupID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid group ID"), w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := h.accountManager.GetGroup(account.Id, groupID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, toGroupResponse(account, group))
|
2022-05-03 16:02:51 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(status.Errorf(status.NotFound, "HTTP method not found"), w)
|
|
|
|
return
|
|
|
|
}
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func peerIPsToKeys(account *server.Account, peerIPs *[]string) []string {
|
|
|
|
var mappedPeerKeys []string
|
|
|
|
if peerIPs == nil {
|
|
|
|
return mappedPeerKeys
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
peersChecked := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, requestPeersIP := range *peerIPs {
|
|
|
|
_, ok := peersChecked[requestPeersIP]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
peersChecked[requestPeersIP] = struct{}{}
|
|
|
|
for _, accountPeer := range account.Peers {
|
|
|
|
if accountPeer.IP.String() == requestPeersIP {
|
|
|
|
mappedPeerKeys = append(mappedPeerKeys, accountPeer.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mappedPeerKeys
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
2022-05-21 15:21:39 +02:00
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func toGroupResponse(account *server.Account, group *server.Group) *api.Group {
|
|
|
|
cache := make(map[string]api.PeerMinimum)
|
|
|
|
gr := api.Group{
|
|
|
|
Id: group.ID,
|
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pid := range group.Peers {
|
2022-06-14 10:32:54 +02:00
|
|
|
_, ok := cache[pid]
|
2022-05-21 15:21:39 +02:00
|
|
|
if !ok {
|
|
|
|
peer, ok := account.Peers[pid]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-14 10:32:54 +02:00
|
|
|
peerResp := api.PeerMinimum{
|
|
|
|
Id: peer.IP.String(),
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: peer.Name,
|
|
|
|
}
|
|
|
|
cache[pid] = peerResp
|
2022-06-14 10:32:54 +02:00
|
|
|
gr.Peers = append(gr.Peers, peerResp)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &gr
|
|
|
|
}
|