2015-04-10 20:34:46 +02:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2015-04-30 18:02:33 +02:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-04-10 20:34:46 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-08-20 19:53:48 +02:00
|
|
|
"github.com/masterzen/winrm"
|
2015-04-10 20:34:46 +02:00
|
|
|
"github.com/packer-community/winrmcp/winrmcp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Communicator represents the WinRM communicator
|
|
|
|
type Communicator struct {
|
|
|
|
connInfo *connectionInfo
|
|
|
|
client *winrm.Client
|
|
|
|
endpoint *winrm.Endpoint
|
2016-06-29 15:58:33 +02:00
|
|
|
rand *rand.Rand
|
2015-04-10 20:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new communicator implementation over WinRM.
|
|
|
|
func New(s *terraform.InstanceState) (*Communicator, error) {
|
|
|
|
connInfo, err := parseConnectionInfo(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := &winrm.Endpoint{
|
|
|
|
Host: connInfo.Host,
|
|
|
|
Port: connInfo.Port,
|
|
|
|
HTTPS: connInfo.HTTPS,
|
|
|
|
Insecure: connInfo.Insecure,
|
2020-06-25 14:41:09 +02:00
|
|
|
Timeout: connInfo.TimeoutVal,
|
2017-08-20 19:53:48 +02:00
|
|
|
}
|
2017-10-23 22:28:41 +02:00
|
|
|
if len(connInfo.CACert) > 0 {
|
|
|
|
endpoint.CACert = []byte(connInfo.CACert)
|
2015-04-10 20:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
comm := &Communicator{
|
|
|
|
connInfo: connInfo,
|
|
|
|
endpoint: endpoint,
|
2016-06-29 15:58:33 +02:00
|
|
|
// Seed our own rand source so that script paths are not deterministic
|
|
|
|
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
|
2015-04-10 20:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return comm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) Connect(o terraform.UIOutput) error {
|
2020-05-05 18:13:01 +02:00
|
|
|
// Set the client to nil since we'll (re)create it
|
|
|
|
c.client = nil
|
2015-04-10 20:34:46 +02:00
|
|
|
|
2017-08-20 19:53:48 +02:00
|
|
|
params := winrm.DefaultParameters
|
2015-04-10 20:34:46 +02:00
|
|
|
params.Timeout = formatDuration(c.Timeout())
|
2020-05-01 11:05:08 +02:00
|
|
|
if c.connInfo.NTLM {
|
2018-03-31 03:11:53 +02:00
|
|
|
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} }
|
|
|
|
}
|
2015-04-10 20:34:46 +02:00
|
|
|
|
|
|
|
client, err := winrm.NewClientWithParameters(
|
|
|
|
c.endpoint, c.connInfo.User, c.connInfo.Password, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if o != nil {
|
|
|
|
o.Output(fmt.Sprintf(
|
|
|
|
"Connecting to remote host via WinRM...\n"+
|
|
|
|
" Host: %s\n"+
|
|
|
|
" Port: %d\n"+
|
|
|
|
" User: %s\n"+
|
|
|
|
" Password: %t\n"+
|
|
|
|
" HTTPS: %t\n"+
|
|
|
|
" Insecure: %t\n"+
|
2018-03-31 03:11:53 +02:00
|
|
|
" NTLM: %t\n"+
|
2015-04-10 20:34:46 +02:00
|
|
|
" CACert: %t",
|
|
|
|
c.connInfo.Host,
|
|
|
|
c.connInfo.Port,
|
|
|
|
c.connInfo.User,
|
|
|
|
c.connInfo.Password != "",
|
|
|
|
c.connInfo.HTTPS,
|
|
|
|
c.connInfo.Insecure,
|
2018-03-31 03:11:53 +02:00
|
|
|
c.connInfo.NTLM,
|
2017-10-23 22:28:41 +02:00
|
|
|
c.connInfo.CACert != "",
|
2015-04-10 20:34:46 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:41:03 +02:00
|
|
|
log.Printf("[DEBUG] connecting to remote shell using WinRM")
|
2015-04-10 20:34:46 +02:00
|
|
|
shell, err := client.CreateShell()
|
|
|
|
if err != nil {
|
2018-04-05 16:54:59 +02:00
|
|
|
log.Printf("[ERROR] error creating shell: %s", err)
|
2015-04-10 20:34:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = shell.Close()
|
|
|
|
if err != nil {
|
2018-04-05 16:54:59 +02:00
|
|
|
log.Printf("[ERROR] error closing shell: %s", err)
|
2015-04-10 20:34:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if o != nil {
|
|
|
|
o.Output("Connected!")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.client = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) Disconnect() error {
|
|
|
|
c.client = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) Timeout() time.Duration {
|
|
|
|
return c.connInfo.TimeoutVal
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptPath implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) ScriptPath() string {
|
2015-04-30 18:02:33 +02:00
|
|
|
return strings.Replace(
|
|
|
|
c.connInfo.ScriptPath, "%RAND%",
|
2016-06-29 15:58:33 +02:00
|
|
|
strconv.FormatInt(int64(c.rand.Int31()), 10), -1)
|
2015-04-10 20:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) Start(rc *remote.Cmd) error {
|
2018-03-15 15:50:17 +01:00
|
|
|
rc.Init()
|
2018-04-05 16:41:03 +02:00
|
|
|
log.Printf("[DEBUG] starting remote command: %s", rc.Command)
|
2018-03-15 15:50:17 +01:00
|
|
|
|
2018-04-05 16:54:59 +02:00
|
|
|
// TODO: make sure communicators always connect first, so we can get output
|
|
|
|
// from the connection.
|
2018-04-05 16:41:03 +02:00
|
|
|
if c.client == nil {
|
2018-04-05 16:54:59 +02:00
|
|
|
log.Println("[WARN] winrm client not connected, attempting to connect")
|
|
|
|
if err := c.Connect(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-10 20:34:46 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 16:41:03 +02:00
|
|
|
status, err := c.client.Run(rc.Command, rc.Stdout, rc.Stderr)
|
|
|
|
rc.SetExitStatus(status, err)
|
2015-04-10 20:34:46 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) Upload(path string, input io.Reader) error {
|
|
|
|
wcp, err := c.newCopyClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-05 16:41:03 +02:00
|
|
|
log.Printf("[DEBUG] Uploading file to '%s'", path)
|
2015-04-10 20:34:46 +02:00
|
|
|
return wcp.Write(path, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadScript implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) UploadScript(path string, input io.Reader) error {
|
|
|
|
return c.Upload(path, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadDir implementation of communicator.Communicator interface
|
|
|
|
func (c *Communicator) UploadDir(dst string, src string) error {
|
2018-04-05 16:41:03 +02:00
|
|
|
log.Printf("[DEBUG] Uploading dir '%s' to '%s'", src, dst)
|
2015-04-10 20:34:46 +02:00
|
|
|
wcp, err := c.newCopyClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wcp.Copy(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) newCopyClient() (*winrmcp.Winrmcp, error) {
|
|
|
|
addr := fmt.Sprintf("%s:%d", c.endpoint.Host, c.endpoint.Port)
|
2016-03-21 18:12:45 +01:00
|
|
|
|
|
|
|
config := winrmcp.Config{
|
2015-04-10 20:34:46 +02:00
|
|
|
Auth: winrmcp.Auth{
|
|
|
|
User: c.connInfo.User,
|
|
|
|
Password: c.connInfo.Password,
|
|
|
|
},
|
2016-03-21 18:12:45 +01:00
|
|
|
Https: c.connInfo.HTTPS,
|
|
|
|
Insecure: c.connInfo.Insecure,
|
2015-04-10 20:34:46 +02:00
|
|
|
OperationTimeout: c.Timeout(),
|
|
|
|
MaxOperationsPerShell: 15, // lowest common denominator
|
2016-03-21 18:12:45 +01:00
|
|
|
}
|
2018-03-31 03:45:09 +02:00
|
|
|
|
2020-05-01 11:05:08 +02:00
|
|
|
if c.connInfo.NTLM {
|
2018-03-31 03:32:46 +02:00
|
|
|
config.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} }
|
|
|
|
}
|
2016-03-21 18:12:45 +01:00
|
|
|
|
2017-10-23 22:28:41 +02:00
|
|
|
if c.connInfo.CACert != "" {
|
|
|
|
config.CACertBytes = []byte(c.connInfo.CACert)
|
2016-03-21 18:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return winrmcp.New(addr, &config)
|
2015-04-10 20:34:46 +02:00
|
|
|
}
|