Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

logging, added tests, fixed code only flow. #202

Closed
wants to merge 15 commits into from
Closed

logging, added tests, fixed code only flow. #202

wants to merge 15 commits into from

Conversation

brentschmaltz
Copy link
Contributor

still lots of additional testing to come.

renamed id's, spelling
_verbose controls test output
@ghost ghost added the cla-not-required label Mar 31, 2015

if (logLevelDebug)
{
if (logLevelDebug)
Copy link
Member

Choose a reason for hiding this comment

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

Duplicated debug check?

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 will fix that.

@HaoK
Copy link
Member

HaoK commented Mar 31, 2015

yay for tests, only major thing worth considering is removing all of the log level guards for readability, as its really not needed given how straight forward your log message generation is.

@brentschmaltz
Copy link
Contributor Author

This is just the beginning for tests. I was trying to figure out a pattern.

About the annoying external log level check, it's a perf thing, stops the context switch. I plan on lots and lots of logging.

Can you live with it?

Sent from my Windows Phone


From: Hao Kungmailto:notifications@github.com
Sent: ‎3/‎31/‎2015 10:44 AM
To: aspnet/Securitymailto:Security@noreply.github.com
Cc: BrentSchmaltzmailto:brentschmaltz@hotmail.com
Subject: Re: [Security] logging, added tests, fixed code only flow. (#202)

yay for tests, only major thing worth considering is removing all of the log level guards for readability, as its really not needed given how straight forward your log message generation is.


Reply to this email directly or view it on GitHub:
#202 (comment)

@kevinchalet
Copy link
Contributor

@brentschmaltz honestly, these changes make the code far harder to read (it's really a PITA 😄)

If you're particularly concerned about performance, why not using the logger extensions that take a format string and multiple arguments instead of manually calling string.Format?

https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Framework.Logging.Interfaces/LoggerExtensions.cs#L52

BTW, @GrabYourPitchforks has done great things with string interpolation in aspnet/DataProtection, and it really makes logging clearer: aspnet/DataProtection@4365b53 😄

@GrabYourPitchforks
Copy link
Contributor

@PinpointTownes Thanks for the kind words! Unfortunately one of the downsides to what I did in DataProtection is that logging messages are hardcoded literal strings and thus are non-localized. The code Brent submitted here is more future-proof once localization work is performed.

Calling the logger extensions that take format strings is certainly a good idea. Not only is performance improved, it also gives logging sinks a crack at the raw values without requiring them to parse the string manually.

@rynowak
Copy link
Member

rynowak commented Apr 1, 2015

re: logging - the state of the art is like this: https://github.com/aspnet/Routing/pull/172/files#diff-4997dfe8056272b794ccb3bcdfe214d9R117

Our logging framework understands the key-values - so that loggers that work with key-value-pairs can have them, and loggers that don't still get a new message. /cc @loudej in case he wants to comment.

@kevinchalet
Copy link
Contributor

@GrabYourPitchforks interesting, I didn't know that the ASP.NET team wanted to localize tracing messages 😄
Do you know whether tracing components will have their own culture or simply use CultureInfo.CurrentCulture?

I wonder if an i18n + string interpolation mix wouldn't be the best of both worlds: you'd simply call logger.LogWarning($"Request did not match the route '{RouteName}' with template '{RouteTemplate}'.") and it would internally retrieve the localized format string corresponding to "Request did not match the route '{0}' with template '{1}'." and call FormattableString.ToString() if needed.

@HaoK
Copy link
Member

HaoK commented Apr 1, 2015 via email

@GrabYourPitchforks
Copy link
Contributor

Just to clarify, the existing ASP.NET runtime and other .NET Framework components localize log messages using the system installed culture, not the invoking thread's culture. That way sysadmins always see log messages in their own language. Presumably this will be the behavior in the future, but it's just not ready at this time.

@brentschmaltz
Copy link
Contributor Author

I always use CurrentCulture for the localization reason, just a habit. I actually like to use string.cat as it is much faster than string.format. If you are not convinced, run some numbers on loops of: string.Format vs.
str1 + str2.

About the 'loglevel' check, I favor that even though it's a bit messy, macros remember them made that style neater, I want to minimize the perf hit.

…ts.cs.

Added some comments on the 'public' handler and middleware types.
@Eilon
Copy link
Contributor

Eilon commented Apr 2, 2015

BTW log level guidance is available here: https://github.com/aspnet/Logging/wiki/Guidelines#log-levels

Regarding string concatenation, that doesn't work with resource strings and so should never be used for anything that goes in a resx file (regardless of whether it's ultimately localized).

And regarding the culture, there's no matter of taste or style, it's a correctness issue, per what @GrabYourPitchforks mentioned.

@brentschmaltz
Copy link
Contributor Author

clarifying commnet on what @GrabYourPitchforks said, using String.Format(CultureInfo.CurrentCulture, ...) is correct or NOT correct?

@Eilon
Copy link
Contributor

Eilon commented Apr 6, 2015

CurrentCulture is incorrect. I think you want InvariantCulture.

@brentschmaltz
Copy link
Contributor Author

Yes, it is not a style thing but a correctness thing. I have always used InvariantCulture when the data was not presented to the user and CurrentCulture when the data was displayed to the user, such as a log entry. Are you saying that is not correct and we should always use InvariantCulture?

@Tratcher
Copy link
Member

Tratcher commented Apr 6, 2015

The problem is that there are two users, the remote client and the local admin. CurrentCulture represents the remote client, correct?

@brentschmaltz
Copy link
Contributor Author

Welcome back Tratcher :-)

I thought CurrentCulture represents the culture of the current thread. For logs, I am tailoring them for the dev who uses them for debugging. I know GrabYourPitchforks spoke about the system admin, but I was leaning towards the dev.
If we use InvariantCulture, we are essentially using English culture for all logs when they are localized.

@Eilon
Copy link
Contributor

Eilon commented Apr 7, 2015

Right, the CurrentCulture will be set to whatever the person using the site/service/API wanted. I.e. someone accessing an API in France could conceivably get French messages. But the developer should always get a single culture. Currently we've said that's the InvariantCulture, but in the future we might want a way for the developer to configure in which culture they want to see all their log messages.

@brentschmaltz
Copy link
Contributor Author

OK, do you want to introduce extensibility for 'culture' now or just go with InvariantCulture.
If we go later, we can always set the default to InvariantCulture.

@Eilon
Copy link
Contributor

Eilon commented Apr 7, 2015

Right, the dev doing debugging certainly doesn't want to use the culture of the person visiting the site.

I wouldn't do anything special here: just use InvariantCulture.

@kevinchalet
Copy link
Contributor

I wouldn't do anything special here: just use InvariantCulture.

Or better, don't use string.Format at all and let aspnet/Logging handle it internally? 😄

@brentschmaltz
Copy link
Contributor Author

@PinpointTownes yep, I am going to investigate just that, let the logger do the string format. Then we just need to modify the logger.

@Eilon
Copy link
Contributor

Eilon commented Apr 7, 2015

Oh right yeah that's best. Do as little work as possible here, let the logging system do it.

/// </summary>
public string SignInScheme
{
// [brentschmaltz] - it is not clear to me why we need both AuthenticationScheme and SignInScheme.
// and, why this maps to AuthenticationType.
get { return TokenValidationParameters.AuthenticationType; }
Copy link
Contributor

Choose a reason for hiding this comment

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

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 am not ignoring this, but there are some other issues in the runtime that when addressed I am hoping will make this moot.

@brentschmaltz
Copy link
Contributor Author

@Eilon @HaoK @PinpointTownes We have spent a lot of time on style and related items but not bugs, I include a 'codestyle' project in Wilson so that developers can easily confirm to Wilsons style. Can we do the same here?

@Eilon
Copy link
Contributor

Eilon commented Apr 9, 2015

@brentschmaltz unfortunately StyleCop doesn't support C# 6. We worked on trying to add support for it but it was too complex.

@brentschmaltz
Copy link
Contributor Author

@HaoK @Eilon @PinpointTownes I would like to merge this in, any pushback?

@kevinchalet
Copy link
Contributor

@brentschmaltz not particularly. I'll clone and play with the final bits in the next hours 🎮

Response.StatusCode = 403;
return;
}

if (Response.StatusCode != 401)
{
_logger.LogDebug(Resources.OIDCH_0028_StatusCodeNot401, Response.StatusCode.ToString());
Copy link
Member

Choose a reason for hiding this comment

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

Why are you doing .ToString everywhere?

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 do that to ensure a string is passed in the case another API shows up that takes the type.

Copy link
Contributor

Choose a reason for hiding this comment

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

It will cause the data type to change (obviously), which would affect loggers that store strongly-typed data. E.g. some loggers will store int's as int's, which would allow for richer querying of logs (e.g. select * from logs where log.statusCode >= 400 && log.statusCode < 500).

Copy link
Contributor

Choose a reason for hiding this comment

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

(As such it's not strictly wrong, but its making the data potentially less useful in some logging systems.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is good reasoning, I will change it.

@brentschmaltz
Copy link
Contributor Author

@HaoK good, then who can merge it in?
I am going to explore some additional changes in test. I will post a new PR.

Updated tests
Fixed resource string
Conflicts:
	src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationMiddleware.cs
	src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs
}
}

public class CustomConfigureOptions : ConfigureOptions<OpenIdConnectAuthenticationOptions>
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need a custom configure options that doesn't do anything different than the base?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good Question. Extensibility.
I want to make sure that my class is called.
Currently, name isn't used, future tests will start making use of the name.

Backed out change
Added adjustment for 'ExtenalAuthenticationOptions'
/// responsible of persisting user's identity after a successful authentication.
/// This value typically corresponds to a cookie middleware registered in the Startup class.
/// When omitted, <see cref="ExternalAuthenticationOptions.SignInScheme"/> is used as a fallback value.
/// Gets or sets the name of another authentication middleware which will be responsible for actually issuing a user <see cref="System.Security.Claims.ClaimsIdentity"/>.
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, why does this PR contain already merged commits? 😢

/cc @HaoK

Copy link
Member

Choose a reason for hiding this comment

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

Rebasing messes up PRs, there are ways to fix it (force push)

Copy link
Member

Choose a reason for hiding this comment

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

Another option is to just create a new PR and branch with squashed changes for final review

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm definitely not a git expert, but it seems that @brentschmaltz did a git merge instead of a git rebase, which causes these weird issues: https://github.com/brentschmaltz/Security/commit/efaef8952a09c4ec0681db5e0a3c28cb1d96270f

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 am also not a git expert, I did a 'git pull' and git went off on its own.
I will tidy it up guys.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@PinpointTownes @HaoK it looks like when I did the pull, it hit a merge conflict. The updated versions are showing as 'unstagged'. I will commit them and push to my fork.

@brentschmaltz
Copy link
Contributor Author

@HaoK Is there anything blocking this pull request?

@HaoK
Copy link
Member

HaoK commented Apr 15, 2015

I don't think there's anything particularly blocking no

@brentschmaltz
Copy link
Contributor Author

@HaoK Can you merge it? I don't think I have the ability to execute the merge.

@HaoK
Copy link
Member

HaoK commented Apr 16, 2015

You've checked in changes before, so you must have permission. Just you will want to squash all your changes together before you push to dev (when you merge this branch, just do git merge --squash), and then it should just be one change that you push to dev

@brentschmaltz
Copy link
Contributor Author

I till try, I had pushed to Katana, but never aspnet.

@HaoK
Copy link
Member

HaoK commented Apr 16, 2015

Merged 501bd4f

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants