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

Do not realize AntreaNetworkPolicy applied to Pods in other Namespace #4119

Merged
merged 1 commit into from
Aug 17, 2022

Conversation

tnqn
Copy link
Member

@tnqn tnqn commented Aug 16, 2022

When AntreaNetworkPolicy uses Group as AppliedTo, the Group should not
select Pods in other Namespaces, otherwise the policy would be applied
to other Namespaces. This was prevented by using a validation when
creating AppliedToGroup for Group, which ensures that the Group doesn't
have a NamespaceSelector. However, the validation could be bypassed by
several approaches; the most straightforward one is to use a parent
Group as AppliedTo and make one of its child Groups use
NamespaceSelector.

It's hard to cover all cases if the validation is only in the phase of
creating AppliedToGroup because of its dynamic nature. This patch
implements a validation when syncing AppliedToGroup. The validation
ensures that the AppliedToGroup cannot have any members in other
Namespaces if it's derived from a namespaced Group, regardless of the
way by which the members are selected. The error encountered when
syncing AppliedToGroup will be reflected in the statuses of the
NetworkPolicies that use this AppliedToGroup.

This patch also unifies the behavior of ClusterNetworkPolicy and
AntreaNetworkPolicy when a ClusterGroup/Group used as AppliedTo contains
IPBlocks only: it would be treated like empty AppliedTo in both cases.

Fixes #4116

Signed-off-by: Quan Tian qtian@vmware.com

@tnqn tnqn added this to the Antrea v1.8 release milestone Aug 16, 2022
@tnqn
Copy link
Member Author

tnqn commented Aug 16, 2022

/test-all

@codecov
Copy link

codecov bot commented Aug 16, 2022

Codecov Report

Merging #4119 (dd06dcc) into main (d1c6a43) will decrease coverage by 2.48%.
The diff coverage is 81.45%.

❗ Current head dd06dcc differs from pull request most recent head 0ff0557. Consider uploading reports for the commit 0ff0557 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4119      +/-   ##
==========================================
- Coverage   65.70%   63.22%   -2.49%     
==========================================
  Files         304      304              
  Lines       46604    46580      -24     
==========================================
- Hits        30621    29450    -1171     
- Misses      13557    14777    +1220     
+ Partials     2426     2353      -73     
Flag Coverage Δ *Carryforward flag
e2e-tests 39.32% <39.51%> (?)
integration-tests 34.74% <ø> (-0.21%) ⬇️ Carriedforward from b97b7b1
kind-e2e-tests 41.35% <43.54%> (-7.99%) ⬇️ Carriedforward from b97b7b1
unit-tests 44.66% <82.78%> (+0.29%) ⬆️ Carriedforward from b97b7b1

*This pull request uses carry forward flags. Click here to find out more.

Impacted Files Coverage Δ
pkg/controller/types/group.go 52.38% <0.00%> (-2.62%) ⬇️
pkg/controller/types/networkpolicy.go 100.00% <ø> (ø)
pkg/controller/networkpolicy/status_controller.go 68.56% <57.14%> (+3.45%) ⬆️
...ntroller/networkpolicy/networkpolicy_controller.go 82.97% <81.25%> (+0.39%) ⬆️
...kg/controller/networkpolicy/antreanetworkpolicy.go 90.90% <100.00%> (+16.36%) ⬆️
...g/controller/networkpolicy/clusternetworkpolicy.go 74.14% <100.00%> (-0.97%) ⬇️
pkg/controller/networkpolicy/crd_utils.go 92.78% <100.00%> (+3.06%) ⬆️
pkg/agent/proxy/endpointslicecache.go 0.00% <0.00%> (-83.60%) ⬇️
pkg/agent/secondarynetwork/cnipodcache/cache.go 0.00% <0.00%> (-77.56%) ⬇️
pkg/controller/egress/store/egressgroup.go 1.72% <0.00%> (-54.32%) ⬇️
... and 66 more

@tnqn tnqn force-pushed the fix-nested-namespace-group branch from b97b7b1 to 891b95b Compare August 16, 2022 11:54
@tnqn
Copy link
Member Author

tnqn commented Aug 16, 2022

/test-all

antoninbas
antoninbas previously approved these changes Aug 16, 2022
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

someone else should review as well, but LGTM

