-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(aws): add check secretsmanager_has_restrictive_resource_policy
#6985
Open
kagahd
wants to merge
9
commits into
prowler-cloud:master
Choose a base branch
from
kagahd:secretsmanager_has_restrictive_resource_policy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(aws): add check secretsmanager_has_restrictive_resource_policy
#6985
kagahd
wants to merge
9
commits into
prowler-cloud:master
from
kagahd:secretsmanager_has_restrictive_resource_policy
+1,005
−0
Conversation
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
jfagoagas
reviewed
Feb 19, 2025
...nager_has_restrictive_resource_policy/secretsmanager_has_restrictive_resource_policy_test.py
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #6985 +/- ##
==========================================
+ Coverage 88.66% 88.76% +0.09%
==========================================
Files 1200 1208 +8
Lines 34647 35120 +473
==========================================
+ Hits 30721 31175 +454
- Misses 3926 3945 +19
Flags with carried forward coverage won't be shown. Click here to find out more.
|
secretsmanager_has_restrictive_resource_policy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Context
This feature request offers a new AWS check
secretsmanager_has_restrictive_resource_policy
.The check evaluates resource-based policies for AWS Secrets Manager secrets.
Description
To pass the check, resource-based policies attached to secrets must meet all of the following criteria:
1. Explicit "Deny" Statement for Unauthorized Principals
The resource policy must include an explicit
Deny
statement that applies to all principals, all actions, and all resources, unless exceptions are defined in theStringNotEquals
orStringNotEqualsIfExists
Condition block.Here is an example:
The
StringNotEquals
orStringNotEqualsIfExists
Condition block refines the denial by allowing exceptions, such as specific roles defined inaws:PrincipalArn
.One mandatory exception must be given to
<ASSUMED-ROLE-ARN-FOR-SECURY-AUDIT>
because this role, which is deployed in every organizations AWS account, must be allowed to audit it by the IT security team.All Principal ARNs that will have an own Policy Entry need to be listed here too, as shown in this example by the entry
arn:aws:iam::<YOUR_AWS_ACCOUNT_ID>:role/Role2
. Otherwise this rule already blocks any access tries.When to use which Condition Operator?
Use
StringNotEquals
if you want to specify Principals by their ARN, as shown in the example above.Use
StringNotEqualsIfExists
if you furthermore need to specify AWS services by their name, as shown in the following example. Since the request will contain either an AWS principal or the AWS service, we need to useStringNotEqualsIfExists
instead ofStringNotEquals
in such cases.2. Restrict Access Outside the Organization
The second mandatory
Deny
statement ensures that principals outside of the organization are denied to access the secret:The first four fields,
Effect
,Principal
,Action
andResource
have the same meaning as in theDeny
statementDenyUnauthorizedPrincipals
shown and described above.The
StringNotEquals
Condition ensures that the denial applies to any request coming from outside of the organization, as identified by theaws:PrincipalOrgID
condition key. Only requests originating from the organization are exempted.The value(s) of
aws:PrincipalOrgID
is configurable by the keyorganizations_trusted_ids
within prowler's config.yaml file.Leave this value empty in prowler's config.yaml if the AWS account is not bound to an organization. The check
secretsmanager_has_restrictive_resource_policy
will then not require theDenyOutsideOrganization
statement in the policy.However, AWS services e.g. "appflow.amazonaws.com", do not send a PrincipalOrgID in their request. Therefore, the policy statement must exclude those service principals by using
NotPrincipal
as the following "DenyOutsideOrganization" statement demonstrates:3. Deny Statement to Permit Only Specific Actions for Designated Principals
The third
Deny
statements ensures that specific principals are restricted to performing only specific actions. ThisDeny
statements is not mandatory but if you omit it, the AWS principals specified in theDenyUnauthorizedPrincipals
statement will be denied for all actions.Any principal listed in the
StringNotEquals
condition of the exampleDeny
statementDenyUnauthorizedPrincipals
shown above (e.g.,<ASSUMED-ROLE-ARN-FOR-SECURY-AUDIT>
,arn:aws:iam::<YOUR_AWS_ACCOUNT_ID>:role/Role2
) needs a correspondingDeny
statement with a tailoredNotAction
clause to ensure, that each principal can perform only the specific actions they require, while all other actions are denied by default.Wildcards
*
in theNotAction
field are not permitted, please refer to the supported AWS Secrets Manager actions.It is mandatory to ensure, that the IT Security team can audit the resource-based policy attached to a Secrets Manager secret. That's why the principal representing the security team (e.g.,
<ASSUMED-ROLE-ARN-FOR-SECURY-AUDIT>
) must be permitted to both actionssecretsmanager:DescribeSecret
andsecretsmanager:GetResourcePolicy
, as show in the following example.4. Allow Statement for AWS services
AWS services need an explizit
Allow
Statement in order to be granted to execute actions on the secret. There are two requirements:StringEquals
Condition operator as shown in the following example:Cross-account access to secrets is not allowed. Resource based policies attached to secrets that allow cross-account access to the secret result in failing the check. But maybe this should/could be configurable.
Unit Tests
As you can see in the
secretsmanager_has_restrictive_resource_policy_test.py
, I have created five unit tests, but three of them cover around 50 test cases by modifying specific parts of a valid resource based policy to let the check FAIL. In order to proof that the check PASSes without this invalid part of the policy, a corresponding test case is run right after. This counter-test is also intended to proof that the logic in the unit test itself is correct.These around 50 test cases in only three unit tests reduce redundancies in the code. We could reduce still more code redundancies but I think that would have affected the readability of the unit tests.
Update: As suggested by @jfagoagas ,
@pytest.mark.parametrize
is now used for these unit tests.Checklist
API
License
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.