Azure WebJobs SDK - What purpose does method ToInvokeString serve? - azure-webjobssdk

When implementing an IValueProvider in a custom WebJobs SDK extension, we are supposed provide implementation of method ToInvokeString. When does the SDK call this method? What should be the considerations when implementing the method?

ToInvokeString will return the method's return value as an Object directly.
ToInvokeString:- Returns object string.
Examples :
public string ToInvokeString()
{
return string.Empty;
}
HttpRequestMessage request = value as HttpRequestMessage;
string invokeString = ToInvokeString(request);
Please refer Custom Extension for more details.

Related

How do i extract information from Jwt which is stored in ReactiveSecurityContextHolder. It returns a Mono<String> but I need String

Below code return Mono from ReactiveSecurityContextHolder
#Component
public class SomeClass {
public static Mono<String> issuesID() {
return ReactiveSecurityContextHolder.getContext().switchIfEmpty(Mono.empty()).flatMap((securityContext) -> {
Authentication authentication = securityContext.getAuthentication();
Jwt jwt = (Jwt) authentication.getPrincipal();
String issuer = (String) jwt.getClaims().get("some_claim");
log.info("{}",issuer);
return Mono.justOrEmpty(issuer);
}).switchIfEmpty(Mono.empty());
}
}
I need something like this
String mutatedId = PREXIX+SomeClass.issuesID();
If i do followings,
PREXIX+SomeClass.issuesID().block();
PREXIX+SomeClass.issuesID().subscribeOn(Schedulers.elastic()).block();
PREXIX+SomeClass.issuesID().toProcessor().block();
PREXIX+SomeClass.issuesID().toFuture().get();
They all give the same error.
block()/blockFirst()/blockLast() are blocking, which is not supported in thread rector-xxx
I have also tried delaying the Mono but that's also not helping.
I know reactor is a non-blocking framework and blocking is discourages but in my case I can't figure out another way to solve this.
I need to pass the mutatedId to mongoFactory so i can switch databases based on Jwt property per request. I do not want to inject #AuthenticationPrincipal in controller and keep passing it to downward layers and finally make decision at DOA layer.
#Override
public Mono<MongoDatabase> getMongoDatabase(String dbName) throws DataAccessException {
String mutatedId = PREXIX+SomeClass.issuesID();
return super.getMongoDatabase(mutatedId+"#"+dbName);
}
Any suggestion how this can be achieved or is there any better approach.
You use for instance flatMap.
Mono<MongoDatabase> db = issuesID()
.flatMap(id -> getMongoDatabase(id));

Add Authorize Attribute Filter in Swashbuckler Implementation of Swagger

Looking to add the AuthorizeFilterAttribute or AnonymousFilterAttribute to an endpoint in Swashbuckle's implementation of Swagger so I can see which attribute is used on each endpoint in the generated documentation file in a running webapi that ends in /swagger. Is this currenlty possible?
I specifically would like to add a big bold label that says this endpoint is [Anonymous] or that endpoint is using [Authorize] and have them look differently that the summary or remark text.
Also I would like to be able to filter out all the different types of these restriction filter attributes for each endpoint including [NonAction], [Authorize], and [Anonymous] where one of these might be at the top of each controller endpoint. Maybe even eventually add other types of FilterAttributes besides these on each endpoint.
Currently it looks like only the HTTP Methods, the request and response objects can be retrieved in the current implementation so I was not able to find definitive information on this.
Since this is a Swagger implementation do these .NET specific attribute filters not translate to Swashbuckle b/c they only implement what's in the Swagger specification and nothing else?
Finally are their .NET specific extensions to Swashbuckle's implementation that do this?
Thanks!
For the part adding the label to unprotected methods/actions you could use an operation filter like this
public class UnprotectedOperationFilter : IOperationFilter
{
private bool HasAttribute(MethodInfo methodInfo, Type type, bool inherit)
{
// inhertit = true also checks inherited attributes
var actionAttributes = methodInfo.GetCustomAttributes(inherit);
var controllerAttributes = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(inherit);
var actionAndControllerAttributes = actionAttributes.Union(controllerAttributes);
return actionAndControllerAttributes.Any(attr => attr.GetType() == type);
}
public void Apply(Operation operation, OperationFilterContext context)
{
bool hasAuthorizeAttribute = HasAttribute(context.MethodInfo, typeof(AuthorizeAttribute), true);
bool hasAnonymousAttribute = HasAttribute(context.MethodInfo, typeof(AllowAnonymousAttribute), true);
// so far as I understood the action/operation is public/unprotected
// if there is no authorize or an allow anonymous (allow anonymous overrides all authorize)
bool isAuthorized = hasAuthorizeAttribute && !hasAnonymousAttribute;
if (!isAuthorized)
{
operation.Description =
"<p><bold>BIG BOLD LABEL indicating an UPROTECTED PUBLIC method</bold></p>"
+ operation.Description;
}
}
}
and add it with
services.AddSwaggerGen(c => { c.OperationFilter<UnprotectedOperationFilter>();} );
I didn't understand what you mean with filter out different attributes but I hope the code above helps you to check if the attribute is present and do what you desire to do.

