Skip to content

Commit

Permalink
Add \r\n check for SSH keys in Azure (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealFalcon authored May 5, 2021
1 parent 5f5fa5e commit f17f78f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cloudinit/sources/DataSourceAzure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,9 @@ def _key_is_openssh_formatted(key):
"""
Validate whether or not the key is OpenSSH-formatted.
"""
# See https://bugs.launchpad.net/cloud-init/+bug/1910835
if '\r\n' in key.strip():
return False

parser = ssh_util.AuthKeyLineParser()
try:
Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/test_datasource/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,18 @@ def test_get_public_ssh_keys_with_imds(self, m_parse_certificates):
self.assertEqual(ssh_keys, ["ssh-rsa key1"])
self.assertEqual(m_parse_certificates.call_count, 0)

def test_key_without_crlf_valid(self):
test_key = 'ssh-rsa somerandomkeystuff some comment'
assert True is dsaz._key_is_openssh_formatted(test_key)

def test_key_with_crlf_invalid(self):
test_key = 'ssh-rsa someran\r\ndomkeystuff some comment'
assert False is dsaz._key_is_openssh_formatted(test_key)

def test_key_endswith_crlf_valid(self):
test_key = 'ssh-rsa somerandomkeystuff some comment\r\n'
assert True is dsaz._key_is_openssh_formatted(test_key)

@mock.patch(
'cloudinit.sources.helpers.azure.OpenSSLManager.parse_certificates')
@mock.patch(MOCKPATH + 'get_metadata_from_imds')
Expand Down

0 comments on commit f17f78f

Please sign in to comment.