How to set tenant to header in mediator with WSO2 API Manager
Asked Answered
P

2

7

I have an API that requires the tenant as a header.

If I create a custom in-sequence:

<sequence name="WSO2AM--Ext--In">
    <header
       name="X-Tenant-Id"
       scope="transport"
       action="set"
       expression="???????????????????"
    />
</sequence>

Is there an expression that I can use to achieve this? Or should I resort to creating a per-API mediator to set it?

PS: Looking at WSO2 source code (CarbonTenantInfoConfigurator.java), I found this fragment that could be useful as a hint:

PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String tenantDomain = cc.getTenantDomain();
int tenantId = cc.getTenantId();
messageContext.setProperty("tenant.info.domain", tenantDomain);
messageContext.setProperty("tenant.info.id", tenantId);

But I don't know how to access to those properties in the custom sequence, if possible.

Ptisan answered 18/8, 2015 at 15:1 Comment(0)
Q
4

After checking the debug output from ApiManager, I noticed that custom sequences are being executed right after handlers. Luckily, the OAuthAuthenticator class (used by APIAuthenticationHandler) sets some handy properties like END_USER_NAME and APPLICATION_NAME.

END_USER_NAME contains the name and tenant of the caller ([email protected]).

This custom sequence worked for me:

<sequence name="add_service_header" trace="enable" statistics="enable" xmlns="http://ws.apache.org/ns/synapse">
    <log/>
    <property name="tenant" expression="fn:substring-after(get-property('END_USER_NAME'), '@')" />
    <header name="X-Tenant" scope="transport" expression="get-property('tenant')"/>
    <header name="X-AppName" scope="transport" expression="get-property('APPLICATION_NAME')"/>
</sequence>

I could not find documentation for the property other than source code and this other question

Quicklime answered 9/9, 2015 at 20:35 Comment(1)
That's exactly what I was looking for and it works great. Thanks!Ptisan
C
0

As the code suggests these are set to synapse MessageContext. You can retrieve these properties using following expressions.

get-property('tenant.info.domain')

get-property('tenant.info.id')

Thanks

Tishan

Crossbreed answered 24/8, 2015 at 10:1 Comment(2)
I tried that. Also tried get-property(tenant.info.domain), $ctx:tenant.info.domain and $axis2:tenant.info.domain without success.Ptisan
I tried with with ESB 4.8.0 and it is working as expected. Below is the synapse config segment used to set HTTP header. <header name="X-Tenant" scope="transport" expression="get-property('tenant.info.domain')"/>Crossbreed

© 2022 - 2024 — McMap. All rights reserved.