Skip to content

Commit

Permalink
fix hostname params and add full-disable params (chaos-mesh#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Savchenko <nikisavchenko@ozon.ru>
  • Loading branch information
Nikita Savchenko committed Jan 30, 2023
1 parent 14f3ff6 commit 9497688
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
7 changes: 4 additions & 3 deletions cmd/attack/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func NewNetworkDelayCommand(dep fx.Option, options *core.NetworkCommand) *cobra.
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
cmd.Flags().StringVarP(&options.AcceptTCPFlags, "accept-tcp-flags", "", "", "only the packet which match the tcp flag can be accepted, others will be dropped. only set when the protocol is tcp.")
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")

return cmd
}
Expand Down Expand Up @@ -114,7 +115,7 @@ func NewNetworkLossCommand(dep fx.Option, options *core.NetworkCommand) *cobra.C
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "full net on device disable flag")
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")

return cmd
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func NewNetworkCorruptCommand(dep fx.Option, options *core.NetworkCommand) *cobr
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")

cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")
return cmd
}

Expand Down Expand Up @@ -173,7 +174,7 @@ func NetworkDuplicateCommand(dep fx.Option, options *core.NetworkCommand) *cobra
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")

cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")
return cmd
}

Expand Down
32 changes: 24 additions & 8 deletions pkg/core/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func (n *NetworkCommand) validNetworkDelay() error {
return errors.Errorf("protocol should be 'tcp' when set accept-tcp-flags")
}

if err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable); err != nil {
return err
}

return checkProtocolAndPorts(n.IPProtocol, n.SourcePort, n.EgressPort)
}

Expand All @@ -151,6 +155,10 @@ func (n *NetworkCommand) validNetworkBandwidth() error {
return errors.Errorf("rate, limit and buffer both are required when action is bandwidth")
}

if err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable); err != nil {
return err
}

return nil
}

Expand All @@ -175,14 +183,8 @@ func (n *NetworkCommand) validNetworkCommon() error {
return errors.Errorf("ip addressed %s not valid", n.IPAddress)
}

if len(n.Hostname) == 0 && len(n.IPAddress) == 0 && !n.FullDisable {
return errors.New("hostname or ip address is required")
}

if n.FullDisable {
if len(n.Hostname) > 0 || len(n.IPAddress) > 0 {
return errors.New("the host and address are set, but the flag full-disable is enabled")
}
if err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable); err != nil {
return err
}

return checkProtocolAndPorts(n.IPProtocol, n.SourcePort, n.EgressPort)
Expand Down Expand Up @@ -344,6 +346,20 @@ func checkProtocolAndPorts(p string, sports string, dports string) error {
return nil
}

func checkNetworkLimitParams(hostname string, ipaddress string, fullDisable bool) error {
if len(hostname) == 0 && len(ipaddress) == 0 && !fullDisable {
return errors.New("hostname or ip address is required")
}

if fullDisable {
if len(hostname) > 0 || len(ipaddress) > 0 {
return errors.New("the host and address are set, but the flag full-disable is enabled")
}
}

return nil
}

func (n NetworkCommand) RecoverData() string {
data, _ := json.Marshal(n)

Expand Down

0 comments on commit 9497688

Please sign in to comment.