Skip to content

Commit

Permalink
Export SID for security principals (hashicorp#76)
Browse files Browse the repository at this point in the history
Add a computed field that holds an object's SID.
This applies to user, group, and computer resources and datasources.

Also fixes a bug in the ad_user datasource caused after renaming the `guid` field to `user_id`.
Closes hashicorp#60.
  • Loading branch information
koikonom authored Jan 21, 2021
1 parent b1c4f8a commit 7f0bc09
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ad/data_source_ad_computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func dataSourceADComputer() *schema.Resource {
Optional: true,
Description: "The Distinguished Name of the computer object.",
},
"sid": {
Type: schema.TypeString,
Computed: true,
Description: "The SID of the computer object.",
},
},
}
}
Expand Down Expand Up @@ -59,6 +64,7 @@ func dataSourceADComputerRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("name", computer.Name)
_ = d.Set("dn", computer.DN)
_ = d.Set("guid", computer.GUID)
_ = d.Set("sid", computer.SID.Value)

return nil
}
6 changes: 6 additions & 0 deletions ad/data_source_ad_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func dataSourceADGroup() *schema.Resource {
Computed: true,
Description: "The Group's container object.",
},
"sid": {
Type: schema.TypeString,
Computed: true,
Description: "The SID of the group object.",
},
},
}
}
Expand Down Expand Up @@ -75,6 +80,7 @@ func dataSourceADGroupRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("container", g.Container)
_ = d.Set("name", g.Name)
_ = d.Set("group_id", groupID)
_ = d.Set("sid", g.SID.Value)

d.SetId(g.GUID)
return nil
Expand Down
8 changes: 7 additions & 1 deletion ad/data_source_ad_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func dataSourceADUser() *schema.Resource {
Computed: true,
Description: "Postal code of the user object.",
},
"sid": {
Type: schema.TypeString,
Computed: true,
Description: "The SID of the user object.",
},
"smart_card_logon_required": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -201,7 +206,7 @@ func dataSourceADUserRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("sam_account_name", u.SAMAccountName)
_ = d.Set("display_name", u.DisplayName)
_ = d.Set("principal_name", u.PrincipalName)
_ = d.Set("guid", u.GUID)
_ = d.Set("user_id", u.GUID)
_ = d.Set("city", u.City)
_ = d.Set("company", u.Company)
_ = d.Set("country", u.Country)
Expand All @@ -225,6 +230,7 @@ func dataSourceADUserRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("other_name", u.OtherName)
_ = d.Set("po_box", u.POBox)
_ = d.Set("postal_code", u.PostalCode)
_ = d.Set("sid", u.SID.Value)
_ = d.Set("state", u.State)
_ = d.Set("street_address", u.StreetAddress)
_ = d.Set("surname", u.Surname)
Expand Down
1 change: 1 addition & 0 deletions ad/internal/winrmhelper/winrm_computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Computer struct {
DN string `json:"DistinguishedName"`
SAMAccountName string `json:"SamAccountName"`
Path string
SID SID `json:"SID"`
}

// NewComputerFromResource returns a new Machine struct populated from resource data
Expand Down
1 change: 1 addition & 0 deletions ad/internal/winrmhelper/winrm_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Group struct {
Scope string
Category string
Container string
SID SID `json:"SID"`
}

// AddGroup creates a new group
Expand Down
6 changes: 6 additions & 0 deletions ad/internal/winrmhelper/winrm_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/masterzen/winrm"
)

// SID is a common structure by all "security principals". This means domains, users, computers, and groups.
// The structure we get from powershell contains more fields, but we're only interested in the Value.
type SID struct {
Value string `json:"Value"`
}

