From 2e046232a08712dececffac4664b83b21fff811f Mon Sep 17 00:00:00 2001 From: James Nugent Date: Tue, 25 Oct 2016 16:14:00 -0500 Subject: [PATCH] provider/aws: Add Login Profile acceptance tests --- .../resource_aws_iam_user_login_profile.go | 19 +- ...esource_aws_iam_user_login_profile_test.go | 278 ++++++++++++++++++ 2 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/aws/resource_aws_iam_user_login_profile_test.go diff --git a/builtin/providers/aws/resource_aws_iam_user_login_profile.go b/builtin/providers/aws/resource_aws_iam_user_login_profile.go index 46b7cd90b..dea063a3b 100644 --- a/builtin/providers/aws/resource_aws_iam_user_login_profile.go +++ b/builtin/providers/aws/resource_aws_iam_user_login_profile.go @@ -128,6 +128,21 @@ func resourceAwsIamUserLoginProfileCreate(d *schema.ResourceData, meta interface passwordResetRequired := d.Get("password_reset_required").(bool) passwordLength := d.Get("password_length").(int) + _, err := iamconn.GetLoginProfile(&iam.GetLoginProfileInput{ + UserName: aws.String(username), + }) + if err != nil { + if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "NoSuchEntity" { + // If there is already a login profile, bring it under management (to prevent + // resource creation diffs) - we will never modify it, but obviously cannot + // set the password. + d.SetId(username) + d.Set("key_fingerprint", "") + d.Set("encrypted_password", "") + return nil + } + } + var pgpKey string if pgpKeyInterface, ok := d.GetOk("pgp_key"); ok { pgpKey = pgpKeyInterface.(string) @@ -152,7 +167,9 @@ func resourceAwsIamUserLoginProfileCreate(d *schema.ResourceData, meta interface // If there is already a login profile, bring it under management (to prevent // resource creation diffs) - we will never modify it, but obviously cannot // set the password. - d.SetId(*createResp.LoginProfile.UserName) + d.SetId(username) + d.Set("key_fingerprint", "") + d.Set("encrypted_password", "") return nil } return errwrap.Wrapf(fmt.Sprintf("Error creating IAM User Login Profile for %q: {{err}}", username), err) diff --git a/builtin/providers/aws/resource_aws_iam_user_login_profile_test.go b/builtin/providers/aws/resource_aws_iam_user_login_profile_test.go new file mode 100644 index 000000000..d9fd9f33b --- /dev/null +++ b/builtin/providers/aws/resource_aws_iam_user_login_profile_test.go @@ -0,0 +1,278 @@ +package aws + +import ( + "errors" + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/iam" + "github.com/davecgh/go-spew/spew" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/hashicorp/vault/helper/pgpkeys" + "log" + "time" +) + +func TestAccAWSUserLoginProfile_basic(t *testing.T) { + var conf iam.GetLoginProfileOutput + + username := fmt.Sprintf("test-user-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSUserDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSUserLoginProfileConfig(username, "/", testPubKey1), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSUserLoginProfileExists("aws_iam_user_login_profile.user", &conf), + testDecryptPasswordAndTest("aws_iam_user_login_profile.user", "aws_iam_access_key.user", testPrivKey1), + ), + }, + }, + }) +} + +func TestAccAWSUserLoginProfile_keybase(t *testing.T) { + var conf iam.GetLoginProfileOutput + + username := fmt.Sprintf("test-user-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSUserDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSUserLoginProfileConfig(username, "/", "keybase:terraformacctest"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSUserLoginProfileExists("aws_iam_user_login_profile.user", &conf), + resource.TestCheckResourceAttrSet("aws_iam_user_login_profile.user", "encrypted_password"), + resource.TestCheckResourceAttrSet("aws_iam_user_login_profile.user", "key_fingerprint"), + ), + }, + }, + }) +} + +func TestAccAWSUserLoginProfile_keybaseDoesntExist(t *testing.T) { + username := fmt.Sprintf("test-user-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSUserDestroy, + Steps: []resource.TestStep{ + { + // Hope no-one creates this keybase user... + Config: testAccAWSUserLoginProfileConfig(username, "/", "keybase:terraform_nope"), + ExpectError: true, + }, + }, + }) +} + +func testDecryptPasswordAndTest(nProfile, nAccessKey, key string) resource.TestCheckFunc { + return func(s *terraform.State) error { + profileResource, ok := s.RootModule().Resources[nProfile] + if !ok { + return fmt.Errorf("Not found: %s", nProfile) + } + + password, ok := profileResource.Primary.Attributes["encrypted_password"] + if !ok { + return errors.New("No password in state") + } + + accessKeyResource, ok := s.RootModule().Resources[nAccessKey] + if !ok { + return fmt.Errorf("Not found: %s", nAccessKey) + } + + accessKeyId := accessKeyResource.Primary.ID + secretAccessKey, ok := accessKeyResource.Primary.Attributes["secret"] + if !ok { + return errors.New("No secret access key in state") + } + + decryptedPassword, err := pgpkeys.DecryptBytes(password, key) + if err != nil { + return fmt.Errorf("Error decrypting password: %s", err) + } + + iamAsCreatedUserSession := session.New(&aws.Config{ + Region: aws.String("us-west-2"), + Credentials: credentials.NewStaticCredentials(accessKeyId, secretAccessKey, ""), + }) + _, err = iamAsCreatedUserSession.Config.Credentials.Get() + if err != nil { + return fmt.Errorf("Error getting session credentials: %s", err) + } + + return resource.Retry(2*time.Minute, func() *resource.RetryError { + iamAsCreatedUser := iam.New(iamAsCreatedUserSession) + _, err = iamAsCreatedUser.ChangePassword(&iam.ChangePasswordInput{ + OldPassword: aws.String(decryptedPassword.String()), + NewPassword: aws.String(generatePassword(20)), + }) + if err != nil { + if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "InvalidClientTokenId" { + return resource.RetryableError(err) + } + + return resource.NonRetryableError(fmt.Errorf("Error changing decrypted password: %s", err)) + } + + return nil + }) + } +} + +func testAccCheckAWSUserLoginProfileExists(n string, res *iam.GetLoginProfileOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("No UserName is set") + } + + iamconn := testAccProvider.Meta().(*AWSClient).iamconn + resp, err := iamconn.GetLoginProfile(&iam.GetLoginProfileInput{ + UserName: aws.String(rs.Primary.ID), + }) + if err != nil { + return err + } + + *res = *resp + + return nil + } +} + +func testAccAWSUserLoginProfileConfig(r, p, key string) string { + return fmt.Sprintf(` +resource "aws_iam_user" "user" { + name = "%s" + path = "%s" + force_destroy = true +} + +data "aws_caller_identity" "current" {} + +data "aws_iam_policy_document" "user" { + statement { + effect = "Allow" + actions = ["iam:GetAccountPasswordPolicy"] + resources = ["*"] + } + statement { + effect = "Allow" + actions = ["iam:ChangePassword"] + resources = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/&{aws:username}"] + } +} + +resource "aws_iam_user_policy" "user" { + name = "AllowChangeOwnPassword" + user = "${aws_iam_user.user.name}" + policy = "${data.aws_iam_policy_document.user.json}" +} + +resource "aws_iam_access_key" "user" { + user = "${aws_iam_user.user.name}" +} + +resource "aws_iam_user_login_profile" "user" { + user = "${aws_iam_user.user.name}" + pgp_key = "%s" +} +`, r, p, key) +} + +const testPubKey1 = `mQENBFXbjPUBCADjNjCUQwfxKL+RR2GA6pv/1K+zJZ8UWIF9S0lk7cVIEfJiprzzwiMwBS5cD0da +rGin1FHvIWOZxujA7oW0O2TUuatqI3aAYDTfRYurh6iKLC+VS+F7H+/mhfFvKmgr0Y5kDCF1j0T/ +063QZ84IRGucR/X43IY7kAtmxGXH0dYOCzOe5UBX1fTn3mXGe2ImCDWBH7gOViynXmb6XNvXkP0f +sF5St9jhO7mbZU9EFkv9O3t3EaURfHopsCVDOlCkFCw5ArY+DUORHRzoMX0PnkyQb5OzibkChzpg +8hQssKeVGpuskTdz5Q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