-
Couldn't load subscription status.
- Fork 11
[CONTP-962] Add tests for IAM role names with paths. #42
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
Merged
Mathew-Estafanous
merged 6 commits into
main
from
mathew.estafanous/add-iam-role-parse-tests
Sep 9, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea23940
add tests for iam roles with and without path
Mathew-Estafanous 049ef86
chore: pass just arn object to validate both cases.
Mathew-Estafanous d8f836f
chore: rename task role path name
Mathew-Estafanous a6b8946
chore: terraform fmt
Mathew-Estafanous d1bcbb2
chore: use terraform-test path circumventing ci iam perms
Mathew-Estafanous b4e3aae
chore: task def comments
Mathew-Estafanous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2025-present Datadog, Inc. | ||
|
|
||
| ################################################################################ | ||
| # Task Definition: IAM Role with path in name | ||
| ################################################################################ | ||
|
|
||
| # Create IAM roles with paths to test the parsing logic | ||
| resource "aws_iam_role" "test_task_role_with_path" { | ||
| name = "${var.test_prefix}-task-role-with-path" | ||
| path = "/terraform-test/" | ||
|
|
||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [{ | ||
| Effect = "Allow" | ||
| Principal = { | ||
| Service = "ecs-tasks.amazonaws.com" | ||
| } | ||
| Action = "sts:AssumeRole" | ||
| }] | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_iam_role" "test_execution_role_with_path" { | ||
| name = "${var.test_prefix}-execution-role-with-path" | ||
| path = "/terraform-test/" | ||
|
|
||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [{ | ||
| Effect = "Allow" | ||
| Principal = { | ||
| Service = "ecs-tasks.amazonaws.com" | ||
| } | ||
| Action = "sts:AssumeRole" | ||
| }] | ||
| }) | ||
| } | ||
|
|
||
| # Attach required policies to execution role | ||
| resource "aws_iam_role_policy_attachment" "test_execution_role_policy" { | ||
| role = aws_iam_role.test_execution_role_with_path.name | ||
| policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" | ||
| } | ||
|
|
||
| module "dd_task_role_parsing_with_path" { | ||
| source = "../../modules/ecs_fargate" | ||
|
|
||
| # Use roles with paths to test parsing | ||
| task_role = aws_iam_role.test_task_role_with_path | ||
| execution_role = { arn = aws_iam_role.test_execution_role_with_path.arn } | ||
|
|
||
| dd_api_key = var.dd_api_key | ||
| dd_site = var.dd_site | ||
| dd_service = var.dd_service | ||
| dd_essential = true | ||
|
|
||
| # Configure Task Definition | ||
| family = "${var.test_prefix}-role-parsing-with-path" | ||
| container_definitions = jsonencode([]) | ||
|
|
||
| requires_compatibilities = ["FARGATE"] | ||
| } |
This file contains hidden or 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 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2025-present Datadog, Inc. | ||
|
|
||
| ################################################################################ | ||
| # Task Definition: IAM Role without path in name | ||
| ################################################################################ | ||
|
|
||
| # Create IAM roles without paths to test the parsing logic | ||
| resource "aws_iam_role" "test_task_role_without_path" { | ||
| name = "${var.test_prefix}-task-role-without-path" | ||
| # No path specified - defaults to "/" | ||
|
|
||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [{ | ||
| Effect = "Allow" | ||
| Principal = { | ||
| Service = "ecs-tasks.amazonaws.com" | ||
| } | ||
| Action = "sts:AssumeRole" | ||
| }] | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_iam_role" "test_execution_role_without_path" { | ||
| name = "${var.test_prefix}-execution-role-without-path" | ||
| # No path specified - defaults to "/" | ||
|
|
||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [{ | ||
| Effect = "Allow" | ||
| Principal = { | ||
| Service = "ecs-tasks.amazonaws.com" | ||
| } | ||
| Action = "sts:AssumeRole" | ||
| }] | ||
| }) | ||
| } | ||
|
|
||
| # Attach required policies to execution role | ||
| resource "aws_iam_role_policy_attachment" "test_execution_role_policy_no_path" { | ||
| role = aws_iam_role.test_execution_role_without_path.name | ||
| policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" | ||
| } | ||
|
|
||
| module "dd_task_role_parsing_without_path" { | ||
| source = "../../modules/ecs_fargate" | ||
|
|
||
| # Use roles without paths to test parsing | ||
| task_role = aws_iam_role.test_task_role_without_path | ||
| execution_role = { arn = aws_iam_role.test_execution_role_without_path.arn } | ||
|
|
||
| dd_api_key = var.dd_api_key | ||
| dd_site = var.dd_site | ||
| dd_service = var.dd_service | ||
| dd_essential = true | ||
|
|
||
| # Configure Task Definition | ||
| family = "${var.test_prefix}-role-parsing-without-path" | ||
| container_definitions = jsonencode([]) | ||
|
|
||
| requires_compatibilities = ["FARGATE"] | ||
| } |
This file contains hidden or 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,73 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2025-present Datadog, Inc. | ||
|
|
||
| package test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log" | ||
| "strings" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/service/ecs/types" | ||
| "github.com/gruntwork-io/terratest/modules/terraform" | ||
| ) | ||
|
|
||
| // TestRoleParsingWithPath tests that the module correctly parses role names from ARNs with paths | ||
| func (s *ECSFargateSuite) TestRoleParsingWithPath() { | ||
| log.Println("TestRoleParsingWithPath: Running test...") | ||
|
|
||
| var containers []types.ContainerDefinition | ||
| task := terraform.OutputMap(s.T(), s.terraformOptions, "role-parsing-with-path") | ||
|
|
||
| s.Equal(s.testPrefix+"-role-parsing-with-path", task["family"], "Unexpected task family name") | ||
|
|
||
| err := json.Unmarshal([]byte(task["container_definitions"]), &containers) | ||
| s.NoError(err, "Failed to parse container definitions") | ||
|
|
||
| s.NotEmpty(task["arn"], "Task definition ARN should not be empty") | ||
| s.NotEmpty(task["revision"], "Task definition revision should not be empty") | ||
|
|
||
| taskRoleArn := task["task_role_arn"] | ||
| s.NotEmpty(taskRoleArn, "Task role ARN should not be empty") | ||
| s.Contains(taskRoleArn, "/terraform-test/", "Task role ARN should contain the path '/test-path/'") | ||
| s.Contains(taskRoleArn, s.testPrefix+"-task-role-with-path", "Task role ARN should contain the expected role name") | ||
|
|
||
| executionRoleArn := task["execution_role_arn"] | ||
| s.NotEmpty(executionRoleArn, "Execution role ARN should not be empty") | ||
| s.Contains(executionRoleArn, "/terraform-test/", "Execution role ARN should contain the path '/terraform-test/'") | ||
| s.Contains(executionRoleArn, s.testPrefix+"-execution-role-with-path", "Execution role ARN should contain the expected role name") | ||
| } | ||
|
|
||
| // TestRoleParsingWithoutPath tests that the module correctly parses role names from ARNs without paths | ||
| func (s *ECSFargateSuite) TestRoleParsingWithoutPath() { | ||
| log.Println("TestRoleParsingWithoutPath: Running test...") | ||
|
|
||
| var containers []types.ContainerDefinition | ||
| task := terraform.OutputMap(s.T(), s.terraformOptions, "role-parsing-without-path") | ||
|
|
||
| s.Equal(s.testPrefix+"-role-parsing-without-path", task["family"], "Unexpected task family name") | ||
|
|
||
| err := json.Unmarshal([]byte(task["container_definitions"]), &containers) | ||
| s.NoError(err, "Failed to parse container definitions") | ||
|
|
||
| s.NotEmpty(task["arn"], "Task definition ARN should not be empty") | ||
| s.NotEmpty(task["revision"], "Task definition revision should not be empty") | ||
|
|
||
| taskRoleArn := task["task_role_arn"] | ||
| s.NotEmpty(taskRoleArn, "Task role ARN should not be empty") | ||
| s.Contains(taskRoleArn, s.testPrefix+"-task-role-without-path", "Task role ARN should contain the expected role name") | ||
|
|
||
| roleArnParts := strings.Split(taskRoleArn, "/") | ||
| s.Equal(2, len(roleArnParts), "Role ARN without path should have exactly 2 parts when split by '/'") | ||
| s.Contains(roleArnParts[1], s.testPrefix+"-task-role-without-path", "Role name should be the second part after splitting by '/'") | ||
|
|
||
| executionRoleArn := task["execution_role_arn"] | ||
| s.NotEmpty(executionRoleArn, "Execution role ARN should not be empty") | ||
| s.Contains(executionRoleArn, s.testPrefix+"-execution-role-without-path", "Execution role ARN should contain the expected role name") | ||
|
|
||
| execRoleArnParts := strings.Split(executionRoleArn, "/") | ||
| s.Equal(2, len(execRoleArnParts), "Execution role ARN without path should have exactly 2 parts when split by '/'") | ||
| s.Contains(execRoleArnParts[1], s.testPrefix+"-execution-role-without-path", "Execution role name should be the second part after splitting by '/'") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: expected role path