Skip to content

Commit

Permalink
fix ? prefix in winrm ssl args
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Nov 11, 2024
1 parent ad3cbe3 commit b2229f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
17 changes: 12 additions & 5 deletions provisioner/testinfra_communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"strings"

"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/tmp"
Expand Down Expand Up @@ -89,19 +90,25 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
}
}

// format string for testinfra connection backend setting
connectionBackend := fmt.Sprintf("--hosts=winrm://%s:%s@%s", user, winrmPassword, httpAddr)

// winrm optional arguments
var optionalArgs []string
// modify connection backend for ssl settings
if useSSL, ok := provisioner.generatedData["WinRMUseSSL"].(bool); ok && !useSSL {
// disable ssl
connectionBackend += "?no_ssl=true"
optionalArgs = append(optionalArgs, "no_ssl=true")
}
if insecure, _ := provisioner.generatedData["WinRMInsecure"].(bool); insecure {
// do not verify ssl
connectionBackend += "?no_verify_ssl=true"
optionalArgs = append(optionalArgs, "no_verify_ssl=true")
}
// prefix first optional argument with ? character if it exists
if len(optionalArgs) > 0 {
optionalArgs[0] = "?" + optionalArgs[0]
}

// format string for testinfra connection backend setting
connectionBackend := fmt.Sprintf("--hosts=winrm://%s:%s@%s%s", user, winrmPassword, httpAddr, strings.Join(optionalArgs, "&"))

// append args with winrm connection backend information (user, password, host, port)
args = append(args, connectionBackend)
case "docker", "podman", "lxc":
Expand Down
16 changes: 9 additions & 7 deletions provisioner/testinfra_communication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ func TestProvisionerDetermineCommunication(test *testing.T) {

// test winrm with empty host, port, and winrmpassword
generatedData = map[string]interface{}{
"ConnType": "winrm",
"User": "me",
"Password": "password",
"Host": "192.168.0.1",
"Port": int64(5986),
"ID": "1234567890",
"ConnType": "winrm",
"User": "me",
"Password": "password",
"Host": "192.168.0.1",
"Port": int64(5986),
"ID": "1234567890",
"WinRMUseSSL": false,
"WinRMInsecure": true,
}

provisioner.generatedData = generatedData
Expand All @@ -81,7 +83,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) {
if err != nil {
test.Errorf("determineCommunication function failed to determine winrm: %s", err)
}
if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=winrm://%s:%s@%s:%d", generatedData["User"], generatedData["Password"], generatedData["Host"], generatedData["Port"])}) {
if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=winrm://%s:%s@%s:%d?no_ssl=true&no_verify_ssl=true", generatedData["User"], generatedData["Password"], generatedData["Host"], generatedData["Port"])}) {
test.Errorf("communication string slice for winrm incorrectly determined: %v", communication)
}

Expand Down

0 comments on commit b2229f4

Please sign in to comment.