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

Newly added test cases : disable/enable audit_logging in libvirtd.log #4421

Merged

Conversation

chunfuwen
Copy link
Contributor

Newly added test cases : disable/enable audit_logging in libvirtd.log

audit_logging = 1 to let audit messages also be sent via libvirt logging infrastructure as well as the auditd logging

Signed-off-by: chunfuwen chwen@redhat.com

@chunfuwen
Copy link
Contributor Author

DATA (filename=output.expected) => NOT FOUND (data sources: variant, test, file)
DATA (filename=stdout.expected) => NOT FOUND (data sources: variant, test, file)
DATA (filename=stderr.expected) => NOT FOUND (data sources: variant, test, file)
PASS 1-type_specific.io-github-autotest-libvirt.conf_file.libvirtd_conf.set_audit_logging.positive_test.enable_audit_log

cleaning libvirtd logs...
DATA (filename=output.expected) => NOT FOUND (data sources: variant, test, file)
DATA (filename=stdout.expected) => NOT FOUND (data sources: variant, test, file)
DATA (filename=stderr.expected) => NOT FOUND (data sources: variant, test, file)
PASS 1-type_specific.io-github-autotest-libvirt.conf_file.libvirtd_conf.set_audit_logging.positive_test.disable_audit_log

@chunfuwen chunfuwen force-pushed the add_enable_disable_audit_log_cases branch from 0074887 to 279e69a Compare August 15, 2022 06:49
@chunfuwen
Copy link
Contributor Author

[xx-185265] [audit_logging] Disable/enable audit_logging in libvirtd.log

@chunfuwen
Copy link
Contributor Author

@yafu-1 , please help take a review

vm.start()

# Check audit daemon service status
check_auditd_service()
Copy link
Contributor

Choose a reason for hiding this comment

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

How about mv 'check_auditd_service()' before start vm and can try to start auditd service if service is not running.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yafu-1
updated as suggested

@chunfuwen chunfuwen force-pushed the add_enable_disable_audit_log_cases branch from 279e69a to 1d207f7 Compare August 26, 2022 06:58
Copy link
Contributor

@yafu-1 yafu-1 left a comment

Choose a reason for hiding this comment

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

LGTM

@chunfuwen
Copy link
Contributor Author

@dzhengfy
please help take a review

@chunfuwen chunfuwen requested a review from dzhengfy September 1, 2022 01:09
Copy link

@vasekhodina vasekhodina left a comment

Choose a reason for hiding this comment

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

I have selected this PR randomly for my first review. I like how the functions are named and generally that the code doesn't jump between abstraction layers in one function. I have added a few minor suggestions.

:return: enable_audit_logging: bool indicating enable/disable audit_logging
"""
log_outputs = "1:file:%s" % log_config_path
libvirtd_config.log_outputs = log_outputs

Choose a reason for hiding this comment

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

Wouldn't it be sufficient to have libvirtd_config = "1:file:%s" % log_config_path instead of log_outputs var that is not used anywhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

"""
Configure audit log level"

:param params: dict wrapped with params

Choose a reason for hiding this comment

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

There is no params parameter to this function. Remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove this line

Configure audit log level"

:param params: dict wrapped with params
:return: enable_audit_logging: bool indicating enable/disable audit_logging

Choose a reason for hiding this comment

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

There is no return statement in this function. The return word here would be better as :param since enable_audit_logging is a parameter to this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove return statement since there is nothing returned

status = service_mgr.status(service_name)
LOG.debug('Service status is %s', status)
if not status:
service_mgr.start(service_name)

Choose a reason for hiding this comment

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

Isn't the clean code rule of "Functions should do one thing. They should do it well. They should do it only." broken here since we're not only checking but also starting the service?

Wouldn't it be better to move lines 52 and 53 out of this function?
OR
Rename the function to "ensure_auditd_started()"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rename the function name to ensure_auditd_started

"""
Check audit message in libvirtd log file
"""
str_to_grep = "virDomainAudit"

