github.com/erikwilson/go-powershell@v0.0.0-20200701182037-6845e6fcfa79/middleware/session_config.go (about)

     1  // Copyright (c) 2017 Gorillalabs. All rights reserved.
     2  
     3  package middleware
     4  
     5  import (
     6  	"fmt"
     7  	"strconv"
     8  
     9  	"github.com/rancher/go-powershell/utils"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  const (
    14  	HTTPPort  = 5985
    15  	HTTPSPort = 5986
    16  )
    17  
    18  type SessionConfig struct {
    19  	ComputerName          string
    20  	AllowRedirection      bool
    21  	Authentication        string
    22  	CertificateThumbprint string
    23  	Credential            interface{}
    24  	Port                  int
    25  	UseSSL                bool
    26  }
    27  
    28  func NewSessionConfig() *SessionConfig {
    29  	return &SessionConfig{}
    30  }
    31  
    32  func (c *SessionConfig) ToArgs() []string {
    33  	args := make([]string, 0)
    34  
    35  	if c.ComputerName != "" {
    36  		args = append(args, "-ComputerName")
    37  		args = append(args, utils.QuoteArg(c.ComputerName))
    38  	}
    39  
    40  	if c.AllowRedirection {
    41  		args = append(args, "-AllowRedirection")
    42  	}
    43  
    44  	if c.Authentication != "" {
    45  		args = append(args, "-Authentication")
    46  		args = append(args, utils.QuoteArg(c.Authentication))
    47  	}
    48  
    49  	if c.CertificateThumbprint != "" {
    50  		args = append(args, "-CertificateThumbprint")
    51  		args = append(args, utils.QuoteArg(c.CertificateThumbprint))
    52  	}
    53  
    54  	if c.Port > 0 {
    55  		args = append(args, "-Port")
    56  		args = append(args, strconv.Itoa(c.Port))
    57  	}
    58  
    59  	if asserted, ok := c.Credential.(string); ok {
    60  		args = append(args, "-Credential")
    61  		args = append(args, asserted) // do not quote, as it contains a variable name when using password auth
    62  	}
    63  
    64  	if c.UseSSL {
    65  		args = append(args, "-UseSSL")
    66  	}
    67  
    68  	return args
    69  }
    70  
    71  type credential interface {
    72  	prepare(Middleware) (interface{}, error)
    73  }
    74  
    75  type UserPasswordCredential struct {
    76  	Username string
    77  	Password string
    78  }
    79  
    80  func (c *UserPasswordCredential) prepare(s Middleware) (interface{}, error) {
    81  	name := "goCred" + utils.CreateRandomString(8)
    82  	pwname := "goPass" + utils.CreateRandomString(8)
    83  
    84  	_, _, err := s.Execute(fmt.Sprintf("$%s = ConvertTo-SecureString -String %s -AsPlainText -Force", pwname, utils.QuoteArg(c.Password)))
    85  	if err != nil {
    86  		return nil, errors.Wrap(err, "Could not convert password to secure string")
    87  	}
    88  
    89  	_, _, err = s.Execute(fmt.Sprintf("$%s = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList %s, $%s", name, utils.QuoteArg(c.Username), pwname))
    90  	if err != nil {
    91  		return nil, errors.Wrap(err, "Could not create PSCredential object")
    92  	}
    93  
    94  	return fmt.Sprintf("$%s", name), nil
    95  }