fix: save for acl

This commit is contained in:
Daniel Ding
2024-03-27 16:29:59 +08:00
parent 4b9a53a54e
commit 933f708e02
6 changed files with 74 additions and 13 deletions

View File

@@ -42,7 +42,7 @@ func (u ACLRule) Add(c *cli.Context) error {
DstIp: c.String("destination"),
SrcPort: c.Int("sport"),
DstPort: c.Int("dport"),
Action: "DROP",
Action: "drop",
}
clt := u.NewHttp(c.String("token"))
@@ -63,7 +63,7 @@ func (u ACLRule) Remove(c *cli.Context) error {
DstIp: c.String("destination"),
SrcPort: c.Int("sport"),
DstPort: c.Int("dport"),
Action: "DROP",
Action: "drop",
}
clt := u.NewHttp(c.String("token"))
@@ -97,6 +97,18 @@ func (u ACLRule) List(c *cli.Context) error {
return u.Out(items, c.String("format"), u.Tmpl())
}
func (u ACLRule) Save(c *cli.Context) error {
name := c.String("name")
url := u.Url(c.String("url"), name)
clt := u.NewHttp(c.String("token"))
if err := clt.PutJSON(url, nil, nil); err != nil {
return err
}
return nil
}
func (u ACLRule) Commands() *cli.Command {
return &cli.Command{
Name: "rule",
@@ -133,6 +145,12 @@ func (u ACLRule) Commands() *cli.Command {
Aliases: []string{"ls"},
Action: u.List,
},
{
Name: "save",
Usage: "Save all acl rules",
Aliases: []string{"sa"},
Action: u.Save,
},
},
}
}

View File

@@ -15,6 +15,7 @@ func (h ACL) Router(router *mux.Router) {
router.HandleFunc("/api/network/{id}/acl", h.List).Methods("GET")
router.HandleFunc("/api/network/{id}/acl", h.Add).Methods("POST")
router.HandleFunc("/api/network/{id}/acl", h.Del).Methods("DELETE")
router.HandleFunc("/api/network/{id}/acl", h.Save).Methods("PUT")
}
func (h ACL) List(w http.ResponseWriter, r *http.Request) {
@@ -85,3 +86,18 @@ func (h ACL) Del(w http.ResponseWriter, r *http.Request) {
return
}
}
func (h ACL) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
worker := GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
}
acl := worker.ACLer()
acl.Save()
ResponseJson(w, "success")
}

View File

@@ -36,6 +36,7 @@ type ACLer interface {
AddRule(rule *schema.ACLRule) error
DelRule(rule *schema.ACLRule) error
ListRules(call func(obj schema.ACLRule))
Save()
}
type ZTruster interface {

View File

@@ -14,6 +14,15 @@ func (ru *ACL) Save() {
}
}
func (ru *ACL) Correct(sw *Switch) {
for _, rule := range ru.Rules {
rule.Correct()
}
if ru.File == "" {
ru.File = sw.Dir("acl", ru.Name+".json")
}
}
type ACLRule struct {
Name string `json:"name,omitempty"`
SrcIp string `json:"source,omitempty"`
@@ -25,4 +34,7 @@ type ACLRule struct {
}
func (ru *ACLRule) Correct() {
if ru.Action == "" {
ru.Action = "drop"
}
}

View File

@@ -196,6 +196,13 @@ func (s *Switch) LoadNetwork() {
if obj.File == "" {
obj.File = s.Dir("network", obj.Name+".json")
}
if _, ok := s.Acl[obj.Name]; !ok {
obj := &ACL{
Name: obj.Name,
}
obj.Correct(s)
s.Acl[obj.Name] = obj
}
}
}
@@ -212,16 +219,9 @@ func (s *Switch) LoadAcl() {
libol.Error("Switch.LoadAcl %s", err)
continue
}
obj.Correct(s)
s.Acl[obj.Name] = obj
}
for _, obj := range s.Acl {
for _, rule := range obj.Rules {
rule.Correct()
}
if obj.File == "" {
obj.File = s.Dir("acl", obj.Name+".json")
}
}
}
func (s *Switch) Load() error {
@@ -246,9 +246,6 @@ func (s *Switch) SaveAcl() {
}
func (s *Switch) SaveNetwork() {
if s.Network == nil {
return
}
for _, obj := range s.Network {
obj.Save()
}

View File

@@ -162,3 +162,20 @@ func (a *ACL) ListRules(call func(obj schema.ACLRule)) {
call(obj)
}
}
func (a *ACL) Save() {
cfg := co.GetAcl(a.Name)
cfg.Rules = nil
for _, rule := range a.Rules {
cr := &co.ACLRule{
DstIp: rule.DstIp,
SrcIp: rule.SrcIp,
Proto: rule.Proto,
DstPort: rule.DstPort,
SrcPort: rule.SrcPort,
Action: rule.Action,
}
cfg.Rules = append(cfg.Rules, cr)
}
cfg.Save()
}