@@ -45,6 +45,7 @@ type IPTablesManager struct {
4545 ipt IPTablesWrapper
4646 mainChainName string
4747 defaultAction string
48+ logIdentifier string
4849 dryRun bool
4950 logDrops bool
5051}
@@ -55,7 +56,7 @@ var newIPTables = func() (IPTablesWrapper, error) {
5556}
5657
5758// NewIPTablesManager constructs the IPTablesManager with the specified main chain, default action, etc.
58- func NewIPTablesManager (mainChainName , defaultAction string , dryRun , logDrops bool ) (Manager , error ) {
59+ func NewIPTablesManager (mainChainName , defaultAction , logIdentifier string , dryRun , logDrops bool ) (Manager , error ) {
5960 ipt , err := newIPTables ()
6061 if err != nil {
6162 return nil , fmt .Errorf ("failed to initialize iptables: %v" , err )
@@ -72,6 +73,7 @@ func NewIPTablesManager(mainChainName, defaultAction string, dryRun, logDrops bo
7273 ipt : ipt ,
7374 mainChainName : mainChainName ,
7475 defaultAction : defaultAction ,
76+ logIdentifier : logIdentifier ,
7577 dryRun : dryRun ,
7678 logDrops : logDrops ,
7779 }, nil
@@ -116,7 +118,7 @@ func (m *IPTablesManager) CreateContainerChain(containerChain string) error {
116118 // and then ACCEPT instead of dropping.
117119 logSpec := []string {
118120 "-j" , "LOG" ,
119- "--log-prefix" , fmt .Sprintf (`"[CNI-OUTBOUND- %s-%s] "` , containerChain , m .defaultAction ),
121+ "--log-prefix" , fmt .Sprintf (`"%s-%s "` , m . defaultAction , m .logIdentifier ),
120122 }
121123 if err := m .ipt .Append ("filter" , containerChain , logSpec ... ); err != nil {
122124 return fmt .Errorf ("failed to add default action logging rule: %v" , err )
@@ -131,7 +133,7 @@ func (m *IPTablesManager) CreateContainerChain(containerChain string) error {
131133 // Log before the drop
132134 logSpec := []string {
133135 "-j" , "LOG" ,
134- "--log-prefix" , fmt .Sprintf (`"[CNI-OUTBOUND- %s-DEFAULT-BLOCKED] "` , containerChain ),
136+ "--log-prefix" , fmt .Sprintf (`"%s-%s "` , m . defaultAction , m . logIdentifier ),
135137 }
136138 if err := m .ipt .Append ("filter" , containerChain , logSpec ... ); err != nil {
137139 return fmt .Errorf ("failed to add default DROP logging rule: %v" , err )
@@ -147,15 +149,15 @@ func (m *IPTablesManager) CreateContainerChain(containerChain string) error {
147149}
148150
149151// buildRuleSpecs prepares the iptables arguments for a single OutboundRule.
150- func (m * IPTablesManager ) buildRuleSpecs (chainName , host , proto , port , action string ) [][]string {
152+ func (m * IPTablesManager ) buildRuleSpecs (host , proto , port , action string ) [][]string {
151153 // Common rule spec
152154 baseSpec := []string {"-d" , host , "-p" , proto , "--dport" , port }
153155
154156 // If in dry-run, we log + ACCEPT
155157 if m .dryRun {
156158 return [][]string {
157159 append (append ([]string {}, baseSpec ... ), "-j" , "LOG" , "--log-prefix" ,
158- fmt .Sprintf (`"[CNI-OUTBOUND- %s-ACCEPTED] "` , chainName )),
160+ fmt .Sprintf (`"%s-%s "` , action , m . logIdentifier )),
159161 append (append ([]string {}, baseSpec ... ), "-j" , "ACCEPT" ),
160162 }
161163 }
@@ -164,7 +166,7 @@ func (m *IPTablesManager) buildRuleSpecs(chainName, host, proto, port, action st
164166 if m .logDrops && strings .EqualFold (action , "DROP" ) {
165167 return [][]string {
166168 append (append ([]string {}, baseSpec ... ), "-j" , "LOG" , "--log-prefix" ,
167- fmt .Sprintf (`"[CNI-OUTBOUND- %s-BLOCKED] "` , chainName )),
169+ fmt .Sprintf (`"%s-%s "` , action , m . logIdentifier )),
168170 append (append ([]string {}, baseSpec ... ), "-j" , "DROP" ),
169171 }
170172 }
@@ -177,7 +179,7 @@ func (m *IPTablesManager) buildRuleSpecs(chainName, host, proto, port, action st
177179
178180// AddRule inserts a new rule (or rules) into the chain.
179181func (m * IPTablesManager ) AddRule (chainName string , rule OutboundRule ) error {
180- ruleSpecs := m .buildRuleSpecs (chainName , rule .Host , rule .Proto , rule .Port , rule .Action )
182+ ruleSpecs := m .buildRuleSpecs (rule .Host , rule .Proto , rule .Port , rule .Action )
181183
182184 // Insert each spec at position 1 in reverse order so they appear in the chain in the correct sequence
183185 for i := len (ruleSpecs ) - 1 ; i >= 0 ; i -- {
@@ -263,17 +265,17 @@ func (m *IPTablesManager) VerifyRules(chainName string, rules []OutboundRule) er
263265
264266 // In dry run mode, we also expect a default logging rule
265267 if m .dryRun {
266- defaultLogLine := fmt .Sprintf ("-A %s -j LOG --log-prefix [CNI-OUTBOUND-DEFAULT-%s]" , chainName , m .defaultAction )
268+ defaultLogLine := fmt .Sprintf ("-A %s -j LOG --log-prefix \" %s-%s \" " , chainName , m .defaultAction , m . logIdentifier )
267269 if ! lineExistsInIptablesList (defaultLogLine , existingRules ) {
268- return fmt .Errorf ("default action logging rule not found" )
270+ return fmt .Errorf ("default action [%s] logging rule not found" , defaultLogLine )
269271 }
270272 }
271273 return nil
272274}
273275
274276// buildExpectedRuleLines constructs the lines we'll look for in `iptables -S <chain>` output.
275277func (m * IPTablesManager ) buildExpectedRuleLines (chainName , host , proto , port , action string ) []string {
276- specs := m .buildRuleSpecs (chainName , host , proto , port , action )
278+ specs := m .buildRuleSpecs (host , proto , port , action )
277279 lines := make ([]string , 0 , len (specs ))
278280 for _ , s := range specs {
279281 // iptables -S lines typically: "-A <chain> <args>..."
0 commit comments