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

validation: improve error message when failing to load a TracingPolicy #2031

Merged
merged 1 commit into from
Jan 31, 2024

Conversation

christian-2
Copy link
Contributor

Callers of tracingpolicy.FromFile() no longer receive highly specific errors, but a generic error, which can also be presented to end users. Callers of tracingpolicy.FromJSON() still receive the specific errors, and any generic error wraps a specific one.

The implementation is along the lines suggested by @mtardy in #2022. (Thanks for that input.) Please indicate if my use of Panicf() in pkg/tracingpolicy/generictracingpolicy_test.go is unwelcome.

BTW, it seems to me that drop-privileges should be added to contrib/tester-progs/.gitignore. I could create another PR for that, if applicable (plus an issue, if necessary for the trivial change).

Fixes #2022

Signed-off-by: Christian Hörtnagl christian2@univie.ac.at

@christian-2 christian-2 requested a review from a team as a code owner January 27, 2024 19:50
func createTempFile(data string) string {
file, err := ioutil.TempFile("", "*")
if err != nil {
logger.GetLogger().Panic("cannot create temporary file")
Copy link
Member

Choose a reason for hiding this comment

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

Here just return an error, then in the caller do t.Fatalf() in case

Copy link
Member

Choose a reason for hiding this comment

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

yeah don't use the logger and panic in tests, if you have t *testing.T available (see my other remark) just call t.Fatal if you want to stop this test here because of the error.

Copy link
Member

@tixxdz tixxdz left a comment

Choose a reason for hiding this comment

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

So lgtm, just a minor fix in the test so we fatal in the test getting the context.

Also for that .gitignore yes another PR is welcome ;-)

Thank you!

@tixxdz tixxdz added the release-note/minor This PR introduces a minor user-visible change label Jan 29, 2024
@tixxdz
Copy link
Member

tixxdz commented Jan 29, 2024

Some CI code format are failing too. Thank you

@mtardy
Copy link
Member

mtardy commented Jan 29, 2024

BTW, it seems to me that drop-privileges should be added to contrib/tester-progs/.gitignore. I could create another PR for that, if applicable (plus an issue, if necessary for the trivial change).

Also for that .gitignore yes another PR is welcome ;-)

Thanks a lot that's nice, an issue is not needed for a trivial change like that!

Copy link
Member

@mtardy mtardy left a comment

Choose a reason for hiding this comment

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

Welcome to contributing to Tetragon! 🤗

That's very nice of you to take the time to add tests for invalid tracing policies. I have a few minor comments, hope it's helpful with Djalal's review as well.

