-
Notifications
You must be signed in to change notification settings - Fork 155
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
Warn on incompatible aws_security_group use #3788
Comments
Per @EronWright doing a stateful observation of a series of Check calls can help the provider detect this pattern and emit a warning so that the warning is more obvious than just a note in documentation somewhere. This should work out. |
This PR adds the resource URN to the context in the `Check` method, along with methods to set and get the URN from context. The motivation for this change was driven by trying to implement the suggested fix in [this comment](pulumi/pulumi-aws#3788 (comment)) in the aws provider. We want to be able to keep some global state of which resources have been seen by `Check`, but realized that we don't actually get any identifying information in the `PreCheckCallback` function. An alternative approach would be to update the `PreCheckCallback` signature to also contain the URN, but that would be a breaking change. re pulumi/pulumi-aws#3788
Here's that "stateful observation" conversation, for reference. |
This PR adds the resource URN to the context in the `Check` method, along with methods to set and get the URN from context. The motivation for this change was driven by trying to implement the suggested fix in [this comment](pulumi/pulumi-aws#3788 (comment)) in the aws provider. We want to be able to keep some global state of which resources have been seen by `Check`, but realized that we don't actually get any identifying information in the `PreCheckCallback` function. An alternative approach would be to update the `PreCheckCallback` signature to also contain the URN, but that would be a breaking change. re pulumi/pulumi-aws#3788 --------- Co-authored-by: Ian Wahbe <ian@wahbe.com>
After looking into this a little bit more, it doesn't look like we have a good way to solve this. I originally thought we may be able to solve it by using the name: sg_inline_rule_warning
runtime: yaml
resources:
test_sg:
type: aws:ec2:SecurityGroup
properties:
egress:
- fromPort: 0
toPort: 0
protocol: '-1'
cidrBlocks:
- 0.0.0.0/0
test_egress_rule:
type: aws:vpc:SecurityGroupEgressRule
properties:
securityGroupId: ${test_sg.id}
ipProtocol: '-1'
cidrIpv4: 0.0.0.0/0 In order to warn the user we would need to be able to resolve the Another option with An alternative approach is to use policy packs. This would allow us to know about the relationships between resources, but we would have the same limitation of not being able to apply the rule until an new PolicyPack("aws-typescript", {
policies: [
{
name: "validate-sg-rules",
description:
"Security groups should use either inline rules or separate rules, but not both",
enforcementLevel: "advisory",
validateStack: (args, reportViolation) => {
const securityGroupsWithEgress = args.resources.reduce(
(prev, curr) => {
const sg = curr.asType(aws.ec2.SecurityGroup);
if (sg?.egress) {
prev[sg.id] = true;
}
return prev;
},
{} as { [id: string]: boolean },
);
const egressRules = args.resources.flatMap((r) => {
const resource = r.asType(aws.vpc.SecurityGroupEgressRule);
return resource ?? [];
});
for (const rule of egressRules) {
if (rule.securityGroupId in securityGroupsWithEgress) {
reportViolation(
`SecurityGruop ${rule.securityGroupId} is using both the 'egress' property and the 'SecurityGroupEgressRule' resource`,
);
}
}
},
},
],
}); Even though we won't show the warning until after the security group is created, I still think the warning would be worth it. Ideally we could find a way to embed policies into the provider as a way of emitting these warnings, but the only way I could find to apply these policies are via the pulumi cli. This means that users would need to install a separate library in order to get these warnings. |
Thank you for this valuable investigation @corymhall ! I am going to correlate a few comments from internal discussions here as well. Luke pointed out to https://github.com/pulumi/pulumi-policy-aws aka AWSGuard which currently exists as a "best-practices" checker for AWS and could potentially host these checks, though as you point out it implies downloading an additional dependency and limits the reach somewhat. Joe offered a historical note that the design of policy packs like the above one also suffers from the loss of information due to unknown values and generally aborts processing when it cannot extract the required information. As a likely consequence of this, implementing this check in AWSGuard would only emit the warning at |
This is unfortunate and indicates extra work needs to happen but is not necessarily impossible. The provider protocol allocates ID in response to Create calls, and the provider could be made to spy on its own Create calls and recall the ID. Additionally ID is passed to DiffRequest. There are however additional limitations of trying to use the provider protocol to infer warnings that make me lean toward your recommendation of using policy packs instead, for instance multiple provider processes may be involved in the same stack and this style of checking cannot span across resources allocated in multiple providers. |
The [docs](https://www.pulumi.com/registry/packages/aws/api-docs/ec2/securitygroup/) have strongly worded note about not using inline rules along with separate security group resources. This PR adds a new rule which will warn the user when they use both together. re pulumi/pulumi-aws#3788
The [docs](https://www.pulumi.com/registry/packages/aws/api-docs/ec2/securitygroup/) have strongly worded note about not using inline rules along with separate security group resources. This PR adds a new rule which will warn the user when they use both together. re pulumi/pulumi-aws#3788
The [docs](https://www.pulumi.com/registry/packages/aws/api-docs/ec2/securitygroup/) have strongly worded note about not using inline rules along with separate security group resources. This PR adds a new rule which will warn the user when they use both together. re pulumi/pulumi-aws#3788
Hello!
Issue details
Consider emitting a warning to help users spot and correct incompatible resource pairings such as using a vpc_security_group_egress_rule with an aws_security_group with in-line rules.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
Affected area/feature
The text was updated successfully, but these errors were encountered: