2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-21 15:21:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/rs/xid"
|
|
|
|
|
2022-05-21 15:21:39 +02: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-05-21 15:21:39 +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-05-21 15:21:39 +02:00
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// RulesHandler is a handler that returns rules of the account
|
|
|
|
type RulesHandler 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
|
|
|
// NewRulesHandler creates a new RulesHandler HTTP handler
|
2023-02-28 15:01:24 +01:00
|
|
|
func NewRulesHandler(accountManager server.AccountManager, authCfg AuthCfg) *RulesHandler {
|
|
|
|
return &RulesHandler{
|
2022-05-21 15:21:39 +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-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetAllRules list for the account
|
|
|
|
func (h *RulesHandler) GetAllRules(w http.ResponseWriter, r *http.Request) {
|
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)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
accountPolicies, err := h.accountManager.ListPolicies(account.Id, user.Id)
|
2022-11-05 10:24:50 +01:00
|
|
|
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-06-14 10:32:54 +02:00
|
|
|
rules := []*api.Rule{}
|
2023-03-13 15:14:18 +01:00
|
|
|
for _, policy := range accountPolicies {
|
|
|
|
for _, r := range policy.Rules {
|
|
|
|
rules = append(rules, toRuleResponse(account, r.ToRule()))
|
|
|
|
}
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, rules)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// UpdateRule handles update to a rule identified by a given ID
|
|
|
|
func (h *RulesHandler) UpdateRule(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)
|
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)
|
2023-05-03 00:15:25 +02:00
|
|
|
ruleID := vars["ruleId"]
|
2022-06-14 10:32:54 +02:00
|
|
|
if len(ruleID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policy, err := h.accountManager.GetPolicy(account.Id, ruleID, user.Id)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
var req api.PutApiRulesRuleIdJSONRequestBody
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if req.Name == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "rule name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqSources []string
|
|
|
|
if req.Sources != nil {
|
|
|
|
reqSources = *req.Sources
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqDestinations []string
|
|
|
|
if req.Destinations != nil {
|
|
|
|
reqDestinations = *req.Destinations
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
if len(policy.Rules) != 1 {
|
|
|
|
util.WriteError(status.Errorf(status.Internal, "policy should contain exactly one rule"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policy.Name = req.Name
|
|
|
|
policy.Description = req.Description
|
|
|
|
policy.Enabled = !req.Disabled
|
|
|
|
policy.Rules[0].ID = ruleID
|
|
|
|
policy.Rules[0].Name = req.Name
|
|
|
|
policy.Rules[0].Sources = reqSources
|
|
|
|
policy.Rules[0].Destinations = reqDestinations
|
|
|
|
policy.Rules[0].Enabled = !req.Disabled
|
|
|
|
policy.Rules[0].Description = req.Description
|
|
|
|
if err := policy.UpdateQueryFromRules(); err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
switch req.Flow {
|
|
|
|
case server.TrafficFlowBidirectString:
|
|
|
|
policy.Rules[0].Action = server.PolicyTrafficActionAccept
|
|
|
|
default:
|
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "unknown flow type"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
err = h.accountManager.SavePolicy(account.Id, user.Id, policy)
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
resp := toRuleResponse(account, policy.Rules[0].ToRule())
|
2022-06-14 10:32:54 +02:00
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, &resp)
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// CreateRule handles rule creation request
|
|
|
|
func (h *RulesHandler) CreateRule(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)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
var req api.PostApiRulesJSONRequestBody
|
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-21 15:21:39 +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, "rule name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqSources []string
|
|
|
|
if req.Sources != nil {
|
|
|
|
reqSources = *req.Sources
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqDestinations []string
|
|
|
|
if req.Destinations != nil {
|
|
|
|
reqDestinations = *req.Destinations
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rule := server.Rule{
|
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
|
|
|
Source: reqSources,
|
|
|
|
Destination: reqDestinations,
|
|
|
|
Disabled: req.Disabled,
|
|
|
|
Description: req.Description,
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Flow {
|
2022-06-14 10:32:54 +02:00
|
|
|
case server.TrafficFlowBidirectString:
|
2022-05-21 15:21:39 +02:00
|
|
|
rule.Flow = server.TrafficFlowBidirect
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "unknown flow type"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policy, err := server.RuleToPolicy(&rule)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = h.accountManager.SavePolicy(account.Id, user.Id, policy)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
resp := toRuleResponse(account, &rule)
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, &resp)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// DeleteRule handles rule deletion request
|
|
|
|
func (h *RulesHandler) DeleteRule(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)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
aID := account.Id
|
|
|
|
|
2023-05-03 00:15:25 +02:00
|
|
|
rID := mux.Vars(r)["ruleId"]
|
2022-05-21 15:21:39 +02:00
|
|
|
if len(rID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
err = h.accountManager.DeletePolicy(aID, rID, user.Id)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:46:40 +02:00
|
|
|
util.WriteJSONObject(w, emptyObject{})
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetRule handles a group Get request identified by ID
|
|
|
|
func (h *RulesHandler) GetRule(w http.ResponseWriter, r *http.Request) {
|
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)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2023-05-03 00:15:25 +02:00
|
|
|
ruleID := mux.Vars(r)["ruleId"]
|
2022-05-21 15:21:39 +02:00
|
|
|
if len(ruleID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policy, err := h.accountManager.GetPolicy(account.Id, ruleID, user.Id)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2023-03-13 15:14:18 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
util.WriteJSONObject(w, toRuleResponse(account, policy.Rules[0].ToRule()))
|
2022-05-21 15:21:39 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "method not found"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func toRuleResponse(account *server.Account, rule *server.Rule) *api.Rule {
|
|
|
|
cache := make(map[string]api.GroupMinimum)
|
|
|
|
gr := api.Rule{
|
|
|
|
Id: rule.ID,
|
|
|
|
Name: rule.Name,
|
|
|
|
Description: rule.Description,
|
|
|
|
Disabled: rule.Disabled,
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch rule.Flow {
|
|
|
|
case server.TrafficFlowBidirect:
|
2022-06-14 10:32:54 +02:00
|
|
|
gr.Flow = server.TrafficFlowBidirectString
|
2022-05-21 15:21:39 +02:00
|
|
|
default:
|
|
|
|
gr.Flow = "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gid := range rule.Source {
|
2022-06-14 10:32:54 +02:00
|
|
|
_, ok := cache[gid]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
if group, ok := account.Groups[gid]; ok {
|
2022-06-14 10:32:54 +02:00
|
|
|
minimum := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gr.Sources = append(gr.Sources, minimum)
|
|
|
|
cache[gid] = minimum
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gid := range rule.Destination {
|
2022-06-14 10:32:54 +02:00
|
|
|
cachedMinimum, ok := cache[gid]
|
|
|
|
if ok {
|
|
|
|
gr.Destinations = append(gr.Destinations, cachedMinimum)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-21 15:21:39 +02:00
|
|
|
if group, ok := account.Groups[gid]; ok {
|
2022-06-14 10:32:54 +02:00
|
|
|
minimum := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
gr.Destinations = append(gr.Destinations, minimum)
|
|
|
|
cache[gid] = minimum
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gr
|
|
|
|
}
|