2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-03 16:02:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-05-03 16:02:51 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
2022-05-03 16:02:51 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2024-03-27 18:48:48 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-05-03 16:02:51 +02:00
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GroupsHandler is a handler that returns groups of the account
|
|
|
|
type GroupsHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:46:08 +01:00
|
|
|
// NewGroupsHandler creates a new GroupsHandler HTTP handler
|
2023-02-28 15:01:24 +01:00
|
|
|
func NewGroupsHandler(accountManager server.AccountManager, authCfg AuthCfg) *GroupsHandler {
|
|
|
|
return &GroupsHandler{
|
2022-05-03 16:02:51 +02:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetAllGroups list for the account
|
|
|
|
func (h *GroupsHandler) GetAllGroups(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-18 15:40:47 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Error(err)
|
2022-05-03 16:02:51 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:40:47 +02:00
|
|
|
groups, err := h.accountManager.GetAllGroups(r.Context(), account.Id, user.Id)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
groupsResponse := make([]*api.Group, 0, len(groups))
|
|
|
|
for _, group := range groups {
|
2024-03-27 16:11:45 +01:00
|
|
|
groupsResponse = append(groupsResponse, toGroupResponse(account, group))
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, groupsResponse)
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// UpdateGroup handles update to a group identified by a given ID
|
|
|
|
func (h *GroupsHandler) UpdateGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-18 15:40:47 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2023-05-03 00:15:25 +02:00
|
|
|
groupID, ok := vars["groupId"]
|
2022-06-14 10:32:54 +02:00
|
|
|
if !ok {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "group ID field is missing"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(groupID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "group ID can't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-27 16:51:05 +02:00
|
|
|
eg, ok := account.Groups[groupID]
|
2022-06-14 10:32:54 +02:00
|
|
|
if !ok {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), 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 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if allGroup.ID == groupID {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "updating group ALL is not allowed"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
var req api.PutApiGroupsGroupIdJSONRequestBody
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:32:52 +02:00
|
|
|
if req.Name == "" {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "group name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
var peers []string
|
|
|
|
if req.Peers == nil {
|
|
|
|
peers = make([]string, 0)
|
|
|
|
} else {
|
|
|
|
peers = *req.Peers
|
|
|
|
}
|
2024-03-27 18:48:48 +01:00
|
|
|
group := nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: groupID,
|
|
|
|
Name: req.Name,
|
|
|
|
Peers: peers,
|
|
|
|
Issued: eg.Issued,
|
|
|
|
IntegrationReference: eg.IntegrationReference,
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 15:40:47 +02:00
|
|
|
if err := h.accountManager.SaveGroup(r.Context(), account.Id, user.Id, &group); err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Errorf("failed updating group %s under account %s %v", groupID, account.Id, err)
|
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, toGroupResponse(account, &group))
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// CreateGroup handles group creation request
|
|
|
|
func (h *GroupsHandler) CreateGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-18 15:40:47 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), 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 == "" {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), 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
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
var peers []string
|
|
|
|
if req.Peers == nil {
|
|
|
|
peers = make([]string, 0)
|
|
|
|
} else {
|
|
|
|
peers = *req.Peers
|
|
|
|
}
|
2024-03-27 18:48:48 +01:00
|
|
|
group := nbgroup.Group{
|
2023-06-27 16:51:05 +02:00
|
|
|
Name: req.Name,
|
|
|
|
Peers: peers,
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 15:40:47 +02:00
|
|
|
err = h.accountManager.SaveGroup(r.Context(), account.Id, user.Id, &group)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, toGroupResponse(account, &group))
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// DeleteGroup handles group deletion request
|
|
|
|
func (h *GroupsHandler) DeleteGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-18 15:40:47 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
aID := account.Id
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
groupID := mux.Vars(r)["groupId"]
|
2022-06-14 10:32:54 +02:00
|
|
|
if len(groupID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), 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 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if allGroup.ID == groupID {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "deleting group ALL is not allowed"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:40:47 +02:00
|
|
|
err = h.accountManager.DeleteGroup(r.Context(), aID, user.Id, groupID)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
2023-07-14 20:45:40 +02:00
|
|
|
_, ok := err.(*server.GroupLinkError)
|
|
|
|
if ok {
|
|
|
|
util.WriteErrorResponse(err.Error(), http.StatusBadRequest, w)
|
|
|
|
return
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, emptyObject{})
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetGroup returns a group
|
|
|
|
func (h *GroupsHandler) GetGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-18 15:40:47 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2023-05-03 00:15:25 +02:00
|
|
|
groupID := mux.Vars(r)["groupId"]
|
2022-05-03 16:02:51 +02:00
|
|
|
if len(groupID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid group ID"), w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:40:47 +02:00
|
|
|
group, err := h.accountManager.GetGroup(r.Context(), account.Id, groupID, user.Id)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-05-03 16:02:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, toGroupResponse(account, group))
|
2022-05-03 16:02:51 +02:00
|
|
|
default:
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.NotFound, "HTTP method not found"), w)
|
2023-09-04 17:03:44 +02:00
|
|
|
return
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
func toGroupResponse(account *server.Account, group *nbgroup.Group) *api.Group {
|
2022-06-14 10:32:54 +02:00
|
|
|
cache := make(map[string]api.PeerMinimum)
|
|
|
|
gr := api.Group{
|
2024-02-13 10:59:31 +01:00
|
|
|
Id: group.ID,
|
|
|
|
Name: group.Name,
|
2024-03-27 16:11:45 +01:00
|
|
|
Issued: (*api.GroupIssued)(&group.Issued),
|
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{
|
2023-02-03 10:33:28 +01:00
|
|
|
Id: peer.ID,
|
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
|
|
|
}
|
|
|
|
}
|
2024-02-13 10:59:31 +01:00
|
|
|
|
|
|
|
gr.PeersCount = len(gr.Peers)
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
return &gr
|
|
|
|
}
|