Skip to content
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: NZIM-v36-1022-20 NagPack #1068

Closed
wants to merge 0 commits into from
Closed

Conversation

mrpackethead
Copy link

Fixes #1067

@dontirun dontirun changed the title added nzism-v36-1022-20 nag pak feat: NZIM-v36-1022-20 NagPack Nov 1, 2022
Copy link
Collaborator

@dontirun dontirun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did an initial review. Thanks for putting this together @mrpackethead! We'll want to wait for the conformance pack to actually get released before we release this so that we can confirm that we aren't missing anything. That being said, doesn't hurt to get the review started early!

Some additional comments for things that are currently missing for release

  1. The included and excluded rules for the pack listed in the [RULES(https://github.com/cdklabs/cdk-nag/blob/main/RULES.md) file
  2. A Pack Test. This is important so that we don't inadvertently 'lose' rules in the case of internal refactoring.
  3. If we need to write any new rules (which we may), the rules will also need their own unit tests as well

Any new rules that we write

src/packs/nzism-v36-1022-20.ts Outdated Show resolved Hide resolved
src/packs/nzism-v36-1022-20.ts Outdated Show resolved Hide resolved
src/packs/nzism-v36-1022-20.ts Outdated Show resolved Hide resolved
src/packs/nzism-v36-1022-20.ts Outdated Show resolved Hide resolved
src/packs/nzism-v36-1022-20.ts Outdated Show resolved Hide resolved
@mrpackethead
Copy link
Author

On the change to the Rules.md file, i can do some of the work now, and then come back and add the Links once the docs are published.

@mrpackethead
Copy link
Author

@dontirun , I've added a couple of sample lines to the the RULES.md, Before i do all of them, can we check that the sample lines are in the right format?

nzismrule.md Outdated Show resolved Hide resolved
nzismrule.md Outdated Show resolved Hide resolved
nzismrule.md Outdated Show resolved Hide resolved
.projenrc.js Outdated Show resolved Hide resolved
@mrpackethead
Copy link
Author

mrpackethead commented Nov 3, 2022

It looks like there are seven new rules to create. Some of these will be easy, others less so.

A temporary file ( nzismRulestoCreate )

cloudfront-default-root-object-configured
ebs-snapshot-public-restorable-check ( syou can't create a snapshot in CloudFormation)
ec2-imdsv2-check now included.
ec2-instance-managed-by-systems-manager (that's something that would have to be checked in Systems Manager after the instance is launched)
elb-custom-security-policy-ssl-check Already done via ELBTlsHttpsListenersOnly.ts)
lambda-function-public-access-prohibited @dontirun working on this, via #1072
vpc-sg-open-only-to-authorized-ports now done

@mrpackethead
Copy link
Author

EC2SecurityGroupOnlyTcp443 checks security groups to see if they only have tcp 443 open

@dontirun
Copy link
Collaborator

dontirun commented Nov 4, 2022

EC2SecurityGroupOnlyTcp443 checks security groups to see if they only have tcp 443 open

Is this an implementation of
vpc-sg-open-only-to-authorized-ports?

I don't think we should be implementing this rule since it's highly dependent on business logic.

The config rule takes in an input list because it is supposed to be tuned to business logic

@mrpackethead
Copy link
Author

EC2SecurityGroupOnlyTcp443 checks security groups to see if they only have tcp 443 open

Is this an implementation of vpc-sg-open-only-to-authorized-ports?

I don't think we should be implementing this rule since it's highly dependent on business logic.

The config rule takes in an input list because it is supposed to be tuned to business logic

The nzism specifically calls out that only tcp/443 shoudl be open.

    Controls: ['3205']
    Type: AWS::Config::ConfigRule
    Properties:
      ConfigRuleName: vpc-sg-open-only-to-authorized-ports
      InputParameters:
        authorizedTcpPorts: '443'
      Scope:
        ComplianceResourceTypes:
        - AWS::EC2::SecurityGroup
      Source:
        Owner: AWS
        SourceIdentifier: VPC_SG_OPEN_ONLY_TO_AUTHORIZED_PORTS
      Description: "SHOULD 18.1.13.C.02[CID:3205]| Network security/Network Management/Limiting network access"```

@mrpackethead
Copy link
Author

Have added rules for

cloudfront-default-root-object-configured
ec2-imdsv2-check

and added them to the nzism pak.

included lambda-function-public-access-prohibited from the rule that was recently added.

Excluded the other rules, as they are not resolvable at synth time.

To do, Hopefully will get an answer about the tcp443 inbound security group rule, and then will be able to decide what we will do.

Copy link
Collaborator

@dontirun dontirun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a view passes for reviews, but here are some initial comments

README.md Outdated Show resolved Hide resolved
RULES.md Outdated Show resolved Hide resolved
RULES.md Outdated Show resolved Hide resolved
RULES.md Outdated Show resolved Hide resolved
RULES.md Outdated Show resolved Hide resolved
src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
test/Packs.test.ts Outdated Show resolved Hide resolved
test/rules/EC2.test.ts Outdated Show resolved Hide resolved
test/rules/EC2.test.ts Outdated Show resolved Hide resolved
@mrpackethead
Copy link
Author

Tests for EC2IMDv2 are not yet completed.

@mrpackethead
Copy link
Author

@dontirun, I think this is ready to go now.

Copy link
Collaborator

@dontirun dontirun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Focused primarily on the EC2IMDSv2 rule. Will review other rules later

src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
Comment on lines 108 to 138
function hasHttpTokens(node: CfnLaunchTemplate): boolean {
const launchTemplateData: CfnLaunchTemplate.LaunchTemplateDataProperty =
Stack.of(node).resolve(node.launchTemplateData);
const meta = Stack.of(node).resolve(
launchTemplateData.metadataOptions
) as CfnLaunchTemplate.MetadataOptionsProperty;

if (meta == undefined) {
return false;
}

if (meta.httpTokens === 'required') {
return true;
}
return false;
}

function launchConfigurationhasTokens(node: CfnLaunchConfiguration): boolean {
if (node.metadataOptions != undefined) {
const meta: CfnLaunchTemplate.MetadataOptionsProperty = Stack.of(
node
).resolve(
node.metadataOptions
) as CfnLaunchTemplate.MetadataOptionsProperty;

if (meta.httpTokens === 'required') {
return true;
}
}
return false;
}
Copy link
Collaborator

@dontirun dontirun Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified

Suggested change
function hasHttpTokens(node: CfnLaunchTemplate): boolean {
const launchTemplateData: CfnLaunchTemplate.LaunchTemplateDataProperty =
Stack.of(node).resolve(node.launchTemplateData);
const meta = Stack.of(node).resolve(
launchTemplateData.metadataOptions
) as CfnLaunchTemplate.MetadataOptionsProperty;
if (meta == undefined) {
return false;
}
if (meta.httpTokens === 'required') {
return true;
}
return false;
}
function launchConfigurationhasTokens(node: CfnLaunchConfiguration): boolean {
if (node.metadataOptions != undefined) {
const meta: CfnLaunchTemplate.MetadataOptionsProperty = Stack.of(
node
).resolve(
node.metadataOptions
) as CfnLaunchTemplate.MetadataOptionsProperty;
if (meta.httpTokens === 'required') {
return true;
}
}
return false;
}
function hasHttpTokens(node: CfnLaunchTemplate | CfnLaunchConfiguration): boolean {
const metaLocation = node instanceof CfnLaunchTemplate ? Stack.of(node).resolve(node.launchTemplateData) : node;
const meta = Stack.of(node).resolve(metaLocation.metadataOptions)
return Stack.of(node).resolve(meta?.httpTokens) === 'required'
}

for (const child of Stack.of(node).node.findAll()) {
if (child instanceof CfnLaunchConfiguration) {
if (
child.launchConfigurationName === nodeLaunchConfigurationName &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't accounting for the case where the user provides a Ref for the the Launch Configuration Name, the name is also not a required field on a launch configuration.

I propose the following

function isMatchingLaunchConfiguration(
  node: CfnLaunchConfiguration,
  launchConfigurationName: string,
): boolean {
  const resolvedTemplateName = NagRules.resolveResourceFromInstrinsic(node, launchConfigurationName)
  return resolvedTemplateName === node.launchConfigurationName || resolvedTemplateName === resolvedTemplateName
}

src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/rules/ec2/SampleEC2IMDSv2.txt Outdated Show resolved Hide resolved
@@ -161,34 +161,6 @@ jobs:
run: cd .repo && npx projen package:python
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run npx projen locally (don't need to run the entire build process) and commit the change so that these build files return to normal

src/rules/ec2/EC2IMDSv2.ts Outdated Show resolved Hide resolved
@chindaws
Copy link

Andrew / Arun - Is there an ETA on when this PR may be finalised? I have a couple of organizations interested in using this feature.

@dontirun
Copy link
Collaborator

@chindaws There is no ETA. This is a community led contribution by @mrpackethead.

I'll give @mrpackethead a chance to respond, but if this is something you would like and @mrpackethead is no longer working on this then I would say that this PR is available for someone else to pick up and continue working on

@mrpackethead
Copy link
Author

Andrew / Arun - Is there an ETA on when this PR may be finalised? I have a couple of organizations interested in using this feature.

@chindaws , get in touch with me if theres a reason to progress this. I could be convinced to do it if there was a need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Include a Nag Pack for NZISM 3.6
3 participants