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

Double-Hop Solution: Pass Credentials #107

Closed
wants to merge 4 commits into from
Closed
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
43 changes: 23 additions & 20 deletions ad/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (

// ProviderConfig holds all the information necessary to configure the provider
type ProviderConfig struct {
WinRMUsername string
WinRMPassword string
WinRMHost string
WinRMPort int
WinRMProto string
WinRMInsecure bool
KrbRealm string
KrbConfig string
KrbSpn string
WinRMUseNTLM bool
WinRMUsername string
WinRMPassword string
WinRMHost string
WinRMPort int
WinRMProto string
WinRMInsecure bool
KrbRealm string
KrbConfig string
KrbSpn string
WinRMUseNTLM bool
WinRMPassCredentials bool
}

// NewConfig returns a new Config struct populated with Resource Data.
Expand All @@ -47,18 +48,20 @@ func NewConfig(d *schema.ResourceData) ProviderConfig {
krbConfig := d.Get("krb_conf").(string)
krbSpn := d.Get("krb_spn").(string)
winRMUseNTLM := d.Get("winrm_use_ntlm").(bool)
winRMPassCredentials := d.Get("winrm_pass_credentials").(bool)

cfg := ProviderConfig{
WinRMHost: winRMHost,
WinRMPort: winRMPort,
WinRMProto: winRMProto,
WinRMUsername: winRMUsername,
WinRMPassword: winRMPassword,
WinRMInsecure: winRMInsecure,
KrbRealm: krbRealm,
KrbConfig: krbConfig,
KrbSpn: krbSpn,
WinRMUseNTLM: winRMUseNTLM,
WinRMHost: winRMHost,
WinRMPort: winRMPort,
WinRMProto: winRMProto,
WinRMUsername: winRMUsername,
WinRMPassword: winRMPassword,
WinRMInsecure: winRMInsecure,
KrbRealm: krbRealm,
KrbConfig: krbConfig,
KrbSpn: krbSpn,
WinRMUseNTLM: winRMUseNTLM,
WinRMPassCredentials: winRMPassCredentials,
}

return cfg
Expand Down
3 changes: 2 additions & 1 deletion ad/data_source_ad_computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func dataSourceADComputer() *schema.Resource {

func dataSourceADComputerRead(d *schema.ResourceData, meta interface{}) error {
isLocal := meta.(ProviderConf).isConnectionTypeLocal()
isPassCredentialsEnabled := meta.(ProviderConf).isPassCredentialsEnabled()
client, err := meta.(ProviderConf).AcquireWinRMClient()
if err != nil {
return err
Expand All @@ -56,7 +57,7 @@ func dataSourceADComputerRead(d *schema.ResourceData, meta interface{}) error {
identity = dn
}

computer, err := winrmhelper.NewComputerFromHost(client, identity, isLocal)
computer, err := winrmhelper.NewComputerFromHost(client, identity, isLocal, isPassCredentialsEnabled, meta.(ProviderConf).Configuration.WinRMUsername, meta.(ProviderConf).Configuration.WinRMPassword)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ad/data_source_ad_gpo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func dataSourceADGPO() *schema.Resource {

func dataSourceADGPORead(d *schema.ResourceData, meta interface{}) error {
isLocal := meta.(ProviderConf).isConnectionTypeLocal()
isPassCredentialsEnabled := meta.(ProviderConf).isPassCredentialsEnabled()
name := winrmhelper.SanitiseTFInput(d, "name")
guid := winrmhelper.SanitiseTFInput(d, "guid")

Expand All @@ -42,7 +43,7 @@ func dataSourceADGPORead(d *schema.ResourceData, meta interface{}) error {
}
defer meta.(ProviderConf).ReleaseWinRMClient(client)

gpo, err := winrmhelper.GetGPOFromHost(client, name, guid, isLocal)
gpo, err := winrmhelper.GetGPOFromHost(client, name, guid, isLocal, isPassCredentialsEnabled, meta.(ProviderConf).Configuration.WinRMUsername, meta.(ProviderConf).Configuration.WinRMPassword)
if err != nil {
if strings.Contains(err.Error(), "GpoWithNameNotFound") || strings.Contains(err.Error(), "GpoWithIdNotFound") {
d.SetId("")
Expand Down
3 changes: 2 additions & 1 deletion ad/data_source_ad_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func dataSourceADGroup() *schema.Resource {

func dataSourceADGroupRead(d *schema.ResourceData, meta interface{}) error {
isLocal := meta.(ProviderConf).isConnectionTypeLocal()
isPassCredentialsEnabled := meta.(ProviderConf).isPassCredentialsEnabled()
client, err := meta.(ProviderConf).AcquireWinRMClient()
if err != nil {
return err
Expand All @@ -72,7 +73,7 @@ func dataSourceADGroupRead(d *schema.ResourceData, meta interface{}) error {

groupID := d.Get("group_id").(string)

g, err := winrmhelper.GetGroupFromHost(client, groupID, isLocal)
g, err := winrmhelper.GetGroupFromHost(client, groupID, isLocal, isPassCredentialsEnabled, meta.(ProviderConf).Configuration.WinRMUsername, meta.(ProviderConf).Configuration.WinRMPassword)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ad/data_source_ad_ou.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func dataSourceADOU() *schema.Resource {

func dataSourceADOURead(d *schema.ResourceData, meta interface{}) error {
isLocal := meta.(ProviderConf).isConnectionTypeLocal()
isPassCredentialsEnabled := meta.(ProviderConf).isPassCredentialsEnabled()
client, err := meta.(ProviderConf).AcquireWinRMClient()
if err != nil {
return err
Expand All @@ -58,7 +59,7 @@ func dataSourceADOURead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("invalid inputs, dn or a combination of path and name are required")
}

ou, err := winrmhelper.NewOrgUnitFromHost(client, dn, name, path, isLocal)
ou, err := winrmhelper.NewOrgUnitFromHost(client, dn, name, path, isLocal, isPassCredentialsEnabled, meta.(ProviderConf).Configuration.WinRMUsername, meta.(ProviderConf).Configuration.WinRMPassword)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ad/data_source_ad_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ func dataSourceADUser() *schema.Resource {

func dataSourceADUserRead(d *schema.ResourceData, meta interface{}) error {
isLocal := meta.(ProviderConf).isConnectionTypeLocal()
isPassCredentialsEnabled := meta.(ProviderConf).isPassCredentialsEnabled()
userID := d.Get("user_id").(string)
client, err := meta.(ProviderConf).AcquireWinRMClient()
if err != nil {
return err
}
defer meta.(ProviderConf).ReleaseWinRMClient(client)

u, err := winrmhelper.GetUserFromHost(client, userID, nil, isLocal)
u, err := winrmhelper.GetUserFromHost(client, userID, nil, isLocal, isPassCredentialsEnabled, meta.(ProviderConf).Configuration.WinRMUsername, meta.(ProviderConf).Configuration.WinRMPassword)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions ad/internal/winrmhelper/winrm_computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func NewComputerFromResource(d *schema.ResourceData) *Computer {

// NewComputerFromHost return a new Machine struct populated from data we get
// from the domain controller
func NewComputerFromHost(conn *winrm.Client, identity string, execLocally bool) (*Computer, error) {
func NewComputerFromHost(conn *winrm.Client, identity string, execLocally, passCredentials bool, username, password string) (*Computer, error) {
cmd := fmt.Sprintf("Get-ADComputer -Identity %q -Properties *", identity)
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally)
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally, passCredentials, username, password)
if err != nil {
return nil, fmt.Errorf("winrm execution failure in NewComputerFromHost: %s", err)
}
Expand All @@ -55,7 +55,7 @@ func NewComputerFromHost(conn *winrm.Client, identity string, execLocally bool)
}

// Create creates a new Computer object in the AD tree
func (m *Computer) Create(conn *winrm.Client, execLocally bool) (string, error) {
func (m *Computer) Create(conn *winrm.Client, execLocally, passCredentials bool, username, password string) (string, error) {
if m.Name == "" {
return "", fmt.Errorf("Computer.Create: missing name variable")
}
Expand All @@ -73,7 +73,7 @@ func (m *Computer) Create(conn *winrm.Client, execLocally bool) (string, error)
cmd = fmt.Sprintf("%s -Description %q", cmd, m.Description)
}

result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally)
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally, passCredentials, username, password)
if err != nil {
return "", fmt.Errorf("winrm execution failure while creating computer object: %s", err)
}
Expand All @@ -90,14 +90,14 @@ func (m *Computer) Create(conn *winrm.Client, execLocally bool) (string, error)
}

// Update updates an existing Computer objects in the AD tree
func (m *Computer) Update(conn *winrm.Client, changes map[string]interface{}, execLocally bool) error {
func (m *Computer) Update(conn *winrm.Client, changes map[string]interface{}, execLocally, passCredentials bool, username, password string) error {
if m.GUID == "" {
return fmt.Errorf("cannot update computer object with name %q, guid is not set", m.Name)
}

if path, ok := changes["container"]; ok {
cmd := fmt.Sprintf("Move-AdObject -Identity %q -TargetPath %q", m.GUID, path.(string))
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally)
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally, passCredentials, username, password)
if err != nil {
return fmt.Errorf("winrm execution failure while moving computer object: %s", err)
}
Expand All @@ -113,7 +113,7 @@ func (m *Computer) Update(conn *winrm.Client, changes map[string]interface{}, ex
description = fmt.Sprintf("%q", description)
}
cmd := fmt.Sprintf("Set-ADComputer -Identity %q -Description %s", m.GUID, description)
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally)
result, err := RunWinRMCommand(conn, []string{cmd}, true, false, execLocally, passCredentials, username, password)
if err != nil {
return fmt.Errorf("winrm execution failure while modifying computer description: %s", err)
}
Expand All @@ -126,9 +126,9 @@ func (m *Computer) Update(conn *winrm.Client, changes map[string]interface{}, ex
}

// Delete deletes an existing Computer objects from the AD tree
func (m *Computer) Delete(conn *winrm.Client, execLocally bool) error {
func (m *Computer) Delete(conn *winrm.Client, execLocally, passCredentials bool, username, password string) error {
cmd := fmt.Sprintf("Remove-ADComputer -confirm:$false -Identity %q", m.GUID)
result, err := RunWinRMCommand(conn, []string{cmd}, false, false, execLocally)
result, err := RunWinRMCommand(conn, []string{cmd}, false, false, execLocally, passCredentials, username, password)
if err != nil {
return fmt.Errorf("winrm execution failure while removing computer object: %s", err)
}
Expand Down
18 changes: 9 additions & 9 deletions ad/internal/winrmhelper/winrm_gplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type GPLink struct {
}

//NewGPLink creates a link between a GPO and an AD object
func (g *GPLink) NewGPLink(client *winrm.Client, execLocally bool) (string, error) {
func (g *GPLink) NewGPLink(client *winrm.Client, execLocally, passCredentials bool, username, password string) (string, error) {
log.Printf("[DEBUG] Creating new user")
enforced := "No"
if g.Enforced {
Expand All @@ -41,7 +41,7 @@ func (g *GPLink) NewGPLink(client *winrm.Client, execLocally bool) (string, erro
cmds = append(cmds, fmt.Sprintf("-Order %d", g.Order))
}

result, err := RunWinRMCommand(client, cmds, true, false, execLocally)
result, err := RunWinRMCommand(client, cmds, true, false, execLocally, passCredentials, username, password)
if err != nil {
return "", err
}
Expand All @@ -59,7 +59,7 @@ func (g *GPLink) NewGPLink(client *winrm.Client, execLocally bool) (string, erro
return "", fmt.Errorf("error while unmarshalling gplink json document: %s", err)
}

ou, err := NewOrgUnitFromHost(client, gplink.Target, "", "", execLocally)
ou, err := NewOrgUnitFromHost(client, gplink.Target, "", "", execLocally, passCredentials, username, password)
if err != nil {
return "", fmt.Errorf("failed to retrieve details for OU %q: %s", gplink.Target, err)
}
Expand All @@ -71,7 +71,7 @@ func (g *GPLink) NewGPLink(client *winrm.Client, execLocally bool) (string, erro
}

//ModifyGPLink changes a GPO link
func (g *GPLink) ModifyGPLink(client *winrm.Client, changes map[string]interface{}, execLocally bool) error {
func (g *GPLink) ModifyGPLink(client *winrm.Client, changes map[string]interface{}, execLocally, passCredentials bool, username, password string) error {
cmds := []string{fmt.Sprintf("Set-GPLink -guid %q -target %q", g.GPOGuid, g.Target)}
keyMap := map[string]string{
"enforced": "Enforced",
Expand All @@ -95,7 +95,7 @@ func (g *GPLink) ModifyGPLink(client *winrm.Client, changes map[string]interface
if len(cmds) == 1 {
return nil
}
result, err := RunWinRMCommand(client, cmds, false, false, execLocally)
result, err := RunWinRMCommand(client, cmds, false, false, execLocally, passCredentials, username, password)
if err != nil {
return fmt.Errorf("error while running Set-GPLink: %s", err)
}
Expand All @@ -108,9 +108,9 @@ func (g *GPLink) ModifyGPLink(client *winrm.Client, changes map[string]interface
}

//RemoveGPLink deletes a link between a GPO and an AD object
func (g *GPLink) RemoveGPLink(client *winrm.Client, execLocally bool) error {
func (g *GPLink) RemoveGPLink(client *winrm.Client, execLocally, passCredentials bool, username, password string) error {
cmd := fmt.Sprintf("Remove-GPlink -Guid %q -Target %q", g.GPOGuid, g.Target)
_, err := RunWinRMCommand(client, []string{cmd}, false, false, execLocally)
_, err := RunWinRMCommand(client, []string{cmd}, false, false, execLocally, passCredentials, username, password)
if err != nil {
// Check if the resource is already deleted
if strings.Contains(err.Error(), "GpoLinkNotFound") || strings.Contains(err.Error(), "GpoWithIdNotFound") || strings.Contains(err.Error(), "There is no such object on the server") {
Expand All @@ -136,9 +136,9 @@ func GetGPLinkFromResource(d *schema.ResourceData) *GPLink {

//GetGPLinkFromHost returns a GPLink struct populated with data retrieved from the
//Domain Controller
func GetGPLinkFromHost(client *winrm.Client, gpoGUID, containerGUID string, execLocally bool) (*GPLink, error) {
func GetGPLinkFromHost(client *winrm.Client, gpoGUID, containerGUID string, execLocally, passCredentials bool, username, password string) (*GPLink, error) {
cmds := []string{fmt.Sprintf("Get-ADObject -filter {ObjectGUID -eq %q} -properties gplink", containerGUID)}
result, err := RunWinRMCommand(client, cmds, true, false, execLocally)
result, err := RunWinRMCommand(client, cmds, true, false, execLocally, passCredentials, username, password)
if err != nil {
return nil, fmt.Errorf("while running Get-ADObject: %s", err)
}
Expand Down
Loading