//WinRMResult holds the stdout, stderr and exit code of a powershell command
type WinRMResult struct {
Stdout string
Expand Down
1 change: 1 addition & 0 deletions ad/internal/winrmhelper/winrm_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type User struct {
OtherName string
POBox string
PostalCode string
SID SID `json:"SID"`
SmartcardLogonRequired bool
State string
StreetAddress string
Expand Down
6 changes: 6 additions & 0 deletions ad/resource_ad_computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func resourceADComputer() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"sid": {
Type: schema.TypeString,
Computed: true,
Description: "The SID of the computer object.",
},
},
}
}
Expand Down Expand Up @@ -76,6 +81,7 @@ func resourceADComputerRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("guid", computer.GUID)
_ = d.Set("pre2kname", computer.SAMAccountName)
_ = d.Set("container", computer.Path)
_ = d.Set("sid", computer.SID.Value)

return nil
}
Expand Down
6 changes: 6 additions & 0 deletions ad/resource_ad_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func resourceADGroup() *schema.Resource {
Description: "A DN of a container object holding the group.",
DiffSuppressFunc: suppressCaseDiff,
},
"sid": {
Type: schema.TypeString,
Computed: true,
Description: "The SID of the group object.",
},
},
}
}
Expand Down Expand Up @@ -96,6 +101,7 @@ func resourceADGroupRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("scope", g.Scope)
_ = d.Set("category", g.Category)
_ = d.Set("container", g.Container)
_ = d.Set("sid", g.SID.Value)

return nil
}
Expand Down
6 changes: 6 additions & 0 deletions ad/resource_ad_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ func resourceADUser() *schema.Resource {
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: suppressJsonDiff,
},
"sid": {
Type: schema.TypeString,
Computed: true,
Description: "The SID of the user object.",
},
},
}
}
Expand Down Expand Up @@ -333,6 +338,7 @@ func resourceADUserRead(d *schema.ResourceData, meta interface{}) error {
_ = d.Set("other_name", u.OtherName)
_ = d.Set("po_box", u.POBox)
_ = d.Set("postal_code", u.PostalCode)
_ = d.Set("sid", u.SID.Value)
_ = d.Set("state", u.State)
_ = d.Set("street_address", u.StreetAddress)
_ = d.Set("surname", u.Surname)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/computer.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ output "computer_guid" {
### Read-only

- **name** (String, Read-only) The name of the computer object.
- **sid** (String, Read-only) The SID of the computer object.


1 change: 1 addition & 0 deletions docs/data-sources/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ output "g2_guid" {
- **name** (String, Read-only) The name of the Group object.
- **sam_account_name** (String, Read-only) The SAM account name of the Group object.
- **scope** (String, Read-only) The Group's scope.
- **sid** (String, Read-only) The SID of the group object.


1 change: 1 addition & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ output "testuser_guid" {
- **postal_code** (String, Read-only) Postal code of the user object.
- **principal_name** (String, Read-only) The principal name of the user object.
- **sam_account_name** (String, Read-only) The SAM account name of the user object.
- **sid** (String, Read-only) The SID of the user object.
- **smart_card_logon_required** (Boolean, Read-only) Smart card required to logon or not
- **state** (String, Read-only) State of the user object.
- **street_address** (String, Read-only) Address of the user object.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/computer.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ resource "ad_computer" "c" {

- **dn** (String, Read-only)
- **guid** (String, Read-only)
- **sid** (String, Read-only) The SID of the computer object.

## Import

Expand Down
4 changes: 4 additions & 0 deletions docs/resources/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ resource "ad_group" "g" {
- **id** (String, Optional) The ID of this resource.
- **scope** (String, Optional) The group's scope. Can be one of `global`, `local`, or `universal` (case sensitive).

### Read-only

- **sid** (String, Read-only) The SID of the group object.

## Import

Import is supported using the following syntax:
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ resource "ad_user" "u2" {
- **title** (String, Optional) Specifies the user's title. This parameter sets the Title property of a user object
- **trusted_for_delegation** (Boolean, Optional) If set to true, the user account is trusted for Kerberos delegation. A service that runs under an account that is trusted for Kerberos delegation can assume the identity of a client requesting the service. This parameter sets the TrustedForDelegation property of an account object.

### Read-only

- **sid** (String, Read-only) The SID of the user object.

## Import

Import is supported using the following syntax:
Expand Down

0 comments on commit 7f0bc09

Please sign in to comment.