func New(addr string, config *Config) (*Winrmcp, error) { endpoint, err := parseEndpoint(addr, config.Https, config.Insecure, config.CACertBytes, config.ConnectTimeout) if err != nil { return nil, err } if config == nil { config = &Config{} } params := winrm.NewParameters( winrm.DefaultParameters.Timeout, winrm.DefaultParameters.Locale, winrm.DefaultParameters.EnvelopeSize, ) if config.TransportDecorator != nil { params.TransportDecorator = config.TransportDecorator } if config.OperationTimeout.Seconds() > 0 { params.Timeout = iso8601.FormatDuration(config.OperationTimeout) } client, err := winrm.NewClientWithParameters( endpoint, config.Auth.User, config.Auth.Password, params) return &Winrmcp{client, config}, err }
// NewClient creates a new secure winrm client for initiating connections with the winrm listener func NewClient(config ClientConfig) (*Client, error) { if err := config.Validate(); err != nil { return nil, errors.Annotatef(err, "cannot create winrm client") } cli := &Client{} params := winrm.NewParameters("PT60S", "en-US", 153600) var err error cli.pass, err = config.password() if err != errNoPasswdFn && err != nil { return nil, errors.Annotatef(err, "cannot get password") } // if we didn't provided a callback password that means // we want to use the https auth method that only works with https if err == errNoPasswdFn && config.Secure { // when creating a new client the winrm.DeafultParameters // will be used to make a new client conneciton to a endpoint // TransportDecorator will enable us to switch transports // this will be used for https client x509 authentication params.TransportDecorator = func() winrm.Transporter { logger.Debugf("Switching WinRM transport to HTTPS") // winrm https module return &winrm.ClientAuthRequest{} } } port := httpPort cli.secure = false if config.Secure { port = httpsPort cli.secure = true } endpoint := winrm.NewEndpoint(config.Host, port, config.Secure, config.Insecure, config.CACert, config.Cert, config.Key, config.Timeout, ) // if the user is empty if config.User == "" { // use the default one, Administrator config.User = defaultWinndowsUser } cli.conn, err = winrm.NewClientWithParameters(endpoint, config.User, cli.pass, params) if err != nil { return nil, errors.Annotatef(err, "cannot create WinRM https client conn") } return cli, nil }