2022-10-10 11:06:54 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"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-10-10 11:06: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"
|
2022-10-10 11:06:54 +02:00
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// NameserversHandler is the nameserver group handler of the account
|
|
|
|
type NameserversHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// NewNameserversHandler returns a new instance of NameserversHandler handler
|
|
|
|
func NewNameserversHandler(accountManager server.AccountManager, authCfg AuthCfg) *NameserversHandler {
|
|
|
|
return &NameserversHandler{
|
2022-10-10 11:06:54 +02:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetAllNameservers returns the list of nameserver groups for the account
|
|
|
|
func (h *NameserversHandler) GetAllNameservers(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Error(err)
|
2022-10-10 11:06:54 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
nsGroups, err := h.accountManager.ListNameServerGroups(r.Context(), account.Id, user.Id)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiNameservers := make([]*api.NameserverGroup, 0)
|
|
|
|
for _, r := range nsGroups {
|
|
|
|
apiNameservers = append(apiNameservers, toNameserverGroupResponse(r))
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, apiNameservers)
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// CreateNameserverGroup handles nameserver group creation request
|
|
|
|
func (h *NameserversHandler) CreateNameserverGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
var req api.PostApiDnsNameserversJSONRequestBody
|
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-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsList, err := toServerNSList(req.Nameservers)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid NS servers format"), w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
nsGroup, err := h.accountManager.CreateNameServerGroup(r.Context(), account.Id, req.Name, req.Description, nsList, req.Groups, req.Primary, req.Domains, req.Enabled, user.Id, req.SearchDomainsEnabled)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := toNameserverGroupResponse(nsGroup)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, &resp)
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// UpdateNameserverGroup handles update to a nameserver group identified by a given ID
|
|
|
|
func (h *NameserversHandler) UpdateNameserverGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
nsGroupID := mux.Vars(r)["nsgroupId"]
|
2022-10-10 11:06:54 +02:00
|
|
|
if len(nsGroupID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid nameserver group ID"), w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
var req api.PutApiDnsNameserversNsgroupIdJSONRequestBody
|
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-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsList, err := toServerNSList(req.Nameservers)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid NS servers format"), w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedNSGroup := &nbdns.NameServerGroup{
|
2023-10-19 19:32:42 +02:00
|
|
|
ID: nsGroupID,
|
|
|
|
Name: req.Name,
|
|
|
|
Description: req.Description,
|
|
|
|
Primary: req.Primary,
|
|
|
|
Domains: req.Domains,
|
|
|
|
NameServers: nsList,
|
|
|
|
Groups: req.Groups,
|
|
|
|
Enabled: req.Enabled,
|
|
|
|
SearchDomainsEnabled: req.SearchDomainsEnabled,
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = h.accountManager.SaveNameServerGroup(r.Context(), account.Id, user.Id, updatedNSGroup)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := toNameserverGroupResponse(updatedNSGroup)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, &resp)
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:27:43 +01:00
|
|
|
// DeleteNameserverGroup handles nameserver group deletion request
|
|
|
|
func (h *NameserversHandler) DeleteNameserverGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
nsGroupID := mux.Vars(r)["nsgroupId"]
|
2022-10-10 11:06:54 +02:00
|
|
|
if len(nsGroupID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid nameserver group ID"), w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = h.accountManager.DeleteNameServerGroup(r.Context(), account.Id, nsGroupID, user.Id)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, emptyObject{})
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:27:43 +01:00
|
|
|
// GetNameserverGroup handles a nameserver group Get request identified by ID
|
|
|
|
func (h *NameserversHandler) GetNameserverGroup(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(r.Context(), claims)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Error(err)
|
2022-10-10 11:06:54 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
nsGroupID := mux.Vars(r)["nsgroupId"]
|
2022-10-10 11:06:54 +02:00
|
|
|
if len(nsGroupID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid nameserver group ID"), w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
nsGroup, err := h.accountManager.GetNameServerGroup(r.Context(), account.Id, user.Id, nsGroupID)
|
2022-10-10 11:06:54 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-10-10 11:06:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := toNameserverGroupResponse(nsGroup)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, &resp)
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func toServerNSList(apiNSList []api.Nameserver) ([]nbdns.NameServer, error) {
|
|
|
|
var nsList []nbdns.NameServer
|
|
|
|
for _, apiNS := range apiNSList {
|
|
|
|
parsed, err := nbdns.ParseNameServerURL(fmt.Sprintf("%s://%s:%d", apiNS.NsType, apiNS.Ip, apiNS.Port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nsList = append(nsList, parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toNameserverGroupResponse(serverNSGroup *nbdns.NameServerGroup) *api.NameserverGroup {
|
|
|
|
var nsList []api.Nameserver
|
|
|
|
for _, ns := range serverNSGroup.NameServers {
|
|
|
|
apiNS := api.Nameserver{
|
|
|
|
Ip: ns.IP.String(),
|
|
|
|
NsType: api.NameserverNsType(ns.NSType.String()),
|
|
|
|
Port: ns.Port,
|
|
|
|
}
|
|
|
|
nsList = append(nsList, apiNS)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.NameserverGroup{
|
2023-10-19 19:32:42 +02:00
|
|
|
Id: serverNSGroup.ID,
|
|
|
|
Name: serverNSGroup.Name,
|
|
|
|
Description: serverNSGroup.Description,
|
|
|
|
Primary: serverNSGroup.Primary,
|
|
|
|
Domains: serverNSGroup.Domains,
|
|
|
|
Groups: serverNSGroup.Groups,
|
|
|
|
Nameservers: nsList,
|
|
|
|
Enabled: serverNSGroup.Enabled,
|
|
|
|
SearchDomainsEnabled: serverNSGroup.SearchDomainsEnabled,
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
}
|