How to use UnitOfWorkAwareProxyFactory in Dropwizard v1.1.0

I need to call DAO methods outside resource in dropwizard.
Looking at the manual Im unclear how to use it. The manual says
SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory());
ExampleAuthenticator exampleAuthenticator = new
UnitOfWorkAwareProxyFactory(hibernateBundle)
.create(ExampleAuthenticator.class, SessionDao.class, dao);
Can anyone show me the usage of exampleAuthenticator methods which call DAO.
Thanks, Kedar
Working solution
/** initializing proxy dao for authorization */
AuthenticatorDAOProxy authenticatorDAOProxy = new UnitOfWorkAwareProxyFactory(hibernateBundle)
.create(AuthenticatorDAOProxy.class, DeviceDAO.class, deviceDAO);
We can now use authenticatorDAOProxy outside jersey resources
One thing to note., AuthenticatorDAOProxy should have a constructor accepting DeviceDAO
Now your proxyDao will look like
public class AuthenticatorDAOProxy {
private DeviceDAO deviceDAO;
public AuthenticatorDAOProxy(DeviceDAO deviceDAO) {
this.deviceDAO = deviceDAO;
}
#UnitOfWork
public Boolean checkIsDeviceValid(String deviceId, User user) {
Device device = deviceDAO.getByDeviceIdAndUser(deviceId, user);
if (device != null && device.getIsActive() == true) {
return true;
}
return false;
}
}
Each Dropwizard module have a testsuite. Here is the answer you are looking for: https://github.com/dropwizard/dropwizard/blob/release/1.1.x/dropwizard-hibernate/src/test/java/io/dropwizard/hibernate/UnitOfWorkAwareProxyFactoryTest.java#L121-L151
The logic is:
The DAO object instance holds a reference to the Hibernate SessionFactory;
A method that access the DB calls sessionFactory.getCurrentSession(). The sample code is executing a native query and returns true if at least one result row is returned from the DB;
The OAuthAuthenticator instance holds a reference to the DAO instance and calls the appropriate method of the DAO.
The test case is here: https://github.com/dropwizard/dropwizard/blob/release/1.1.x/dropwizard-hibernate/src/test/java/io/dropwizard/hibernate/UnitOfWorkAwareProxyFactoryTest.java#L64-L74

How to ignore #FeignClient apis from being processed by swagger?

I am using swagger and Feign in one project, and the swagger will take #RequestMapping annotated methods and create the documentation. But this is weird to do so for classes and methods annotated by both #FeightClient and #RequestMapping. So how to ignore these apis in swagger? Which class of swagger
do the scan job so that I could learn and add some other class to ignore these apis annotated by #FeightClient?
#FeignClient(name = TodoItemRpcRepository.SERVICE_NAME)
#RequestMapping("/api/todos")
public interface TodoItemRpcRepository {
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
TodoItem findById(#RequestHeader("X-Auth-Token") final String token, //
#PathVariable("id") final Long id);
}
In your docket select you can specify a predicate. You could use the withClassAnnotation method to specify #FeignClient as the annotation. You'd need to combine it with the Predicates.not to ignore in your case.
I removed the #RequestMapping over the #FeignClient annotated class and add a path attribute in the #FeignClient. This time, issue was resolved perfectly. I guess #RequestMapping is not permitted to #FeignClient annotated class.
#FeignClient(name = TodoItemRpcRepository.SERVICE_NAME, name="/api/todos")
public interface TodoItemRpcRepository {
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
TodoItem findById(#RequestHeader("X-Auth-Token") final String token, //
#PathVariable("id") final Long id);
}

StructureMap dynamic set properties per HttpRequest

StructureMap Configuration
Is there a way in SM to dynamically inject property value only for the duration of a request then set the those property back to default after the request is completed?
I'm specifically referring in the HttpRequest context.
I have a IDBAccessor interface and a DBAccessor concrete implementation.
IDBAccessor has a public property for connection string.
I want to set the connectionstring dynamically for each HttpRequest depending on some parameter that is passed in.
Is there an easy to accomplish this?
Thanks for the input.
I assume you have a class that encapsulates the logic to determine the connection string for each request. I'll call it ConnectionStringSource. You could then configure StructureMap like this:
ObjectFactory.Initialize(x =>
{
x.For<IDBAccessor>().HybridHttpOrThreadLocalScoped()
.Use(ctx =>
{
var connectionString = ctx.GetInstance<ConnectionStringSource>().GetConnectionString();
var dbAccessor = new DBAccessor {ConnectionString = connectionString};
return dbAccessor;
});
});
public class ConnectionStringSource
{
public string GetConnectionString()
{
// determine the connection string somehow
return "connection string";
}
}
The HybridHttpOrThreadLocalScoped call will make sure you get a new instance of DBAccessor for each HTTP request. And by using the Func<> overload of Use(), you can execute the code to determine and set the connection string during each request.
Note: You might want to just make the connection string a constructor parameter of DBAccessor instead of making it a property on the interface.

Resources