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

cc_ca_certs.py: fix blank line problem when removing CAs and adding n… #483

Merged
merged 7 commits into from
Jul 15, 2020

Conversation

dermotbradley
Copy link
Contributor

…ew one.

Problem: When cc_ca_certs configuration has both "remove-defaults: true"
and also specifies one, or more, new trusted CAs to add then the resultant
/etc/ca-certificates.conf file's 1st line is blank. As noted in comments
in the existing cc_ca_certs.py code blank lines in this file cause problems.

Fix: Before adding the cloud-init CA filename to this file first check the
size of the file - if is is empty (as all existing CAs have been deleted)
then write only the cloud-init CA filename to the file rather than appending
it to the file.

…ew one.

Problem: When cc_ca_certs configuration has both "remove-defaults: true"
and also specifies one, or more, new trusted CAs to add then the resultant
/etc/ca-certificates.conf file's 1st line is blank. As noted in comments
in the existing cc_ca_certs.py code blank lines in this file cause problems.

Fix: Before adding the cloud-init CA filename to this file first check the
size of the file - if is is empty (as all existing CAs have been deleted)
then write only the cloud-init CA filename to the file rather than appending
it to the file.
Copy link
Collaborator

@OddBloke OddBloke left a comment

Choose a reason for hiding this comment

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

Hi @dermotbradley, thanks for the submission, this is a good bug to fix! I have one inline question, but at a higher-level I'd also like to see a test (or tests) written for this change in behaviour, so we can (a) confirm this fix is behaving as expected now, and (b) detect any regressions to this behaviour in future.

There are already tests for this function in https://github.com/canonical/cloud-init/blob/master/tests/unittests/test_handler/test_handler_ca_certs.py#L139, so hopefully extending that should be relatively simple. If you have any question about how to proceed, please don't hesitate to ask!

cloudinit/config/cc_ca_certs.py Show resolved Hide resolved
@OddBloke OddBloke self-assigned this Jul 8, 2020
…ng new one

Existing testcases do not handle the situation where all the existing CA
certs are deleted (so resulting in an empty /etc/ca-certificates.conf file
and when one, or more, new CAs are added). This testcase verifies that the
resultant ca-certificates.conf file does not contain any blank lines which
are known to cause problems for the update-ca-certificates utility.
@dermotbradley
Copy link
Contributor Author

@OddBloke

Added a new testcase but its failing - looks like I forgot about needing to mock the os.stat.st_size call. I'm not that familiar with mocking in Python so I'll try and figure it out.

Copy link
Collaborator

@OddBloke OddBloke left a comment

Choose a reason for hiding this comment

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

Thanks for the revisions, Dermot, this is really close now! One remaining test issue (with a suggested fix), then I'll be +1 on this.

Comment on lines 212 to 213
mock_write = mocks.enter_context(
mock.patch.object(util, 'write_file'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per our unit testing guidelines (search for "An important exception"), if we're going to use the assert_* methods on a mock then it needs to be autospecced:

Suggested change
mock_write = mocks.enter_context(
mock.patch.object(util, 'write_file'))
mock_write = mocks.enter_context(
mock.patch.object(util, 'write_file', autospec=True))

Copy link
Collaborator

@OddBloke OddBloke left a comment

Choose a reason for hiding this comment

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

Thanks @dermotbradley, I look forward to your future contributions!

@OddBloke OddBloke merged commit c0d239c into canonical:master Jul 15, 2020
@dermotbradley dermotbradley deleted the cc_ca_certs-blank-line-fix branch July 16, 2020 12:17
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.

2 participants