Introduce traceId to freemarker attributes #34435#47338
Introduce traceId to freemarker attributes #34435#47338sonOfRa wants to merge 1 commit intokeycloak:mainfrom
Conversation
a9dc5b2 to
3c383ed
Compare
|
Thank you for this PR. As usual, we need PRs to link to an issue. I found issue #44090 to link it, so consider this resolved. If we want people to use it in their custom templates, it would need to be documented on the tracing page and the the page to customize themes, and maybe the error template should have a commented-out section that people could enable when they want to show that information. In addition, it would be great to have a test for that - and it would be so much easier if it would be showing automatically on the error page. So I'd say I'm +1 for item (2), which is adding it by default to the error page. Once that's done, TracingDistTest might a please for a test? Let's also hear from others what they think. |
|
One more comment: The trace ID might be considered user provided content, and we should ensure not to encode malicious content. Given that it is "32-character lowercase hexadecimal string representing a 16-byte value", I'd assume it is safe to reflect it to user. Happy to hear from other if they think differently. |
|
Thanks for linking it to one of the issues, I'm used to an issue number being part of a commit message automatically linking an issue, so I forgot that that's necessary :)
maybe it would be a good idea to introduce a realm attribute (or a top level config flag?) along the lines of
Personally, I've always assumed that the opentelemetry integration would reject inbound trace IDs that are not valid in some way. I've not personally verified it, but everything else just seems wrong, and I feel like I would have heard about CVEs with malicious trace IDs, if that was not the case. What remains is the potential DoS vector: If you accept trace IDs from the public internet without filtering, people can just always send 00000000000000000000000000000000 as a trace ID and confuse the tracing system. But that is, at least to me, outside the scope of this PR Any opinion to also add the traceId to the freemarker context for emails? I don't really see a usecase for it to be included in any emails that Keycloak sends by default, but others may have use cases for their own custom email templates |
3c383ed to
03b7569
Compare
03b7569 to
3967c58
Compare
|
I've added a small message template to I did not add an additional flag to guard it, so for now, when tracing is enabled, and the theme is not changed, the message will always be displayed every time error.ftl is rendered and a valid trace context is available. I'm not sure if this is something that is wanted - if users don't want this part of the default template in their themes, they can always override it and not include the message in their theme. I've also added a short paragraph on the tracing.adoc on how it can be used. Do you think it should be additionally also mentioned explicitly somewhere in themes.adoc? |
ahus1
left a comment
There was a problem hiding this comment.
I'm ok to make this not configurable in the realm settings, but leaving it to be a theme configuration as IMHO it is ok for Keycloak to be reasonably opinionated.
See below for a suggested change to the docs.
| == Including trace information in Freemarker templates | ||
|
|
||
| When tracing is enabled, you can include trace information in Freemarker templates by using the `traceId` variable. | ||
| By default, the {project_name} base theme includes the trace ID in the `error.ftl` template. | ||
| With the trace ID included in the error page, you can encourage your users to report the trace ID when they encounter an error, which can help you quickly identify and investigate the issue in your tracing system. | ||
|
|
There was a problem hiding this comment.
There is the subtle problem that not all traces are sampled. Still the information will be helpful for the log files. See a revised version below - please review for functional correctness, as well as grammar, clarity and typos.
| == Including trace information in Freemarker templates | |
| When tracing is enabled, you can include trace information in Freemarker templates by using the `traceId` variable. | |
| By default, the {project_name} base theme includes the trace ID in the `error.ftl` template. | |
| With the trace ID included in the error page, you can encourage your users to report the trace ID when they encounter an error, which can help you quickly identify and investigate the issue in your tracing system. | |
| == Including the trace ID in user login pages | |
| When tracing is enabled, you can include the trace ID in the Freemarker templates of the login theme by using the `traceId` variable. | |
| By default, the base login theme includes the trace ID in the `error.ftl` template. | |
| With the trace ID included in the error page, users can report it when they encounter an error, which can help you quickly identify and investigate additional recorded information for that operation. | |
| You will then find the trace ID in all logged messages for this request, and in your tracing system if this trace was sampled. | |
There was a problem hiding this comment.
I've applied the suggestion locally and rebased everything on top of main again
3967c58 to
735d381
Compare
Signed-off-by: Simon Levermann <github@simon.slevermann.de>
735d381 to
660f80e
Compare
This introduces a
traceIdattribute in the common freemarker attributes.This allows any freemarker template to include the traceId. This is especially useful for error.ftl or custom error-templates, as it allows Keycloak admins to include a message like "When contacting support, please include this identifier in your message: TRACEID_HERE".
Possible additions to this PR:
FreemarkerEmailTemplateProvider#processTemplate, so emails can include the trace as wellerror.ftlin the base themeCloses #44090
Closes #34435