Skip to content

Commit

Permalink
Updated snat rule test logic
Browse files Browse the repository at this point in the history
Install iptables in the test agent image
  • Loading branch information
Chinmay Gadgil committed Aug 27, 2021
1 parent 21206a6 commit ed56743
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions test/agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build \

FROM public.ecr.aws/amazonlinux/amazonlinux:2
RUN yum update -y && \
yum install -y iptables && \
yum clean all

WORKDIR /
Expand Down
46 changes: 33 additions & 13 deletions test/agent/cmd/snat-utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -72,33 +73,52 @@ func validateIPTableRules(randomizedSNATValue string, numOfCidrs int) error {
}

containsExpectedString := false
rule := ""

for i := 0; i <= numOfCidrs; i++ {
curr := fmt.Sprintf("AWS-SNAT-CHAIN-%d", i)
fmt.Printf("Checking: %s\n", curr)
chains, err := iptables.List("nat", curr)
currChain := "AWS-SNAT-CHAIN-0"
lastChain := fmt.Sprintf("AWS-SNAT-CHAIN-%d", numOfCidrs)
i := 0
for i < numOfCidrs {
rules, err := iptables.List("nat", currChain)
if err != nil {
return err
}

for _, chain := range chains {
if strings.Contains(chain, expectedString) {
rule = chain
containsExpectedString = true
i = i + 1
nextChain := fmt.Sprintf("AWS-SNAT-CHAIN-%d", i)
foundNextChain := false
for _, rule := range rules {
target := fmt.Sprintf("-j %s", nextChain)
if strings.Contains(rule, target) {
currChain = nextChain
foundNextChain = true
break
}
}
if foundNextChain == false {
return fmt.Errorf("failed: AWS-SNAT chain broken for %s", currChain)
}
}

// Fetch rules from lastChain
rules, err := iptables.List("nat", lastChain)
if err != nil {
return err
}

// Check for rule with following pattern
match := fmt.Sprintf(".*-j SNAT.*%s", expectedString)
r, _ := regexp.Compile(match)

if containsExpectedString {
for _, rule := range rules {
if r.Match([]byte(rule)) {
containsExpectedString = true
break
}
}

if randomizedSNATValue == "none" && containsExpectedString {
return fmt.Errorf("failed: found unexpected %s for SNAT rule: %s", expectedString, rule)
return fmt.Errorf("failed: found unexpected %s for SNAT rule", expectedString)
} else if randomizedSNATValue != "none" && !containsExpectedString {
return fmt.Errorf("failed: did not find expected %s for any of the SNAT rules", expectedString)
return fmt.Errorf("failed: did not find expected %s for any of the SNAT rule", expectedString)
}
return nil
}

0 comments on commit ed56743

Please sign in to comment.