Skip to content

Commit

Permalink
implement COMMIT at the end of iptables-save
Browse files Browse the repository at this point in the history
  • Loading branch information
MEschenbacher committed Feb 16, 2024
1 parent 65029ea commit 7a26f2e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
8 changes: 8 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func (d Policy) String() string {
return fmt.Sprintf("%s%s %s", prefix, d.Chain, d.Action)
}

type Commit struct{}

func (c Commit) String() string {
return "COMMIT"
}

// Rule represents a rule in an iptables dump. Normally the start with -A.
// The parser treats the -A flag like any other flag, thus does not require
// the -A flag as the leading flag.
Expand Down Expand Up @@ -373,6 +379,8 @@ func (p *Parser) Parse() (l Line, err error) {
return p.parseRule()
case COLON:
return p.parseDefault(p.s.scanLine())
case COMMIT:
return Commit{}, nil
case EOF:
return nil, io.EOF // ErrEOF
case NEWLINE:
Expand Down
6 changes: 4 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ func TestParser_ParseMore(t *testing.T) {
},
},
{
name: "Parse some rules from iptables -S",
name: "Parse some rules from iptables -S as well as iptables-save",
s: `-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
Expand All @@ -1233,7 +1233,8 @@ func TestParser_ParseMore(t *testing.T) {
-N DOCKER-ISOLATION-STAGE-2
-N DOCKER-USER
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1`,
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
COMMIT`,
r: []interface{}{
Policy{
UserDefined: &_false,
Expand Down Expand Up @@ -1278,6 +1279,7 @@ func TestParser_ParseMore(t *testing.T) {
Name: "DOCKER-ISOLATION-STAGE-1",
},
},
Commit{},
},
},
} {
Expand Down
6 changes: 5 additions & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func (s *scanner) scan() (tok Token, lit string) {
return s.scanWhitespace()
case isLetter(ch) || isDigit(ch):
s.unread()
return s.scanIdent()
tok, lit := s.scanIdent()
if lit == "COMMIT" {
return COMMIT, "COMMIT"
}
return tok, lit
}

// Otherwise read the individual character.
Expand Down
1 change: 1 addition & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (

// Literals
IDENT // main
COMMIT

// Misc characters
COLON // :
Expand Down

0 comments on commit 7a26f2e

Please sign in to comment.