Comments (5)
With Guard 2.0 you can select keys for a map that match a regular expression. The above rule in 2.0 is as follows
AWS::IAM::Role Properties {
Policies.*.PolicyDocument.Statement.*.Resource[ KEYS == /.+Sub/ ].* == "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
}
It would work for example given. However, it isn't correct as written. For AWS::IAM::Role
the Resource
attribute is multi-valued or in other words is an array of values. It is possible to have any of the following specified in a CFN template
"Resource": [
{
"Fn::Sub": "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
},
"aws:arn...."
]
or
"Resource": "aws:arn...."
or
"Resource": ["aws:arn...", "aws:arn"]
Here you are only interested in targeting those that contain a Fn::Sub
reference. Here is the way to specify this in Guard 2.0
AWS::IAM::Role Properties {
#
# Select all Resource elements that have 'Fn::Sub'
#
let resources = Policies.*.PolicyDocument.Statement.*.Resource[ 'Fn::Sub' EXISTS ]
when %resources !EMPTY {
%resources.* == "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
}
}
How do we know if the rule really worked? You can unit test this rule with Guard 2.0. Here is a sample test suite
- input:
Resources:
role:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyDocument:
Statement:
- Resource: "aws:arn"
expectations:
rules:
default: SKIP
- input:
Resources:
role:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyDocument:
Statement:
- Resource:
"Fn::Sub": "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
expectations:
rules:
default: PASS
- input:
Resources:
role:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyDocument:
Statement:
- Resource:
- "Fn::Sub": "fail"
- "*"
expectations:
rules:
default: FAIL
- input:
Resources:
role:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyDocument:
Statement:
- Resource:
- "aws:arn"
- "*"
expectations:
rules:
default: SKIP
We are internally tracking a feature where this expression would simplify to
AWS::IAM::Role Properties {
Policies.*.PolicyDocument.Statement.*.Resource[ 'Fn::Sub' EXISTS ].* == "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
}
where it SKIPs the results if there are no such Resources present. For now, we need to use the when !EMPTY
condition check.
from cloudformation-guard.
+1 Regex support will be great for complicated conditions
from cloudformation-guard.
We are providing enhanced property access feature in the next release of the tooling, as detailed in #115. We hope to encourage users to do these checks using the KEYS operator to avoid using regular expression inside of json. So instead of
AWS::IAM::Role Policies.*.PolicyDocument.Statement.*.Resource == {/.+Sub/: "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"}
one could write in the new version of the rules language
AWS::IAM::Role Policies.*.PolicyDocument.Statement.*.Resource KEYS == /.+Sub/
AWS::IAM::Role Policies.*.PolicyDocument.Statement.*.Resource.* == "arn:aws:acm-pca:${AWS::Region}:${AWS::AccountId}:certificate-authority/as"
}
As of now, we have no plan to support regular expression for json keys as listed above.
from cloudformation-guard.
Attaching samples with tests
policies-examples.zip
from cloudformation-guard.
Resolving, please re-open if you needed any more support on this. thank you
from cloudformation-guard.
Related Issues (20)
- Add support for SARIF [Enhancement] HOT 2
- [GENERAL ISSUE] Documentation HOT 4
- [BUG] cfn-guard error via GitHub Actions HOT 2
- [Enhancement] Only show errors/failures in output HOT 5
- [BUG] Failed rule does not print details of the failure HOT 6
- [Enhancement] Add cspell action to check for spelling errors
- [Documentation] Update docs for all public facing functions, and structs
- [BUG] SAM CLI deployed lambda returns InvalidEntryPoint error HOT 3
- [Enhancement] Add support to retrieve the key of a given node HOT 1
- [BUG] '!=' does not work as expected even for same type while using 2 literals. HOT 2
- [Enhancement] Make variables mutable HOT 1
- [Enhancement] Support for CloudFormation Pseudo Parameters
- [GENERAL ISSUE] String concatenation with literal and variable HOT 2
- [BUG] OR is not working HOT 8
- [BUG] "true" does not equal true HOT 2
- Best practice for cfn-guard rules for CDK synthesized resources that are wrapped in a CustomResource, e.g. aws-eks.Cluster HOT 1
- [BUG] Problem with --show-summary HOT 2
- Question about specificity in wildcards. HOT 4
- Trying to search an If statement for a value HOT 2
- [GENERAL ISSUE] cfn-guard and Terraform JSON output HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cloudformation-guard.