File tree Expand file tree Collapse file tree 3 files changed +27
-7
lines changed
bpf-prog/azure-block-iptables Expand file tree Collapse file tree 3 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,11 @@ import (
1414)
1515
1616// ProgramVersion is set during build
17- var version = "unknown"
17+ var (
18+ version = "unknown"
19+ ErrModeRequired = errors .New ("mode is required" )
20+ ErrInvalidMode = errors .New ("invalid mode. Use -mode=attach or -mode=detach" )
21+ )
1822
1923// Config holds configuration for the application
2024type Config struct {
@@ -45,11 +49,11 @@ func parseArgs() (*Config, error) {
4549 }
4650
4751 if * mode == "" {
48- return nil , fmt . Errorf ( "mode is required. Use -mode=attach or -mode=detach" )
52+ return nil , ErrModeRequired
4953 }
5054
5155 if * mode != "attach" && * mode != "detach" {
52- return nil , fmt . Errorf ( "invalid mode '%s'. Must be 'attach' or 'detach'" , * mode )
56+ return nil , ErrInvalidMode
5357 }
5458
5559 return & Config {
@@ -107,7 +111,7 @@ func run(config *Config) error {
107111 case "detach" :
108112 return detachMode (config )
109113 default :
110- return fmt . Errorf ( "unsupported mode: %s" , config . Mode )
114+ return ErrInvalidMode
111115 }
112116}
113117
Original file line number Diff line number Diff line change @@ -49,7 +49,9 @@ func TestHandleFileEventWithMock(t *testing.T) {
4949 // Reset mock state
5050 mockAttacher .Reset ()
5151
52- run (& Config {Mode : tc .mode , Overwrite : tc .overwrite , AttacherFactory : func () bpfprogram.Attacher { return mockAttacher }})
52+ if err := run (& Config {Mode : tc .mode , Overwrite : tc .overwrite , AttacherFactory : func () bpfprogram.Attacher { return mockAttacher }}); err != nil {
53+ t .Errorf ("Failed to run: %v" , err )
54+ }
5355
5456 // Verify expectations
5557 if mockAttacher .AttachCallCount () != tc .expectedAttach {
Original file line number Diff line number Diff line change @@ -188,7 +188,12 @@ func (p *Program) Attach() error {
188188 }
189189
190190 pinPath := filepath .Join (BPFMapPinPath , IptablesLegacyBlockProgramName )
191- l .Pin (pinPath )
191+ err = l .Pin (pinPath )
192+ if err != nil {
193+ l .Close ()
194+ return errors .Wrap (err , "failed to pin iptables_legacy_block LSM" )
195+ }
196+
192197 links = append (links , l )
193198 }
194199
@@ -208,7 +213,16 @@ func (p *Program) Attach() error {
208213 return errors .Wrap (err , "failed to attach block_nf_netlink LSM" )
209214 }
210215 pinPath := filepath .Join (BPFMapPinPath , IptablesNftablesBlockProgramName )
211- l .Pin (pinPath )
216+ err = l .Pin (pinPath )
217+ if err != nil {
218+ for _ , link := range links {
219+ link .Close ()
220+ }
221+
222+ l .Close ()
223+ return errors .Wrap (err , "failed to pin iptables_nftables_block LSM" )
224+ }
225+
212226 links = append (links , l )
213227 }
214228
You can’t perform that action at this time.
0 commit comments