-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Current issues: 1) Tags not supported yet 2) Terradozer cannot delete policy attachments to user
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/iam" | ||
|
||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
res "github.com/cloudetc/awsweeper/resource" | ||
) | ||
|
||
func TestAcc_IamUser_DeleteByID(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping acceptance test.") | ||
} | ||
|
||
env := InitEnv(t) | ||
|
||
terraformDir := "./test-fixtures/iam-user" | ||
|
||
terraformOptions := getTerraformOptions(terraformDir, env) | ||
|
||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
terraform.InitAndApply(t, terraformOptions) | ||
|
||
id := terraform.Output(t, terraformOptions, "id") | ||
assertIamUserExists(t, env, id) | ||
|
||
writeConfigID(t, terraformDir, res.IamUser, id) | ||
defer os.Remove(terraformDir + "/config.yml") | ||
|
||
logBuffer, err := runBinary(t, terraformDir, "YES\n") | ||
require.NoError(t, err) | ||
|
||
assertIamUserDeleted(t, env, id) | ||
|
||
fmt.Println(logBuffer) | ||
} | ||
|
||
func TestAcc_IamUser_DeleteByTag(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping acceptance test.") | ||
} | ||
|
||
env := InitEnv(t) | ||
|
||
terraformDir := "./test-fixtures/iam-user" | ||
|
||
terraformOptions := getTerraformOptions(terraformDir, env) | ||
|
||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
terraform.InitAndApply(t, terraformOptions) | ||
|
||
id := terraform.Output(t, terraformOptions, "id") | ||
assertIamUserExists(t, env, id) | ||
|
||
writeConfigTag(t, terraformDir, res.IamUser) | ||
defer os.Remove(terraformDir + "/config.yml") | ||
|
||
logBuffer, err := runBinary(t, terraformDir, "YES\n") | ||
require.NoError(t, err) | ||
|
||
assertIamUserDeleted(t, env, id) | ||
|
||
fmt.Println(logBuffer) | ||
} | ||
|
||
func assertIamUserExists(t *testing.T, env EnvVars, id string) { | ||
assert.True(t, iamUserExists(t, env, id)) | ||
} | ||
|
||
func assertIamUserDeleted(t *testing.T, env EnvVars, id string) { | ||
assert.False(t, iamUserExists(t, env, id)) | ||
} | ||
|
||
func iamUserExists(t *testing.T, env EnvVars, id string) bool { | ||
opts := &iam.GetUserInput{ | ||
UserName: &id, | ||
} | ||
|
||
_, err := env.AWSClient.GetUser(opts) | ||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
t.Fatal() | ||
} | ||
if ec2err.Code() == "NoSuchEntity" { | ||
return false | ||
} | ||
t.Fatal() | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
provider "aws" { | ||
version = "~> 2.0" | ||
|
||
profile = var.profile | ||
region = var.region | ||
} | ||
|
||
terraform { | ||
# The configuration for this backend will be filled in by Terragrunt | ||
backend "s3" { | ||
} | ||
} | ||
|
||
resource "aws_iam_user" "test" { | ||
name = var.name | ||
path = "/awsweeper-test-acc/" | ||
|
||
tags = { | ||
awsweeper = "test-acc" | ||
} | ||
} | ||
|
||
resource "aws_iam_access_key" "test" { | ||
user = aws_iam_user.test.name | ||
} | ||
|
||
resource "aws_iam_user_policy" "test" { | ||
name = var.name | ||
user = aws_iam_user.test.id | ||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"ec2:Describe*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
resource "aws_iam_policy" "test_policy" { | ||
name = var.name | ||
description = "A test policy" | ||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"ec2:Describe*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
resource "aws_iam_user_policy_attachment" "test" { | ||
user = aws_iam_user.test.name | ||
policy_arn = aws_iam_policy.test_policy.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "id" { | ||
value = aws_iam_user.test.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
variable "profile" { | ||
description = "The named profile for the AWS account that will be deployed to" | ||
} | ||
|
||
variable "region" { | ||
description = "The AWS region to deploy to" | ||
} | ||
|
||
variable "name" { | ||
description = "The name of test" | ||
} |