Linting tool for CloudFormation templates
The cfn-nag tool looks for patterns in CloudFormation templates that may indicate insecure infrastructure. Roughly speaking, it will look for:
For more background on the tool, please see this post at Stelligent's blog:
Presuming Ruby >= 2.5.x is installed, installation is just a matter of:
gem install cfn-nagOn MacOS or Linux you can alternatively install with brew:
brew install ruby brew-gem
brew gem install cfn-nagTo run cfn_nag as an action in CodePipeline, you can deploy via the AWS Serverless Application Repository.
To execute:
cfn_nag_scan --input-path The path can be a directory or a particular template. If it is a directory, all .json, .template, .yml and .yaml files will be processed, including recursing into subdirectories.
The default output format is free-form text, but json output can be selected with the --output-format json flag.
Optionally, a --debug flag will dump information about the internals of rule loading.
Run with --help for a full listing of supported switches.
To see a list of all the rules cfn-nag currently supports, there is a command-line utility that will dump them to stdout:
cfn_nag_rulesA Dockerfile is provided for convenience. It is published on DockerHub as stelligent/cfn_nag.
You can also build it locally.
docker build -t stelligent/cfn_nag .You can mount a local directory containing templates into the Docker container and then call cfn_nag in the container. This example uses the test templates used in unit testing cfn_nag:
$ docker run -v `pwd`/spec/test_templates:/templates -t stelligent/cfn_nag /templates/json/efs/filesystem_with_encryption.json
{
"failure_count": 0,
"violations": [
]
}
$ docker run -v `pwd`/spec/test_templates:/templates -t stelligent/cfn_nag /templates/json/efs/filesystem_with_no_encryption.json
{
"failure_count": 1,
"violations": [
{
"id": "F27",
"type": "FAIL",
"message": "EFS FileSystem should have encryption enabled",
"logical_resource_ids": [
"filesystem"
]
}
]
}cfn_nag_scan can be run as part of a GitHub Workflow to evaluate code during continuous integration pipelines.
In your GitHub Workflow file, create a step which uses the cfn_nag Action:
- name: Simple test
uses: stelligent/cfn_nag@master
with:
input_path: testsMore information about the GitHub Action can be found here.
cfn-nag supports the notion of a "profile" which is effectively an allow list of rules to apply. The profile is a text file
that must contain a rule identifier per line. When specified via the --profile-path command line argument,
cfn-nag will ONLY return violations from those particular rules.
The motivation behind creating a "profile" is that different developers might care about different rules. For example, an "infrastructure_developer" might care about IAM rules, while an "app_developer" might not even be able to create IAM resources and therefore not care about those rules.
Here is an example profile:
F1
F2
F27
W3
W5The deny list is basically the opposite of the profile: it's a list of rules to NEVER apply. When specified via the
--deny-list-path command line argument, cfn-nag will NEVER return violations from those particular rules specified
in the file.
In case a rule is specified in both, the deny list will take priority over the profile, and the rule will not be applied.
The format is as follows. The only two salient fields are RulesToSuppress and the id per item. The reason won't
be interpreted by cfn-nag, but it is recommended to justify and document why the rule should never be applied.
RulesToSuppress:
- id: W3
reason: W3 is something we never care about at enterprise XIn the event that there is a rule that you want to suppress, a cfn_nag Metadata key can be added to the affected resource to tell cfn_nag to not raise a failure or warning for that rule.
For example, if you are setting up a public-facing ELB that's open to inbound connections from the internet with resources like the following:
public_alb.yaml
# Partial template
PublicAlbSecurityGroup:
Properties:
GroupDescription: 'Security group for a public Application Load Balancer'
VpcId:
Ref: vpc
Type: AWS::EC2::SecurityGroup
PublicAlbSecurityGroupHttpIngress:
Properties:
CidrIp: 0.0.0.0/0
FromPort: 80
GroupId:
Ref: PublicAlbSecurityGroup
IpProtocol: tcp
ToPort: 80
Type: AWS::EC2::SecurityGroupIngresscfn_nag will raise warnings like the following:
…By adding the metadata, these warnings can be suppressed:
public_alb_with_suppression.yaml
…$ cfn_nag_scan -i public_alb_with_suppression.yaml
------------------------------------------------------------
public_alb_with_supression.yaml
------------------------------------------------------------
Failures count: 0
Warnings count: 0CloudFormation Template Parameters can present a problem for static analysis as the values are specified at the point of deployment. In other words, the values aren't available when the static analysis is done - static analysis can only look at the "code" that is in front of it. Therefore a security group ingress rule of 0.0.0.0/0 won't be flagged if the cidr is parameterized and the 0.0.0.0/0 is passed in at deploy time.
To allow for checking parameter values, a user can specify the parameter values in a JSON file passed on the command line
to both cfn_nag and cfn_nag_scan with the --parameter-values-path= flag.
The format of the JSON is a single key, "Parameters", whose value is a dictionary with each key/value pair mapping to the Parameters:
{
"Parameters": {
"Cidr": "0.0.0.0/0"
}
}This will provide "0.0.0.0/0" to the following Parameter:
Parameters:
Cidr:
Type: StringBEWARE that if there are extra parameters in the JSON, they are quietly ignored (to allow cfn_nag_scan to apply
the same JSON across all the templates).
If the JSON is malformed or doesn't meet the above specification, then parsing will fail with a FATAL violation.
Prior to 0.5.55, calls to Fn::FindInMap were effectively ignored. The underlying model would
leave them be, and so they would appear as Hash values to rules. For example: { "Fn::FindInMap" => [map1, key1, key2]}
Starting in 0.5.55, the model will attempt to compute the value for a call to FindInMap and present that value to the rules. This evaluation supports keys that are:
If the evaluation logic can't figure out the value for a key, it will default to the old behavior of returning the Hash for the whole expression.
Also prior to 0.5.55, calls to AWS pseudofunctions were effectively ignored. The underlying model would
leave them be, and so they would appear as Hash values to rules. For example: {"Ref"=>"AWS::Region"}.
A common use case is to organize mappings by region, so pseudofunction evaluation is important to better supporting
map evaluation.
Starting in 0.5.55, the model will present the following AWS pseudofunctions to rules with the default values:
'AWS::URLSuffix' => 'amazonaws.com',
'AWS::Partition' => 'aws',
'AWS::NotificationARNs' => '',
'AWS::AccountId' => '111111111111',
'AWS::Region' => 'us-east-1',
'AWS::StackId' => 'arn:aws:cloudformation:us-east-1:111111111111:stack/stackname/51af3dc0-da77-11e4-872e-1234567db123',
'AWS::StackName' => 'stackname'Additionally, the end user can override the value supplied via the traditional parameter substitution mechanism. For example:
{
"Parameters": {
"AWS::Region": "eu-west-1"
}
}Up until version 0.4.66 of cfn_nag, the underlying model did not do any processing of Fn::If within a template. This meant that if a property had a conditional value, it was up to the rule to parse the Fn::If. Given that an Fn::If could appear just about anywhere, it created a whack-a-mole situation for rule developers. At best, the rule logic could ignore values that were Hash presuming the value wasn't a Hash in the first place.
In order to address this issue, the default behavior for cfn_nag is now to substitute Fn::If with the true outcome. This means by default that rules will not inspect the false outcomes for security violations.
In addition to substituting Fn::If at the property value level, the same behavior is applied to Fn::If at the top-level of Properties. For example:
Resource1:
Type: Foo
Properties: !If
- IsNone
- Description: Up
- Description: DOwnWill look the same as:
Resource1:
Type: Foo
Properties:
Description: UpTo provide some control over this behavior, a user can specify the condition values in a JSON file passed on the command line
to both cfn_nag and cfn_nag_scan with the --condition-values-path= flag.
The format of the JSON is a a dictionary with each key/value pair mapping to the Conditions:
{
"Condition1": true,
"Condition2": false
}The basis for SPCM is described in the blog post Thought Experiment Proposed Complexity Metric for IAM Policy Documents.
Starting in version 0.6.0 of cfn_nag:
spcm_scan can scan a directory of CloudFormation templates (like cfn_nag_scan) and generate a report with the SPCM
metrics in either JSON or HTML formatcfn_nag_scan --rule-arguments spcm_threshold:100--rule-arguments mechanism.
The Rule object only needs to declare an attr_accessor, e.g. attr_accessor :spcm_threshold and cfn_nag
will take care of the details to inject values from the --rule-argumentsThe release of 0.5.x includes some major changes in how custom rules (can) be distributed and loaded. Before this release,
there were two places where rules were loaded from: the lib/cfn-nag/custom_rules directory within the core cfn_nag gem,
and the custom-rule-directory specified on the command line.
There are two use cases that forced a redesign of how/where custom rules are loaded. The rule loading mechanism has been generalized such that custom rule repositories can be used to discover rules.
No open issues yet, or sync has not completed.