2016-09-25 22:46:52 -04:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-09-25 22:46:52 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2018-09-10 19:48:42 -04:00
|
|
|
commonhelper "github.com/hashicorp/packer/helper/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-09-25 22:46:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateWindowsPassword represents a Packer build step that sets the windows password on a Windows GCE instance.
|
|
|
|
type StepCreateWindowsPassword struct {
|
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the Packer build step that sets the windows password on a Windows GCE instance.
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepCreateWindowsPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-09-25 22:46:52 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
d := state.Get("driver").(Driver)
|
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
name := state.Get("instance_name").(string)
|
|
|
|
|
|
|
|
if c.Comm.WinRMPassword != "" {
|
|
|
|
state.Put("winrm_password", c.Comm.WinRMPassword)
|
2018-08-10 17:25:14 -04:00
|
|
|
packer.LogSecretFilter.Set(c.Comm.WinRMPassword)
|
2016-09-25 22:46:52 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
create, ok := state.GetOk("create_windows_password")
|
|
|
|
|
|
|
|
if !ok || !create.(bool) {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
|
|
|
}
|
|
|
|
ui.Say("Creating windows user for instance...")
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(buf, uint32(priv.E))
|
|
|
|
|
|
|
|
data := WindowsPasswordConfig{
|
|
|
|
key: priv,
|
|
|
|
UserName: c.Comm.WinRMUser,
|
|
|
|
Modulus: base64.StdEncoding.EncodeToString(priv.N.Bytes()),
|
|
|
|
Exponent: base64.StdEncoding.EncodeToString(buf[1:]),
|
|
|
|
Email: c.Account.ClientEmail,
|
|
|
|
ExpireOn: time.Now().Add(time.Minute * 5),
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Debug {
|
|
|
|
|
|
|
|
priv_blk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(priv),
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
|
|
|
f, err := os.Create(s.DebugKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the key
|
|
|
|
err = pem.Encode(f, &priv_blk)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh, err := d.CreateOrResetWindowsPassword(name, c.Zone, &data)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
ui.Message("Waiting for windows password to complete...")
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-time.After(c.stateTimeout):
|
|
|
|
err = errors.New("time out while waiting for the password to be created")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating windows password: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Created password.")
|
|
|
|
|
2016-09-30 15:17:38 -04:00
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Password (since debug is enabled): %s", data.password))
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:46:52 -04:00
|
|
|
state.Put("winrm_password", data.password)
|
2018-09-10 19:48:42 -04:00
|
|
|
commonhelper.SetSharedState("winrm_password", data.password, c.PackerConfig.PackerBuildName)
|
2018-08-10 17:25:14 -04:00
|
|
|
packer.LogSecretFilter.Set(data.password)
|
2016-09-25 22:46:52 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to clean up. The windows password is only created on the single instance.
|
|
|
|
func (s *StepCreateWindowsPassword) Cleanup(state multistep.StateBag) {}
|