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

#842 AuthenticationOptions in GlobalConfiguration #1215

Conversation

jlukawska
Copy link
Contributor

@jlukawska jlukawska commented Apr 28, 2020

Closes #842

Proposed Changes

It is possible to configure AuthenticationOptions in GlobalConfiguration. Then all routes use these settings. To configure an exception to this rule (e.g. for identity service), AllowAnonymousForGlobalAuthenticationOptions property should be set to true in the route AuthenticationOptions.

If a route has its own AuthenticationOptions with AuthenticationProviderKey configured additionally, it has priority over the global one.

At the moment if a route uses a global AuthenticationProviderKey (when AuthenticationProviderKey is not configured for route explicitly), it uses also global AllowedScopes, even if AllowedScopes is configured for the route additionally.

@rathga
Copy link

rathga commented Jul 16, 2020

+1 to please add/merge this feature.

Question: does your implementation allow for a given route to NOT have authentication? Like an 'allow anonymous' setting. In my project this would be needed for my identity service.

@jlukawska
Copy link
Contributor Author

@rathga, it should be quite easy to add this option. I'll try to update the pull request soon.

Thanks for the feedback!

@jlukawska jlukawska marked this pull request as draft July 17, 2020 11:19
@jlukawska jlukawska marked this pull request as ready for review July 22, 2020 11:20
@raman-m raman-m changed the title Feature/842 AuthenticationOptions in GlobalConfiguration #842 AuthenticationOptions in GlobalConfiguration Jul 19, 2023
@raman-m raman-m changed the base branch from master to develop July 19, 2023 20:03
@raman-m raman-m added the conflicts Feature branch has merge conflicts label Jul 19, 2023
@raman-m raman-m force-pushed the feature/842-authenticationoptions-in-globalconfiguration branch from 6e53f98 to c1afca2 Compare July 27, 2023 18:21
@raman-m raman-m added feature A new feature needs feedback Issue is waiting on feedback before acceptance and removed conflicts Feature branch has merge conflicts labels Jul 27, 2023
@raman-m
Copy link
Member

raman-m commented Jul 27, 2023

@jlukawska Hi J!
Thanks for the great PR!
Wow! I am impressed. 😍

What I've done:

  • Feature branch has been rebased onto ThreeMammals:develop to remove all merge commits and introduce top commits from target
  • Merge conflicts have been resolved during rebasing. That was quite difficult, but done
  • Fixed errors. There was some ambiguity in resolutions, not an issue now
  • Fixed some errors, warning and messages
  • Made green build finally

Now we can start code review...

@raman-m
Copy link
Member

raman-m commented Jul 27, 2023

@rathga commented on Jul 16, 2020

Hey, Richard!
It was a long story, but I hope, we will get this feature delivered soon... 😄

Could you join to code review please?
Your opinion is highly prioritized.

Also, check please,
Is your small feature request implemented?

Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

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

@RaynaldM @wast
Wow! 2 approvals from you, guys, without issues, really? 😉 👇
I am impressed! 😄


I see 2 major issues now:

  • Unit & acceptance tests, and their methods have a lot of copied & pasted code snippets. The fact-methods are long. But they could be shorter.
  • Some lines are too long. It is hard to read.

I would appreciate if PR author will fix them.

@@ -42,6 +42,63 @@ When Ocelot runs it will look at this Routes AuthenticationOptions.Authenticatio

If a Route is authenticated Ocelot will invoke whatever scheme is associated with it while executing the authentication middleware. If the request fails authentication Ocelot returns a http status code 401.

If you want to configure AuthenticationOptions the same for all Routes, do it in GlobalConfiguration the same way as for Route. If there are AuthenticationOptions configured both for GlobalConfiguration and Route (AuthenticationProviderKey is set), the Route section has priority. If you want to exclude route from global AuthenticationOptions, you can do that by setting AllowAnonymousForGlobalAuthenticationOptions to true in the route AuthenticationOptions - then this route will not be authenticated.
Copy link
Member

Choose a reason for hiding this comment

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

It is better to write a new subsection of the chapter like:

Global Authentication
^^^^^^^^^^^^^^^^^^^^^

before this line.

And this line is too long! Splitting phrases should help.

Comment on lines +8 to +9
public AuthenticationOptions Create(FileAuthenticationOptions routeAuthOptions,
FileAuthenticationOptions globalConfAuthOptions)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public AuthenticationOptions Create(FileAuthenticationOptions routeAuthOptions,
FileAuthenticationOptions globalConfAuthOptions)
public AuthenticationOptions Create(
FileAuthenticationOptions routeAuthOptions,
FileAuthenticationOptions globalConfAuthOptions)

Or, Don't make new line at all. Having 2-3 parameters in signature on the same line is totally fine.

public List<string> AllowedScopes { get; set; }

/// <summary>
/// Allows anonymous authentication globally.
Copy link
Member

Choose a reason for hiding this comment

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

It allows per route actually.

