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

SSHPublicKey created too late for cd_content #182

Open
timeu opened this issue Oct 4, 2024 · 6 comments
Open

SSHPublicKey created too late for cd_content #182

timeu opened this issue Oct 4, 2024 · 6 comments
Labels
bug stage/needs-investigation stage/thinking Flagged for internal discussions about possible enhancements.

Comments

@timeu
Copy link

timeu commented Oct 4, 2024

Overview of the Issue

Similar to the issue in the vmware plugin (hashicorp/packer-plugin-vmware#177) this is also happening with the QEMU plugin. The SSHPublicKey step runs after the creation of the CD/Floppy and thus cannot be used there.

Reproduction Steps

Add to the source definition

cd_content = {
    meta-data = "instance-id: \"ubuntu-${uuidv4()}\""
    user-data = <<-EOF
      #cloud-config
      manage_etc_hosts: localhost
      disable_root: false
      ssh_authorized_keys:
        - {{ .SSHPublicKey }}
      EOF
  }
  cd_label             = "cidata"

Plugin and Packer version

From packer version
packer-plugin-qemu_v1.1.0_x5.0_linux_amd64

@timeu timeu added the bug label Oct 4, 2024
@lbajolet-hashicorp
Copy link
Contributor

Hi @timeu,

I'd have to investigate, but if I had to guess, this doesn't look feasible as the builder's the one creating the ssh key, and the source configuration needs to be complete (i.e. interpolated) at that point, so I'm not certain we'll be able to do what you want here.

As a workaround, I'd suggest using a provisioner to add this to the cloud-config file (though this implies you'll be able to connect, which I assume is the whole point of this addition), or alternatively, to provide your own SSH private/public keys that you can interpolate with a reference to a variable.

Something like the following:

variable "ssh_public_key" {
  type = string
}

source "qemu" "build" {
[...]
      ssh_authorized_keys:
        - file(var.ssh_public_key)
      EOF
[...]
}

Hope that helps!

@lbajolet-hashicorp lbajolet-hashicorp added stage/thinking Flagged for internal discussions about possible enhancements. stage/needs-investigation labels Oct 7, 2024
@timeu
Copy link
Author

timeu commented Oct 8, 2024

@lbajolet-hashicorp : thanks for the response.
The PR for the vmware plugin gave me the impression that it works. I can try to apply the change to the qemu plugin and test it locally and report back and if it works create a PR forr it.
Right now we provide an ssh key pair but it would be nice if we could simply leverage the ephemeral ssh key support and not have to provide a dedicated key-pair.

@lbajolet-hashicorp
Copy link
Contributor

Hi @timeu,

Out of curiosity, may I ask which PR you are referring to? It is possible I missed something which would make it possible, though at first glance I would still think we have a bootstrapping problem, but I'll be happy to be proved wrong on this one!

If you are willing to do a PR with an implementation please feel free to do so, and let me know when it's ready to be reviewed, I'll definitely take a look at it, thanks!

@timeu
Copy link
Author

timeu commented Oct 8, 2024

@lbajolet-hashicorp : Sorry forgot to mention the PR. It's this one: hashicorp/packer-plugin-vmware#203

@LIV2
Copy link
Contributor

LIV2 commented Oct 9, 2024

I tested this myself but it still didn't work when I reordered the steps, I think @lbajolet-hashicorp is correct that the interpolation happens too early for this to work

I make use of the variable by adding this to the boot args before ---

PACKER_AUTHORIZED_KEY={{ .SSHPublicKey | urlquery }}

Then use late-commands in the Ubuntu autoinstall config to install the key

  late-commands:
    - mkdir -p /target/etc/ssh/sshd_config.d
    - echo "PermitRootLogin prohibit-password" > /target/etc/ssh/sshd_config.d/permitroot.conf
    - mkdir -p -m 700 /target/root/.ssh
    - 'grep -oP "PACKER_AUTHORIZED_KEY\=\K\S+(?=%0A)" /proc/cmdline | sed "s@+@ @g;s@%@\\\x@g" | xargs -0 printf "%b" >> /target/root/.ssh/authorized_keys'
    - chmod 600 /target/root/.ssh/authorized_keys

@timeu
Copy link
Author

timeu commented Oct 9, 2024

Yes, I tried it myself and I can confirm the findings of @LIV2 :-/
There is a related issue open in the packer repo
I think we can close this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug stage/needs-investigation stage/thinking Flagged for internal discussions about possible enhancements.
Projects
None yet
Development

No branches or pull requests

3 participants