|
9 | 9 | "bytes" |
10 | 10 | "context" |
11 | 11 | "fmt" |
| 12 | + "net" |
12 | 13 | "net/http" |
13 | 14 | "net/http/httptest" |
14 | 15 | "os" |
@@ -575,6 +576,21 @@ func TestNginxConfigParser_Parse(t *testing.T) { |
575 | 576 | allowedDirectories: []string{dir}, |
576 | 577 | expectedLog: "Found NAP syslog server", |
577 | 578 | }, |
| 579 | + { |
| 580 | + name: "Test 10a: NAP V5 syslog server without docker0 interface", |
| 581 | + instance: protos.NginxPlusInstance([]string{}), |
| 582 | + content: testconfig.NginxConfigWithMultipleSysLogs(errorLog.Name(), accessLog.Name(), |
| 583 | + "192.168.12.34:1517", "my.domain.com:1517", "192.0.10.1:1514"), |
| 584 | + expectedConfigContext: modelHelpers.ConfigContextWithSysLog( |
| 585 | + accessLog.Name(), |
| 586 | + errorLog.Name(), |
| 587 | + protos.NginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(), |
| 588 | + "", // Empty because docker0 interface not found and 192.0.10.1 is not localhost |
| 589 | + ), |
| 590 | + allowedDirectories: []string{dir}, |
| 591 | + expectedLog: "Could not find available local NGINX App Protect syslog server " + |
| 592 | + "configured on port 1514. Security violations will not be collected.", |
| 593 | + }, |
578 | 594 | { |
579 | 595 | name: "Test 11: Unavailable NAP syslog server", |
580 | 596 | instance: protos.NginxPlusInstance([]string{}), |
@@ -732,18 +748,64 @@ func TestNginxConfigParser_SyslogServerParse(t *testing.T) { |
732 | 748 | } |
733 | 749 |
|
734 | 750 | func TestNginxConfigParser_findValidSysLogServers(t *testing.T) { |
735 | | - servers := []string{ |
736 | | - "syslog:server=192.168.12.34:1517", "syslog:server=my.domain.com:1517", "syslog:server=127.0.0.1:1514", |
737 | | - "syslog:server=localhost:1516", "syslog:server=localhost:1514", "syslog:server=127.255.255.255:1517", |
738 | | - } |
739 | | - expected := []string{"", "", "127.0.0.1:1514", "", "localhost:1514", ""} |
740 | | - ncp := NewNginxConfigParser(types.AgentConfig()) |
| 751 | + // Test with no docker0 interface (empty docker0IP) |
| 752 | + t.Run("without docker0 interface", func(t *testing.T) { |
| 753 | + servers := []string{ |
| 754 | + "syslog:server=192.168.12.34:1517", "syslog:server=my.domain.com:1517", "syslog:server=127.0.0.1:1514", |
| 755 | + "syslog:server=localhost:1516", "syslog:server=localhost:1514", "syslog:server=127.255.255.255:1517", |
| 756 | + "syslog:server=192.0.10.1:1514", |
| 757 | + } |
| 758 | + // When docker0IP is empty, only localhost and loopback addresses should match |
| 759 | + expected := []string{"", "", "127.0.0.1:1514", "", "localhost:1514", "", ""} |
| 760 | + ncp := NewNginxConfigParser(types.AgentConfig()) |
| 761 | + ncp.docker0IP = "" // Simulate no docker0 interface |
741 | 762 |
|
742 | | - for i, server := range servers { |
743 | | - result := ncp.findLocalSysLogServers(server) |
| 763 | + for i, server := range servers { |
| 764 | + result := ncp.findLocalSysLogServers(server) |
744 | 765 |
|
745 | | - assert.Equal(t, expected[i], result) |
746 | | - } |
| 766 | + assert.Equal(t, expected[i], result) |
| 767 | + } |
| 768 | + }) |
| 769 | + |
| 770 | + // Test with custom docker0 IP |
| 771 | + t.Run("with custom docker0 IP", func(t *testing.T) { |
| 772 | + ncp := NewNginxConfigParser(types.AgentConfig()) |
| 773 | + // Override the docker0IP with a custom value for testing |
| 774 | + ncp.docker0IP = "172.17.0.1" |
| 775 | + |
| 776 | + servers := []string{ |
| 777 | + "syslog:server=172.17.0.1:1514", // should match custom docker0 IP |
| 778 | + "syslog:server=192.0.10.1:1514", // should NOT match (old default) |
| 779 | + "syslog:server=127.0.0.1:1514", // should match localhost |
| 780 | + "syslog:server=172.17.0.1:1515", // wrong port |
| 781 | + "syslog:server=172.17.0.2:1514", // wrong IP |
| 782 | + } |
| 783 | + expected := []string{"172.17.0.1:1514", "", "127.0.0.1:1514", "", ""} |
| 784 | + |
| 785 | + for i, server := range servers { |
| 786 | + result := ncp.findLocalSysLogServers(server) |
| 787 | + |
| 788 | + assert.Equal(t, expected[i], result, "server: %s", server) |
| 789 | + } |
| 790 | + }) |
| 791 | + |
| 792 | + // Test with another docker0 IP variation |
| 793 | + t.Run("with docker0 IP 172.18.0.1", func(t *testing.T) { |
| 794 | + ncp := NewNginxConfigParser(types.AgentConfig()) |
| 795 | + ncp.docker0IP = "172.18.0.1" |
| 796 | + |
| 797 | + servers := []string{ |
| 798 | + "syslog:server=172.18.0.1:1514", |
| 799 | + "syslog:server=localhost:1514", |
| 800 | + } |
| 801 | + expected := []string{"172.18.0.1:1514", "localhost:1514"} |
| 802 | + |
| 803 | + for i, server := range servers { |
| 804 | + result := ncp.findLocalSysLogServers(server) |
| 805 | + |
| 806 | + assert.Equal(t, expected[i], result) |
| 807 | + } |
| 808 | + }) |
747 | 809 | } |
748 | 810 |
|
749 | 811 | func TestNginxConfigParser_checkLog(t *testing.T) { |
@@ -1522,6 +1584,23 @@ func TestNginxConfigParser_checkDuplicate(t *testing.T) { |
1522 | 1584 | } |
1523 | 1585 | } |
1524 | 1586 |
|
| 1587 | +func TestGetDocker0IP(t *testing.T) { |
| 1588 | + t.Run("getDocker0IP returns valid IP or empty string", func(t *testing.T) { |
| 1589 | + ip := getDocker0IP() |
| 1590 | + |
| 1591 | + // The function should return either: |
| 1592 | + // 1. A valid IP address if docker0 interface exists |
| 1593 | + // 2. Empty string if docker0 doesn't exist |
| 1594 | + if ip != "" { |
| 1595 | + // If an IP is returned, validate it's a proper IPv4 address |
| 1596 | + parsedIP := net.ParseIP(ip) |
| 1597 | + assert.NotNil(t, parsedIP, "should return a valid IP address") |
| 1598 | + assert.NotNil(t, parsedIP.To4(), "should be an IPv4 address") |
| 1599 | + } |
| 1600 | + // Empty string is also valid when docker0 doesn't exist |
| 1601 | + }) |
| 1602 | +} |
| 1603 | + |
1525 | 1604 | func TestNginxConfigParser_parseIncludeDirective(t *testing.T) { |
1526 | 1605 | parser := NewNginxConfigParser(types.AgentConfig()) |
1527 | 1606 |
|
|
0 commit comments