Choose a reason for hiding this comment

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

Wouldn't this be better as a function argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated with str_to_grep as function parameter

process.run(cmd, ignore_status=True,
shell=True)

def check_msg_in_libvirtd_log():

Choose a reason for hiding this comment

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

Wouldn't it be better to move this function definition and well as all other function definitions outside of the run() function? It would make run() function a lot smaller.

Copy link
Contributor Author

@chunfuwen chunfuwen Sep 5, 2022

Choose a reason for hiding this comment

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

@vasekhodina
Before team members had some discussion about which is better whether function is outside run or inside run.
There is no conclusion for this, and it is decided by individual to trade off this.
1)Outside run, it may need pass more function parameter since those function can not share variables already in run method
2) Inside run, it may look like run is full busy code, but if functions are relative small, it sound ok, and it also can save parameter passing

Choose a reason for hiding this comment

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

Ok, I understand. Thank you for the explanation.

vm.start()
# Check virt type in audit.log
check_virt_type_from_audit_log()
# Check audit msg in libvirtd log

Choose a reason for hiding this comment

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

I think the comment here and on line 100 is redundant and should be deleted. It says the same thing as the function name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated by removing comments

@chunfuwen chunfuwen force-pushed the add_enable_disable_audit_log_cases branch from 1d207f7 to 97aac82 Compare September 5, 2022 07:20
@chunfuwen chunfuwen requested review from vasekhodina and yafu-1 and removed request for vasekhodina and yafu-1 September 5, 2022 07:22
@chunfuwen
Copy link
Contributor Author

@vasekhodina
comments are addressed, please double check

@chunfuwen chunfuwen force-pushed the add_enable_disable_audit_log_cases branch from 97aac82 to 74b702a Compare September 5, 2022 07:25

