nocloud: handle dict-of-strings public-keys format#198
Conversation
ader1990
left a comment
There was a problem hiding this comment.
Hello, thank you for the fix. Can you please confirm that this code is your own contribution and not AI created? If AI assisted, please state in the commit message that you abide by the current DCO terms: https://github.com/cloudbase/cloudbase-init/blob/master/DCO#L16 and that your contribution is yours.
Also, if you can remove the AI bloat from the commit message and keep it as short as possible with real links pointing to the actual KubeVirt documentation or KubeVirt implementation.
|
|
||
| @mock.patch(MODULE_PATH + '.NoCloudConfigDriveService._get_meta_data') | ||
| def test_get_public_keys_dict_of_strings(self, mock_get_metadata): | ||
| # KubeVirt serialises accessCredentials sshPublicKey noCloud as |
There was a problem hiding this comment.
unit tests should not have the same copy-pasted AI information as the initial implementation.
There was a problem hiding this comment.
Removed the comment; the test now keeps only the fixture and the assertion.
| return raw_ssh_keys | ||
|
|
||
| return [raw_ssh_keys[key].get('openssh-key') for key in raw_ssh_keys] | ||
| # raw_ssh_keys may be a dict in two shapes: |
There was a problem hiding this comment.
Hello, thanks for the fix. the code was complicated to read as it were, now it is even more complicated to read. Can you please update the code with logical sequential steps?
There was a problem hiding this comment.
Refactored to a sequential for loop with explicit if/else. Let me know if it reads better.
Some sources serialise public-keys as a dict of {name: key-string}
rather than {name: {'openssh-key': key-string}}. KubeVirt's
accessCredentials sshPublicKey noCloud propagation does this, defining
NoCloudMetadata.PublicSSHKeys as map[string]string [1] and assigning
it directly in readCloudInitNoCloudMetaData [2]. The current third
branch of get_public_keys() then calls .get('openssh-key') on a string
value and raises "'str' object has no attribute 'get'". Accept both
shapes.
[1] https://github.com/kubevirt/kubevirt/blob/623bf15e7553875860ece1a382a9e35bfbd61a26/pkg/cloud-init/cloud-init.go#L84
[2] https://github.com/kubevirt/kubevirt/blob/623bf15e7553875860ece1a382a9e35bfbd61a26/pkg/cloud-init/cloud-init.go#L402-L408
AI-assisted contribution: I reviewed the change and submit it under
the DCO terms at https://github.com/cloudbase/cloudbase-init/blob/master/DCO#L16
(clause (a) — the contribution is mine).
Signed-off-by: mattia-eleuteri <mattia@hidora.io>
5960904 to
fe5a68e
Compare
|
AI-assisted, disclosure added to the commit message per the DCO clause you linked. Commit body trimmed; KubeVirt source now linked at the type definition ( |
Summary
NoCloudConfigDriveService.get_public_keys()assumedpublic-keysin the metadata was either a list or an OpenStack-style dict of{<name>: {'openssh-key': <key>}}, and called.get('openssh-key')on each value. KubeVirt'saccessCredentials.sshPublicKey.propagationMethod.noCloudserialises the keys asmap[string]string, producing{'key0': '<ssh-rsa …>', 'key1': '<ssh-rsa …>'}— a dict whose values are plain SSH key strings. With that input the plugin crashed:SetUserSSHPublicKeysPluginaborted and no key was ever written to eitherC:\Users\<user>\.ssh\authorized_keysorC:\ProgramData\ssh\administrators_authorized_keys, so SSH key authentication never worked.This PR accepts both shapes: if a dict value is a string, it is treated as the SSH key directly; otherwise the previous OpenStack-style extraction (
v.get('openssh-key')) is kept.How to reproduce
accessCredentials.sshPublicKeywithsource.secretandpropagationMethod.noCloud: {}.pkg/cloud-init/cloud-init.go,type NoCloudMetadata struct { … PublicSSHKeys map[string]string \json:"public-keys,omitempty"` }`.Test plan
test_get_public_keys_dict_of_stringsmirroring the existingtest_get_public_keys/test_get_public_keys_alt_fmtpattern.Related
listshape in 2023; the third dict-of-strings shape (used by KubeVirt) was still missing.