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

event on update_package_repository #200

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

Conversation

h4l0gen
Copy link
Contributor

@h4l0gen h4l0gen commented Apr 8, 2024

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:
This PR triggers sandbox rule update package repository
Which issue(s) this PR fixes:

Fixes #120

Special notes for your reviewer:

@h4l0gen
Copy link
Contributor Author

h4l0gen commented Apr 8, 2024

@leogr @FedeDP Here I first check whether that file exists or not. Then open the file in writing mode. Please provide your feedback and any changes if you need them. Thank You!!

and this rule triggered successfully
image

events/syscall/update_package_repository.go Outdated Show resolved Hide resolved
events/syscall/update_package_repository.go Outdated Show resolved Hide resolved
@leogr
Copy link
Member

leogr commented Apr 8, 2024

@leogr @FedeDP Here I first check whether that file exists or not. Then open the file in writing mode. Please provide your feedback and any changes if you need them. Thank You!!

and this rule triggered successfully image

Does the rule trigger when the file does not exist?
If yes, avoid the check
If not, there're to options when the file does not exist:

  • create (and then cleanup) the file
  • or skip the action (with a proper log message)

@h4l0gen
Copy link
Contributor Author

h4l0gen commented Apr 8, 2024

@leogr The rule is not triggering when the file doesn't exist. So I made these changes:

If the file doesn't exist:

  • Create the file.
  • Open it in write-only mode.
  • Remove the file after processing.

If the file already exists:

  • Open it in write-only mode
  • Close the file after processing

@h4l0gen h4l0gen requested a review from leogr April 8, 2024 12:44
@leogr
Copy link
Member

leogr commented Apr 8, 2024

Thank you,

SGTM. Just, I'd like to get a second look from @FedeDP 🙏

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

LGTM too!

@GLVSKiriti
Copy link
Contributor

GLVSKiriti commented Apr 8, 2024

@FedeDP Cant we write the code like this. WDYT? Like writing defer statement before, bcz it executes after function return so it cleanups perfectly. Also tested with this code and rule triggers fine in both cases file exists and not exists

func UpdatePackageRepository(h events.Helper) error {
	path := "/etc/apt/sources.list"

	// Check if the file exists and remove it if it is created by you
	if _, err := os.Stat(path); err != nil {
		defer os.Remove(path)
	}
	// Open file with writeonly access, create a file if there is none
	if _, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0755)); err != nil {
		return err
	}
	return nil
}


@FedeDP
Copy link
Contributor

FedeDP commented Apr 9, 2024

@GLVSKiriti even if more compact, i think it is less readable:

if _, err := os.Stat(path); err != nil {
		defer os.Remove(path)
	}

defer the remove when stat gives an error is super hard to read if one does not know the full context :)

@h4l0gen
Copy link
Contributor Author

h4l0gen commented Apr 9, 2024

Thank you, @FedeDP, @leogr and @GLVSKiriti, for your feedback. I agree with @FedeDP; as this project continues to evolve and attract new contributors, it's beneficial to maintain readable code as much as possible. Clear code reduces confusion for newcomers, helping them understand it easily.

events/syscall/update_package_repository.go Show resolved Hide resolved
Comment on lines 34 to 36
_, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0755))
if err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

We are not calling Close() in this branch. Is it intended?

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.

Completely agree with this @leogr, we should close it first. While researching about it found this

  • Each open file is associated with a file descriptor, which is a reference to the open file If you remove the file without closing it, the file descriptor may still be in use.

  • It is quite often employed in Unix-only software to store temporary data:

  • A temporary file is created
  • It is then immediately removed on the filesystem (while being kept open).
  • It is then used via the file descriptor for any amount of time.
  • When the descriptor is closed, the filesystem finally reclaims the space the file's data were occupying.

Hence instead of creating space and other potential issues, its preferable to close file first.
@leogr @FedeDP Please provide your thoughts on this.

@h4l0gen h4l0gen requested review from FedeDP and leogr April 9, 2024 12:28
Signed-off-by: h4l0gen <[email protected]>

commits squashed

Signed-off-by: h4l0gen <[email protected]>
@poiana
Copy link

poiana commented Jul 23, 2024

Issues go stale after 90d of inactivity.

Mark the issue as fresh with /remove-lifecycle stale.

Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Provide feedback via https://github.com/falcosecurity/community.

/lifecycle stale

@leogr
Copy link
Member

leogr commented Jul 23, 2024

/remove-lifecycle stale

@leogr
Copy link
Member

leogr commented Jul 23, 2024

Closing and reopening to trigger the CI
/close

@poiana poiana closed this Jul 23, 2024
@poiana
Copy link

poiana commented Jul 23, 2024

@leogr: Closed this PR.

In response to this:

Closing and reopening to trigger the CI
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@leogr
Copy link
Member

leogr commented Jul 23, 2024

/reopen

@poiana poiana reopened this Jul 23, 2024
@poiana
Copy link

poiana commented Jul 23, 2024

@leogr: Reopened this PR.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@poiana
Copy link

poiana commented Jul 23, 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 alacuku 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

@leogr leogr added this to the v0.13.0 milestone Oct 2, 2024
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.

event on update package repository
5 participants