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

Fix permanent diff on aws_opsworks ssh_key and password #10175

Merged
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
10 changes: 7 additions & 3 deletions aws/resource_aws_opsworks_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,16 @@ func resourceAwsOpsworksSetApplicationSource(d *schema.ResourceData, v *opsworks
if v.Username != nil {
m["username"] = *v.Username
}
if v.Password != nil {
m["password"] = *v.Password
}
if v.Revision != nil {
m["revision"] = *v.Revision
}

// v.Password and v.SshKey will, on read, contain the placeholder string
// "*****FILTERED*****", so we ignore it on read and let persist
// the value already in the state.
m["password"] = d.Get("app_source.0.password").(string)
m["ssh_key"] = d.Get("app_source.0.ssh_key").(string)

nv = append(nv, m)
}

Expand Down
6 changes: 5 additions & 1 deletion aws/resource_aws_opsworks_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,13 @@ func resourceAwsOpsworksSetStackCustomCookbooksSource(d *schema.ResourceData, v
if v.Revision != nil {
m["revision"] = *v.Revision
}
// v.Password will, on read, contain the placeholder string

// v.Password and v.SshKey will, on read, contain the placeholder string
// "*****FILTERED*****", so we ignore it on read and let persist
// the value already in the state.
m["password"] = d.Get("custom_cookbooks_source.0.password").(string)
m["ssh_key"] = d.Get("custom_cookbooks_source.0.ssh_key").(string)

nv = append(nv, m)
}

Expand Down
234 changes: 234 additions & 0 deletions aws/resource_aws_opsworks_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,37 @@ func TestAccAWSOpsworksStack_NoVpcCreateTags(t *testing.T) {
})
}

/////////////////////////////
// Tests for Custom Cookbooks
/////////////////////////////

func TestAccAWSOpsworksStack_CustomCookbooks_SetPrivateProperties(t *testing.T) {
stackName := fmt.Sprintf("tf-opsworks-acc-%d", acctest.RandInt())
var opsstack opsworks.Stack
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsOpsworksStackDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSOpsworksStackConfig_CustomCookbooks_Set(stackName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSOpsworksStackExists("aws_opsworks_stack.tf-acc", true, &opsstack),
testAccCheckAWSOpsworksCreateStackAttributesWithCookbooks(&opsstack, "us-west-2a", stackName),
resource.TestCheckResourceAttr(
"aws_opsworks_stack.tf-acc",
"custom_cookbooks_source.0.password",
"password"),
resource.TestCheckResourceAttr(
"aws_opsworks_stack.tf-acc",
"custom_cookbooks_source.0.ssh_key",
sshKey),
),
},
},
})
}

// Tests the addition of regional endpoints and supporting the classic link used
// to create Stack's prior to v0.9.0.
// See https://github.com/hashicorp/terraform/issues/12842
Expand Down Expand Up @@ -551,6 +582,61 @@ func testAccCheckAWSOpsworksCreateStackAttributes(
}
}

func testAccCheckAWSOpsworksCreateStackAttributesWithCookbooks(
opsstack *opsworks.Stack, zone, stackName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *opsstack.Name != stackName {
return fmt.Errorf("Unnexpected stackName: %s", *opsstack.Name)
}

if *opsstack.DefaultAvailabilityZone != zone {
return fmt.Errorf("Unnexpected DefaultAvailabilityZone: %s", *opsstack.DefaultAvailabilityZone)
}

if *opsstack.DefaultOs != "Amazon Linux 2016.09" {
return fmt.Errorf("Unnexpected defaultOs: %s", *opsstack.DefaultOs)
}

if *opsstack.DefaultRootDeviceType != "ebs" {
return fmt.Errorf("Unnexpected DefaultRootDeviceType: %s", *opsstack.DefaultRootDeviceType)
}

if *opsstack.CustomJson != `{"key": "value"}` {
return fmt.Errorf("Unnexpected CustomJson: %s", *opsstack.CustomJson)
}

if *opsstack.ConfigurationManager.Version != "11.10" {
return fmt.Errorf("Unnexpected Version: %s", *opsstack.ConfigurationManager.Version)
}

if *opsstack.UseOpsworksSecurityGroups {
return fmt.Errorf("Unnexpected UseOpsworksSecurityGroups: %t", *opsstack.UseOpsworksSecurityGroups)
}

if !*opsstack.UseCustomCookbooks {
return fmt.Errorf("Unnexpected UseCustomCookbooks: %t", *opsstack.UseCustomCookbooks)
}

if *opsstack.CustomCookbooksSource.Type != "git" {
return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Type)
}

if *opsstack.CustomCookbooksSource.Revision != "master" {
return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Revision)
}

if *opsstack.CustomCookbooksSource.Url != "https://github.com/aws/opsworks-example-cookbooks.git" {
return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Url)
}

if *opsstack.CustomCookbooksSource.Username != "username" {
return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Username)
}

return nil
}
}

