Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MCC-732241] Fix caching with authentication when using utility extension method #71

Merged
merged 10 commits into from
Feb 17, 2021

Conversation

lschreck-mdsol
Copy link
Contributor

@lschreck-mdsol lschreck-mdsol commented Feb 16, 2021

Background

Although MAuth provides a variety of extensions to use with external applications (ASP.NET Core middleware, ASP.NET WebAPI, OWIN etc.) there are cases when authentication is required in a way that's not supported by the library. For those cases the library provides utility extension methods to call externally (like the UtilityExtensions.Authenticate() method).

The Issue

Under the hood the library is using an internal class called MAuthAuthenticator which sets up its own memory cache. As long as this class is used, caching will work. The utility method is using this class.
The issue is that the above mentioned utility method has to instantiate this class for every call (as it's a static method therefore its options will be passed with every call), therefore a new memory cache will be also instantiated for every call effectively disabling the caching.

The Solution

For the MAuthAuthenticator instances, we are providing an optional constructor argument to set the cache externally. This will enable the utility extension to reuse its own cache therefore it won't recreate again and agin.

@mdsol/libraries_dotnet @mdsol/architecture-enablement Please review and merge - thanks!

@@ -13,15 +13,15 @@ namespace Medidata.MAuth.Core
internal class MAuthAuthenticator
{
private readonly MAuthOptionsBase _options;
private readonly IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
private readonly IMemoryCache _cache;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we declare this as

private static readonly IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());

here instead of in UtilityExtensions?
Having a variable in an extension method class feels a bit weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that looks like a better solution! Thanks, let me update the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to unresolve this, can we have a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted back to my original solution because having the static cache in the MauthAuthenticator will screw up the unit tests.

@h-kurniawan
Copy link

Should we take a look at pika pika, find out which apps/services use 5.1.0 and urge the team to upgrade to 5.1.1?
If the app/service is a busy one, the memory usage can go high quite a bit which may cause the ECS instance to crash.

@lschreck-mdsol
Copy link
Contributor Author

@hkurniawan-mdsol This only applies for cases where the relevant framework extension does not exist (i.e. where the utility extension must be used).

So far I only know about PB using this utility so I don't think this is an urgent patch.

@h-kurniawan
Copy link

@lschreck-mdsol

So far I only know about PB using this utility so I don't think this is an urgent patch.

Okay cool. If we're sure that only PB is affected then we're fine then.

@jfeltesse-mdsol
Copy link
Contributor

Just a friendly reminder: this repository is public.

@h-kurniawan
Copy link

Just a friendly reminder: this repository is public.

Okay maybe we do need to urge the public to update then.

@jfeltesse-mdsol
Copy link
Contributor

@hkurniawan-mdsol

dfa7a0bc6f7c3d2f5d29ffdd0ced99fb

prajon84
prajon84 previously approved these changes Feb 16, 2021
@prajon84
Copy link
Contributor

prajon84 commented Feb 16, 2021

@lschreck-mdsol : I see version is bumped, but there is no update on changelog. Missed that ?

Also, this unit test is failing too.
AuthenticateRequest_AfterNumberOfAttempts_WillThrowExceptionWithRequestFailure(policy: Agressive) [FAIL]

@prajon84 prajon84 dismissed their stale review February 16, 2021 14:17

Found one unit test is failing.

@lschreck-mdsol
Copy link
Contributor Author

@prajon84 Thanks, you are right I forgot to update the changelog (but now it's done: 33f59f6).

I am checking the unit test now.

@h-kurniawan
Copy link

Can we have a preview version released for testing?
@psangha-mdsol is working on both load testing and memory consumption story right now, it'd be good if he can prove that the changes work.

@bvillanueva-mdsol bvillanueva-mdsol merged commit 87ba132 into develop Feb 17, 2021
@bvillanueva-mdsol bvillanueva-mdsol deleted the MCC-732241 branch February 17, 2021 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants