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

fix: Check standard logs in AwsSolutions-CFR3 #1877

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

georeeve
Copy link
Contributor

@georeeve georeeve commented Jan 14, 2025

Fixes #1876

Opening as a draft as the base implementation works, but I've ran into an issue checking if any CfnDeliverySource resources are defined in the entire stack. I'll explain in a code comment.

I've also had to update aws-cdk-lib to 2.167.0, which is the first version to include outputFormat in CfnDeliveryDestination. As I'm only using this in the tests, am I alright not updating it in the peerDependencies as well?

@georeeve
Copy link
Contributor Author

@dontirun Another question which may be out of scope of this PR, but because the logs templates here need to be deployed to us-east-1 and our main stack is in a different region, we are passing through the distribution's ARN using a StringParameter to avoid cross Stack export errors. Unfortunately when using this, it means that child.resourceArn in this PR resolves to SsmParameterValuedistributionarnC96584B6F00A464EAD1953AFF4B05118Parameter and not directly to the ARN value.

Do you know if there's a way to look up this in the Stack? I can see a resource with the ID of SsmParameterValue:distribution-arn:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter but obviously it's dropped the :-. characters at some point. We could loop through all of the CfnParameter's manually and store them in a hashmap, but I was wondering if you knew of a more efficient helper method to do it for us? Let me know if any of that doesn't make any sense and I'll try to clarify it some more!

@georeeve
Copy link
Contributor Author

georeeve commented Jan 14, 2025

I've just pushed a commit to resolve SSM parameters as well by traversing the stack again. If it's out of scope for this PR we can just revert the commit, but I think others will also be using parameters to try and avoid any cross-stack reference errors as I detailed above, so it could be useful.

@georeeve
Copy link
Contributor Author

georeeve commented Feb 6, 2025

@dontirun Have you had time to take an initial look into this? I hope I haven't confused you with too many questions! I'm hoping that the initial issue explains what I'm trying to do, and we can always descope it if we think it's too much.

@dontirun
Copy link
Collaborator

Sorry about the delay. I'll try to get a review in this week

@dontirun
Copy link
Collaborator

@dontirun Another question which may be out of scope of this PR, but because the logs templates here need to be deployed to us-east-1 and our main stack is in a different region, we are passing through the distribution's ARN using a StringParameter to avoid cross Stack export errors. Unfortunately when using this, it means that child.resourceArn in this PR resolves to SsmParameterValuedistributionarnC96584B6F00A464EAD1953AFF4B05118Parameter and not directly to the ARN value.

Do you know if there's a way to look up this in the Stack? I can see a resource with the ID of SsmParameterValue:distribution-arn:C96584B6-F00A-464E-AD19-53AFF4B05118.Parameter but obviously it's dropped the :-. characters at some point. We could loop through all of the CfnParameter's manually and store them in a hashmap, but I was wondering if you knew of a more efficient helper method to do it for us? Let me know if any of that doesn't make any sense and I'll try to clarify it some more!

I don't think there is a good way to this. In this particular case cdk-nag would have to determine whether something is a CfnParameter then search through a Map, then search for a Stack that might exist in the application, and then traverse that stack. I think this is a good use case for using the suppression system and documenting how this was addressed

@georeeve
Copy link
Contributor Author

I don't think there is a good way to this. In this particular case cdk-nag would have to determine whether something is a CfnParameter then search through a Map, then search for a Stack that might exist in the application, and then traverse that stack. I think this is a good use case for using the suppression system and documenting how this was addressed

Thanks, I thought it might be out of scope for this PR. I'll remove it, but do you think it'll be useful to implement in a separate PR later down the line? I feel like a few rules could benefit from tracking parameters and/or stack exports and we may be able to seamlessly integrate it into the traversal flow.

@georeeve georeeve marked this pull request as ready for review February 13, 2025 09:49
@georeeve georeeve requested a review from dontirun February 13, 2025 09:49
@dontirun
Copy link
Collaborator

I don't think there is a good way to this. In this particular case cdk-nag would have to determine whether something is a CfnParameter then search through a Map, then search for a Stack that might exist in the application, and then traverse that stack. I think this is a good use case for using the suppression system and documenting how this was addressed

Thanks, I thought it might be out of scope for this PR. I'll remove it, but do you think it'll be useful to implement in a separate PR later down the line? I feel like a few rules could benefit from tracking parameters and/or stack exports and we may be able to seamlessly integrate it into the traversal flow.

Yes, it's definitely something worth looking into

@georeeve
Copy link
Contributor Author

I've updated aws-cdk-lib in the devDependencies so that I can use outputFormat in CfnDeliveryDestination in the tests, but when I run npx projen build, it downgrades the package again. Is it because it needs to stay in line with peerDependencies even if we only use it in the tests?

@dontirun
Copy link
Collaborator

dontirun commented Feb 17, 2025

I've updated aws-cdk-lib in the devDependencies so that I can use outputFormat in CfnDeliveryDestination in the tests, but when I run npx projen build, it downgrades the package again. Is it because it needs to stay in line with peerDependencies even if we only use it in the tests?

You'll need to update the version in the .projen.js file

The .projen.js file controls all dependencies and project configurations. The package.json is generated from what is specified there.

@georeeve
Copy link
Contributor Author

Ah I see, does that look good to you now?

Comment on lines +37 to +50
let deliverySourceName;
for (const child of Stack.of(node).node.findAll()) {
if (child instanceof CfnDeliverySource) {
const deliverySourceArn = flattenCfnReference(
Stack.of(child).resolve(child.resourceArn)
);
const logType = Stack.of(child).resolve(child.logType);
if (
deliverySourceArn === distributionArn &&
logType === 'ACCESS_LOGS'
) {
deliverySourceName = Stack.of(child).resolve(child.name);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't take into account if the stack has multiple delivery sources defined that reference the distribution. If multiple are found then it will always pick the last one

Comment on lines +53 to +62
for (const child of Stack.of(node).node.findAll()) {
if (child instanceof CfnDelivery) {
if (
deliverySourceName ===
Stack.of(child).resolve(child.deliverySourceName)
) {
return NagRuleCompliance.COMPLIANT;
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be a nested for loop. We don't need this to execute if a matching delivery source has not been found

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.

bug: AwsSolutions-CFR3 does not support standard logs V2
2 participants