mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-26 10:03:47 +01:00
6ae27c9a9b
* feature: add User entity to Account * test: new file store creation test * test: add FileStore persist-restore tests * test: add GetOrCreateAccountByUser Accountmanager test * refactor: rename account manager users file * refactor: use userId instead of accountId when handling Management HTTP API * fix: new account creation for every request * fix: golint * chore: add account creator to Account Entity to identify who created the account. * chore: use xid ID generator for account IDs * fix: test failures * test: check that CreatedBy is stored when account is stored * chore: add account copy method * test: remove test for non existent GetOrCreateAccount func * chore: add accounts conversion function * fix: golint * refactor: simplify admin user creation * refactor: move migration script to a separate package
134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gorilla/mux"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/wiretrustee/wiretrustee/management/server"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
//Peers is a handler that returns peers of the account
|
|
type Peers struct {
|
|
accountManager *server.AccountManager
|
|
}
|
|
|
|
//PeerResponse is a response sent to the client
|
|
type PeerResponse struct {
|
|
Name string
|
|
IP string
|
|
Connected bool
|
|
LastSeen time.Time
|
|
OS string
|
|
}
|
|
|
|
//PeerRequest is a request sent by the client
|
|
type PeerRequest struct {
|
|
Name string
|
|
}
|
|
|
|
func NewPeers(accountManager *server.AccountManager) *Peers {
|
|
return &Peers{
|
|
accountManager: accountManager,
|
|
}
|
|
}
|
|
|
|
func (h *Peers) updatePeer(accountId string, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
|
|
req := &PeerRequest{}
|
|
peerIp := peer.IP
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
peer, err = h.accountManager.RenamePeer(accountId, peer.Key, req.Name)
|
|
if err != nil {
|
|
log.Errorf("failed updating peer %s under account %s %v", peerIp, accountId, err)
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSONObject(w, toPeerResponse(peer))
|
|
}
|
|
func (h *Peers) deletePeer(accountId string, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
|
|
_, err := h.accountManager.DeletePeer(accountId, peer.Key)
|
|
if err != nil {
|
|
log.Errorf("failed deleteing peer %s, %v", peer.IP, err)
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSONObject(w, "")
|
|
}
|
|
|
|
func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) {
|
|
userId := extractUserIdFromRequestContext(r)
|
|
account, err := h.accountManager.GetOrCreateAccountByUser(userId)
|
|
if err != nil {
|
|
log.Errorf("failed getting account of a user %s: %v", userId, err)
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
vars := mux.Vars(r)
|
|
peerId := vars["id"] //effectively peer IP address
|
|
if len(peerId) == 0 {
|
|
http.Error(w, "invalid peer Id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
peer, err := h.accountManager.GetPeerByIP(account.Id, peerId)
|
|
if err != nil {
|
|
http.Error(w, "peer not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
switch r.Method {
|
|
case http.MethodDelete:
|
|
h.deletePeer(account.Id, peer, w, r)
|
|
return
|
|
case http.MethodPut:
|
|
h.updatePeer(account.Id, peer, w, r)
|
|
return
|
|
case http.MethodGet:
|
|
writeJSONObject(w, toPeerResponse(peer))
|
|
return
|
|
|
|
default:
|
|
http.Error(w, "", http.StatusNotFound)
|
|
}
|
|
|
|
}
|
|
|
|
func (h *Peers) GetPeers(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
userId := extractUserIdFromRequestContext(r)
|
|
//new user -> create a new account
|
|
account, err := h.accountManager.GetOrCreateAccountByUser(userId)
|
|
if err != nil {
|
|
log.Errorf("failed getting account of a user %s: %v", userId, err)
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
respBody := []*PeerResponse{}
|
|
for _, peer := range account.Peers {
|
|
respBody = append(respBody, toPeerResponse(peer))
|
|
}
|
|
writeJSONObject(w, respBody)
|
|
return
|
|
default:
|
|
http.Error(w, "", http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func toPeerResponse(peer *server.Peer) *PeerResponse {
|
|
return &PeerResponse{
|
|
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),
|
|
}
|
|
}
|