Display Cloudhub Logs in Json Pattern along with Custom fields - mule

Our client is not interested in using any 3rd party Logger like JSON Logger or any other logger comnectors available. So is there any way to modify the default Anypoint Logging pattern in cloudhub in Json format like how a Json Logger does. Is there any way to add custom fields using default logger like api_name or flow_start_time, end_time? Currently I am creating a variable and defining required fields in a Json pattern and further configure the variable in the default logger which is a workaround and working fine but its just I was curious if this is possible in some easier way without writing any dwl?

You can add custom variables using Mule 4.4 MDC logging feature. Note that it is not available with previous versions.
You can also request to override the default logging configuration in CloudHub, otherwise the log4j2.xml in the application is ignored, and try to use the JsonLayout. I don't recommend it though.

Related

Authorization in Helidon MP

Helidon uses annotations like
#RoleValidator.Roles({“my_admins”, “test”})
to do the authorization.
I am wondering if there is a way to do authorization differently using configuration settings for paths, for example.
Basically, the question is.
Is there a way to use configuration instead of annotation to authorize requests to particular endpoints?
If yes, would it be possible to get the SecurityContext like in a case of annotation?
Example with multiple roles for one endpoint would be helpful
I am successfully using annotations but in some cases it is not convenient
You should be able to do what you want using configuration instead of annotations. It would look similar to what our documentation describes here: https://helidon.io/docs/latest/index.html#/se/guides/security-oidc#Restrict-access-to-a-specific-role
You might not even use the annotations given your use case.
You would define the user-to-roles mapping however makes sense for you (Helidon config would work as would some other provider) and then use Helidon config to set up each endpoint's roles-allowed setting as needed.
As you are using Helidon MP, you could for example add something like this to your META-INF/microprofile-config.properties file:
web-server.paths.0.path=/greet
web-server.paths.0.methods=get
web-server.paths.0.roles-allowed=admin,dev
web-server.paths.0.authenticate=true
(These particular settings are drawn from Helidon's MP QuickStart example but you get the idea.)

what is the easy way to manually parse the RAML file in mule?

In mule application I am trying to parse RAML file. I knew that APIKit is doing same as it creates flows after parsing the RAML file. But still, what if I want to parse it in middle of the flow manually?
I have seen the raml parsers available but not finding the proper usage of those javascript libraries or java libraries on how to use them in mule application..
Yes you can parse your RAML in your java application using java class or groovy component implementing java.
There are java parser available like RamlModelBuilder which you can use to parse your application RAML like validation of your RAML file, getting APIs name, getting all resources name, method name, scopes, security schema and their names, query parameters, headers and many more...
Just check the example how it is used here. You can simply create a java class and get your RAML parsed
https://github.com/anirban37/Anirban-Custom-Oauth-Policy/blob/master/Anirban-RAML-Oauth-V3/OauthPolicies.xml#L594.
ramlModelResult = new RamlModelBuilder().buildApi(ac.getRaml())
will give you the current RAML file access of the application in the java class
Theres nothing in Mule to work with the RAML file at runtime.
But you can create any Java component that uses RAML Java libraries and invoke that from Mule in your flows.
The Mule4 SDK is one way of extending mule through Java.
For more information on Mule SDK can be found here https://mule4-docs.mulesoft.com/mule-sdk/v/1.1/
You can also invoke Java classes but they need to be decoupled from the Mule API and you need to extract any variables, properties or payload and explicitly pass the values to your class. For example passing a static String and a flow var as arguments to a Java constructor:
<java:new class="com.foo.AppleEater" constructor="MyClass(String, Apple)">
<java:args>#[{name: 'some string arg', apple: vars.apple}]</java:args>
</java:new>
In your class you could use the RAML Java libraries, and pass the file or path to RAML file to load from the classpath.
More on Java integration with Mule 4 here: https://docs.mulesoft.com/mule-runtime/4.1/intro-java-integration

Wrapper for Common Logging based on NLog?

We want to use a common logging library for ASP.Net Core web applications, Azure Functions and Web Services. NLog seems like a good option since it has multiple targets e.g. Application Insights and Azure Table Storage. Ideally, I'd like to do something like call CommonTrace(message) and have that function
(1) track other information such as (a) Environment - DEV, TEST, PROD; (b) the local time, (c) System.Runtime.CompilerServices.CallerLineNumber (d) calling application etc.
(2) Call NLog to write out the message and other information. Note the other information may be prepended to the message or persisted in some other way.
But as I learn more about NLog and ASP.NET Core, it seems that if one naively creates a basic wrapper class to add additional information, one could lose out on a lot of features e.g. accessing the callsite and header information that NLog and Microsoft.Extensions.Logging provides.
So, is it possible to subclass NLog entirely and then use that wrapper as one would normally use NLog with ASP.Net Core. Or is there a better way to add common information from multiple applications when using NLog?
NLog has the ability to capture lots of context properties, without them being injected directly with the log-operation.
They are captured automatically by the Layouts configured for the NLog-target. Some NLog targets allows one to add any number of properties.
This can be seen with the NLog Application Insight Target (ver. 2.6.4), that now has the ability to add one (or more) "ContextProperty"-elements. These will automatically be included as meta-data with each log-operation:
https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/183
If the NLog target doesn't have support for ContextProperty, then one can also turn to the NLog JsonLayout that allows one add metadata as additional Json-Attributes:
https://github.com/NLog/NLog/wiki/JsonLayout
If your favorite custom NLog target supports structured logging properties, then it should be a small change to add support for ContextProperty-elements (Easy way is to inherit from NLog TargetWithContext and just use GetAllProperties-method):
https://github.com/AlanBarber/NLog.Targets.Splunk

Best way to load properties file during runtime in mule

I would like to know what would be the best way to load the properties file in mule for the following scenario.
I have a properties file where in I set all the email related fields set in it like templates, to and from etc.
All these need to be set to a specific object along with other changes to that object so I'm planning to use a Java transformer and now I need to load all those values from that properties file and send to transformer. So what would be a best approach to work in above scenario.
Load properties in Java transformer using core java load properties
Load properties using spring context and send it to transformer and access using inbound properties
Kindly let me know if there is any other better approach other than above
First, you should be able to load the property file using spring context as shown below:
<context:property-placeholder location="somename.properties" />
Then, you can set specific property value as flow variable as shown below:
<set-variable variableName="fromAddress" value="${xyz.fromAddress}" />
Finally, you can access this flow variable in your processor class as shown below:
String fromAddress = muleEvent.getFlowVariable("fromAddress");
I will suggest to use Java load properties if these properties are only used in one Transformer. There is one more benefit out of Java load properties is that after modifying your properties file you do not need to restart the application.

How can I cosume a GET REST call and mapping to a java bean (object) through Apache Camel?

I am new in apache camel. I want to do a GET REST call to get data and then I want to mapping these data to my Java bean. How can I do that with camel? I want to do it in a spring MVC web application.
I know how to do it with RestTemplate for example, but I want to use apache camel.
I've checked this documentation http://camel.apache.org/cxfrs.html but still I don't know how to set up for accomplishing this.
Please if you can provide some examples will be great.
There are a few different options. I'll walk through one...
First, define your rest configuration with bindingMode=auto
restConfiguration()
.component("jetty").host("0.0.0.0").port(9000)
.bindingMode(RestBindingMode.auto);
Next, when you define your particular rest service, specify a type (this is the type of the incoming body:
rest("/")
.put("/A/{subpath1}/{subpath2}")
.type(MyPojo.class)
.to("direct:XYZ");
That's it! The unmarshalling will be magical ;)
Alternatively, you can unmarshal things yourself.
If you'd like to see a working example of the above, check out this program: it has a main() to test it. https://github.com/DariusX/CamelSandbox/blob/master/CamelSandbox/src/main/java/com/zerses/camelsandbox/rest/RestConsumerBindingTest.java