Skip to content

Does not work with certificate auth #171

@owais

Description

@owais

I spent a few days trying to get this to work with client certificate authentication thinking my server had issues but looks issue might be with the library. I tried pywinrm and it works perfectly with equivalent python code. The following python snippet works:

import winrm


def main():
    s = winrm.Session(
        "https://localhost:5986/wsman",
        auth=("", ""),
        transport="certificate",
        cert_pem="winrm_client_cert.pem",
        cert_key_pem="cert.key",
        server_cert_validation="ignore",
    )
    r = s.run_cmd("whoami")
    print(r.status_code)
    print(r.std_out)


if __name__ == "__main__":
    main()

and prints:

Image

While as the following equivalent Go program does not work:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/masterzen/winrm"
)

func main() {
	clientCert, err := os.ReadFile("winrm_client_cert.pem")
	if err != nil {
		log.Fatalf("failed to read client certificate: %q", err)
	}

	clientKey, err := os.ReadFile("cert.key")
	if err != nil {
		log.Fatalf("failed to read client key: %q", err)
	}

	winrm.DefaultParameters.TransportDecorator = func() winrm.Transporter {
		// winrm https module
		return &winrm.ClientAuthRequest{}
	}

	endpoint := winrm.NewEndpoint(
		"localhost", // host to connect to
		5986,        // winrm port
		true,        // use TLS
		true,        // Allow insecure connection
		nil,         // CA certificate
		clientCert,  // Client Certificate
		clientKey,   // Client Key
		0,           // Timeout
	)

	client, err := winrm.NewClient(endpoint, "Administrator", "")
	if err != nil {
		log.Fatalf("failed to create client: %q", err)
	}
	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)
	}
}

produces the following output:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions