From cf4675c7e519453b8f451b67850ea82e03d1a01d Mon Sep 17 00:00:00 2001 From: sgayangi Date: Thu, 6 Jun 2024 10:37:20 +0530 Subject: [PATCH] Add propagation to APIM CP --- .../internal/controlplane/eventPublisher.go | 25 ++++++++-- .../operator/controllers/dp/api_controller.go | 47 ++++++++++++++++++- .../ballerina/APIClient.bal | 7 ++- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/adapter/internal/controlplane/eventPublisher.go b/adapter/internal/controlplane/eventPublisher.go index 23352a484..3f760656b 100644 --- a/adapter/internal/controlplane/eventPublisher.go +++ b/adapter/internal/controlplane/eventPublisher.go @@ -100,11 +100,30 @@ type API struct { APIHash string `json:"-"` } +// Headers contains the request and response header modifier information +type Headers struct { + RequestHeaders HeaderModifier `json:"requestHeaders"` + ResponseHeaders HeaderModifier `json:"responseHeaders"` +} + +// HeaderModifier contains header modifier values +type HeaderModifier struct { + AddHeaders []Header `json:"addHeaders"` + RemoveHeaders []string `json:"removeHeaders"` +} + +// Header contains the header information +type Header struct { + Name string `json:"headerName"` + Value string `json:"headerValue,omitempty"` +} + // Operation holds the path, verb, throttling and interceptor policy type Operation struct { - Path string `json:"path"` - Verb string `json:"verb"` - Scopes []string `json:"scopes"` + Path string `json:"path"` + Verb string `json:"verb"` + Scopes []string `json:"scopes"` + Headers Headers `json:"headers"` } // CORSPolicy hold cors configs diff --git a/adapter/internal/operator/controllers/dp/api_controller.go b/adapter/internal/operator/controllers/dp/api_controller.go index 0c13b4570..5c08c41a5 100644 --- a/adapter/internal/operator/controllers/dp/api_controller.go +++ b/adapter/internal/operator/controllers/dp/api_controller.go @@ -2673,6 +2673,10 @@ func prepareOperations(apiState *synchronizer.APIState) []controlplane.Operation if apiState.ProdHTTPRoute != nil && apiState.ProdHTTPRoute.HTTPRouteCombined != nil { for _, rule := range apiState.ProdHTTPRoute.HTTPRouteCombined.Spec.Rules { scopes := []string{} + requestAddHeaders := []controlplane.Header{} + responseAddHeaders := []controlplane.Header{} + requestRemoveHeaders := []string{} + responseRemoveHeaders := []string{} for _, filter := range rule.Filters { if filter.ExtensionRef != nil && filter.ExtensionRef.Kind == "Scope" { scope, found := apiState.ProdHTTPRoute.Scopes[types.NamespacedName{Namespace: apiState.APIDefinition.ObjectMeta.Namespace, Name: string(filter.ExtensionRef.Name)}.String()] @@ -2680,7 +2684,34 @@ func prepareOperations(apiState *synchronizer.APIState) []controlplane.Operation scopes = append(scopes, scope.Spec.Names...) } } + + if filter.RequestHeaderModifier != nil { + requestHeaderModifier := filter.RequestHeaderModifier + for _, addHeader := range requestHeaderModifier.Add { + requestAddHeaders = append(requestAddHeaders, controlplane.Header{Name: string(addHeader.Name), Value: string(addHeader.Value)}) + } + for _, setHeader := range requestHeaderModifier.Set { + requestAddHeaders = append(requestAddHeaders, controlplane.Header{Name: string(setHeader.Name), Value: string(setHeader.Value)}) + } + for _, removeHeader := range requestHeaderModifier.Remove { + requestRemoveHeaders = append(requestRemoveHeaders, removeHeader) + } + } + + if filter.ResponseHeaderModifier != nil { + responseHeaderModifier := filter.ResponseHeaderModifier + for _, addHeader := range responseHeaderModifier.Add { + responseAddHeaders = append(responseAddHeaders, controlplane.Header{Name: string(addHeader.Name), Value: string(addHeader.Value)}) + } + for _, setHeader := range responseHeaderModifier.Set { + responseAddHeaders = append(responseAddHeaders, controlplane.Header{Name: string(setHeader.Name), Value: string(setHeader.Value)}) + } + for _, removeHeader := range responseHeaderModifier.Remove { + responseRemoveHeaders = append(responseRemoveHeaders, removeHeader) + } + } } + for _, match := range rule.Matches { path := "/" verb := "GET" @@ -2694,7 +2725,21 @@ func prepareOperations(apiState *synchronizer.APIState) []controlplane.Operation path = path + "*" } path = "^" + path + "$" - operations = append(operations, controlplane.Operation{Path: path, Verb: verb, Scopes: scopes}) + operations = append(operations, controlplane.Operation{ + Path: path, + Verb: verb, + Scopes: scopes, + Headers: controlplane.Headers{ + RequestHeaders: controlplane.HeaderModifier{ + AddHeaders: requestAddHeaders, + RemoveHeaders: requestRemoveHeaders, + }, + ResponseHeaders: controlplane.HeaderModifier{ + AddHeaders: responseAddHeaders, + RemoveHeaders: responseRemoveHeaders, + }, + }, + }) } } } diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 335b15be6..0a6d3d8a5 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -822,8 +822,11 @@ public class APIClient { }; routeFilters.push(replacePathFilter); APIOperationPolicies? operationPoliciesToUse = (); - if (apkConf.apiPolicies is APIOperationPolicies) { - operationPoliciesToUse = apkConf.apiPolicies; + APIOperationPolicies? operationPolicies = apkConf.apiPolicies; + if (operationPolicies is APIOperationPolicies && operationPolicies != {}) { + if operationPolicies.request is APKOperationPolicy[] || operationPolicies.response is APKOperationPolicy[] { + operationPoliciesToUse = apkConf.apiPolicies; + } } else { operationPoliciesToUse = operation.operationPolicies; }