|
1 | | -from unittest.mock import patch |
| 1 | +from unittest.mock import MagicMock, patch |
2 | 2 |
|
3 | 3 | from boto3 import client |
4 | 4 | from moto import mock_aws |
@@ -182,6 +182,59 @@ def test_project_github_not_allowed_organization(self): |
182 | 182 | ) |
183 | 183 | assert result[0].region == AWS_REGION_EU_WEST_1 |
184 | 184 |
|
| 185 | + @mock_aws |
| 186 | + def test_project_github_with_unlisted_roles(self): |
| 187 | + # iam:ListRoles denied leaves iam_client.roles as None. |
| 188 | + aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1]) |
| 189 | + codebuild_client = client("codebuild", region_name=AWS_REGION_EU_WEST_1) |
| 190 | + codebuild_client.create_project( |
| 191 | + name="test-project-github-unlisted-roles", |
| 192 | + source={ |
| 193 | + "type": "GITHUB", |
| 194 | + "location": "https://github.com/allowed-org/repo", |
| 195 | + }, |
| 196 | + artifacts={"type": "NO_ARTIFACTS"}, |
| 197 | + environment={ |
| 198 | + "type": "LINUX_CONTAINER", |
| 199 | + "image": "aws/codebuild/standard:4.0", |
| 200 | + "computeType": "BUILD_GENERAL1_SMALL", |
| 201 | + "environmentVariables": [], |
| 202 | + }, |
| 203 | + serviceRole=f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:role/codebuild-test-role", |
| 204 | + ) |
| 205 | + |
| 206 | + from prowler.providers.aws.services.codebuild.codebuild_service import Codebuild |
| 207 | + |
| 208 | + iam_client = MagicMock() |
| 209 | + iam_client.roles = None |
| 210 | + |
| 211 | + with ( |
| 212 | + patch( |
| 213 | + "prowler.providers.common.provider.Provider.get_global_provider", |
| 214 | + return_value=aws_provider, |
| 215 | + ), |
| 216 | + patch( |
| 217 | + "prowler.providers.aws.services.codebuild.codebuild_project_uses_allowed_github_organizations.codebuild_project_uses_allowed_github_organizations.codebuild_client", |
| 218 | + new=Codebuild(aws_provider), |
| 219 | + ), |
| 220 | + patch( |
| 221 | + "prowler.providers.aws.services.codebuild.codebuild_project_uses_allowed_github_organizations.codebuild_project_uses_allowed_github_organizations.iam_client", |
| 222 | + new=iam_client, |
| 223 | + ), |
| 224 | + patch( |
| 225 | + "prowler.providers.aws.services.codebuild.codebuild_project_uses_allowed_github_organizations.codebuild_project_uses_allowed_github_organizations.codebuild_client.audit_config", |
| 226 | + {"codebuild_github_allowed_organizations": ["allowed-org"]}, |
| 227 | + ), |
| 228 | + ): |
| 229 | + from prowler.providers.aws.services.codebuild.codebuild_project_uses_allowed_github_organizations.codebuild_project_uses_allowed_github_organizations import ( |
| 230 | + codebuild_project_uses_allowed_github_organizations, |
| 231 | + ) |
| 232 | + |
| 233 | + assert ( |
| 234 | + len(codebuild_project_uses_allowed_github_organizations().execute()) |
| 235 | + == 0 |
| 236 | + ) |
| 237 | + |
185 | 238 | @mock_aws |
186 | 239 | def test_project_github_no_codebuild_trusted_principal(self): |
187 | 240 | aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1]) |
|
0 commit comments