-
Notifications
You must be signed in to change notification settings - Fork 77
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
Add class for keystone audit middleware testing #1212
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a7c49b
Add class for keystone audit middleware testing
MylesJP df3858d
Add fix for when service and app name differ
MylesJP 38a7c9b
Make corrections as per review comments
MylesJP 3af8d41
Fix dictionary lookup method
MylesJP 2ddde43
Remove redundant cinder tests
MylesJP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Keystone audit middleware. | ||
|
||
Collection of code for setting up and testing Keystone audit middleware | ||
functionality. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Keystone audit middleware API logging testing. | ||
|
||
These methods test the rendering of the charm api-paste.ini file to | ||
ensure the appropriate sections are rendered or not rendered depending | ||
on the state of the audit-middleware configuration option. | ||
""" | ||
|
||
import textwrap | ||
import logging | ||
import zaza.model | ||
import zaza.openstack.charm_tests.test_utils as test_utils | ||
|
||
|
||
class KeystoneAuditMiddlewareTest(test_utils.OpenStackBaseTest): | ||
"""Keystone audit middleware test class.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Run class setup for Keystone audit middleware tests.""" | ||
super(KeystoneAuditMiddlewareTest, cls).setUpClass() | ||
test_config = cls.test_config['tests_options']['audit-middleware'] | ||
cls.service_name = test_config['service'] | ||
|
||
if test_config['application']: | ||
cls.application_name = test_config['application'] | ||
else: | ||
cls.application_name = cls.service_name | ||
logging.info('Application name not set, using service name:\ | ||
%s', cls.service_name) | ||
|
||
cls.initial_audit_middleware = zaza.model.get_application_config( | ||
cls.application_name)['audit-middleware']['value'] | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
"""Restore the audit-middleware configuration to its original state.""" | ||
super(KeystoneAuditMiddlewareTest, cls).tearDownClass() | ||
logging.info("Running teardown on %s" % cls.application_name) | ||
zaza.model.set_application_config( | ||
cls.application_name, | ||
{'audit-middleware': str(cls.initial_audit_middleware)}, | ||
model_name=cls.model_name | ||
) | ||
zaza.model.wait_for_application_states( | ||
states={cls.application_name: { | ||
'workload-status': 'active', | ||
'workload-status-message': 'Unit is ready'}}, | ||
model_name=cls.model_name | ||
) | ||
|
||
def fetch_api_paste_content(self): | ||
"""Fetch content of api-paste.ini file.""" | ||
api_paste_ini_path = f"/etc/{self.service_name}/api-paste.ini" | ||
lead_unit = zaza.model.get_lead_unit_name( | ||
self.application_name, | ||
model_name=self.model_name | ||
) | ||
try: | ||
return zaza.model.file_contents( | ||
lead_unit, | ||
api_paste_ini_path, | ||
) | ||
except zaza.model.CommandRunFailed as e: | ||
self.fail("Error fetching api-paste.ini: %s" % e) | ||
|
||
def test_101_apipaste_includes_audit_section(self): | ||
"""Test api-paste.ini renders audit section when enabled.""" | ||
expected_content = textwrap.dedent(f"""\ | ||
[filter:audit] | ||
paste.filter_factory = keystonemiddleware.audit:filter_factory | ||
audit_map_file = /etc/{self.service_name}/api_audit_map.conf | ||
service_name = {self.service_name} | ||
""") | ||
|
||
set_default = {'audit-middleware': False} | ||
set_alternate = {'audit-middleware': True} | ||
|
||
with self.config_change(default_config=set_default, | ||
alternate_config=set_alternate, | ||
application_name=self.application_name): | ||
api_paste_content = self.fetch_api_paste_content() | ||
self.assertIn(expected_content, api_paste_content) | ||
|
||
def test_102_apipaste_excludes_audit_section(self): | ||
"""Test api_paste.ini does not render audit section when disabled.""" | ||
section_heading = '[filter:audit]' | ||
set_default = {'audit-middleware': True} | ||
set_alternate = {'audit-middleware': False} | ||
|
||
with self.config_change(default_config=set_default, | ||
alternate_config=set_alternate, | ||
application_name=self.application_name): | ||
api_paste_content = self.fetch_api_paste_content() | ||
self.assertNotIn(section_heading, api_paste_content) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fancy, I like it :-)