-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Retain reference to stdout for exceptional cases #77299
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
Conversation
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 preffed place to capture the sorts of messages that are typically printed to System.out This commit adds a new "ElasticsearchConsole" that retains a reference to the original value of System.out and can be used for the very rare cases where code should write to the console rather than the log file. The ElasticsearchConsole is only initialised if Elasticsearch is started in the foreground, without the --quiet CLI parameter
|
Pinging @elastic/es-core-infra (Team:Core/Infra) |
|
@rjernst I think this is the sort of thing you had in mind when discussing moving the printing of the initial I didn't include any tests because it really doesn't do anything that can be reasonably tests, and it will get real tests once it's used within security (but I wanted to factor it out into its own PR so it can be reviewed on its own) |
rjernst
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of reusing Terminal as we do in CLIs. It doesnt look like the logic about closing streams has changed, we would still have them open in the desired case because ES would be in the foreground. Am I missing something as to why we need another console wrapper?
|
My assumption was that we wouldn't want everything that terminal does (e.g. reading input) to end up mixed into non-CLI code and potentially need to be disentangled later. I can make sure The other part of my concern is that Tomorrow, I'll update my PR to
|
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 preffed place to capture the sorts of messages that are typically printed to System.out This commit makes two changes: 1. The default Terminal instance is loaded during bootstrap so that it can make use of the original stdout (System.out) stream before that stream is redirect to the log file 2. Adds a new field/method on BootstrapInfo to record whether the Terminal is useful. When daemonized, or in quiet mode, Elasticsearch closes the original output stream, so writing to the Terminal will fail. The new isTerminalOutputAvailable method tracks whether the terminal output is usable.
You are right! I take back my suggestion, we should not use Terminal outside of cli.
I wonder if we could do this without any additional state. We know that we want to write only the case that a console exists (ie interactive). Can we just grab |
I tried this out ( in foreground, not quiet) and I get |
|
foreground + not quiet doesn't guarantee a non-null console, but I believe the non-null console is what we ultimately want. It means that we are definitely attached to a terminal and not redirected to a file. |
|
I think (without tracking it down) that we have a null console because stdin is reading from a heredoc (for keystore password) |
I've now confirmed this - the method by which we pass the keystore password cause stdin to not be the user's terminal, and that means
I suppose we could try and come up with a different method to pass in the keystore password, but it seems like a lot of work for not much value - nothing we're trying to do here cares about have console input, we just want to know if we have a reliable output stream to the console. |
|
Fair enough, thanks for tracking that down. If all we care about is being able to write to System.out, then could we catch the exceptional case (as I mentioned in a previous comment) when trying to write to System.out? |
We can, provided we can retain a copy of A combination of Terminal works (as long as it's loaded before the stream gets redirected) but if we don't want to use Terminal, we'll need something else. |
|
Ok, how about we use BootstrapInfo to stash the original |
|
Replaced by #77460 |
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 preffed place to capture the sorts of messages
that are typically printed to System.out
This commit makes two changes:
The default Terminal instance is loaded during bootstrap so that
it can make use of the original stdout (System.out) stream before
that stream is redirect to the log file
Adds a new field/method on BootstrapInfo to record whether the
Terminal is useful. When daemonized, or in quiet mode,
Elasticsearch closes the original output stream, so writing to the
Terminal will fail. The new isTerminalOutputAvailable method tracks
whether the terminal output is usable.