-
Notifications
You must be signed in to change notification settings - Fork 94
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
Conversation
0234576
to
a498253
Compare
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" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use match
here - https://ballerina.io/learn/by-example/match-statement/
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
model:HTTPRouteRule httpRouteRule = { | ||
matches: self.retrieveHTTPMatches(apkConf, operation, organization), | ||
backendRefs: self.retrieveGeneratedBackend(apkConf, endpointToUse, endpointType), | ||
filters: self.generateFilters(apiArtifact, apkConf, endpointToUse, operation, endpointType, organization) | ||
}; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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 != []) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (addPolicies != []) { | |
if addPolicies != [] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2f908b5
} else if (policyName == "AddHeaders" || policyName == "SetHeaders" || policyName == "RemoveHeaders") { | ||
|
||
} else { | ||
return e909052(error("Incorrect API Policy name provided.")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} 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.")); | |
} |
There was a problem hiding this comment.
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") { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 != []) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove brackets here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2f908b5
d0e33c3
to
3e75dbe
Compare
3e75dbe
to
2f908b5
Compare
Purpose
This PR adds the config deployer level support for the HeaderModifier filter, as detailed in #2378
Sample apk-conf file would be