Skip to content

Conversation

@jkakavas
Copy link
Contributor

@jkakavas jkakavas commented Sep 8, 2021

In exceptional cases, there is a need for the ES process to print to
the user's "console" without the output appearing in log files.
An example is sensitive information such as the initial password for
an administrative user.

In these cases we would like to print to System.out instead of using
log4j.

However, we intentionally redirect stdout to go a log4j logger,
because that is the preferred place to capture the sorts of messages
that are typically printed to System.out

This change introduces a stashed reference to the original stdout
PrintWriter before we redirect to log4g in BootstrapInfo, that
can be used in said cases.
It also updates the relevant code that was printing the sensitive
information to the log, to use this newly introduced reference.

Supersedes: #77299

Co-authored-by: Tim Vernum [email protected]

In exceptional cases, there is a need for the ES process to print to
the user's "console" without the output appearing in log files.
An example is sensitive information such as the initial password for
an administrative user.

In these cases we would like to print to System.out instead of using
log4j.

However, we intentionally redirect stdout to go a log4j logger,
because that is the preferred place to capture the sorts of messages
that are typically printed to System.out

This change introduces a stashed reference to the original stdout
PrintWriter before we redirect to log4g in BootstrapInfo, that
can be used in said cases.
It also updates the relevant code that was printing the sensitive
information to the log, to use this newly introduced reference.
@jkakavas jkakavas added >enhancement :Core/Infra/Core Core issues without another label v8.0.0 labels Sep 8, 2021
@jkakavas jkakavas requested review from rjernst and tvernum September 8, 2021 19:43
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Sep 8, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@Override
public void accept(SecurityIndexManager.State previousState, SecurityIndexManager.State currentState) {
final PrintStream out = BootstrapInfo.getOriginalStandardOut();
if (null == out) {
Copy link
Member

@rjernst rjernst Sep 8, 2021

Choose a reason for hiding this comment

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

How is this case possible? We could add a not null assertion when setting in BootstrapInfo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. There should be nothing else calling BootstrapInfo#init and there is no setter for originalStandardOut so I think we are ok ?

LOGGER.info("-----------------------------------------------------------------");
LOGGER.info("");
private void outputOnSuccess(SecureString elasticPassword, SecureString kibanaSystemPassword, PrintStream out) {
out.println();
Copy link
Member

Choose a reason for hiding this comment

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

The stream could still have been closed, in which case we are not attached to a terminal. So we need to catch that case, and probably log a warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'll check this first before even generating the passwords. If we can't show them, there is no need to even generate them

@Override
public void accept(SecurityIndexManager.State previousState, SecurityIndexManager.State currentState) {
final PrintStream out = BootstrapInfo.getOriginalStandardOut();
// Check if it has been closed, try to write something so that we trigger PrintStream#ensureOpen
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if there is any other way to check if stdout is closed

@jkakavas jkakavas requested a review from rjernst September 8, 2021 20:51
@jkakavas
Copy link
Contributor Author

jkakavas commented Sep 8, 2021

@elasticmachine update branch

@jkakavas
Copy link
Contributor Author

jkakavas commented Sep 8, 2021

If both of you are happy with this, @tvernum you can merge this at will


/**
* This method is invoked by {@link Elasticsearch#main(String[])} to startup elasticsearch.
*/
Copy link
Member

Choose a reason for hiding this comment

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

We should isolate this, otherwise other forbidden uses can creep into bootstrap init.

// Check if it has been closed, try to write something so that we trigger PrintStream#ensureOpen
out.println();
if (out.checkError()) {
outputOnError(new IllegalStateException("Stashed standard output stream is closed."));
Copy link
Member

Choose a reason for hiding this comment

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

Is this really an illegal state? It just means that we’ve closed the streams, which is normal when not attached to a console.

@jkakavas
Copy link
Contributor Author

jkakavas commented Sep 9, 2021

@elasticmachine update branch

@jkakavas jkakavas requested a review from rjernst September 9, 2021 16:09
@jkakavas
Copy link
Contributor Author

jkakavas commented Sep 9, 2021

Not sure how that mix up-happened, I'm going to force push to bring this to a merge-able state again :(

Turned out to be a Github UI hiccup, so I just ended up re-running a full CI round for no reason 🎉

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Core Core issues without another label >enhancement Team:Core/Infra Meta label for core/infra team v8.0.0-alpha2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants