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

Ensure ssh_authorized_keys is a list in cloud-init #164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cjeanner
Copy link

According to the official documentation[1], ssh_authorized_keys is a
list, not a string.

This patch should hopefully correct the issue we faced while trying to
inject multiple authorized keys: the cloud-init configuration file was
broken, preventing to apply any credential related data, leading to
failures when RHOSO deploy actually started.

[1] https://cloudinit.readthedocs.io/en/latest/reference/examples.html#configure-instance-s-ssh-keys

Co-Authored-By: @pablintino [email protected]

@openshift-ci openshift-ci bot requested review from hjensas and stuggi April 30, 2024 13:55
Copy link
Contributor

openshift-ci bot commented Apr 30, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: cjeanner
Once this PR has been reviewed and has the lgtm label, please assign abays for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

Build failed (check pipeline). Post recheck (without leading slash)
to rerun all jobs. Make sure the failure cause has been resolved before
you rerun jobs.

https://review.rdoproject.org/zuul/buildset/db56a202b8c9457ab62766e8d85486c8

openstack-baremetal-operator-content-provider FAILURE in 8m 24s
⚠️ openstack-baremetal-operator-crc-podified-edpm-baremetal SKIPPED Skipped due to failed job openstack-baremetal-operator-content-provider

Copy link

Build failed (check pipeline). Post recheck (without leading slash)
to rerun all jobs. Make sure the failure cause has been resolved before
you rerun jobs.

https://review.rdoproject.org/zuul/buildset/fc4d0078022045e0ac719c9eb8a5f89c

openstack-baremetal-operator-content-provider FAILURE in 7m 55s
⚠️ openstack-baremetal-operator-crc-podified-edpm-baremetal SKIPPED Skipped due to failed job openstack-baremetal-operator-content-provider

@cjeanner cjeanner force-pushed the cloud-init/ssh_authorized_keys-list branch 2 times, most recently from 432faf0 to 992e29d Compare April 30, 2024 15:19
Comment on lines 74 to 77
splitKeys := strings.Split(strings.TrimSuffix(string(sshSecret.Data["authorized_keys"]), "\n"), "\n")
sshKeys := make([]string, len(splitKeys))
sshKeys = append(sshKeys, splitKeys...)
templateParameters["AuthorizedKeys"] = sshKeys
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be cleaned up and simplified. Right now it duplicates the total number of keys by injecting a blank one for every actual key. The cloud-init ends up looking like this:

ssh_authorized_keys:
      - 
      - ssh-rsa somekeyyyyyyyyyyyyyyyyyyyyyyyy

I think it might be as simple as:

templateParameters["AuthorizedKeys"] = strings.Split(strings.TrimSuffix(string(sshSecret.Data["authorized_keys"]), "\n"), "\n")

Choose a reason for hiding this comment

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

@abays What happens if the content the user provided have line breaks with more than one consecutive \n. I'd ensure that the keys passed to the template are not empty string to avoid such cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, validation of some sort might be a good idea

Copy link
Contributor

Choose a reason for hiding this comment

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

It depends on how the keys are in the secret data, whether the way it's expected by cloud-init (as a list) or how it's in authorized_keys file (multiple lines with newline separator).

Copy link
Author

Choose a reason for hiding this comment

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

maybe we should be clear about the format then?

until we pushed a workaround (inject only one key), the framework was:

  • loading the ~/.ssh/authorized_keys
  • generate the nodeset ConfigMap by injecting the file content as-is, in b64 format

We can push a proper list instead - so that the operator would just need to check if it's a string or a list; if string, it probably should enforce some format (either "only one key", or "use that separator only"?); if list, just nudge it in the cloud-init config file.

Any thoughts? Checking the input is more than probably mandatory at some point. And getting some proper testing around the generated cloud-init config file is also something we really should consider, in order to avoid future regressions/issues.

Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

This approach [1] works for OSPdO, where we assume that the authorized_keys in the Secret is a newline-separated string of keys. i.e. we expect the format you'd see in a ~/.ssh/authorized_keys.

[1] openstack-k8s-operators/osp-director-operator#1043

Copy link
Contributor

@rabi rabi May 3, 2024

Choose a reason for hiding this comment

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

where we assume that the authorized_keys in the Secret is a newline-separated string of keys

Should we just assume or document that and throw a proper error if it's not a string separated by newline char.

Copy link
Author

Choose a reason for hiding this comment

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

@abays just updated this current patch, adding the check for non-empty string. Up to you if you want to import the patch from OSPdO instead of getting this one. I can also import the comment as-is to match your own commit - just lemme know :).

@cjeanner cjeanner force-pushed the cloud-init/ssh_authorized_keys-list branch from 992e29d to e085dba Compare May 3, 2024 06:14
templateParameters["AuthorizedKeys"] = strings.TrimSuffix(string(sshSecret.Data["authorized_keys"]), "\n")
// Prepare ssh_authorized_keys list for template
splitKeys := strings.Split(strings.TrimSuffix(string(sshSecret.Data["authorized_keys"]), "\n"), "\n")
sshKeys := make([]string, len(splitKeys))
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to do this as in @abays' patch.. strings.Split() returns a slice.

Copy link
Author

Choose a reason for hiding this comment

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

I've shamelessly copy-pasted code then - with mention of the original PR in the comment. cc @abays - guess we should be good?

@cjeanner cjeanner force-pushed the cloud-init/ssh_authorized_keys-list branch from e085dba to e50810e Compare May 6, 2024 07:50
Copy link

Build failed (check pipeline). Post recheck (without leading slash)
to rerun all jobs. Make sure the failure cause has been resolved before
you rerun jobs.

https://review.rdoproject.org/zuul/buildset/89876a28c20a431195c6e26e9f8de0f7

openstack-baremetal-operator-content-provider FAILURE in 7m 16s
⚠️ openstack-baremetal-operator-crc-podified-edpm-baremetal SKIPPED Skipped due to failed job openstack-baremetal-operator-content-provider

According to the official documentation[1], `ssh_authorized_keys` is a
list, not a string.

This patch should hopefully correct the issue we faced while trying to
inject multiple authorized keys: the cloud-init configuration file was
broken, preventing to apply any credential related data, leading to
failures when RHOSO deploy actually started.

[1] https://cloudinit.readthedocs.io/en/latest/reference/examples.html#configure-instance-s-ssh-keys

Co-Authored-By: @pablintino <[email protected]>
@cjeanner cjeanner force-pushed the cloud-init/ssh_authorized_keys-list branch from e50810e to b4ddb1a Compare May 7, 2024 11:51
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.

4 participants