Comment on lines 552 to 548
func deleteTempFile(path string) {
err := os.Remove(path)
if err != nil {
logger.GetLogger().Panicf("cannot remove temporary file %q", path)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

you can remove this function by passing t *testing.T to your createTempFile function and register your cleanup function t.Cleanup(f func()). It's a bit like a defer statement but that executes when the test or subtest is done.

func createTempFile(data string) string {
file, err := ioutil.TempFile("", "*")
if err != nil {
logger.GetLogger().Panic("cannot create temporary file")
Copy link
Member

Choose a reason for hiding this comment

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

yeah don't use the logger and panic in tests, if you have t *testing.T available (see my other remark) just call t.Fatal if you want to stop this test here because of the error.

@@ -527,3 +529,82 @@ func TestYamlNamespaced(t *testing.T) {
_, ok := tp.(TracingPolicyNamespaced)
require.True(t, ok)
}

func createTempFile(data string) string {
file, err := ioutil.TempFile("", "*")
Copy link
Member

Choose a reason for hiding this comment

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

As the linter said, you can use https://pkg.go.dev/os#CreateTemp here, you can add a prefix or a suffix with tetragon in it btw it can be useful in case the temp file fails to cleanup, not necessary since it should be in tmp but still it's nice. Maybe you can find other example in the codebase, not sure!

}
err = file.Close()
if err != nil {
logger.GetLogger().Panicf("cannot close temporary file %q", path)
Copy link
Member

Choose a reason for hiding this comment

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

maybe you can just T.Fail here or just warn since it's not a big deal if we fail to close that file, and should not happen that often.

christian-2 added a commit to christian-2/tetragon that referenced this pull request Jan 29, 2024
Incorporates reviewer's comments on PR cilium#2031

Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
@christian-2
Copy link
Contributor Author

@tixxdz @mtardy thanks for your constructive comments; they should be on board now.

christian-2 added a commit to christian-2/tetragon that referenced this pull request Jan 29, 2024
As agreed in PR cilium#2031

Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
Copy link
Member

@mtardy mtardy left a comment

Choose a reason for hiding this comment

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

Here are a few comments, could you also squash your commits? You can use:

git rebase -i <first commit of PR>^
# mark commit to squash as s
# if you are happy with the rebase, you can check with git log and git show
git push origin <your branch> --force-with-lease

Comment on lines 548 to 553
t.Cleanup(func() {
err := os.Remove(path)
if err != nil {
t.Error(err)
}
})
Copy link
Member

Choose a reason for hiding this comment

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

since you are using file, err := os.CreateTemp(t.TempDir(), "tetragon-"), you can remove the cleanup part since the TempDir folder is already automatically removed:

TempDir returns a temporary directory for the test to use. The directory is automatically removed by Cleanup when the test and all its subtests complete.

func createTempFile(t *testing.T, data string) string {
file, err := os.CreateTemp(t.TempDir(), "tetragon-")
if err != nil {
t.Error(err)
Copy link
Member

Choose a reason for hiding this comment

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

I think you should use Fatal here since you cannot recover from this and you should stop the test here.


_, err = file.WriteString(data)
if err != nil {
t.Error(err)
Copy link
Member

Choose a reason for hiding this comment

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

Same I think you should use Fatal here.

}
})

return path
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
return path
return file.Name()

if err != nil {
t.Error(err)
}
path := file.Name()
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
path := file.Name()

christian-2 added a commit to christian-2/tetragon that referenced this pull request Jan 31, 2024
As agreed in PR cilium#2031

Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
@christian-2
Copy link
Contributor Author

@mtardy I've taken your suggestions from the second round of reviewing on board now. The only difference is that I opted for testing.assert.NoError() instead of testing.Fatal(); both should stop the test's execution.

@mtardy
Copy link
Member

mtardy commented Jan 31, 2024

@mtardy I've taken your suggestions from the second round of reviewing on board now. The only difference is that I opted for testing.assert.NoError() instead of testing.Fatal(); both should stop the test's execution.

Yeah it's fine.

One can argue that it should not be an assert.NoErrro() since it's not strictly speaking a test error, meaning the test found something that is wrong but that the test machinery failed. In other words the test did not uncover an issue but just failed in itself. But honestly, this is just a nit at this stage, let's merge this as-is, it's not a big deal!

mtardy pushed a commit that referenced this pull request Jan 31, 2024
As agreed in PR #2031

Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
@christian-2
Copy link
Contributor Author

@mtardy Yep. I'd propose I'll consider this if we shall need another round of changes. All your input on this issue has been very constructive and helpful so far, so I'd have absolutely no problem with that. Thanks again.

@mtardy
Copy link
Member

mtardy commented Jan 31, 2024

@mtardy Yep. I'd propose I'll consider this if we shall need another round of changes. All your input on this issue has been very constructive and helpful so far, so I'd have absolutely no problem with that. Thanks again.

it's up to you, we can merge as-is if you want.

@christian-2
Copy link
Contributor Author

Then I'd rather revert to testing.Fatal(), it's the (slightly) better choice also IMO. Just a sec ... ;)

Callers of tracingpolicy.FromFile() no longer receive highly specific
errors, but a generic error, which can also be presented to end users.
Callers of tracingpolicy.FromJSON() still receive the specific errors,
and any generic error wraps a specific one.

Signed-off-by: Christian Hörtnagl <christian2@univie.ac.at>
Copy link
Member

@mtardy mtardy left a comment

Choose a reason for hiding this comment

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

Thanks again! 🎉

@mtardy mtardy merged commit 1da0a48 into cilium:main Jan 31, 2024
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/minor This PR introduces a minor user-visible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

validation: improve error message when failing to load a TracingPolicy
3 participants