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

GCP: Allow to select algo when generating temporary SSH keypair #10111

Merged
merged 24 commits into from
Oct 28, 2020

Conversation

azr
Copy link
Contributor

@azr azr commented Oct 15, 2020

This is based on top of #10101, to solve the GCP case of #10074 this:

  • adds temporary_key_pair_type/temporary_key_pair_bits option to the communicator.SSH struct
  • makes the GCP builder use those options along with the sshkey Package to generate the correct temporary keypair when needed.
  • updates GCP docs + generate all code

@codecov
Copy link

codecov bot commented Oct 15, 2020

Codecov Report

Merging #10111 into master will decrease coverage by 0.03%.
The diff coverage is 0.00%.

Impacted Files Coverage Δ
builder/googlecompute/builder.go 0.00% <0.00%> (ø)
helper/communicator/config.go 24.60% <ø> (ø)
helper/communicator/step_debug_ssh_keys.go 0.00% <0.00%> (ø)
helper/communicator/step_ssh_keygen.go 0.00% <0.00%> (ø)
helper/multistep/basic_runner.go 89.18% <0.00%> (-5.10%) ⬇️
helper/multistep/debug_runner.go 89.13% <0.00%> (-4.06%) ⬇️
helper/multistep/if.go 0.00% <0.00%> (ø)
...t-processor/googlecompute-export/post-processor.go 0.00% <0.00%> (ø)
... and 3 more

},
multistep.If(b.config.PackerDebug,
&communicator.StepDumpSSHKey{
Copy link
Contributor

Choose a reason for hiding this comment

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

ooooh shiny.

@SwampDragons
Copy link
Contributor

Tried to run it and it crashed on me --

{
  "variables": {
      "image_name": "ubuntu1804-jenkins-{{isotime}}"
  },
  "builders": [
    {
      "type": "googlecompute",
      "image_name": "{{ user `image_name` | clean_resource_name}}",
      "account_file": "/myaccountfile.json",
      "project_id": "gifted-station-178619",
      "source_image_family": "ubuntu-1804-lts",
      "ssh_username": "packer",
      "zone": "us-central1-c",
      "state_timeout": "20m",
      "startup_script_file": "./shell_local_dummy.sh",
      "tags": ["packer"]
    }
  ],
  "provisioners": [
    {
      "pause_before": "10s",
      "type": "shell",
      "inline": ["echo {{ build `ID` }}"]
    }
  ]
}

@azr
Copy link
Contributor Author

azr commented Oct 20, 2020

Thanks ! Weird, your file works on my machine 🤔 any special options ? Do you have the crash log ?

@azr
Copy link
Contributor Author

azr commented Oct 20, 2020

NVM found it !

@azr azr marked this pull request as draft October 20, 2020 15:07
@azr azr marked this pull request as ready for review October 20, 2020 15:16
@azr
Copy link
Contributor Author

azr commented Oct 20, 2020

Here's a testfile that works with any value:

{
    "builders": [
        {
            "type": "googlecompute",
            "project_id": "azr-hashicorp",
            "image_name": "base-centos-7-{{timestamp}}",
            "source_image": "centos-7-v20200521",
            "source_image_family": "centos-7",
            "ssh_username": "centos",
            "temporary_key_pair_type": "ecdsa",
            "temporary_key_pair_bits": 521,
            "zone": "europe-west2-a",
            "machine_type": "n1-standard-1",
            "disk_size": 20,
            "enable_secure_boot": true,
            "enable_vtpm": true,
            "enable_integrity_monitoring": true
        }
    ],
    "provisioners": [
        {
            "destination": "/tmp/hm hi,\\ I have a space \u0026",
            "source": "log.go",
            "type": "file"
        }
    ],
    "variables": {
        "zone": "europe-west4-a"
    }
}

Edit: HCL2 is not 100%, I'm on it.

@azr azr marked this pull request as draft October 20, 2020 15:48
@azr azr marked this pull request as ready for review October 21, 2020 10:08
@azr
Copy link
Contributor Author

azr commented Oct 21, 2020

Here's a cool HCL2 test file, ( you need a log.go file there ):

variable "zone" {
  type    = string
  default = "europe-west4-a"
}

source "googlecompute" "centos" {
  disk_size                   = 20
  enable_integrity_monitoring = true
  enable_secure_boot          = true
  enable_vtpm                 = true
  machine_type                = "n1-standard-1"
  project_id                  = "azr-hashicorp"
  source_image                = "centos-7-v20200521"
  source_image_family         = "centos-7"
  ssh_username                = "centos"
  zone                        = "europe-west2-a"
}

build {
  source "googlecompute.centos" {
    temporary_key_pair_type = "dsa"
    image_name              = "base-centos-7-dsa"
  }
  source "googlecompute.centos" {
    temporary_key_pair_type = "rsa"
    image_name              = "base-centos-7-rsa"
  }
  source "googlecompute.centos" {
    temporary_key_pair_type = "ecdsa"
    image_name              = "base-centos-7-ecdsa"
  }
  source "googlecompute.centos" {
    temporary_key_pair_type = "ed25519"
    image_name              = "base-centos-7-ed25519"
  }

  provisioner "file" {
    destination = "/tmp/hm hi,\\ I have a space & ☺"
    source      = "log.go"
  }
}

@SwampDragons
Copy link
Contributor

cool! Your example works, but my original example still doesn't, and it does work on the master branch so we need to make sure that the default case still works.

@azr
Copy link
Contributor Author

azr commented Oct 26, 2020

Ah indeed ! Thanks !

@azr azr force-pushed the azr_selectable_temp_keygen_type_gcp branch from b321023 to 522d122 Compare October 26, 2020 14:34
@azr
Copy link
Contributor Author

azr commented Oct 26, 2020

Okay found the why, with no parameter this changed the default keytype from rsa to dsa ( because this was the default for ssh-keygen too and I thought this was the safest approach and that it would not break anything), this is clearly a breaking change so I'm making the default type rsa for backwards compat.

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

This looks great. Lots of nice little add-ons here. I have not tested against the examples but code wise it looks good. I approve, but will defer until @SwampDragons has given the green light that their use case has been satisfied.

package multistep

// if returns step only if on is true.
func If(on bool, step Step) Step {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

@SwampDragons
Copy link
Contributor

yep, works now!

@SwampDragons SwampDragons merged commit 6931245 into master Oct 28, 2020
@SwampDragons SwampDragons deleted the azr_selectable_temp_keygen_type_gcp branch October 28, 2020 18:25
@ghost
Copy link

ghost commented Nov 28, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants