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

Address review comments. #2

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
run:
go: "1.14"
deadline: 10m
allow-parallel-runners: true
linters:
disable-all: true
enable:
# Default linters
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
# Optional linters
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- durationcheck
- errchkjson
- errname
- errorlint
- exportloopref
- gosec
- misspell
- noctx
- nolintlint
- revive
- stylecheck
- whitespace
linters-settings:
revive:
ignore-generated-header: true
severity: error
staticcheck:
go: "1.14"
stylecheck:
go: "1.14"
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ client, err := winrm.NewClient(endpoint, "Administrator", "secret")
if err != nil {
panic(err)
}
client.Run("ipconfig /all", os.Stdout, os.Stderr)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client.RunWithContext(ctx, "ipconfig /all", os.Stdout, os.Stderr)
```

or
Expand All @@ -113,7 +115,9 @@ if err != nil {
panic(err)
}

_, err := client.RunWithInput("ipconfig", os.Stdout, os.Stderr, os.Stdin)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := client.RunWithContextWithInput(ctx, "ipconfig", os.Stdout, os.Stderr, os.Stdin)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -178,7 +182,9 @@ if err != nil {
panic(err)
}

_, err := client.RunWithInput("ipconfig", os.Stdout, os.Stderr, os.Stdin)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := client.RunWithContextWithInput(ctx, "ipconfig", os.Stdout, os.Stderr, os.Stdin)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -215,7 +221,9 @@ package main
panic(err)
}

_, err = client.RunWithInput("ipconfig", os.Stdout, os.Stderr, os.Stdin)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = client.RunWithContextWithInput(ctx, "ipconfig", os.Stdout, os.Stderr, os.Stdin)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -246,8 +254,10 @@ shell, err := client.CreateShell()
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var cmd *winrm.Command
cmd, err = shell.Execute("cmd.exe")
cmd, err = shell.ExecuteWithContext(ctx, "cmd.exe")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -301,13 +311,20 @@ func main() {
if err != nil {
log.Fatalf("failed to create client: %q", err)
}
_, err = client.Run("whoami", os.Stdout, os.Stderr)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = client.RunWithContext(ctx, "whoami", os.Stdout, os.Stderr)
if err != nil {
log.Fatalf("failed to run command: %q", err)
}
}
```

Note: canceling the `context.Context` passed as first argument to the various
functions of the API will not cancel the HTTP requests themselves, it will
rather cause a running command to be aborted on the remote machine via a call to
`command.Stop()`.

## Developing on WinRM

If you wish to work on `winrm` itself, you'll first need [Go](http://golang.org)
Expand Down
4 changes: 2 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (c *ClientAuthRequest) Transport(endpoint *Endpoint) error {
dial = c.dial
}

//nolint:gosec
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
Expand Down Expand Up @@ -61,8 +62,7 @@ func (c *ClientAuthRequest) Transport(endpoint *Endpoint) error {

// parse func reads the response body and return it as a string
func parse(response *http.Response) (string, error) {

// if we recived the content we expected
// if we received the content we expected
if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") {
body, err := ioutil.ReadAll(response.Body)
defer func() {
Expand Down
12 changes: 5 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func NewClient(endpoint *Endpoint, user, password string) (*Client, error) {
// NewClientWithParameters will create a new remote client on url, connecting with user and password
// This function doesn't connect (connection happens only when CreateShell is called)
func NewClientWithParameters(endpoint *Endpoint, user, password string, params *Parameters) (*Client, error) {

// alloc a new client
client := &Client{
Parameters: *params,
Expand All @@ -59,7 +58,7 @@ func NewClientWithParameters(endpoint *Endpoint, user, password string, params *

// set the transport to some endpoint configuration
if err := client.http.Transport(endpoint); err != nil {
return nil, fmt.Errorf("Can't parse this key and certs: %w", err)
return nil, fmt.Errorf("can't parse this key and certs: %w", err)
}

return client, nil
Expand All @@ -69,7 +68,7 @@ func readCACerts(certs []byte) (*x509.CertPool, error) {
certPool := x509.NewCertPool()

if !certPool.AppendCertsFromPEM(certs) {
return nil, fmt.Errorf("Unable to read certificates")
return nil, errors.New("unable to read certificates")
}

return certPool, nil
Expand All @@ -92,7 +91,6 @@ func (c *Client) CreateShell() (*Shell, error) {
}

return c.NewShell(shellID), nil

}

// NewShell will create a new WinRM Shell for the given shellID
Expand Down Expand Up @@ -202,15 +200,15 @@ func (c *Client) RunWithContextWithInput(ctx context.Context, command string, st
defer func() {
cmd.Stdin.Close()
}()
io.Copy(cmd.Stdin, stdin)
_, _ = io.Copy(cmd.Stdin, stdin)
}()
go func() {
defer wg.Done()
io.Copy(stdout, cmd.Stdout)
_, _ = io.Copy(stdout, cmd.Stdout)
}()
go func() {
defer wg.Done()
io.Copy(stderr, cmd.Stderr)
_, _ = io.Copy(stderr, cmd.Stderr)
}()

cmd.Wait()
Expand Down
11 changes: 5 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type Requester struct {
dial func(network, addr string) (net.Conn, error)
}

func (r Requester) Post(client *Client, request *soap.SoapMessage) (string, error) {
func (r *Requester) Post(client *Client, request *soap.SoapMessage) (string, error) {
return r.http(client, request)
}

func (r Requester) Transport(endpoint *Endpoint) error {
func (r *Requester) Transport(endpoint *Endpoint) error {
//nolint:gosec
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: endpoint.Insecure,
Expand All @@ -43,9 +44,7 @@ func (r Requester) Transport(endpoint *Endpoint) error {
}

r.transport = transport

return nil

}

func (s *WinRMSuite) TestNewClient(c *C) {
Expand All @@ -59,7 +58,6 @@ func (s *WinRMSuite) TestNewClient(c *C) {
}

func (s *WinRMSuite) TestClientCreateShell(c *C) {

endpoint := NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
Expand All @@ -68,7 +66,7 @@ func (s *WinRMSuite) TestClientCreateShell(c *C) {
c.Assert(message.String(), Contains, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create")
return createShellResponse, nil
}
client.http = r
client.http = &r

shell, _ := client.CreateShell()
c.Assert(shell.id, Equals, "67A74734-DD32-4F10-89DE-49A060483810")
Expand Down Expand Up @@ -256,6 +254,7 @@ func (s *WinRMSuite) TestReplaceDial(c *C) {

endpoint := NewEndpoint(host, port, false, false, nil, nil, nil, 0)
client, err := NewClientWithParameters(endpoint, "Administrator", "v3r1S3cre7", params)
c.Assert(err, IsNil)
var stdout, stderr bytes.Buffer
_, err = client.Run("ipconfig /all", &stdout, &stderr)
c.Assert(err, IsNil)
Expand Down
13 changes: 6 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Command struct {
shell *Shell
id string
exitCode int
finished bool
err error

Stdin *commandWriter
Expand Down Expand Up @@ -74,8 +73,8 @@ func newCommandReader(stream string, command *Command) *commandReader {
}

func fetchOutput(ctx context.Context, command *Command) {
ctxDone := ctx.Done()
for {
ctxDone := ctx.Done()
select {
case <-command.cancel:
_, _ = command.slurpAllOutput()
Expand Down Expand Up @@ -165,15 +164,15 @@ func (c *Command) slurpAllOutput() (bool, error) {
return true, err
}
if stdout.Len() > 0 {
c.Stdout.write.Write(stdout.Bytes())
_, _ = c.Stdout.write.Write(stdout.Bytes())
}
if stderr.Len() > 0 {
c.Stderr.write.Write(stderr.Bytes())
_, _ = c.Stderr.write.Write(stderr.Bytes())
}
if finished {
c.exitCode = exitCode
c.Stderr.write.Close()
c.Stdout.write.Close()
_ = c.Stderr.write.Close()
_ = c.Stdout.write.Close()
}

return finished, nil
Expand Down Expand Up @@ -264,7 +263,7 @@ func (w *commandWriter) Close() error {
// Read data from this Pipe
func (r *commandReader) Read(buf []byte) (int, error) {
n, err := r.read.Read(buf)
if err != nil && err != io.EOF {
if err != nil && errors.Is(err, io.EOF) {
return 0, err
}
return n, err
Expand Down
Loading