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(kuma-cp): implement MeshTrafficPermisson for ExternalServices with ZoneEgress #7061

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pkg/plugins/policies/core/xds/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Listeners struct {
Inbound map[core_rules.InboundListener]*envoy_listener.Listener
Outbound map[mesh_proto.OutboundInterface]*envoy_listener.Listener
Egress map[core_rules.InboundListener]*envoy_listener.Listener
Egress *envoy_listener.Listener
Gateway map[core_rules.InboundListener]*envoy_listener.Listener
Ipv4Passthrough *envoy_listener.Listener
Ipv6Passthrough *envoy_listener.Listener
Expand Down Expand Up @@ -46,10 +46,7 @@ func GatherListeners(rs *xds.ResourceSet) Listeners {
Port: address.GetPortValue(),
}] = listener
case egress_generator.OriginEgress:
listeners.Inbound[core_rules.InboundListener{
Address: address.GetAddress(),
Port: address.GetPortValue(),
}] = listener
listeners.Egress = listener
case generator.OriginTransparent:
switch listener.Name {
case generator.OutboundNameIPv4:
Expand All @@ -71,6 +68,5 @@ func GatherListeners(rs *xds.ResourceSet) Listeners {
continue
}
}

return listeners
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
core_xds "github.com/kumahq/kuma/pkg/core/xds"
"github.com/kumahq/kuma/pkg/plugins/policies/core/matchers"
core_rules "github.com/kumahq/kuma/pkg/plugins/policies/core/rules"
policies_xds "github.com/kumahq/kuma/pkg/plugins/policies/core/xds"
api "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/api/v1alpha1"
v3 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/xds"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
"github.com/kumahq/kuma/pkg/xds/envoy/names"
"github.com/kumahq/kuma/pkg/xds/generator"
)

var (
_ core_plugins.PolicyPlugin = &plugin{}
log = core.Log.WithName("MeshTrafficPermission")
_ core_plugins.EgressPolicyPlugin = &plugin{}
log = core.Log.WithName("MeshTrafficPermission")
)

type plugin struct{}
Expand All @@ -31,19 +33,16 @@ func (p plugin) MatchedPolicies(dataplane *core_mesh.DataplaneResource, resource
return matchers.MatchedPolicies(api.MeshTrafficPermissionType, dataplane, resources)
}

func (p plugin) EgressMatchedPolicies(es *core_mesh.ExternalServiceResource, resources xds_context.Resources) (core_xds.TypedMatchingPolicies, error) {
return matchers.EgressMatchedPolicies(api.MeshTrafficPermissionType, es, resources)
}

func (p plugin) Apply(rs *core_xds.ResourceSet, ctx xds_context.Context, proxy *core_xds.Proxy) error {
if proxy.Dataplane == nil {
// MeshTrafficPermission policy is applied only on DPP
// todo(lobkovilya): add support for ExternalService and ZoneEgress, https://github.com/kumahq/kuma/issues/5050
return nil
if proxy.ZoneEgressProxy != nil {
return p.configureEgress(rs, proxy)
}

if proxy.Dataplane.Spec.IsBuiltinGateway() {
return nil
}

mtp, ok := proxy.Policies.Dynamic[api.MeshTrafficPermissionType]
if !ok {
if proxy.Dataplane == nil || proxy.Dataplane.Spec.IsBuiltinGateway() {
return nil
}

Expand All @@ -54,6 +53,11 @@ func (p plugin) Apply(rs *core_xds.ResourceSet, ctx xds_context.Context, proxy *
return nil
}

mtp, ok := proxy.Policies.Dynamic[api.MeshTrafficPermissionType]
if !ok {
return nil
}

for _, res := range rs.Resources(envoy_resource.ListenerType) {
if res.Origin != generator.OriginInbound {
continue
Expand Down Expand Up @@ -84,3 +88,49 @@ func (p plugin) Apply(rs *core_xds.ResourceSet, ctx xds_context.Context, proxy *
}
return nil
}

func (p plugin) configureEgress(rs *core_xds.ResourceSet, proxy *core_xds.Proxy) error {
listeners := policies_xds.GatherListeners(rs)
for _, resource := range proxy.ZoneEgressProxy.MeshResourcesList {
if !resource.Mesh.MTLSEnabled() {
log.V(1).Info("skip applying MeshTrafficPermission, MTLS is disabled",
"mesh", resource.Mesh.GetMeta().GetName())
continue
}
for _, es := range resource.ExternalServices {
meshName := resource.Mesh.GetMeta().GetName()
esName := es.Meta.GetName()
policies, ok := resource.Dynamic[esName]
if !ok {
continue
}
mtp, ok := policies[api.MeshTrafficPermissionType]
if !ok {
continue
}
if listeners.Egress == nil {
log.V(1).Info("skip applying MeshTrafficPermission, Egress has no listener",
"proxyName", proxy.ZoneEgressProxy.ZoneEgressResource.GetMeta().GetName(),
"mesh", resource.Mesh.GetMeta().GetName(),
)
return nil
}

for _, rule := range mtp.FromRules.Rules {
configurer := &v3.RBACConfigurer{
StatsName: listeners.Egress.Name,
Rules: rule,
Mesh: resource.Mesh.GetMeta().GetName(),
}
for _, filterChain := range listeners.Egress.FilterChains {
if filterChain.Name == names.GetEgressFilterChainName(esName, meshName) {
if err := configurer.Configure(filterChain); err != nil {
return err
}
}
}
}
}
}
return nil
}
Loading