def ensure_auditd_started():
"""
Check audit service status"

Choose a reason for hiding this comment

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

I think it would be good to change the comment to Check audit service status and start it if it's not running.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vasekhodina updated as suggested

@chunfuwen chunfuwen force-pushed the add_enable_disable_audit_log_cases branch from 74b702a to 6c3dc7d Compare September 13, 2022 08:09
@chunfuwen
Copy link
Contributor Author

@dzhengfy , please help take a review also since there is coming cases, which depends on this

@chunfuwen chunfuwen requested review from vasekhodina and yafu-1 and removed request for vasekhodina September 13, 2022 08:11
Copy link

@vasekhodina vasekhodina left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -0,0 +1,10 @@
- conf_file.libvirtd_conf.set_audit_logging:
Copy link
Contributor

Choose a reason for hiding this comment

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

Refer to other naming style, we do not add verb in the fie name. So would you consider to change it like audit_log.cfg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dzhengfy
I have no idea why file name don't add verb, could you point to me the link?
In libvirt virtualization, update/attach/detach/set/enable/disable are fundamental features and API exposed by libvirt.
So I am confused why not those key actions can be not in filename

variants:
- positive_test:
variants:
- disable_audit_log:
Copy link
Contributor

Choose a reason for hiding this comment

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

- disable: is clear enough

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dzhengfy
variants name will become part of test case
In this specific situation, the test case is related short, it looks like libvirtd_conf.set_audit_logging.positive_test.enable_audit_log is
not worse than libvirtd_conf.set_audit_logging.positive_test.enable in human friendly and human readable ways

variants:
- disable_audit_log:
enable_audit = no
- enable_audit_log:
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dzhengfy
see previous replied comment

Check whether virt type: VIRT_CONTROL|VIRT_MACHINE_ID|VIRT_RESOURCE in audit.log
"""
cmd = 'cat /var/log/audit/audit.log |grep -E "type=VIRT_CONTROL|VIRT_MACHINE_ID|VIRT_RESOURCE"'
process.system_output(cmd, shell=True, verbose=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems this line is duplicated. Remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, remove it

cmd = 'cat /var/log/audit/audit.log |grep -E "type=VIRT_CONTROL|VIRT_MACHINE_ID|VIRT_RESOURCE"'
process.system_output(cmd, shell=True, verbose=True)
if process.system(cmd, ignore_status=True, shell=True):
test.fail("Check virt type failed with %s" % cmd)
Copy link
Contributor

Choose a reason for hiding this comment

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

Before test.fail(), we'd better to log the content of 'cat /var/log/audit/audit.log'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dzhengfy
/var/log/audit/audit.log' has large numbers of logs, which may distract troubleshooter's mind if all dump into output console
By the way, the command : /var/log/audit/audit.log clearly indicate if you want all log, just cat existed log file:/var/log/audit/audit.log

Clean up audit message in log file.
"""
cmd = "> /var/log/audit/audit.log; > %s" % log_config_path
process.run(cmd, ignore_status=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

ignore_status=True should be False because there is no code to check the return result.

Copy link
Contributor Author

@chunfuwen chunfuwen Sep 15, 2022

Choose a reason for hiding this comment

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

@dzhengfy
if this is called at the beginning of test case, log_config_path may not exist at that time, so it may not be proper to check process.run
,in other situation, for example at end of test case, it is proper to check that.
Personally thinking,whether clean up succeed or not has no direct impact on test case itself

By the way, I also met similar situation during my review, see avocado-framework/avocado-vt#3521

enable_audit_logging = "yes" == params.get("enable_audit", "no")

# Get LibvirtdConfig or VirtQemuConfig
log_config_path = os.path.join(data_dir.get_data_dir(), "libvirtd.log")
Copy link
Contributor

Choose a reason for hiding this comment

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

log file could be put into get_tmp_dir() where you need not to clean it by yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

:param enable_audit_logging: bool indicating enable/disable audit_logging
"""
libvirtd_config.log_outputs = "1:file:%s" % log_config_path
libvirtd_config.log_level = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Above two lines are unnecessary because the avocado-vt env_process.py has enabled the debug log by default and you could get the default log file path via utils_misc.get_path(test.debugdir, "libvirtd.log") easily. And you need to define libvirtd_debug_level = '1' in your cfg. That is all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dzhengfy
I think we can think of libvirtd enable in specific context.
In this specific test case,
Here libvirtd config is critical part of this case itself, so put each necessary libvirtd config in one block will give reader one whole picture what are needed, rather than distributed the config by define libvirtd_debug_level = '1' in your cfg and utils_misc.get_path(test.debugdir, "libvirtd.log").
In general case, e.g where it just search message from libvirtd.log, it is proper to just use utils_misc.get_path(test.debugdir, "libvirtd.log") to get the path of libvirtd log file, and then grep it from this libvirtd log file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vasekhodina , what's your opinion ? since I noticed you have some thoughts about clean code

Copy link

@vasekhodina vasekhodina Sep 20, 2022

Choose a reason for hiding this comment

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

I agree with Chunfu.
I think we can apply one sentence from the Zen of Python: Explicit is better than implicit. So the way I understand it if it's important for the test to have the specific log location, then having it in one block and one var is better.
Anyone reading the code later can see the var log_config_path, but they might not know they can call the function utils_misc.get_path(test.debugdir, "libvirtd.log") to get the log.

Plus the log location is already configured on line 83, so if I would do any changes I would do them there.

audit_logging = 1 to let audit messages also be sent via libvirt logging infrastructure as well as the auditd logging

Signed-off-by: chunfuwen <chwen@redhat.com>
@chunfuwen chunfuwen force-pushed the add_enable_disable_audit_log_cases branch from 6c3dc7d to 1ebf2a5 Compare September 15, 2022 01:44
@chunfuwen chunfuwen requested review from dzhengfy and removed request for yafu-1 September 15, 2022 01:48
Copy link

@vasekhodina vasekhodina left a comment

Choose a reason for hiding this comment

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

LGTM

@dzhengfy dzhengfy merged commit 4874a87 into autotest:master Sep 21, 2022
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.

4 participants