intGrp := ig.(*antreatypes.Group)
if len(intGrp.IPBlocks) > 0 {
klog.V(2).InfoS("Group with IPBlocks can not be used as AppliedTo", "Group", key)
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

could you clarify why this is not an error case, maybe with a small comment?
is that not a case that needs to be reported to the user through the status?

Copy link
Member Author

Choose a reason for hiding this comment

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

ClusterNetworkPolicy handles this case in this way. The patch unifies the behavior of ClusterNetworkPolicy and AntreaNetworkPolicy. I don't have strong preference about which action it should take, but I think the following reason may justify ignoring IPBlocks, and I have added it to code comment:

A Group may have child Groups, some of which contain regular Pod selectors and some of which contain IPBlocks.
When the Group is used as AppliedTo, it seems obvious that we should just apply NetworkPolicy to the selected
Pods and ignore the IPBlocks, instead of reporting errors and asking users to remove IPBlocks from child Groups,
as the Group could also be used as AddressGroup.
To keep the behavior consistent regarding IPBlocks, we ignore Groups containing only IPBlocks when it's used as AppliedTo.

Comment on lines 1294 to 1295
klog.V(2).Infof("Updating existing AppliedToGroup %s with %d Pods and %d External Entities on %d Nodes",
key, scheduledPodNum, scheduledExtEntityNum, appGroupNodeNames.Len())
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe grab this opportunity to change this to structured logging?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

},
},
{
name: "anp with invalid group",
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a more verbose name would be useful to understand why the group is invalid in this case: "anp with invalid group selecting pods in multiple Namespaces"

same for the test case below

Copy link
Member Author

Choose a reason for hiding this comment

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

updated, thanks

jianjuns
jianjuns previously approved these changes Aug 16, 2022
Copy link
Contributor

@jianjuns jianjuns left a comment

Choose a reason for hiding this comment

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

LGTM

}
return &antreatypes.AppliedToGroup{UID: intGrp.UID, Name: key}, nil
func (e *ErrNetworkPolicyAppliedToUnsupportedGroup) Error() string {
return fmt.Sprintf("group %s/%s with Pods in other Namespaces can not be used as AppliedTo", e.namespace, e.groupName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should "group" be "Group"?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Dyanngg
Dyanngg previously approved these changes Aug 16, 2022
group, found, _ := n.internalGroupStore.Get(g.Name)
if found {
// This AppliedToGroup is derived from a ClusterGroup.
grp := group.(*antreatypes.Group)
return n.getInternalGroupWorkloads(grp)
pods, ees := n.getInternalGroupWorkloads(grp)
Copy link
Contributor

Choose a reason for hiding this comment

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

We could potentially add an optional namespace check in GetEntities used by getInternalGroupWorkloads, so that we can return early and don't need to loop thru all entities again after getting a list of them. Doesn't need to be address in this PR; a TODO will suffice

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I moved the namespace check into getInternalGroupWorkloads. I prefer not to add custom checks to GetEntities, which is a generic interface while checking namespace is a too business specific condition. There are other similar business specific checking/filtering and currently all of them are performed on caller side.

When AntreaNetworkPolicy uses Group as AppliedTo, the Group should not
select Pods in other Namespaces, otherwise the policy would be applied
to other Namespaces. This was prevented by using a validation when
creating AppliedToGroup for Group, which ensures that the Group doesn't
have a NamespaceSelector. However, the validation could be bypassed by
several approaches, the most straightforward one is to use a parent
Group as AppliedTo and make one of its child Groups use
NamespaceSelector.

It's hard to cover all cases if the validation is only in the phase of
creating AppliedToGroup because of its dynamic nature. This patch
implements a validation when syncing AppliedToGroup. The validation
ensures that the AppliedToGroup cannot have any members in other
Namespaces if it's derived from a namespaced Group, regardless of the
way by which the members are selected. The error encountered when
syncing AppliedToGroup will be reflected in the statuses of the
NetworkPolicies that use this AppliedToGroup.

This patch also unifies the behavior of ClusterNetworkPolicy and
AntreaNetworkPolicy when a ClusterGroup/Group used as AppliedTo contains
IPBlocks only: it would be treated like empty AppliedTo in both cases.

Fixes antrea-io#4116

Signed-off-by: Quan Tian <qtian@vmware.com>
@tnqn tnqn dismissed stale reviews from Dyanngg, jianjuns, and antoninbas via 0ff0557 August 17, 2022 03:46
@tnqn tnqn force-pushed the fix-nested-namespace-group branch from 891b95b to 0ff0557 Compare August 17, 2022 03:46
Copy link
Contributor

@GraysonWu GraysonWu left a comment

Choose a reason for hiding this comment

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

LGTM

@tnqn
Copy link
Member Author

tnqn commented Aug 17, 2022

/test-all

@tnqn tnqn merged commit 18aee9d into antrea-io:main Aug 17, 2022
@tnqn tnqn deleted the fix-nested-namespace-group branch August 17, 2022 09:41
heanlan pushed a commit to heanlan/antrea that referenced this pull request Mar 29, 2023
…antrea-io#4119)

When AntreaNetworkPolicy uses Group as AppliedTo, the Group should not
select Pods in other Namespaces, otherwise the policy would be applied
to other Namespaces. This was prevented by using a validation when
creating AppliedToGroup for Group, which ensures that the Group doesn't
have a NamespaceSelector. However, the validation could be bypassed by
several approaches, the most straightforward one is to use a parent
Group as AppliedTo and make one of its child Groups use
NamespaceSelector.

It's hard to cover all cases if the validation is only in the phase of
creating AppliedToGroup because of its dynamic nature. This patch
implements a validation when syncing AppliedToGroup. The validation
ensures that the AppliedToGroup cannot have any members in other
Namespaces if it's derived from a namespaced Group, regardless of the
way by which the members are selected. The error encountered when
syncing AppliedToGroup will be reflected in the statuses of the
NetworkPolicies that use this AppliedToGroup.

This patch also unifies the behavior of ClusterNetworkPolicy and
AntreaNetworkPolicy when a ClusterGroup/Group used as AppliedTo contains
IPBlocks only: it would be treated like empty AppliedTo in both cases.

Fixes antrea-io#4116

Signed-off-by: Quan Tian <qtian@vmware.com>

Signed-off-by: Quan Tian <qtian@vmware.com>
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.

AntreaNetworkPolicy should not be applied to Pods in other Namespaces
5 participants