From 76f13deaf4a7aa443807cd68d3347d290ab476d7 Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Mon, 9 Mar 2020 17:25:05 +0100 Subject: [PATCH] Make template variables SSHPublicKey and SSHPrivateKey as strings (#8829) --- common/step_provision.go | 4 +- common/step_provision_test.go | 10 ++--- helper/communicator/config.go | 6 +-- website/source/docs/templates/engine.html.md | 40 +++++++++++++++----- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/common/step_provision.go b/common/step_provision.go index c02192f92..888bea431 100644 --- a/common/step_provision.go +++ b/common/step_provision.go @@ -61,8 +61,8 @@ func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{} hookData["User"] = commConf.User() hookData["Password"] = commConf.Password() hookData["ConnType"] = commConf.Type - hookData["SSHPublicKey"] = commConf.SSHPublicKey - hookData["SSHPrivateKey"] = commConf.SSHPrivateKey + hookData["SSHPublicKey"] = string(commConf.SSHPublicKey) + hookData["SSHPrivateKey"] = string(commConf.SSHPrivateKey) // Backwards compatibility; in practice, WinRMPassword is fulfilled by // Password. diff --git a/common/step_provision_test.go b/common/step_provision_test.go index afd4019b9..f1b1d76e2 100644 --- a/common/step_provision_test.go +++ b/common/step_provision_test.go @@ -83,13 +83,11 @@ func TestPopulateProvisionHookData(t *testing.T) { if hookData["ConnType"] != commConfig.Type { t.Fatalf("Bad: Expecting hookData[\"ConnType\"] was %s but actual value was %s", commConfig.Type, hookData["ConnType"]) } - sshPublicKey := fmt.Sprintf("%v", hookData["SSHPublicKey"].(interface{})) - if sshPublicKey == string(commConfig.SSHPublicKey) { - t.Fatalf("Bad: Expecting hookData[\"SSHPublicKey\"] was %s but actual value was %s", string(commConfig.SSHPublicKey), sshPublicKey) + if hookData["SSHPublicKey"] != string(commConfig.SSHPublicKey) { + t.Fatalf("Bad: Expecting hookData[\"SSHPublicKey\"] was %s but actual value was %s", string(commConfig.SSHPublicKey), hookData["SSHPublicKey"]) } - sshPrivateKey := fmt.Sprintf("%v", hookData["SSHPrivateKey"].(interface{})) - if sshPrivateKey == string(commConfig.SSHPrivateKey) { - t.Fatalf("Bad: Expecting hookData[\"SSHPrivateKey\"] was %s but actual value was %s", string(commConfig.SSHPrivateKey), sshPrivateKey) + if hookData["SSHPrivateKey"] != string(commConfig.SSHPrivateKey) { + t.Fatalf("Bad: Expecting hookData[\"SSHPrivateKey\"] was %s but actual value was %s", string(commConfig.SSHPrivateKey), hookData["SSHPrivateKey"]) } if hookData["WinRMPassword"] != commConfig.WinRMPassword { t.Fatalf("Bad: Expecting hookData[\"WinRMPassword\"] was %s but actual value was %s", commConfig.WinRMPassword, hookData["WinRMPassword"]) diff --git a/helper/communicator/config.go b/helper/communicator/config.go index 83de5aea0..3b8ea6243 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -326,7 +326,7 @@ func (c *Config) Port() int { } } -// Host returns the port that will be used for access based on config. +// Host returns the host that will be used for access based on config. func (c *Config) Host() string { switch c.Type { case "ssh": @@ -338,7 +338,7 @@ func (c *Config) Host() string { } } -// User returns the port that will be used for access based on config. +// User returns the user that will be used for access based on config. func (c *Config) User() string { switch c.Type { case "ssh": @@ -350,7 +350,7 @@ func (c *Config) User() string { } } -// Password returns the port that will be used for access based on config. +// Password returns the password that will be used for access based on config. func (c *Config) Password() string { switch c.Type { case "ssh": diff --git a/website/source/docs/templates/engine.html.md b/website/source/docs/templates/engine.html.md index ac3e38f30..b77e20c8a 100644 --- a/website/source/docs/templates/engine.html.md +++ b/website/source/docs/templates/engine.html.md @@ -74,15 +74,37 @@ Here is a full list of the available functions for reference. } ``` - Valid variables to request are: "ID", "Host", - "Port", "User", "Password", "ConnType", - "PackerRunUUID", "PackerHTTPAddr", "SSHPublicKey", and "SSHPrivateKey". - Depending on which communicator you are using, some of these values may be - empty -- for example, the public and private keys are unique to the SSH - communicator. InstanceID represents the vm being provisioned. For example, - in Amazon it is the instance id; in digitalocean, it is the droplet id; in - Vmware, it is the vm name. - + Valid variables to request are: + - __ID__: Represents the vm being provisioned. For example, in Amazon it is the instance id; in digitalocean, + it is the droplet id; in Vmware, it is the vm name. + + - __Host__, __Port__, __User__ and __Password__: The host, port, user, and password that Packer uses to access the machine. + Useful for using the shell local provisioner to run Ansible or Inspec against the provisioned instance. + + - __ConnType__: Type of communicator being used. For example, for SSH communicator this will be "ssh". + + - __PackerRunUUID__: Current build's unique id. Can be used to specify build artifacts. + + - __PackerHTTPAddr__: HTTP address of the file server Packer creates to serve items in the "http" dir to the vm, displayed in the format `IP:PORT`. + + - __SSHPublicKey__ and __SSHPrivateKey__: The public and private key that Packer uses to connect to the instance. + These are unique to the SSH communicator and are unset when using other communicators. + __SSHPublicKey__ and __SSHPrivateKey__ can have escape sequences and special characters so their output should be single quoted to avoid surprises. For example: + + ``` + { + ... + "provisioners": [ + { + "type": "shell", + "inline": [ + "echo '{{ build `SSHPrivateKey`}}' > /tmp/packer-session.pem" + ] + } + ] + } + ``` + For backwards compatability, `WinRMPassword` is also available through this engine, though it is no different than using the more general `Password`.