From 87469b7578a74e55a933274bc9647ca013584d77 Mon Sep 17 00:00:00 2001 From: tharindu1st Date: Wed, 15 Nov 2023 09:02:37 +0530 Subject: [PATCH] remove unused code --- adapter/internal/adapter/adapter.go | 5 - adapter/internal/discovery/xds/marshaller.go | 181 ------------------ adapter/internal/discovery/xds/server.go | 125 ++---------- .../discovery/protocol/server/v3/server.go | 55 ------ .../internal/operator/constant/constant.go | 25 +-- .../controllers/cp/application_controller.go | 1 - .../internal/utils/event_utils.go | 54 +++--- 7 files changed, 53 insertions(+), 393 deletions(-) diff --git a/adapter/internal/adapter/adapter.go b/adapter/internal/adapter/adapter.go index 56621e1d8e..7330f08354 100644 --- a/adapter/internal/adapter/adapter.go +++ b/adapter/internal/adapter/adapter.go @@ -29,7 +29,6 @@ import ( "github.com/wso2/apk/adapter/internal/operator" apiservice "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/api" configservice "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/config" - keymanagerservice "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/keymgt" subscriptionservice "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/subscription" wso2_server "github.com/wso2/apk/adapter/pkg/discovery/protocol/server/v3" "github.com/wso2/apk/adapter/pkg/health" @@ -120,10 +119,6 @@ func runManagementServer(conf *config.Config, server xdsv3.Server, enforcerServe configservice.RegisterConfigDiscoveryServiceServer(grpcServer, enforcerServer) apiservice.RegisterApiDiscoveryServiceServer(grpcServer, enforcerServer) subscriptionservice.RegisterApiListDiscoveryServiceServer(grpcServer, enforcerAPIDsSrv) - subscriptionservice.RegisterApplicationPolicyDiscoveryServiceServer(grpcServer, enforcerAppPolicyDsSrv) - subscriptionservice.RegisterSubscriptionPolicyDiscoveryServiceServer(grpcServer, enforcerSubPolicyDsSrv) - keymanagerservice.RegisterKMDiscoveryServiceServer(grpcServer, enforcerKeyManagerDsSrv) - keymanagerservice.RegisterRevokedTokenDiscoveryServiceServer(grpcServer, enforcerRevokedTokenDsSrv) subscriptionservice.RegisterJWTIssuerDiscoveryServiceServer(grpcServer, enforcerJwtIssuerDsSrv) // register health service healthservice.RegisterHealthServer(grpcServer, &health.Server{}) diff --git a/adapter/internal/discovery/xds/marshaller.go b/adapter/internal/discovery/xds/marshaller.go index e60192fd87..9abff744fe 100644 --- a/adapter/internal/discovery/xds/marshaller.go +++ b/adapter/internal/discovery/xds/marshaller.go @@ -18,14 +18,11 @@ package xds import ( - "encoding/json" - "fmt" "strconv" "github.com/wso2/apk/adapter/config" logger "github.com/wso2/apk/adapter/internal/loggers" "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/config/enforcer" - "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/keymgt" "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/subscription" "github.com/wso2/apk/adapter/pkg/eventhub/types" ) @@ -33,16 +30,6 @@ import ( var ( // APIListMap has the following mapping label -> apiUUID -> API (Metadata) APIListMap map[string]map[string]*subscription.APIs - // SubscriptionMap contains the subscriptions recieved from API Manager Control Plane - SubscriptionMap map[int32]*subscription.Subscription - // ApplicationMap contains the applications recieved from API Manager Control Plane - ApplicationMap map[string]*subscription.Application - // ApplicationKeyMappingMap contains the application key mappings recieved from API Manager Control Plane - ApplicationKeyMappingMap map[string]*subscription.ApplicationKeyMapping - // ApplicationPolicyMap contains the application policies recieved from API Manager Control Plane - ApplicationPolicyMap map[int32]*subscription.ApplicationPolicy - // SubscriptionPolicyMap contains the subscription policies recieved from API Manager Control Plane - SubscriptionPolicyMap map[int32]*subscription.SubscriptionPolicy ) // EventType is a enum to distinguish Create, Update and Delete Events @@ -186,30 +173,6 @@ func marshalAnalyticsPublishers(config config.Config) []*enforcer.AnalyticsPubli return resolvedAnalyticsPublishers } -// marshalSubscriptionMapToList converts the data into SubscriptionList proto type -func marshalSubscriptionMapToList(subscriptionMap map[int32]*subscription.Subscription) *subscription.SubscriptionList { - subscriptions := []*subscription.Subscription{} - for _, sub := range subscriptionMap { - subscriptions = append(subscriptions, sub) - } - - return &subscription.SubscriptionList{ - List: subscriptions, - } -} - -// marshalApplicationMapToList converts the data into ApplicationList proto type -func marshalApplicationMapToList(appMap map[string]*subscription.Application) *subscription.ApplicationList { - applications := []*subscription.Application{} - for _, app := range appMap { - applications = append(applications, app) - } - - return &subscription.ApplicationList{ - List: applications, - } -} - // marshalAPIListMapToList converts the data into APIList proto type func marshalAPIListMapToList(apiMap map[string]*subscription.APIs) *subscription.APIList { apis := []*subscription.APIs{} @@ -222,125 +185,6 @@ func marshalAPIListMapToList(apiMap map[string]*subscription.APIs) *subscription } } -// marshalApplicationPolicyMapToList converts the data into ApplicationPolicyList proto type -func marshalApplicationPolicyMapToList(appPolicyMap map[int32]*subscription.ApplicationPolicy) *subscription.ApplicationPolicyList { - applicationPolicies := []*subscription.ApplicationPolicy{} - for _, policy := range appPolicyMap { - applicationPolicies = append(applicationPolicies, policy) - } - - return &subscription.ApplicationPolicyList{ - List: applicationPolicies, - } -} - -// marshalSubscriptionPolicyMapToList converts the data into SubscriptionPolicyList proto type -func marshalSubscriptionPolicyMapToList(subPolicyMap map[int32]*subscription.SubscriptionPolicy) *subscription.SubscriptionPolicyList { - subscriptionPolicies := []*subscription.SubscriptionPolicy{} - - for _, policy := range subPolicyMap { - subscriptionPolicies = append(subscriptionPolicies, policy) - } - - return &subscription.SubscriptionPolicyList{ - List: subscriptionPolicies, - } -} - -// marshalKeyMappingMapToList converts the data into ApplicationKeyMappingList proto type -func marshalKeyMappingMapToList(keyMappingMap map[string]*subscription.ApplicationKeyMapping) *subscription.ApplicationKeyMappingList { - applicationKeyMappings := []*subscription.ApplicationKeyMapping{} - - for _, keyMapping := range keyMappingMap { - // TODO: (VirajSalaka) tenant domain check missing - applicationKeyMappings = append(applicationKeyMappings, keyMapping) - } - - return &subscription.ApplicationKeyMappingList{ - List: applicationKeyMappings, - } -} - -// MarshalKeyManager converts the data into KeyManager proto type -func MarshalKeyManager(keyManager *types.KeyManager) *keymgt.KeyManagerConfig { - configList, err := json.Marshal(keyManager.Configuration) - configuration := string(configList) - if err == nil { - newKeyManager := &keymgt.KeyManagerConfig{ - Name: keyManager.Name, - Type: keyManager.Type, - Enabled: keyManager.Enabled, - TenantDomain: keyManager.TenantDomain, - Configuration: configuration, - } - return newKeyManager - } - logger.LoggerXds.Debugf("Error happens while marshaling key manager data for " + fmt.Sprint(keyManager.Name)) - return nil -} - -// MarshalMultipleApplicationPolicies is used to update the applicationPolicies during the startup where -// multiple application policies are pulled at once. And then it returns the ApplicationPolicyList. -func MarshalMultipleApplicationPolicies(policies *types.ApplicationPolicyList) *subscription.ApplicationPolicyList { - resourceMap := make(map[int32]*subscription.ApplicationPolicy) - for item := range policies.List { - policy := policies.List[item] - appPolicy := marshalApplicationPolicy(&policy) - resourceMap[policy.ID] = appPolicy - logger.LoggerXds.Infof("appPolicy Entry is added : %v", appPolicy) - } - ApplicationPolicyMap = resourceMap - return marshalApplicationPolicyMapToList(ApplicationPolicyMap) -} - -// MarshalApplicationPolicyEventAndReturnList handles the Application Policy Event corresponding to the event received -// from message broker. And then it returns the ApplicationPolicyList. -func MarshalApplicationPolicyEventAndReturnList(policy *types.ApplicationPolicy, eventType EventType) *subscription.ApplicationPolicyList { - if eventType == DeleteEvent { - delete(ApplicationPolicyMap, policy.ID) - logger.LoggerXds.Infof("Application Policy: %s is deleted.", policy.Name) - } else { - appPolicy := marshalApplicationPolicy(policy) - ApplicationPolicyMap[policy.ID] = appPolicy - if eventType == UpdateEvent { - logger.LoggerSvcDiscovery.Infof("Application Policy: %s is updated.", appPolicy.Name) - } else { - logger.LoggerSvcDiscovery.Infof("Application Policy: %s is added.", appPolicy.Name) - } - } - return marshalApplicationPolicyMapToList(ApplicationPolicyMap) -} - -// MarshalMultipleSubscriptionPolicies is used to update the subscriptionPolicies during the startup where -// multiple subscription policies are pulled at once. And then it returns the SubscriptionPolicyList. -func MarshalMultipleSubscriptionPolicies(policies *types.SubscriptionPolicyList) *subscription.SubscriptionPolicyList { - resourceMap := make(map[int32]*subscription.SubscriptionPolicy) - for item := range policies.List { - policy := policies.List[item] - resourceMap[policy.ID] = marshalSubscriptionPolicy(&policy) - } - SubscriptionPolicyMap = resourceMap - return marshalSubscriptionPolicyMapToList(SubscriptionPolicyMap) -} - -// MarshalSubscriptionPolicyEventAndReturnList handles the Subscription Policy Event corresponding to the event received -// from message broker. And then it returns the subscriptionPolicyList. -func MarshalSubscriptionPolicyEventAndReturnList(policy *types.SubscriptionPolicy, eventType EventType) *subscription.SubscriptionPolicyList { - if eventType == DeleteEvent { - delete(ApplicationPolicyMap, policy.ID) - logger.LoggerXds.Infof("Application Policy: %s is deleted.", policy.Name) - } else { - subPolicy := marshalSubscriptionPolicy(policy) - SubscriptionPolicyMap[policy.ID] = subPolicy - if eventType == UpdateEvent { - logger.LoggerSvcDiscovery.Infof("Subscription Policy: %s is updated.", subPolicy.Name) - } else { - logger.LoggerSvcDiscovery.Infof("Subscription Policy: %s is added.", subPolicy.Name) - } - } - return marshalSubscriptionPolicyMapToList(SubscriptionPolicyMap) -} - // MarshalAPIMetataAndReturnList updates the internal APIListMap and returns the XDS compatible APIList. // apiList is the internal APIList object (For single API, this would contain a List with just one API) // initialAPIUUIDListMap is assigned during startup when global adapter is associated. This would be empty otherwise. @@ -420,31 +264,6 @@ func marshalAPIMetadata(api *types.API) *subscription.APIs { } } -func marshalApplicationPolicy(policy *types.ApplicationPolicy) *subscription.ApplicationPolicy { - return &subscription.ApplicationPolicy{ - Id: policy.ID, - TenantId: policy.TenantID, - Name: policy.Name, - QuotaType: policy.QuotaType, - } -} - -func marshalSubscriptionPolicy(policy *types.SubscriptionPolicy) *subscription.SubscriptionPolicy { - return &subscription.SubscriptionPolicy{ - Id: policy.ID, - Name: policy.Name, - QuotaType: policy.QuotaType, - GraphQLMaxComplexity: policy.GraphQLMaxComplexity, - GraphQLMaxDepth: policy.GraphQLMaxDepth, - RateLimitCount: policy.RateLimitCount, - RateLimitTimeUnit: policy.RateLimitTimeUnit, - StopOnQuotaReach: policy.StopOnQuotaReach, - TenantId: policy.TenantID, - TenantDomain: policy.TenantDomain, - Timestamp: policy.TimeStamp, - } -} - // CheckIfAPIMetadataIsAlreadyAvailable returns true only if the API Metadata for the given API UUID // is already available func CheckIfAPIMetadataIsAlreadyAvailable(apiUUID, label string) bool { diff --git a/adapter/internal/discovery/xds/server.go b/adapter/internal/discovery/xds/server.go index faa0e09beb..0cc2d476fa 100644 --- a/adapter/internal/discovery/xds/server.go +++ b/adapter/internal/discovery/xds/server.go @@ -77,13 +77,9 @@ type EnvoyGatewayConfig struct { // EnforcerInternalAPI struct use to hold enforcer resources type EnforcerInternalAPI struct { - configs []types.Resource - keyManagers []types.Resource - apiList []types.Resource - applicationPolicies []types.Resource - subscriptionPolicies []types.Resource - revokedTokens []types.Resource - jwtIssuers []types.Resource + configs []types.Resource + apiList []types.Resource + jwtIssuers []types.Resource } var ( @@ -91,15 +87,15 @@ var ( mutexForXdsUpdate sync.Mutex mutexForInternalMapUpdate sync.Mutex - cache envoy_cachev3.SnapshotCache - enforcerCache wso2_cache.SnapshotCache - enforcerJwtIssuerCache wso2_cache.SnapshotCache - enforcerAPICache wso2_cache.SnapshotCache - enforcerApplicationPolicyCache wso2_cache.SnapshotCache - enforcerSubscriptionPolicyCache wso2_cache.SnapshotCache - enforcerKeyManagerCache wso2_cache.SnapshotCache - enforcerRevokedTokensCache wso2_cache.SnapshotCache - enforcerThrottleDataCache wso2_cache.SnapshotCache + cache envoy_cachev3.SnapshotCache + enforcerCache wso2_cache.SnapshotCache + enforcerJwtIssuerCache wso2_cache.SnapshotCache + enforcerAPICache wso2_cache.SnapshotCache + enforcerApplicationPolicyCache wso2_cache.SnapshotCache + enforcerSubscriptionPolicyCache wso2_cache.SnapshotCache + enforcerKeyManagerCache wso2_cache.SnapshotCache + enforcerRevokedTokensCache wso2_cache.SnapshotCache + enforcerThrottleDataCache wso2_cache.SnapshotCache orgAPIMap map[string]map[string]*EnvoyInternalAPI // organizationID -> Vhost:API_UUID -> EnvoyInternalAPI struct map @@ -121,8 +117,6 @@ var ( isReady = false ) -var void struct{} - const ( commonEnforcerLabel string = "commonEnforcerLabel" maxRandomInt int = 999999999 @@ -596,46 +590,6 @@ func UpdateEnforcerAPIList(label string, apis *subscription.APIList) { logger.LoggerXds.Infof("New API List cache update for the label: " + label + " version: " + fmt.Sprint(version)) } -// UpdateEnforcerApplicationPolicies sets new update to the enforcer's Application Policies -func UpdateEnforcerApplicationPolicies(applicationPolicies *subscription.ApplicationPolicyList) { - logger.LoggerXds.Debug("Updating Enforcer Application Policy Cache") - label := commonEnforcerLabel - applicationPolicyList := append(enforcerLabelMap[label].applicationPolicies, applicationPolicies) - - version, _ := crand.Int(crand.Reader, maxRandomBigInt()) - snap, _ := wso2_cache.NewSnapshot(fmt.Sprint(version), map[wso2_resource.Type][]types.Resource{ - wso2_resource.ApplicationPolicyListType: applicationPolicyList, - }) - snap.Consistent() - - errSetSnap := enforcerApplicationPolicyCache.SetSnapshot(context.Background(), label, snap) - if errSetSnap != nil { - logger.LoggerXds.ErrorC(logging.PrintError(logging.Error1414, logging.MAJOR, "Error while setting the snapshot : %v", errSetSnap.Error())) - } - enforcerLabelMap[label].subscriptionPolicies = applicationPolicyList - logger.LoggerXds.Infof("New Application Policy cache update for the label: " + label + " version: " + fmt.Sprint(version)) -} - -// UpdateEnforcerSubscriptionPolicies sets new update to the enforcer's Subscription Policies -func UpdateEnforcerSubscriptionPolicies(subscriptionPolicies *subscription.SubscriptionPolicyList) { - logger.LoggerXds.Debug("Updating Enforcer Subscription Policy Cache") - label := commonEnforcerLabel - subscriptionPolicyList := append(enforcerLabelMap[label].subscriptionPolicies, subscriptionPolicies) - - version, _ := crand.Int(crand.Reader, maxRandomBigInt()) - snap, _ := wso2_cache.NewSnapshot(fmt.Sprint(version), map[wso2_resource.Type][]types.Resource{ - wso2_resource.SubscriptionPolicyListType: subscriptionPolicyList, - }) - snap.Consistent() - - errSetSnap := enforcerSubscriptionPolicyCache.SetSnapshot(context.Background(), label, snap) - if errSetSnap != nil { - logger.LoggerXds.ErrorC(logging.PrintError(logging.Error1414, logging.MAJOR, "Error while setting the snapshot : %v", errSetSnap.Error())) - } - enforcerLabelMap[label].subscriptionPolicies = subscriptionPolicyList - logger.LoggerXds.Infof("New Subscription Policy cache update for the label: " + label + " version: " + fmt.Sprint(version)) -} - // UpdateXdsCacheWithLock uses mutex and lock to avoid different go routines updating XDS at the same time func UpdateXdsCacheWithLock(label string, endpoints []types.Resource, clusters []types.Resource, routes []types.Resource, listeners []types.Resource) bool { @@ -690,59 +644,6 @@ func ExtractUUIDFromAPIIdentifier(id string) (string, error) { return "", err } -// GenerateAndUpdateKeyManagerList converts the data into KeyManager proto type -func GenerateAndUpdateKeyManagerList() { - var keyManagerConfigList = make([]types.Resource, 0) - for item := range KeyManagerList { - keyManager := KeyManagerList[item] - kmConfig := MarshalKeyManager(&keyManager) - if kmConfig != nil { - keyManagerConfigList = append(keyManagerConfigList, kmConfig) - } - } - UpdateEnforcerKeyManagers(keyManagerConfigList) -} - -// UpdateEnforcerKeyManagers Sets new update to the enforcer's configuration -func UpdateEnforcerKeyManagers(keyManagerConfigList []types.Resource) { - logger.LoggerXds.Debug("Updating Key Manager Cache") - label := commonEnforcerLabel - - version, _ := crand.Int(crand.Reader, maxRandomBigInt()) - snap, _ := wso2_cache.NewSnapshot(fmt.Sprint(version), map[wso2_resource.Type][]types.Resource{ - wso2_resource.KeyManagerType: keyManagerConfigList, - }) - snap.Consistent() - - errSetSnap := enforcerKeyManagerCache.SetSnapshot(context.Background(), label, snap) - if errSetSnap != nil { - logger.LoggerXds.ErrorC(logging.PrintError(logging.Error1414, logging.MAJOR, "Error while setting the snapshot : %v", errSetSnap.Error())) - } - enforcerLabelMap[label].keyManagers = keyManagerConfigList - logger.LoggerXds.Infof("New key manager cache update for the label: " + label + " version: " + fmt.Sprint(version)) -} - -// UpdateEnforcerRevokedTokens method update the revoked tokens -// in the enforcer -func UpdateEnforcerRevokedTokens(revokedTokens []types.Resource) { - logger.LoggerXds.Debug("Updating enforcer cache for revoked tokens") - label := commonEnforcerLabel - tokens := append(enforcerLabelMap[label].revokedTokens, revokedTokens...) - - version, _ := crand.Int(crand.Reader, maxRandomBigInt()) - snap, _ := wso2_cache.NewSnapshot(fmt.Sprint(version), map[wso2_resource.Type][]types.Resource{ - wso2_resource.RevokedTokensType: revokedTokens, - }) - snap.Consistent() - - errSetSnap := enforcerRevokedTokensCache.SetSnapshot(context.Background(), label, snap) - if errSetSnap != nil { - logger.LoggerXds.ErrorC(logging.PrintError(logging.Error1414, logging.MAJOR, "Error while setting the snapshot : %v", errSetSnap.Error())) - } - enforcerLabelMap[label].revokedTokens = tokens - logger.LoggerXds.Infof("New Revoked token cache update for the label: " + label + " version: " + fmt.Sprint(version)) -} - // RemoveAPICacheForEnv will remove all the internal mappings for a specific environment func RemoveAPICacheForEnv(adapterInternalAPI model.AdapterInternalAPI, envType string) { vHostIdentifier := GetvHostsIdentifier(adapterInternalAPI.UUID, envType) @@ -757,7 +658,7 @@ func RemoveAPICacheForEnv(adapterInternalAPI model.AdapterInternalAPI, envType s } } } - } + } } // RemoveAPIFromOrgAPIMap removes api from orgAPI map diff --git a/adapter/pkg/discovery/protocol/server/v3/server.go b/adapter/pkg/discovery/protocol/server/v3/server.go index 23877b7b62..1332178d91 100644 --- a/adapter/pkg/discovery/protocol/server/v3/server.go +++ b/adapter/pkg/discovery/protocol/server/v3/server.go @@ -25,9 +25,7 @@ import ( streamv3 "github.com/envoyproxy/go-control-plane/pkg/server/stream/v3" xdsv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3" "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/api" - "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/apkmgt" "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/config" - "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/keymgt" "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/service/subscription" "github.com/wso2/apk/adapter/pkg/discovery/protocol/resource/v3" "github.com/wso2/apk/adapter/pkg/discovery/protocol/server/sotw/v3" @@ -39,18 +37,8 @@ import ( type Server interface { config.ConfigDiscoveryServiceServer api.ApiDiscoveryServiceServer - subscription.SubscriptionDiscoveryServiceServer - subscription.ApplicationDiscoveryServiceServer subscription.ApiListDiscoveryServiceServer - subscription.ApplicationPolicyDiscoveryServiceServer subscription.JWTIssuerDiscoveryServiceServer - subscription.SubscriptionPolicyDiscoveryServiceServer - subscription.ApplicationKeyMappingDiscoveryServiceServer - subscription.ApplicationMappingDiscoveryServiceServer - keymgt.KMDiscoveryServiceServer - keymgt.RevokedTokenDiscoveryServiceServer - apkmgt.APKMgtDiscoveryServiceServer - rest.Server envoy_sotw.Server envoy_delta.Server @@ -70,17 +58,8 @@ func NewServerAdvanced(restServer rest.Server, sotwServer envoy_sotw.Server, del type server struct { config.UnimplementedConfigDiscoveryServiceServer api.UnimplementedApiDiscoveryServiceServer - subscription.UnimplementedSubscriptionDiscoveryServiceServer - subscription.UnimplementedApplicationDiscoveryServiceServer subscription.UnimplementedJWTIssuerDiscoveryServiceServer subscription.UnimplementedApiListDiscoveryServiceServer - subscription.UnimplementedApplicationPolicyDiscoveryServiceServer - subscription.UnimplementedSubscriptionPolicyDiscoveryServiceServer - subscription.UnimplementedApplicationKeyMappingDiscoveryServiceServer - subscription.UnimplementedApplicationMappingDiscoveryServiceServer - keymgt.UnimplementedKMDiscoveryServiceServer - keymgt.UnimplementedRevokedTokenDiscoveryServiceServer - apkmgt.UnimplementedAPKMgtDiscoveryServiceServer rest rest.Server sotw envoy_sotw.Server delta envoy_delta.Server @@ -98,45 +77,11 @@ func (s *server) StreamApis(stream api.ApiDiscoveryService_StreamApisServer) err return s.StreamHandler(stream, resource.APIType) } -func (s *server) StreamSubscriptions(stream subscription.SubscriptionDiscoveryService_StreamSubscriptionsServer) error { - return s.StreamHandler(stream, resource.SubscriptionListType) -} func (s *server) StreamApiList(stream subscription.ApiListDiscoveryService_StreamApiListServer) error { return s.StreamHandler(stream, resource.APIListType) } -func (s *server) StreamApplications(stream subscription.ApplicationDiscoveryService_StreamApplicationsServer) error { - return s.StreamHandler(stream, resource.ApplicationListType) -} - -func (s *server) StreamApplicationPolicies(stream subscription.ApplicationPolicyDiscoveryService_StreamApplicationPoliciesServer) error { - return s.StreamHandler(stream, resource.ApplicationPolicyListType) -} - -func (s *server) StreamSubscriptionPolicies(stream subscription.SubscriptionPolicyDiscoveryService_StreamSubscriptionPoliciesServer) error { - return s.StreamHandler(stream, resource.SubscriptionPolicyListType) -} - -func (s *server) StreamApplicationKeyMappings(stream subscription.ApplicationKeyMappingDiscoveryService_StreamApplicationKeyMappingsServer) error { - return s.StreamHandler(stream, resource.ApplicationKeyMappingListType) -} - -func (s *server) StreamApplicationMappings(stream subscription.ApplicationMappingDiscoveryService_StreamApplicationMappingsServer) error { - return s.StreamHandler(stream, resource.ApplicationMappingListType) -} - -func (s *server) StreamKeyManagers(stream keymgt.KMDiscoveryService_StreamKeyManagersServer) error { - return s.StreamHandler(stream, resource.KeyManagerType) -} - -func (s *server) StreamTokens(stream keymgt.RevokedTokenDiscoveryService_StreamTokensServer) error { - return s.StreamHandler(stream, resource.RevokedTokensType) -} - -func (s *server) StreamAPKMgtApplications(stream apkmgt.APKMgtDiscoveryService_StreamAPKMgtApplicationsServer) error { - return s.StreamHandler(stream, resource.APKMgtApplicationType) -} func (s *server)StreamJWTIssuers(stream subscription.JWTIssuerDiscoveryService_StreamJWTIssuersServer) error { return s.StreamHandler(stream, resource.JWTIssuerListType) } diff --git a/common-controller/internal/operator/constant/constant.go b/common-controller/internal/operator/constant/constant.go index a9bb47769e..2b16b7c87b 100644 --- a/common-controller/internal/operator/constant/constant.go +++ b/common-controller/internal/operator/constant/constant.go @@ -32,19 +32,20 @@ const ( Delete string = "DELETED" ) +// Subscriprion events related constants const ( - APPLICATION_CREATED string = "APPLICATION_CREATED" - APPLICATION_UPDATED string = "APPLICATION_UPDATED" - APPLICATION_DELETED string = "APPLICATION_DELETED" - SUBSCRIPTION_CREATED string = "SUBSCRIPTION_CREATED" - SUBSCRIPTION_UPDATED string = "SUBSCRIPTION_UPDATED" - SUBSCRIPTION_DELETED string = "SUBSCRIPTION_DELETED" - APPLICATION_MAPPING_CREATED string = "APPLICATION_MAPPING_CREATED" - APPLICATION_MAPPING_UPDATED string = "APPLICATION_MAPPING_UPDATED" - APPLICATION_MAPPING_DELETED string = "APPLICATION_MAPPING_DELETED" - APPLICATION_KEY_MAPPING_CREATED string = "APPLICATION_KEY_MAPPING_CREATED" - APPLICATION_KEY_MAPPING_UPDATED string = "APPLICATION_KEY_MAPPING_UPDATED" - APPLICATION_KEY_MAPPING_DELETED string = "APPLICATION_KEY_MAPPING_DELETED" + ApplicationCreated string = "APPLICATION_CREATED" + ApplicationUpdated string = "APPLICATION_UPDATED" + ApplicationDeleted string = "APPLICATION_DELETED" + SubscriptionCreated string = "SUBSCRIPTION_CREATED" + SubscriptionUpdated string = "SUBSCRIPTION_UPDATED" + SubscriptionDeleted string = "SUBSCRIPTION_DELETED" + ApplicationMappingCreated string = "APPLICATION_MAPPING_CREATED" + ApplicationMappingUpdated string = "APPLICATION_MAPPING_UPDATED" + ApplicationMappingDeleted string = "APPLICATION_MAPPING_DELETED" + ApplicationKeyMappingCreated string = "APPLICATION_KEY_MAPPING_CREATED" + ApplicationKeyMappingUpdated string = "APPLICATION_KEY_MAPPING_UPDATED" + ApplicationKeyMappingDeleted string = "APPLICATION_KEY_MAPPING_DELETED" ) // Environment variable names and default values diff --git a/common-controller/internal/operator/controllers/cp/application_controller.go b/common-controller/internal/operator/controllers/cp/application_controller.go index 9c681ee92d..a105f4e7b7 100644 --- a/common-controller/internal/operator/controllers/cp/application_controller.go +++ b/common-controller/internal/operator/controllers/cp/application_controller.go @@ -100,7 +100,6 @@ func (applicationReconciler *ApplicationReconciler) Reconcile(ctx context.Contex if found { utils.SendAppDeletionEvent(applicationKey.Name, applicationSpec) applicationReconciler.ods.DeleteApplicationFromStore(applicationKey) - return ctrl.Result{}, nil } else { loggers.LoggerAPKOperator.Debugf("Application %s/%s does not exist in k8s", applicationKey.Namespace, applicationKey.Name) } diff --git a/common-controller/internal/utils/event_utils.go b/common-controller/internal/utils/event_utils.go index 4adc378acd..b7d3835a5c 100644 --- a/common-controller/internal/utils/event_utils.go +++ b/common-controller/internal/utils/event_utils.go @@ -15,7 +15,7 @@ func SendAppDeletionEvent(applicationUUID string, applicationSpec cpv1alpha2.App milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: applicationUUID, - Type: constants.APPLICATION_DELETED, + Type: constants.ApplicationDeleted, TimeStamp: milliseconds, Application: &subscription.Application{ Uuid: applicationUUID, @@ -25,7 +25,7 @@ func SendAppDeletionEvent(applicationUUID string, applicationSpec cpv1alpha2.App Attributes: applicationSpec.Attributes, }, } - sendEvent(event) + sendEvent(&event) } // SendAppUpdateEvent sends an application update event to the enforcer @@ -34,7 +34,7 @@ func SendAppUpdateEvent(applicationUUID string, oldApplicationSpec cpv1alpha2.Ap milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: applicationUUID, - Type: constants.APPLICATION_UPDATED, + Type: constants.ApplicationUpdated, TimeStamp: milliseconds, Application: &subscription.Application{ Uuid: applicationUUID, @@ -44,9 +44,9 @@ func SendAppUpdateEvent(applicationUUID string, oldApplicationSpec cpv1alpha2.Ap Attributes: newApplicationSpec.Attributes, }, } - sendEvent(event) - SendDeleteApplicationKeyMappingEvent(applicationUUID, oldApplicationSpec) - SendApplicationKeyMappingEvent(applicationUUID, newApplicationSpec) + sendEvent(&event) + sendDeleteApplicationKeyMappingEvent(applicationUUID, oldApplicationSpec) + sendApplicationKeyMappingEvent(applicationUUID, newApplicationSpec) } // SendAddApplicationEvent sends an application creation event to the enforcer @@ -55,7 +55,7 @@ func SendAddApplicationEvent(application cpv1alpha2.Application) { milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: application.ObjectMeta.Name, - Type: constants.APPLICATION_CREATED, + Type: constants.ApplicationCreated, TimeStamp: milliseconds, Application: &subscription.Application{ Uuid: application.ObjectMeta.Name, @@ -65,7 +65,7 @@ func SendAddApplicationEvent(application cpv1alpha2.Application) { Attributes: application.Spec.Attributes, }, } - sendEvent(event) + sendEvent(&event) } // SendAddSubscriptionEvent sends an subscription creation event to the enforcer @@ -74,7 +74,7 @@ func SendAddSubscriptionEvent(sub cpv1alpha2.Subscription) { milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: sub.ObjectMeta.Name, - Type: constants.SUBSCRIPTION_CREATED, + Type: constants.SubscriptionCreated, TimeStamp: milliseconds, Subscription: &subscription.Subscription{ Uuid: sub.ObjectMeta.Name, @@ -86,7 +86,7 @@ func SendAddSubscriptionEvent(sub cpv1alpha2.Subscription) { }, }, } - sendEvent(event) + sendEvent(&event) } // SendDeleteSubscriptionEvent sends an subscription deletion event to the enforcer @@ -95,7 +95,7 @@ func SendDeleteSubscriptionEvent(subscriptionUUID string, subscriptionSpec cpv1a milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: subscriptionUUID, - Type: constants.SUBSCRIPTION_DELETED, + Type: constants.SubscriptionDeleted, TimeStamp: milliseconds, Subscription: &subscription.Subscription{ Uuid: subscriptionUUID, @@ -107,7 +107,7 @@ func SendDeleteSubscriptionEvent(subscriptionUUID string, subscriptionSpec cpv1a }, }, } - sendEvent(event) + sendEvent(&event) } // SendCreateApplicationMappingEvent sends an application mapping event to the enforcer @@ -116,7 +116,7 @@ func SendCreateApplicationMappingEvent(applicationMapping cpv1alpha2.Application milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: applicationMapping.ObjectMeta.Name, - Type: constants.APPLICATION_MAPPING_CREATED, + Type: constants.ApplicationMappingCreated, TimeStamp: milliseconds, ApplicationMapping: &subscription.ApplicationMapping{ Uuid: applicationMapping.ObjectMeta.Name, @@ -124,7 +124,7 @@ func SendCreateApplicationMappingEvent(applicationMapping cpv1alpha2.Application SubscriptionRef: applicationMapping.Spec.SubscriptionRef, }, } - sendEvent(event) + sendEvent(&event) } // SendDeleteApplicationMappingEvent sends an application mapping deletion event to the enforcer @@ -133,7 +133,7 @@ func SendDeleteApplicationMappingEvent(applicationMappingUUID string, applicatio milliseconds := currentTime.UnixNano() / int64(time.Millisecond) event := subscription.Event{ Uuid: applicationMappingUUID, - Type: constants.APPLICATION_DELETED, + Type: constants.ApplicationDeleted, TimeStamp: milliseconds, ApplicationMapping: &subscription.ApplicationMapping{ Uuid: applicationMappingUUID, @@ -141,9 +141,9 @@ func SendDeleteApplicationMappingEvent(applicationMappingUUID string, applicatio SubscriptionRef: applicationMappingSpec.SubscriptionRef, }, } - sendEvent(event) + sendEvent(&event) } -func SendDeleteApplicationKeyMappingEvent(applicationUUID string, applicationKeyMapping cpv1alpha2.ApplicationSpec) { +func sendDeleteApplicationKeyMappingEvent(applicationUUID string, applicationKeyMapping cpv1alpha2.ApplicationSpec) { currentTime := time.Now() milliseconds := currentTime.UnixNano() / int64(time.Millisecond) var oauth2SecurityScheme = applicationKeyMapping.SecuritySchemes.OAuth2 @@ -151,7 +151,7 @@ func SendDeleteApplicationKeyMappingEvent(applicationUUID string, applicationKey for _, env := range oauth2SecurityScheme.Environments { event := subscription.Event{ Uuid: applicationUUID, - Type: constants.APPLICATION_KEY_MAPPING_DELETED, + Type: constants.ApplicationKeyMappingDeleted, TimeStamp: milliseconds, ApplicationKeyMapping: &subscription.ApplicationKeyMapping{ ApplicationUUID: applicationUUID, @@ -161,11 +161,11 @@ func SendDeleteApplicationKeyMappingEvent(applicationUUID string, applicationKey EnvID: env.EnvID, }, } - sendEvent(event) + sendEvent(&event) } } } -func SendApplicationKeyMappingEvent(applicationUUID string, applicationSpec cpv1alpha2.ApplicationSpec) { +func sendApplicationKeyMappingEvent(applicationUUID string, applicationSpec cpv1alpha2.ApplicationSpec) { currentTime := time.Now() milliseconds := currentTime.UnixNano() / int64(time.Millisecond) var oauth2SecurityScheme = applicationSpec.SecuritySchemes.OAuth2 @@ -173,7 +173,7 @@ func SendApplicationKeyMappingEvent(applicationUUID string, applicationSpec cpv1 for _, env := range oauth2SecurityScheme.Environments { event := subscription.Event{ Uuid: applicationUUID, - Type: constants.APPLICATION_KEY_MAPPING_CREATED, + Type: constants.ApplicationKeyMappingCreated, TimeStamp: milliseconds, ApplicationKeyMapping: &subscription.ApplicationKeyMapping{ ApplicationUUID: applicationUUID, @@ -183,17 +183,17 @@ func SendApplicationKeyMappingEvent(applicationUUID string, applicationSpec cpv1 EnvID: env.EnvID, }, } - sendEvent(event) + sendEvent(&event) } } } -func sendEvent(event subscription.Event) { - for clientId, stream := range GetAllClientConnections() { - err := stream.Send(&event) +func sendEvent(event *subscription.Event) { + for clientID, stream := range GetAllClientConnections() { + err := stream.Send(event) if err != nil { - loggers.LoggerAPK.Errorf("Error sending event to client %s: %v", clientId, err) + loggers.LoggerAPK.Errorf("Error sending event to client %s: %v", clientID, err) } else { - loggers.LoggerAPK.Debugf("Event sent to client %s", clientId) + loggers.LoggerAPK.Debugf("Event sent to client %s", clientID) } } }