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

CFE-2318: Added findlocalusers() policy function #5714

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

Conversation

victormlg
Copy link
Contributor

@victormlg victormlg commented Mar 12, 2025

@victormlg victormlg requested a review from olehermanse March 12, 2025 16:14

#if defined(HAVE_GETPWENT) && !defined(__ANDROID__)

static FnCallResult FnCallFindLocalUsers(EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs)

Check warning

Code scanning / CodeQL

Poorly documented large function Warning

Poorly documented function: fewer than 2% comments for a function of 126 lines.
@larsewi larsewi self-requested a review March 13, 2025 08:39
Copy link
Contributor

@larsewi larsewi left a comment

Choose a reason for hiding this comment

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

Nice 🚀 Added some comments

@victormlg victormlg force-pushed the CFE-2318-findusers branch 2 times, most recently from 1403e77 to 5a252f7 Compare March 14, 2025 14:24
@victormlg victormlg requested a review from larsewi March 14, 2025 14:25
@victormlg victormlg force-pushed the CFE-2318-findusers branch from 97cde55 to 610913d Compare March 19, 2025 11:00
@victormlg victormlg requested a review from larsewi March 19, 2025 11:00
@olehermanse
Copy link
Member

@cf-bottom jenkins, please

Comment on lines 16 to 31
users:
"test_user1"
policy => "present",
home_dir => "/tmp/test_folder1",
description => "TestUser 1",
group_primary => "users",
shell => "/bin/sh",
uid => "12345";

"test_user2"
policy => "present",
home_dir => "/tmp/test_folder2",
description => "TestUser 2",
group_primary => "users",
shell => "/bin/sh",
uid => "54321";
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an "unsafe" test. If I run the CFEngine acceptance tests, I don't want them to start editing /etc/passwd and adding users on my system. For those kinds of tests which make significant changes to the system they run on, we put them inside of a subdirectory called ./unsafe/ and you have to run the tests with UNSAFE_TESTS=1 to trigger them.

This is also why GitHub Actions is failing currently.

After moving this test, maybe you can make a shorter safe test which just tries to call the function with uid 0, or something. You won't be able to assert so much about the result, but it will be a lot less intrusive.

@olehermanse
Copy link
Member

Jenkins CI with tests: https://ci.cfengine.com/job/pr-pipeline/11854/

@olehermanse olehermanse changed the title CFE-2318: Added findLocalUsers function CFE-2318: Added findlocalusers() policy function Mar 19, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Changelog: Added findlocalusers policy function that returns all the local users matching certain attributes
Ticket: CFE-2318
Signed-off-by: Victor Moene <[email protected]>
@victormlg victormlg force-pushed the CFE-2318-findusers branch from 610913d to 0c0a781 Compare March 19, 2025 15:25
return FnFailure();
}

JsonElement *parent = JsonObjectCreate(10);
Copy link
Contributor

Choose a reason for hiding this comment

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

You will need to destroy this one before every statement that returns failure. Otherwise, the memory is leaked.

Log(LOG_LEVEL_ERR, "Couldn't convert the uid of '%s' to string in function '%s'",
pw->pw_name, fp->name);
return FnFailure();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would still do this as a sanity check

Suggested change
}
}
assert((size_t) val < sizeof(uid_string));

else if (StringEqual(attribute, "uid"))
{
char uid_string[PRINTSIZE(pw->pw_uid) + 1];
int val = snprintf(uid_string, sizeof(uid_string), "%u", pw->pw_uid);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's common convention to name these variables ret or rc (for return code). But val is fine too

"check" usebundle => dcs_check_state(test,
"$(this.promise_filename).expected.json",
$(this.promise_filename));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add trailing newline :)

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

Successfully merging this pull request may close these issues.

5 participants