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

go-selinux: some cleanup/refactor and improve test coverage #190

Merged
merged 3 commits into from
Oct 12, 2022

Conversation

thaJeztah
Copy link
Member

See individual commits for details

@thaJeztah thaJeztah marked this pull request as draft October 10, 2022 21:48
@thaJeztah thaJeztah marked this pull request as ready for review October 10, 2022 22:04
Comment on lines 28 to 29
if err := SetFileLabel(tmp, con); err != nil {
err = SetFileLabel(tmp, con)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand the reason for this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

not a huge one; my editor's linter warns about variables that are shadowed; in this case an err already existed. I could just remove the :, but thought it's more clear to break it in two lines if we're reusing the variable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, in this case the linter warning should be ignored. Yes, we have a variable named err already, and yes, here we create another err which shadows the original one. No, there is no issue here.

Or maybe just remove the :.

Comment on lines 517 to 519
if err := Chcon(dir, con, true); err != nil {
err = Chcon(dir, con, true)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

@@ -549,30 +549,30 @@ func (l *level) parseLevel(levelStr string) error {

// rangeStrToMLSRange marshals a string representation of a range.
func rangeStrToMLSRange(rangeStr string) (*mlsRange, error) {
mlsRange := &mlsRange{}
mlsrange := &mlsRange{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we just use range (or even r) here?

Copy link
Member Author

Choose a reason for hiding this comment

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

range is a reserved keyboard. Can use r if we think it's clear enough

@@ -712,11 +712,11 @@ func readWriteCon(fpath string, val string) (string, error) {

// peerLabel retrieves the label of the client on the other side of a socket
func peerLabel(fd uintptr) (string, error) {
label, err := unix.GetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_PEERSEC)
lbl, err := unix.GetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_PEERSEC)
Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps just l?

Comment on lines 737 to 738
if lvl := c["level"]; lvl != "" {
return c["user"] + ":" + c["role"] + ":" + c["type"] + ":" + lvl
Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps just l?

@@ -606,17 +606,17 @@ func bitsetToStr(c *big.Int) string {
return str
}

func (l1 *level) equal(l2 *level) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like l1 and l2 more in this case, since we compare them. A linter is not always right.

Copy link
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

Commits 1 and 4 LGTM, left a few comments for 2 and 3.

@kolyshkin
Copy link
Collaborator

One thing that bothers me if those linter warnings you fix here will resurface after some time, as apparently we don't have those linters enabled in CI. So, if there is a linter in golangci-lint that warns of these, the PR would make more sense.

The (set)xxxLabel() functions all were shallow wrappers around writeCon() and
readCon(), which as a result both added a bit too much abstraction, but also
had to all be stubbed separately.

This patch inlines their code to reduce the abstraction and to reduce the stubs.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Rename variables that collided with imports, package variables
or types/functions, and updated some code that was shadowing
other variables.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- add missing asserts
- use t.Error() instead of t.Fatal() to not fail early

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah
Copy link
Member Author

@kolyshkin updated; moved some of the changes to #191, which enables more linters (some of which catching the shadowing issues, although I haven't enabled "strict" mode, which seems to closer match what my IDE shows)

@rhatdan
Copy link
Collaborator

rhatdan commented Oct 12, 2022

LGTM

@rhatdan rhatdan merged commit 80d15ea into opencontainers:main Oct 12, 2022
@thaJeztah thaJeztah deleted the selinux_cleanup branch October 12, 2022 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants