Skip to content

Commit

Permalink
Merge pull request #2720 from renuka-fernando/policy-update-with-apim
Browse files Browse the repository at this point in the history
OPA - Send token only if "sendAccessToken" is configured
  • Loading branch information
slahirucd7 authored Mar 5, 2022
2 parents dc02f06 + 112eadf commit de178a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 8 additions & 0 deletions adapter/internal/oasparser/model/policy_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,26 @@ func (p PolicyContainerMap) GetFormattedOperationalPolicies(policies OperationPo
for i, policy := range policies.Request {
if fmtPolicy, err := p.getFormattedPolicyFromTemplated(policy, policyInFlow, inFlowStats, i, swagger); err == nil {
fmtPolicies.Request = append(fmtPolicies.Request, fmtPolicy)
loggers.LoggerOasparser.Debugf("Applying operation policy %q in request flow, for API %q in org %q, formatted policy %v",
policy.PolicyName, swagger.GetID(), swagger.OrganizationID, fmtPolicy)
}
}

outFlowStats := policies.Response.getStats()
for i, policy := range policies.Response {
if fmtPolicy, err := p.getFormattedPolicyFromTemplated(policy, policyOutFlow, outFlowStats, i, swagger); err == nil {
fmtPolicies.Response = append(fmtPolicies.Response, fmtPolicy)
loggers.LoggerOasparser.Debugf("Applying operation policy %q in response flow, for API %q in org %q, formatted policy %v",
policy.PolicyName, swagger.GetID(), swagger.OrganizationID, fmtPolicy)
}
}

faultFlowStats := policies.Fault.getStats()
for i, policy := range policies.Fault {
if fmtPolicy, err := p.getFormattedPolicyFromTemplated(policy, policyFaultFlow, faultFlowStats, i, swagger); err == nil {
fmtPolicies.Fault = append(fmtPolicies.Fault, fmtPolicy)
loggers.LoggerOasparser.Debugf("Applying operation policy %q in fault flow, for API %q in org %q, formatted policy %v",
policy.PolicyName, swagger.GetID(), swagger.OrganizationID, fmtPolicy)
}
}

Expand Down Expand Up @@ -212,6 +218,8 @@ func (spec *PolicySpecification) fillDefaultsInPolicy(policy *Policy) {
for _, attrib := range spec.Data.PolicyAttributes {
if _, ok := paramMap[attrib.Name]; !ok && attrib.DefaultValue != "" {
paramMap[attrib.Name] = attrib.DefaultValue
loggers.LoggerOasparser.Debugf("Update with policy attribute %q of policy %q with default value from spec",
attrib.Name, policy.PolicyName)
}
}
policy.Parameters = paramMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class OPAConstants {
* Constants of the AdditionalParameters map.
*/
public static class AdditionalParameters {
public static final String PARAM_SEPARATOR = ",";
public static final String ADDITIONAL_PROPERTIES = "additionalProperties";
public static final String SEND_ACCESS_TOKEN = "sendAccessToken";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.choreo.connect.enforcer.interceptor.opa;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
Expand All @@ -32,13 +33,15 @@
import org.wso2.choreo.connect.enforcer.constants.APIConstants;
import org.wso2.choreo.connect.enforcer.constants.APISecurityConstants;

import java.util.Arrays;
import java.util.Map;

/**
* Default implementation of the {@link OPARequestGenerator}.
*/
public class OPADefaultRequestGenerator implements OPARequestGenerator {
private static final Logger log = LogManager.getLogger(OPADefaultRequestGenerator.class);
private static final String TRUE = "TRUE";

@Override
public String generateRequest(String policyName, String rule, Map<String, String> additionalParameters,
Expand All @@ -48,7 +51,9 @@ public String generateRequest(String policyName, String rule, Map<String, String
requestPayload.put("input", inputPayload);

// following fields are the same fields sent from the synapse request generator
inputPayload.put("transportHeaders", requestContext.getHeaders());
JSONObject transportHeaders = new JSONObject(requestContext.getHeaders());
transportHeaders.remove(StringUtils.lowerCase(requestContext.getMatchedAPI().getAuthHeader()));
inputPayload.put("transportHeaders", transportHeaders);
inputPayload.put("requestOrigin", requestContext.getClientIp());
inputPayload.put("method", requestContext.getRequestMethod());
inputPayload.put("path", requestContext.getRequestPath());
Expand All @@ -66,14 +71,23 @@ public String generateRequest(String policyName, String rule, Map<String, String
apiContext.put("sandClusterName", requestContext.getSandClusterHeader());

// Authentication Context
if ("TRUE".equalsIgnoreCase(additionalParameters.get(OPAConstants.AdditionalParameters.SEND_ACCESS_TOKEN))) {
if (TRUE.equalsIgnoreCase(additionalParameters.get(OPAConstants.AdditionalParameters.SEND_ACCESS_TOKEN))) {
AuthenticationContext authContext = requestContext.getAuthenticationContext();
JSONObject authContextPayload = new JSONObject();
authContextPayload.put("token", authContext.getRawToken());
authContextPayload.put("tokenType", authContext.getTokenType());
authContextPayload.put("keyType", authContext.getKeyType());
inputPayload.put("authenticationContext", authContextPayload);
}

// Additional Properties
// In APIM additional parameter are appended to the main input payload, handle the same in Choreo Connect
String addProps = additionalParameters.get(OPAConstants.AdditionalParameters.ADDITIONAL_PROPERTIES);
if (StringUtils.isNotEmpty(addProps)) {
Arrays.stream(addProps.split(OPAConstants.AdditionalParameters.PARAM_SEPARATOR))
.forEach(key -> inputPayload.put(key, requestContext.getProperties().get(key)));
}

return requestPayload.toString();
}

Expand Down

0 comments on commit de178a1

Please sign in to comment.