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

Add support for HeaderModifier filter in Config Deployer #2381

Merged
merged 4 commits into from
Jun 18, 2024

Conversation

sgayangi
Copy link
Contributor

@sgayangi sgayangi commented May 29, 2024

Purpose

This PR adds the config deployer level support for the HeaderModifier filter, as detailed in #2378

Sample apk-conf file would be

id: "f7996dce4ac15e2af0f8ee14546c4f72988eddae"
name: "EmployeeServiceAPI"
basePath: "/test"
version: "3.14"
type: "REST"
defaultVersion: false
endpointConfigurations:
  production:
    endpoint: "https://httpbin.org/anything"
operations:
  - target: "/employee"
    verb: "GET"
    secured: false
    scopes: []
    operationPolicies:
      request:
        - policyName: AddHeaders
          policyVersion: v1
          parameters:
            headers:
              - name: "Test-Request-Header"
                value: "Test-Value"
        - policyName: SetHeaders
          policyVersion: v1
          parameters:
            headers:
              - name: "Set-Request-Header"
                value: "Test-Value"
        - policyName: RemoveHeaders
          policyVersion: v1
          parameters:
            headers:
              - "X-Test-Header"
  - target: "/employee"
    verb: "POST"
    secured: true
    scopes: []
  - target: "/employee/{employeeId}"
    verb: "PUT"
    secured: true
    scopes: []
  - target: "/employee/{employeeId}"
    verb: "DELETE"
    secured: true
    scopes: []

@sgayangi sgayangi added the trigger-action When this includes apk actions getting trigger for pr label May 29, 2024
@sgayangi sgayangi force-pushed the #2378-httproute-filters branch from 0234576 to a498253 Compare May 29, 2024 09:17
@github-actions github-actions bot removed the trigger-action When this includes apk actions getting trigger for pr label May 29, 2024
@sgayangi sgayangi added trigger-action When this includes apk actions getting trigger for pr and removed trigger-action When this includes apk actions getting trigger for pr labels May 29, 2024
Comment on lines 847 to 859
if policy.policyName == "AddHeaders" {
ModifierHeader[] addHeaders = <ModifierHeader[]>policyParameters.headers;
foreach ModifierHeader header in addHeaders {
addPolicies.push(header);
}
}
if policy.policyName == "SetHeaders" {
ModifierHeader[] setHeaders = <ModifierHeader[]>policyParameters.headers;
foreach ModifierHeader header in setHeaders {
setPolicies.push(header);
}
}
if (policyName == "removeHeader") {
string httpHeader = <string>policyParameters.get("headerName");
removePolicies.push(httpHeader);
if policy.policyName == "RemoveHeaders" {
Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 2f908b5

if requestPolicies is APKOperationPolicy[] && requestPolicies.length() > 0 {
model:HTTPRouteFilter headerModifierFilter = {'type: "RequestHeaderModifier"};
headerModifierFilter.requestHeaderModifier = self.extractHttpHeaderFilterData(requestPolicies, organization);
routeFilters.push(headerModifierFilter);
Copy link

Choose a reason for hiding this comment

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

Since this line is repeated inside both if blocks, you could move it out

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the filter type (RequestHeaderModifier and ResponseHeaderModifier) and the properties added are different in each if block, I left it as is.

Comment on lines 788 to 792
model:HTTPRouteRule httpRouteRule = {
matches: self.retrieveHTTPMatches(apkConf, operation, organization),
backendRefs: self.retrieveGeneratedBackend(apkConf, endpointToUse, endpointType),
filters: self.generateFilters(apiArtifact, apkConf, endpointToUse, operation, endpointType, organization)
};
Copy link

@gayaldassanayake gayaldassanayake Jun 4, 2024

Choose a reason for hiding this comment

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

We can remove the else block here (and in the code lines above) to reduce the indentation depth. look for other similar places as well

if apkConf.'type == API_TYPE_GRAPHQL {
    model:GQLRouteMatch[]|error routeMatches = self.retrieveGQLMatches(apkConf, operation, organization);
    if routeMatches is model:GQLRouteMatch[] && routeMatches.length() > 0 {
        model:GQLRouteRule gqlRouteRule = {matches: routeMatches};
        return gqlRouteRule;
    }
     return e909022("Provided Type currently not supported for GraphQL APIs.", error("Provided Type currently not supported for GraphQL APIs."));
}
model:HTTPRouteRule httpRouteRule = {
      matches: self.retrieveHTTPMatches(apkConf, operation, organization),
      backendRefs: self.retrieveGeneratedBackend(apkConf, endpointToUse, endpointType),
      filters: self.generateFilters(apiArtifact, apkConf, endpointToUse, operation, endpointType, organization)
                    };
return httpRouteRule;

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Since more API types will be added in the future, I instead modified the else block to be an if else block.

if apkConf.'type == API_TYPE_GRAPHQL {
    model:GQLRouteMatch[]|error routeMatches = self.retrieveGQLMatches(apkConf, operation, organization);
    if routeMatches is model:GQLRouteMatch[] && routeMatches.length() > 0 {
        model:GQLRouteRule gqlRouteRule = {matches: routeMatches};
        return gqlRouteRule;
    } else {
        return e909022("Provided Type currently not supported for GraphQL APIs.", error("Provided Type currently not supported for GraphQL APIs."));
    }
} else if apkConf.'type == API_TYPE_REST {
    {
        model:HTTPRouteRule httpRouteRule = {
            matches: self.retrieveHTTPMatches(apkConf, operation, organization),
            backendRefs: self.retrieveGeneratedBackend(apkConf, endpointToUse, endpointType),
            filters: self.generateFilters(apiArtifact, apkConf, endpointToUse, operation, endpointType, organization)
        };
        return httpRouteRule;
    }
} else {
    return e909018("Invalid API Type specified");
}

}
}
}
model:HTTPHeaderFilter headerModifier = {};
if (addPolicies != []) {

Choose a reason for hiding this comment

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

Suggested change
if (addPolicies != []) {
if addPolicies != [] {

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Fixed in 2f908b5

Comment on lines 1263 to 1267
} else if (policyName == "AddHeaders" || policyName == "SetHeaders" || policyName == "RemoveHeaders") {

} else {
return e909052(error("Incorrect API Policy name provided."));
}

Choose a reason for hiding this comment

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

Suggested change
} else if (policyName == "AddHeaders" || policyName == "SetHeaders" || policyName == "RemoveHeaders") {
} else {
return e909052(error("Incorrect API Policy name provided."));
}
} else if (policyName != "AddHeaders" && policyName != "SetHeaders" && policyName != "RemoveHeaders") {
return e909052(error("Incorrect API Policy name provided."));
}

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Fixed in 2f908b5

@@ -1228,6 +1260,8 @@ public class APIClient {
model:BackendJWT backendJwt = self.retrieveBackendJWTPolicy(apkConf, apiArtifact, backendJWTPolicy, operations, organization);
apiArtifact.backendJwt = backendJwt;
policyReferences.push(<model:BackendJwtReference>{name: backendJwt.metadata.name});
} else if (policyName == "AddHeaders" || policyName == "SetHeaders" || policyName == "RemoveHeaders") {

Choose a reason for hiding this comment

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

Brackets are unnecessary. We can remove them.

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Fixed in 2f908b5

@@ -202,6 +202,11 @@ public type Authentication record {|
boolean enabled = true;
|};

public type HeaderModifierPolicy record {

Choose a reason for hiding this comment

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

Let's add documentation comments for all public records

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Fixed in 2f908b5

addPolicies.push(header);
}
}
if policy.policyName == "SetHeaders" {

Choose a reason for hiding this comment

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

We may use enum for the policyName.

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Fixed in 2f908b5

}
}
}
model:HTTPHeaderFilter headerModifier = {};
if (addPolicies != []) {
headerModifier.add = addPolicies;
}
if (setPolicies != []) {

Choose a reason for hiding this comment

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

Remove brackets here.

Copy link
Contributor Author

@sgayangi sgayangi Jun 4, 2024

Choose a reason for hiding this comment

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

Fixed in 2f908b5

@github-actions github-actions bot removed the trigger-action When this includes apk actions getting trigger for pr label Jun 4, 2024
@sgayangi sgayangi force-pushed the #2378-httproute-filters branch 2 times, most recently from d0e33c3 to 3e75dbe Compare June 4, 2024 08:47
@sgayangi sgayangi added the trigger-action When this includes apk actions getting trigger for pr label Jun 4, 2024
@sgayangi sgayangi force-pushed the #2378-httproute-filters branch from 3e75dbe to 2f908b5 Compare June 4, 2024 09:00
@github-actions github-actions bot removed the trigger-action When this includes apk actions getting trigger for pr label Jun 4, 2024
@sgayangi sgayangi added trigger-action When this includes apk actions getting trigger for pr and removed trigger-action When this includes apk actions getting trigger for pr labels Jun 4, 2024
@github-actions github-actions bot removed the trigger-action When this includes apk actions getting trigger for pr label Jun 6, 2024
@sgayangi sgayangi added the trigger-action When this includes apk actions getting trigger for pr label Jun 6, 2024
@sgayangi sgayangi added trigger-action When this includes apk actions getting trigger for pr and removed trigger-action When this includes apk actions getting trigger for pr labels Jun 6, 2024
@CrowleyRajapakse CrowleyRajapakse merged commit 3115de5 into wso2:main Jun 18, 2024
21 of 24 checks passed
@github-actions github-actions bot removed the trigger-action When this includes apk actions getting trigger for pr label Jun 18, 2024
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.

5 participants