refactor(azure): Extract SSH key cert handling to new certs module#6802
refactor(azure): Extract SSH key cert handling to new certs module#6802peytonr18 wants to merge 13 commits intocanonical:mainfrom
Conversation
…ete openssl and ssh-keygen functionality
…sity Added extract_x509_certificate() to validate certificates in bundles, integrated validation into parse_certificates(), and toned down debug logs to avoid sounding like failures.
Fold the regex-based certificate parsing into cloudinit.sources.azure.certs so helpers no longer reimplement it, and wire OpenSSLManager.parse_certificates to loop over the helper. Added a regression test that confirms CRLF-mixed bundles still yield every fingerprint + key pair.
…all_certificates to avoid openssl dependency
| LOG.debug("Empty key content provided.") | ||
| return False | ||
| # See https://bugs.launchpad.net/cloud-init/+bug/1910835 | ||
| if "\r\n" in key.strip(): |
There was a problem hiding this comment.
what happens with AuthKeyLineParser if \r\n is found in the middle of the key? does it still validate?
There was a problem hiding this comment.
Edit: I've updated in the latest commit: instead of rejecting keys with embedded \r\n, we now sanitize them by stripping the CRLF sequences before validation. This handles the Azure-generated key case (LP: #1910835) where \r\n gets injected into the base64 data.
The flow in _get_public_ssh_keys_from_imds() is now:
- Collect raw keys from IMDS
- Sanitize any keys containing
\r\nviasanitize_openssh_key() - Validate with
is_openssh_formatted() - Return clean, validated keys
For example, a key like ssh-rsa AAAA\r\nBBBB comment gets sanitized to ssh-rsa AAAABBBB comment, validated, and returned cleanly for authorized_keys.
…and iterate them directly in parse_certificates, replacing the previous loop/find slicing flow. Also clarify is_openssh_formatted behavior by explicitly rejecting embedded CRLF and improving debug messages; update tests and type hints to match the new extraction API.
Azure-generated SSH keys may contain \r\n sequences embedded in the base64 key data (LP: #1910835). Previously, keys with embedded CRLF were rejected outright by is_openssh_formatted(). Instead, sanitize the keys by stripping \r\n before validation so they can be properly written to authorized_keys.
b09ed45 to
b481306
Compare
f42d866 to
324e304
Compare
- Use pytest.mark.parametrize, fixtures, and monkeypatch in tests - Remove raw docstring prefix; clarify CRLF deprecation in docstring - Remove redundant _key_is_openssh_formatted wrapper - Simplify sanitize_openssh_key call site and log messages - Reuse variables and constants instead of inline string literals
324e304 to
ff09ae0
Compare
…ct assertions in azure cert tests
f827718 to
3808c99
Compare
|
@blackboxsw this is ready for review when you get a chance, thanks! |
|
Hello! Thank you for this proposed change to cloud-init. This pull request is now marked as stale as it has not seen any activity in 14 days. If no activity occurs within the next 7 days, this pull request will automatically close. If you are waiting for code review and you are seeing this message, apologies! Please reply, tagging blackboxsw, and he will ensure that someone takes a look soon. (If the pull request is closed and you would like to continue working on it, please do tag blackboxsw to reopen it.) |
|
Hi @blackboxsw, this one is on track to be marked stale whenever you get a chance - thank you! |
Proposed Commit Message
Additional Context
Purpose:
Refactors x509 certificate and SSH key handling logic from
OpenSSLManagerandDataSourceAzureinto a dedicated module (cloudinit/sources/azure/certs.py).Changes:
cloudinit/sources/azure/certs.pywith three functions:is_openssh_formatted()- Validates OpenSSH format keysis_x509_certificate()- Validates x509 certificates using opensslconvert_x509_to_openssh()- Converts x509 certs to OpenSSH keyscloudinit/sources/helpers/azure.py-->_get_ssh_key_from_cert()now uses new modulecloudinit/sources/DataSourceAzure.py-->_key_is_openssh_formatted()now uses new moduleTest Coverage:
opensslfor x509 validation & withopenssl+ssh-keygenfor cert conversionMerge type
Replaces #6561