2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-08-24 11:50:19 +02:00
|
|
|
"fmt"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
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"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2021-08-12 12:49:10 +02:00
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// PeersHandler is a handler that returns peers of the account
|
|
|
|
type PeersHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:46:08 +01:00
|
|
|
// NewPeersHandler creates a new PeersHandler HTTP handler
|
2023-02-28 15:01:24 +01:00
|
|
|
func NewPeersHandler(accountManager server.AccountManager, authCfg AuthCfg) *PeersHandler {
|
|
|
|
return &PeersHandler{
|
2021-08-12 12:49:10 +02:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) getPeer(account *server.Account, peerID, userID string, w http.ResponseWriter) {
|
2023-02-07 20:11:08 +01:00
|
|
|
peer, err := h.accountManager.GetPeer(account.Id, peerID, userID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
util.WriteJSONObject(w, toPeerResponse(peer, account, h.accountManager.GetDNSDomain()))
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) updatePeer(account *server.Account, user *server.User, peerID string, w http.ResponseWriter, r *http.Request) {
|
2023-05-26 15:32:52 +02:00
|
|
|
req := &api.PeerRequest{}
|
2021-08-23 21:43:05 +02:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2023-02-14 10:14:00 +01:00
|
|
|
update := &server.Peer{ID: peerID, SSHEnabled: req.SshEnabled, Name: req.Name,
|
|
|
|
LoginExpirationEnabled: req.LoginExpirationEnabled}
|
2023-02-03 10:33:28 +01:00
|
|
|
peer, err := h.accountManager.UpdatePeer(account.Id, user.Id, update)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2022-11-21 11:14:42 +01:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
|
|
|
util.WriteJSONObject(w, toPeerResponse(peer, account, dnsDomain))
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) deletePeer(accountID, userID string, peerID string, w http.ResponseWriter) {
|
2023-02-03 10:33:28 +01:00
|
|
|
_, err := h.accountManager.DeletePeer(accountID, peerID, userID)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2023-03-29 18:46:40 +02:00
|
|
|
util.WriteJSONObject(w, emptyObject{})
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 16:51:30 +01:00
|
|
|
// HandlePeer handles all peer requests for GET, PUT and DELETE operations
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) HandlePeer(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2021-12-27 13:17:15 +01:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2021-12-27 13:17:15 +01:00
|
|
|
return
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
vars := mux.Vars(r)
|
2023-05-03 00:15:25 +02:00
|
|
|
peerID := vars["peerId"]
|
2023-02-03 10:33:28 +01:00
|
|
|
if len(peerID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid peer ID"), w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
2023-02-03 10:33:28 +01:00
|
|
|
h.deletePeer(account.Id, user.Id, peerID, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
case http.MethodPut:
|
2023-02-03 10:33:28 +01:00
|
|
|
h.updatePeer(account, user, peerID, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2023-02-07 20:11:08 +01:00
|
|
|
case http.MethodGet:
|
|
|
|
h.getPeer(account, peerID, user.Id, w)
|
|
|
|
return
|
2021-08-23 21:43:05 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "unknown METHOD"), w)
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 16:51:30 +01:00
|
|
|
// GetAllPeers returns a list of all peers associated with a provided account
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) GetAllPeers(w http.ResponseWriter, r *http.Request) {
|
2021-08-12 12:49:10 +02:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2022-11-11 20:36:45 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2021-08-12 12:49:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
peers, err := h.accountManager.GetPeers(account.Id, user.Id)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-11-05 10:24:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:14:42 +01:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
respBody := []*api.Peer{}
|
2022-11-05 10:24:50 +01:00
|
|
|
for _, peer := range peers {
|
2022-11-21 11:14:42 +01:00
|
|
|
respBody = append(respBody, toPeerResponse(peer, account, dnsDomain))
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, respBody)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2021-08-12 12:49:10 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "unknown METHOD"), w)
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2022-11-21 11:14:42 +01:00
|
|
|
func toPeerResponse(peer *server.Peer, account *server.Account, dnsDomain string) *api.Peer {
|
2022-06-14 10:32:54 +02:00
|
|
|
var groupsInfo []api.GroupMinimum
|
|
|
|
groupsChecked := make(map[string]struct{})
|
|
|
|
for _, group := range account.Groups {
|
|
|
|
_, ok := groupsChecked[group.ID]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupsChecked[group.ID] = struct{}{}
|
|
|
|
for _, pk := range group.Peers {
|
2023-02-03 10:33:28 +01:00
|
|
|
if pk == peer.ID {
|
2022-06-14 10:32:54 +02:00
|
|
|
info := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
|
|
|
}
|
|
|
|
groupsInfo = append(groupsInfo, info)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
|
|
|
|
fqdn := peer.FQDN(dnsDomain)
|
|
|
|
if fqdn == "" {
|
|
|
|
fqdn = peer.DNSLabel
|
2022-11-21 11:14:42 +01:00
|
|
|
}
|
2023-02-14 10:14:00 +01:00
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
return &api.Peer{
|
2023-02-14 10:14:00 +01:00
|
|
|
Id: peer.ID,
|
|
|
|
Name: peer.Name,
|
|
|
|
Ip: peer.IP.String(),
|
|
|
|
Connected: peer.Status.Connected,
|
|
|
|
LastSeen: peer.Status.LastSeen,
|
|
|
|
Os: fmt.Sprintf("%s %s", peer.Meta.OS, peer.Meta.Core),
|
|
|
|
Version: peer.Meta.WtVersion,
|
|
|
|
Groups: groupsInfo,
|
|
|
|
SshEnabled: peer.SSHEnabled,
|
|
|
|
Hostname: peer.Meta.Hostname,
|
|
|
|
UserId: &peer.UserID,
|
|
|
|
UiVersion: &peer.Meta.UIVersion,
|
|
|
|
DnsLabel: fqdn,
|
|
|
|
LoginExpirationEnabled: peer.LoginExpirationEnabled,
|
|
|
|
LastLogin: peer.LastLogin,
|
2023-02-15 11:27:22 +01:00
|
|
|
LoginExpired: peer.Status.LoginExpired,
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|