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

provider/aws: Hash the admin password of AWS directory services in statefile #9848

Closed
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
21 changes: 18 additions & 3 deletions builtin/providers/aws/resource_aws_directory_service_directory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aws

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"time"
Expand All @@ -26,16 +28,23 @@ func resourceAwsDirectoryServiceDirectory() *schema.Resource {
Update: resourceAwsDirectoryServiceDirectoryUpdate,
Delete: resourceAwsDirectoryServiceDirectoryDelete,

SchemaVersion: 1,
MigrateState: resourceAwsDirectoryServiceDirectoryMigrateState,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
StateFunc: func(v interface{}) string {
return directoryServicePasswordHashSha256(v.(string))
},
},
"size": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -487,3 +496,9 @@ func resourceAwsDirectoryServiceDirectoryDelete(d *schema.ResourceData, meta int

return nil
}

func directoryServicePasswordHashSha256(password string) string {
password_hash := sha256.Sum256([]byte(password))
hash_hex := hex.EncodeToString(password_hash[:])
return hash_hex
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package aws

import (
"fmt"
"log"

"github.com/hashicorp/terraform/terraform"
)

func resourceAwsDirectoryServiceDirectoryMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {

switch v {
case 0:
log.Println("[INFO] Found AWS Directory Service Directory State v0; migrating to v1")
return migrateDirectoryServiceStateV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateDirectoryServiceStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}

log.Printf("[DEBUG] Attributes before migration %#v", is.Attributes)

// Replace password with password hash
is.Attributes["password"] = directoryServicePasswordHashSha256(is.Attributes["password"])

log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestResourceAwsDirectoryServicesDirectoryMigrateState(t *testing.T) {
cases := map[string]struct {
StateVersion int
ID string
Attributes map[string]string
Expected string
Meta interface{}
}{
"v0_1": {
StateVersion: 0,
ID: "d-abc123",
Attributes: map[string]string{
"password": "hunter2",
},
Expected: "f52fbd32b2b3b86ff88ef6c490628285f482af15ddcb29541f94bcf526a3f6c7",
},
}

for tn, tc := range cases {
is := &terraform.InstanceState{
ID: tc.ID,
Attributes: tc.Attributes,
}
is, err := resourceAwsDirectoryServiceDirectoryMigrateState(
tc.StateVersion, is, tc.Meta)

if err != nil {
t.Fatalf("bad: %s, err: %#v", tn, err)
}

if is.Attributes["password"] != tc.Expected {
t.Fatalf("Bad password hash migration: %s\n\n expected %s", is.Attributes["password"], tc.Expected)
}
}
}