/// <value>
/// <see langword="true"/> if it is allowed; otherwise, <see langword="false"/>.
/// </value>
public bool AllowAnonymousForGlobalAuthenticationOptions { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

The name could be shorter, like that: AllowAnonymousAuthentication or even AllowAnonymous.


RuleFor(authOptions => authOptions.AuthenticationProviderKey)
.MustAsync(IsSupportedAuthenticationProviders)
.WithMessage("{PropertyName}: {PropertyValue} is unsupported authentication provider");
Copy link
Member

Choose a reason for hiding this comment

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

What placeholders are here?
Are they implicit? Where is definition of these variables?

Comment on lines +113 to +120
var route = new FileRoute
{
AuthenticationOptions = new FileAuthenticationOptions()
{
AuthenticationProviderKey = "routeKey",
AllowedScopes = new List<string>() { "routeScope1", "routeScope2" },
},
};
var globalConfig = new FileGlobalConfiguration
{
AuthenticationOptions = new FileAuthenticationOptions()
{
AuthenticationProviderKey = "globalKey",
AllowedScopes = new List<string>() { "globalScope1", "globalScope2" },
},
};
Copy link
Member

Choose a reason for hiding this comment

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

Could you define a helper to construct FileRoute object plz?
The goal is writing less code.

Copy link
Member

Choose a reason for hiding this comment

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

I believe that all new facts could call new defined helpers.
So, too many copy-pasted code snippets!

var provider = new ServiceCollection()
.BuildServiceProvider();

// Todo - replace with mocks
_configurationValidator = new FileConfigurationFluentValidator(provider, new RouteFluentValidator(_authProvider.Object, new HostAndPortValidator(), new FileQoSOptionsFluentValidator(provider)), new FileGlobalConfigurationFluentValidator(new FileQoSOptionsFluentValidator(provider)));
_configurationValidator = new FileConfigurationFluentValidator(provider, new RouteFluentValidator(new HostAndPortValidator(), new FileQoSOptionsFluentValidator(provider), _fileAuthenticationOptionsValidator), new FileGlobalConfigurationFluentValidator(new FileQoSOptionsFluentValidator(provider), _fileAuthenticationOptionsValidator));
Copy link
Member

Choose a reason for hiding this comment

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

Too long line!
It is hard to read the code...
Could you apply new line vs indentation approach to make code readability better plz?

@@ -1554,7 +1598,7 @@ private void GivenAServiceDiscoveryHandler()
ServiceDiscoveryFinderDelegate del = (a, b, c) => new FakeServiceDiscoveryProvider();
collection.AddSingleton<ServiceDiscoveryFinderDelegate>(del);
var provider = collection.BuildServiceProvider();
_configurationValidator = new FileConfigurationFluentValidator(provider, new RouteFluentValidator(_authProvider.Object, new HostAndPortValidator(), new FileQoSOptionsFluentValidator(provider)), new FileGlobalConfigurationFluentValidator(new FileQoSOptionsFluentValidator(provider)));
_configurationValidator = new FileConfigurationFluentValidator(provider, new RouteFluentValidator(new HostAndPortValidator(), new FileQoSOptionsFluentValidator(provider), _fileAuthenticationOptionsValidator), new FileGlobalConfigurationFluentValidator(new FileQoSOptionsFluentValidator(provider), _fileAuthenticationOptionsValidator));
Copy link
Member

Choose a reason for hiding this comment

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

Too long lines 1592 and 1601 !

Comment on lines +30 to +34
private static bool IsAuthenticated(FileRoute fileRoute, FileGlobalConfiguration globalConfiguration)
{
return (!string.IsNullOrEmpty(globalConfiguration?.AuthenticationOptions?.AuthenticationProviderKey) &&
!fileRoute.AuthenticationOptions.AllowAnonymousForGlobalAuthenticationOptions) ||
!string.IsNullOrEmpty(fileRoute.AuthenticationOptions?.AuthenticationProviderKey);
}
Copy link
Member

Choose a reason for hiding this comment

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

Because the body consists of one operator only, why not to define via expression body 👇

Suggested change
private static bool IsAuthenticated(FileRoute fileRoute, FileGlobalConfiguration globalConfiguration)
{
return (!string.IsNullOrEmpty(globalConfiguration?.AuthenticationOptions?.AuthenticationProviderKey) &&
!fileRoute.AuthenticationOptions.AllowAnonymousForGlobalAuthenticationOptions) ||
!string.IsNullOrEmpty(fileRoute.AuthenticationOptions?.AuthenticationProviderKey);
}
private static bool IsAuthenticated(FileRoute fileRoute, FileGlobalConfiguration globalConfiguration)
=> (!string.IsNullOrEmpty(globalConfiguration?.AuthenticationOptions?.AuthenticationProviderKey)
&& !fileRoute.AuthenticationOptions.AllowAnonymousForGlobalAuthenticationOptions)
|| !string.IsNullOrEmpty(fileRoute.AuthenticationOptions?.AuthenticationProviderKey);

@raman-m raman-m force-pushed the feature/842-authenticationoptions-in-globalconfiguration branch from 19d5e00 to c99d512 Compare October 12, 2023 12:47
@wudiqiang2024
Copy link

wudiqiang2024 commented Apr 11, 2024

I need this change,What time releases this PR?

@raman-m

@raman-m raman-m added low Low priority and removed needs feedback Issue is waiting on feedback before acceptance labels Apr 11, 2024
@raman-m
Copy link
Member

raman-m commented Apr 11, 2024

@wudiqiang2024 commented on Apr 11

Sorry for delaying the delivery! We have a lack of dev time!
This PR has low priority. We have another more important PRs in Annual 2023 milestone.
Having possibility to define in global section is just beneficial option and not required at the moment.
Please try to merge all routes as Catch All ones and define AuthenticationOptions for those Catch All routes.
How many downstream services you have? How many routes are defined in ocelot.json?
Do you consider Consul usage?

@jlukawska
Copy link
Contributor Author

Closed due to creation of a new PR: #2114

@jlukawska jlukawska closed this Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature low Low priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New AuthenticationOptions property in global configuration
6 participants