From b64d9a538bae5a5870f9e59c4feeb025ab70b3c4 Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Wed, 9 Dec 2020 12:19:36 -0500 Subject: [PATCH] ref(pkg/catalog): update ListAllowedOutboundServicesForIdentity (#2173) * handles permissive mode Signed-off-by: Michelle Noorali --- pkg/catalog/helpers_test.go | 8 +++++++- pkg/catalog/routes.go | 14 +++++++++++--- pkg/catalog/routes_test.go | 17 ++++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pkg/catalog/helpers_test.go b/pkg/catalog/helpers_test.go index 25ae484ea9..66e50642c5 100644 --- a/pkg/catalog/helpers_test.go +++ b/pkg/catalog/helpers_test.go @@ -24,7 +24,11 @@ import ( "github.com/openservicemesh/osm/pkg/tests" ) -func newFakeMeshCatalogForRoutes(t *testing.T) *MeshCatalog { +type testParams struct { + permissiveMode bool +} + +func newFakeMeshCatalogForRoutes(t *testing.T, testParams testParams) *MeshCatalog { mockCtrl := gomock.NewController(t) kubeClient := testclient.NewSimpleClientset() @@ -125,6 +129,8 @@ func newFakeMeshCatalogForRoutes(t *testing.T) *MeshCatalog { mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookbuyerService.Namespace).Return(true).AnyTimes() mockKubeController.EXPECT().ListMonitoredNamespaces().Return(listExpectedNs, nil).AnyTimes() + mockConfigurator.EXPECT().IsPermissiveTrafficPolicyMode().Return(testParams.permissiveMode).AnyTimes() + mockMeshSpec.EXPECT().GetAnnouncementsChannel().Return(announcementsChan).AnyTimes() mockMeshSpec.EXPECT().ListTrafficTargets().Return([]*target.TrafficTarget{&tests.TrafficTarget}).AnyTimes() mockMeshSpec.EXPECT().ListHTTPTrafficSpecs().Return([]*specs.HTTPRouteGroup{&tests.HTTPRouteGroup}).AnyTimes() diff --git a/pkg/catalog/routes.go b/pkg/catalog/routes.go index d0b5f35f91..ee46260a13 100644 --- a/pkg/catalog/routes.go +++ b/pkg/catalog/routes.go @@ -104,6 +104,15 @@ func (mc *MeshCatalog) ListAllowedOutboundServices(sourceService service.MeshSer // ListAllowedOutboundServicesForIdentity list the services the given service account is allowed to initiate outbound connections to func (mc *MeshCatalog) ListAllowedOutboundServicesForIdentity(identity service.K8sServiceAccount) []service.MeshService { + allowedServices := []service.MeshService{} + + if mc.configurator.IsPermissiveTrafficPolicyMode() { + for _, svc := range mc.kubeController.ListServices() { + allowedServices = append(allowedServices, utils.K8sSvcToMeshSvc(svc)) + } + return allowedServices + } + serviceSet := mapset.NewSet() for _, t := range mc.meshSpec.ListTrafficTargets() { // loop through all traffic targets for _, source := range t.Spec.Sources { @@ -124,11 +133,10 @@ func (mc *MeshCatalog) ListAllowedOutboundServicesForIdentity(identity service.K } } - serviceSlice := []service.MeshService{} for elem := range serviceSet.Iter() { - serviceSlice = append(serviceSlice, elem.(service.MeshService)) + allowedServices = append(allowedServices, elem.(service.MeshService)) } - return serviceSlice + return allowedServices } //GetWeightedClusterForService returns the weighted cluster for a given service diff --git a/pkg/catalog/routes_test.go b/pkg/catalog/routes_test.go index 6cd6a48504..e6c77e07e4 100644 --- a/pkg/catalog/routes_test.go +++ b/pkg/catalog/routes_test.go @@ -69,7 +69,7 @@ func TestIsValidTrafficTarget(t *testing.T) { func TestGetHostnamesForUpstreamService(t *testing.T) { assert := assert.New(t) - mc := newFakeMeshCatalogForRoutes(t) + mc := newFakeMeshCatalogForRoutes(t, testParams{}) testCases := []struct { name string @@ -391,17 +391,18 @@ func TestListAllowedOutboundServices(t *testing.T) { func TestListAllowedOutboundServicesForIdentity(t *testing.T) { assert := assert.New(t) - mc := newFakeMeshCatalog() testCases := []struct { name string serviceAccount service.K8sServiceAccount expectedList []service.MeshService + permissiveMode bool }{ { name: "traffic targets configured for service account", serviceAccount: tests.BookbuyerServiceAccount, expectedList: []service.MeshService{tests.BookstoreV1Service, tests.BookstoreV2Service, tests.BookstoreApexService}, + permissiveMode: false, }, { name: "traffic targets not configured for service account", @@ -409,12 +410,22 @@ func TestListAllowedOutboundServicesForIdentity(t *testing.T) { Name: "some-name", Namespace: "some-ns", }, - expectedList: nil, + expectedList: nil, + permissiveMode: false, + }, + { + name: "permissive mode enabled", + serviceAccount: tests.BookstoreServiceAccount, + expectedList: []service.MeshService{tests.BookstoreV1Service, tests.BookstoreV2Service, tests.BookstoreApexService, tests.BookbuyerService}, + permissiveMode: true, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + mc := newFakeMeshCatalogForRoutes(t, testParams{ + permissiveMode: tc.permissiveMode, + }) actualList := mc.ListAllowedOutboundServicesForIdentity(tc.serviceAccount) assert.ElementsMatch(actualList, tc.expectedList) })