WSO2: How to use config registry in Kubernete Enviorment?

7/7/2021

We are using WSO2 in VM and used below code into mediation to save data inside registry

<script language="js"><![CDATA[
		    	importPackage(Packages.org.apache.synapse.config);
		    	mc.getConfiguration().getRegistry().newResource("/keycloak/keycloakkey-kid",false);
		    	mc.getConfiguration().getRegistry().updateResource("/keycloak/keycloakkey-kid", mc.getProperty("keycloakkey-kid"));
		    	mc.getConfiguration().getRegistry().newResource("/keycloak/keycloakkey-x5c",false);
		    	mc.getConfiguration().getRegistry().updateResource("/keycloak/keycloakkey-x5c", mc.getProperty("keycloakkey-x5c"));
		    	mc.getConfiguration().getRegistry().newResource("/keycloak/keycloakkey-timestamp",false);
		    	mc.getConfiguration().getRegistry().updateResource("/keycloak/keycloakkey-timestamp", mc.getProperty("current-system-time"));
				]]>
		  	</script>

But now same code i deployed in Docker and Kubenete environment and this is not able to save the data

How to use the registry in the case of Kubernete environment ?

What i tried

in deployment.yaml i added volume mount

 volumeMounts:
            - mountPath: /keycloak/keycloakkey-kid
              name: keycloak
      volumes:
        - name: keycloak

and changed the js code as well

 <script language="js"><![CDATA[
		    	importPackage(Packages.org.apache.synapse.config);
		    	mc.getConfiguration().getRegistry().newResource("/keycloak/keycloakkey-kid",false);
		    	mc.getConfiguration().getRegistry().updateResource("/keycloak/keycloakkey-kid", mc.getProperty("keycloakkey-kid"));
		    	mc.getConfiguration().getRegistry().newResource("/keycloak/keycloakkey-x5c",false);
		    	mc.getConfiguration().getRegistry().updateResource("/keycloak/keycloakkey-x5c", mc.getProperty("keycloakkey-x5c"));
		    	mc.getConfiguration().getRegistry().newResource("/keycloak/keycloakkey-timestamp",false);
		    	mc.getConfiguration().getRegistry().updateResource("/keycloak/keycloakkey-timestamp", mc.getProperty("current-system-time"));
				]]>
		  	</script>

But still failing with error

2021-06-21 16:15:42,576] ERROR {MicroIntegratorRegistry} - Couldn't create resource: keycloakkey-kid
[2021-06-21 16:15:42,577] ERROR {MicroIntegratorRegistry} - Error when adding a new resource org.apache.synapse.SynapseException: Couldn't create resource: keycloakkey-kid
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.handleException(MicroIntegratorRegistry.java:655)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.createFile(MicroIntegratorRegistry.java:830)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.addResource(MicroIntegratorRegistry.java:813)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.newResource(MicroIntegratorRegistry.java:562)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
        at org.mozilla.javascript.gen._Unknown_Source__190._c_script_0(<Unknown Source>:3)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(<Unknown Source>)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(<Unknown Source>)
        at org.mozilla.javascript.gen._Unknown_Source__190.exec(<Unknown Source>)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        at javax.script.CompiledScript.eval(CompiledScript.java:92)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:395)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:290)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:258)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:37)
        at org.apache.synapse.config.xml.SwitchCase.mediate(SwitchCase.java:69)
        at org.apache.synapse.mediators.filters.SwitchMediator.mediate(SwitchMediator.java:134)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:214)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:239)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:244)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:267)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:809)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:309)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:583)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:197)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:285)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

