Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding an event on unexpected_k8s_nodeport_connection #199

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

h4l0gen
Copy link
Contributor

@h4l0gen h4l0gen commented Apr 5, 2024

adding an event on unexpected_k8s_nodeport_connection

What type of PR is this?

Uncomment one (or more) /kind <> lines:

/kind bug

/kind cleanup

/kind documentation

/kind tests

/kind feature

Any specific area of the project related to this PR?

Uncomment one (or more) /area <> lines:

/area commands

/area pkg

/area events

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #166

Special notes for your reviewer:

@poiana
Copy link

poiana commented Apr 5, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: h4l0gen
Once this PR has been reviewed and has the lgtm label, please assign fededp for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana added the size/M label Apr 5, 2024
@h4l0gen
Copy link
Contributor Author

h4l0gen commented Apr 5, 2024

@leogr @FedeDP This event triggered rule successfully
WhatsApp Image 2024-04-05 at 07 48 01_80edadc9

events/syscall/unexpected_k8s_nodeport_connection.go Outdated Show resolved Hide resolved
Comment on lines 34 to 35
cmd := exec.Command("ip", "route", "show", "default")
hostEth0IP, err := cmd.Output()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: are we sure there's no way to obtain this just using Go code (ie. without using an external util, like ip)?
If yes, it would be preferable.
If not, ok to use ip (unless @FedeDP has a better idea 😇 )

Copy link
Contributor Author

@h4l0gen h4l0gen Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leogr I guess we can do it using net module. I will make changes and come up with solution. Thank You leogr for suggestion. @FedeDP Please tell us if you have any other good approach!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leogr I made this change,
I've added a new function getHostEth0IP(), It

  • uses the net package to find the host's eth0 IP address.
  • iterates through the network interfaces
  • finds the "eth0" interface
  • then iterates through its associated IP addresses to find the first non-loopback IPv4 address.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this event inside container, and it is working fine and triggering rule
Screenshot from 2024-04-12 16-09-45

events/syscall/unexpected_k8s_nodeport_connection.go Outdated Show resolved Hide resolved
@h4l0gen h4l0gen requested a review from leogr April 12, 2024 11:10
Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Just left two comments

Comment on lines 50 to 53
func getHostEth0IP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}

for _, iface := range ifaces {
if iface.Name == "eth0" {
addrs, err := iface.Addrs()
if err != nil {
return "", err
}

for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
}
}

return "", errors.New("eth0 interface not found or has no valid IPv4 address")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this function to another file (i.e. utils_net.go)?

This will improve reusability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @leogr I shifted getHosteth0IP function to utils_net.go under events/syscall

return err
}

cmd := exec.Command("nc", hostIP, strconv.Itoa(port), "<", "/dev/null")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check for nc existence before using it?

@poiana poiana added size/L and removed size/M labels Apr 12, 2024
@h4l0gen h4l0gen requested a review from leogr April 12, 2024 16:47
Reason: "netcat utility not found in path",
}
}
cmd := exec.Command(path, hostIP, strconv.Itoa(port), "<", "/dev/null")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmd := exec.Command(path, hostIP, strconv.Itoa(port), "<", "/dev/null")
cmd := exec.Command(path, hostIP, strconv.Itoa(port), "<", "/dev/null")

Can the same result be achieved just using go code (without using an external tool)? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure @leogr let me try..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to use UDP connection from go/net package instead of netcat. Please take a look.

Signed-off-by: h4l0gen <ks3913688@gmail.com>

adding an event on unexpected_k8s_nodeport_connection

Signed-off-by: h4l0gen <ks3913688@gmail.com>

final

Signed-off-by: h4l0gen <ks3913688@gmail.com>
Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. @FedeDP wdyt?

@h4l0gen h4l0gen force-pushed the unexpected_k8s_nodeport_connection branch from 15a1ba1 to 0e969b6 Compare April 23, 2024 09:37
}

for _, iface := range ifaces {
if iface.Name == "eth0" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be eth0? Can't we just return first actually connected interface?

Side note: systemd implements predictable network interafce names since ages and most of the time eth0 won't be present on a system.
See https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes fedeDP, according to article this seems a problem. I will try to implement suggested solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

adding event on unexpected k8s nodeport connection
4 participants