1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

MSDNS Fix pssession; Allow alternative credentials (#1140)

* Add options for PSCredentials
* UTF-8 encoded reading
* Fix cred comparison for session based PSRemoting
* Better conditional
* Fix failing test

Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
split-and-join
2021-05-02 11:25:06 -04:00
committed by GitHub
parent 88c05d9484
commit a0bbc66983
3 changed files with 28 additions and 14 deletions

View File

@@ -35,6 +35,14 @@ func newPowerShell(config map[string]string) (*psHandle, error) {
mconfig := middleware.NewSessionConfig()
mconfig.ComputerName = pssession
cred := &middleware.UserPasswordCredential{
Username: config["psusername"],
Password: config["pspassword"],
}
if cred.Password != "" && cred.Username != "" {
mconfig.Credential = cred
}
session, err := middleware.NewSession(sh, mconfig)
if err != nil {
panic(err)
@@ -95,19 +103,20 @@ func (psh *psHandle) GetDNSZoneRecords(dnsserver, domain string) ([]nativeRecord
}
tmpfile.Close()
stdout, stderr, err := psh.shell.Execute(generatePSZoneDump(dnsserver, domain, tmpfile.Name()))
stdout, stderr, err := psh.shell.Execute(generatePSZoneDump(dnsserver, domain))
if err != nil {
return nil, err
}
if stdout != "" {
fmt.Printf("STDOUT = %q\n", stderr)
//writing all stdout from powershell to file
ioutil.WriteFile(tmpfile.Name(), []byte(stdout), 0)
}
if stderr != "" {
fmt.Printf("STDERROR = %q\n", stderr)
return nil, fmt.Errorf("unexpected stderr from PSZoneDump: %q", stderr)
}
contents, err := utfutil.ReadFile(tmpfile.Name(), utfutil.WINDOWS)
contents, err := utfutil.ReadFile(tmpfile.Name(), utfutil.UTF8)
if err != nil {
return nil, err
}
@@ -120,7 +129,7 @@ func (psh *psHandle) GetDNSZoneRecords(dnsserver, domain string) ([]nativeRecord
}
// powerShellDump runs a PowerShell command to get a dump of all records in a DNS zone.
func generatePSZoneDump(dnsserver, domainname string, filename string) string {
func generatePSZoneDump(dnsserver, domainname string) string {
var b bytes.Buffer
fmt.Fprintf(&b, `Get-DnsServerResourceRecord`)
if dnsserver != "" {
@@ -129,7 +138,8 @@ func generatePSZoneDump(dnsserver, domainname string, filename string) string {
fmt.Fprintf(&b, ` -ZoneName "%v"`, domainname)
fmt.Fprintf(&b, ` | `)
fmt.Fprintf(&b, `ConvertTo-Json -depth 4`) // Tested with 3 (causes errors). 4 and larger work.
fmt.Fprintf(&b, ` > %s`, filename)
// All file writing via dnsserver or pssession should be handled outside this function
//fmt.Fprintf(&b, ` > %s`, filename)
//fmt.Printf("DEBUG PSZoneDump CMD = (\n%s\n)\n", b.String())
return b.String()
}