func testAccCheckAWSOpsworksUpdateStackAttributes(
opsstack *opsworks.Stack, zone, stackName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -1251,3 +1337,151 @@ resource "aws_iam_instance_profile" "opsworks_instance" {
}
`, name, name, name, name, name)
}

/////////////////////////////////////////
// Helpers for Custom Cookbook properties
/////////////////////////////////////////

func testAccAWSOpsworksStackConfig_CustomCookbooks_Set(name string) string {
return fmt.Sprintf(`
resource "aws_vpc" "tf-acc" {
cidr_block = "10.3.5.0/24"

tags = {
Name = "terraform-testacc-opsworks-stack-vpc-update"
}
}

resource "aws_subnet" "tf-acc" {
vpc_id = "${aws_vpc.tf-acc.id}"
cidr_block = "${aws_vpc.tf-acc.cidr_block}"
availability_zone = "us-west-2a"

tags = {
Name = "tf-acc-opsworks-stack-vpc-update"
}
}

resource "aws_opsworks_stack" "tf-acc" {
name = "%s"
region = "us-west-2"
vpc_id = "${aws_vpc.tf-acc.id}"
default_subnet_id = "${aws_subnet.tf-acc.id}"
service_role_arn = "${aws_iam_role.opsworks_service.arn}"
default_instance_profile_arn = "${aws_iam_instance_profile.opsworks_instance.arn}"
default_os = "Amazon Linux 2016.09"
default_root_device_type = "ebs"
custom_json = "{\"key\": \"value\"}"
configuration_manager_version = "11.10"
use_opsworks_security_groups = false
use_custom_cookbooks = true
manage_berkshelf = true

custom_cookbooks_source {
type = "git"
revision = "master"
url = "https://github.com/aws/opsworks-example-cookbooks.git"
username = "username"
password = "password"
ssh_key = "%s"
}
}

resource "aws_iam_role" "opsworks_service" {
name = "%s_opsworks_service"

assume_role_policy = <<EOT
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "opsworks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOT
}

resource "aws_iam_role_policy" "opsworks_service" {
name = "%s_opsworks_service"
role = "${aws_iam_role.opsworks_service.id}"

policy = <<EOT
{
"Statement": [
{
"Action": [
"ec2:*",
"iam:PassRole",
"cloudwatch:GetMetricStatistics",
"elasticloadbalancing:*",
"rds:*"
],
"Effect": "Allow",
"Resource": ["*"]
}
]
}
EOT
}

resource "aws_iam_role" "opsworks_instance" {
name = "%s_opsworks_instance"

assume_role_policy = <<EOT
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOT
}

resource "aws_iam_instance_profile" "opsworks_instance" {
name = "%s_opsworks_instance"
roles = ["${aws_iam_role.opsworks_instance.name}"]
}
`, name, sshKey, name, name, name, name)
}

// One-off, bogus private key generated for use in testing
const sshKey = "-----BEGIN RSA PRIVATE KEY-----" +
"MIIEpAIBAAKCAQEAv/1hnOZadSDMbJUVJsqweDwc/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BLVSG3/ichMcttB8CgYAdzmaebvILzrOKIpqiT4JF" +
"w3dwtO6ehqRNbQCDMmtGC1rY/ICWgJquQjHS/7W8BaSRx7R/JlDEPbNwOWOGU8YO" +
"2g/5NC1d70HpE77lKA5f25gxwvuaj4+9otYW0y0AGxjeB+ulhmsS05cck8v0/jmh" +
"MBB0RyNyGjy1AGQOh7OYBQKBgQCwFq1HFM2K1hVOYkglXPcV5OqRDn1sCo5gEsLZ" +
"oXL1cZKEhIuhLawixPQl8yKMxSDEGjGQ2Acf4axANuRAt5qwskWOBjjdtx66MNoh" +
"yznTgVrdk4cakMBWOMKVJplhx6XDj+gbct3NjB2A775oGRmg+Esnsp6siYzcpq0G" +
"qANFWQKBgQCyv8KoQXsD8f8XMvicRC42uZXfhlDjOUzpo1O7WQKWBYqPBqz4AHzE" +
"Cdy6djI120bqDOifre1qnBjoHezrG+ejaQOTpocOVwT5Zl7BhjoXQZRGiQXj+2aD" +
"tmm0+hpmkjX7jiPcljjs8S8gh+uCWieJoO4JNPk2SXRiePpYgKzdlg==" +
"-----END RSA PRIVATE KEY-----"
4 changes: 2 additions & 2 deletions website/docs/r/opsworks_application.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ An `app_source` block supports the following arguments (can only be defined once
* `type` - (Required) The type of source to use. For example, "archive".
* `url` - (Required) The URL where the app resource can be found.
* `username` - (Optional) Username to use when authenticating to the source.
* `password` - (Optional) Password to use when authenticating to the source.
* `ssh_key` - (Optional) SSH key to use when authenticating to the source.
* `password` - (Optional) Password to use when authenticating to the source. Terraform cannot perform drift detection of this configuration.
* `ssh_key` - (Optional) SSH key to use when authenticating to the source. Terraform cannot perform drift detection of this configuration.
* `revision` - (Optional) For sources that are version-aware, the revision to use.

An `environment` block supports the following arguments:
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/opsworks_stack.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ The `custom_cookbooks_source` block supports the following arguments:
* `type` - (Required) The type of source to use. For example, "archive".
* `url` - (Required) The URL where the cookbooks resource can be found.
* `username` - (Optional) Username to use when authenticating to the source.
* `password` - (Optional) Password to use when authenticating to the source.
* `ssh_key` - (Optional) SSH key to use when authenticating to the source.
* `password` - (Optional) Password to use when authenticating to the source. Terraform cannot perform drift detection of this configuration.
* `ssh_key` - (Optional) SSH key to use when authenticating to the source. Terraform cannot perform drift detection of this configuration.
* `revision` - (Optional) For sources that are version-aware, the revision to use.

## Attributes Reference
Expand Down