[2021-06-21 16:15:42,581] ERROR {ScriptMediator} - {api:Orders} The script engine returned an error executing the inlined js script function mediate com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.WrappedException: Wrapped org.apache.synapse.SynapseException: Error when adding a new resource (<Unknown Source>#3) in <Unknown Source> at line number 3
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
        at javax.script.CompiledScript.eval(CompiledScript.java:92)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:395)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:290)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:258)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:37)
        at org.apache.synapse.config.xml.SwitchCase.mediate(SwitchCase.java:69)
        at org.apache.synapse.mediators.filters.SwitchMediator.mediate(SwitchMediator.java:134)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:214)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:239)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:244)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:267)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:809)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:309)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:583)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:197)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:285)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: org.mozilla.javascript.WrappedException: Wrapped org.apache.synapse.SynapseException: Error when adding a new resource (<Unknown Source>#3)
        at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
        at org.mozilla.javascript.gen._Unknown_Source__190._c_script_0(<Unknown Source>:3)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(<Unknown Source>)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(<Unknown Source>)
        at org.mozilla.javascript.gen._Unknown_Source__190.exec(<Unknown Source>)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        ... 27 more
Caused by: org.apache.synapse.SynapseException: Error when adding a new resource
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.handleException(MicroIntegratorRegistry.java:650)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.newResource(MicroIntegratorRegistry.java:564)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
        ... 36 more
Caused by: org.apache.synapse.SynapseException: Couldn't create resource: keycloakkey-kid
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.handleException(MicroIntegratorRegistry.java:655)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.createFile(MicroIntegratorRegistry.java:830)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.addResource(MicroIntegratorRegistry.java:813)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.newResource(MicroIntegratorRegistry.java:562)
        ... 41 more
-- Subodh Joshi
docker
kubernetes
wso2
wso2mi

1 Answer

8/30/2021

The above error happens when you run the newResource method in MicroIntegratorRegistry class more than once with the same resource path. You should be able to reproduce this error in a VM environment as well if you try out this same mediation flow for the second time. The reason is that newResource method in MicroIntegratorRegistry throws this error if the file already exists at the target location.

As a solution, you may use newNonEmptyResource method 1 in MicroIntegratorRegistry as follows to fulfill your requirement.

<script language="js"><![CDATA[
    importPackage(Packages.org.apache.synapse.config);
    mc.getConfiguration().getRegistry().newNonEmptyResource("/keycloak/keycloakkey-kid", false, null, mc.getProperty("keycloakkey-kid"), null);
    mc.getConfiguration().getRegistry().newNonEmptyResource("/keycloak/keycloakkey-x5c", false, null, mc.getProperty("keycloakkey-x5c"), null);
    mc.getConfiguration().getRegistry().newNonEmptyResource("/keycloak/keycloakkey-timestamp", false, null, mc.getProperty("current-system-time"), null);
    ]]>
</script>

The method signature would be like below.

public void newNonEmptyResource(String path, boolean isDirectory, String mediaType, String content,
                                  String propertyName);

Also, I was able to successfully mount the registry in the Kubernetes environment using the following configuration in pod definition file.

spec:
  containers:
  - image: ...
    ....
    volumeMounts:
    - mountPath: "/home/wso2carbon/wso2mi-4.0.0/registry"
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /registry
      type: DirectoryOrCreate

or else with Docker, with below configuration in docker-compose.yml file ->

volumes:
      - ./registry:/home/wso2carbon/wso2mi-4.0.0/registry

Note: Make sure you give the absolute path to the MI-4.0.0 registry folder as the mount path (in my case it was "/home/wso2carbon/wso2mi-4.0.0/registry" inside the MI-4.0.0 docker container)

Also, I had to change the ownership of my mount path (/registry) as below to start the container successfully inside the pod.

user: wso2carbon
user id: 802
user group: wso2
user group id: 802

Command:

groupadd --system -g 802 wso2 \
&& useradd --system --home /home/wso2carbon -g 802 -u 802 wso2carbon

1 - https://github.com/wso2/micro-integrator/blob/26d25fbe2f2535de2861b80525646ff28035f2f0/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java#L573

-- Sajinie Kavindya
Source: StackOverflow