1212import org .elasticsearch .action .ActionListener ;
1313import org .elasticsearch .action .DocWriteRequest ;
1414import org .elasticsearch .action .support .WriteRequest ;
15+ import org .elasticsearch .bootstrap .BootstrapInfo ;
1516import org .elasticsearch .common .settings .SecureString ;
17+ import org .elasticsearch .core .Nullable ;
1618import org .elasticsearch .index .engine .VersionConflictEngineException ;
1719import org .elasticsearch .xpack .core .security .user .ElasticUser ;
1820import org .elasticsearch .xpack .core .security .user .KibanaSystemUser ;
1921import org .elasticsearch .xpack .security .authc .esnative .NativeUsersStore ;
2022import org .elasticsearch .xpack .security .support .SecurityIndexManager ;
2123
24+ import java .io .PrintStream ;
2225import java .util .function .BiConsumer ;
2326
2427import static org .elasticsearch .xpack .security .tool .CommandUtils .generatePassword ;
2528
2629public class GenerateInitialBuiltinUsersPasswordListener implements BiConsumer <SecurityIndexManager .State , SecurityIndexManager .State > {
2730
2831 private static final Logger LOGGER = LogManager .getLogger (GenerateInitialBuiltinUsersPasswordListener .class );
29- private NativeUsersStore nativeUsersStore ;
30- private SecurityIndexManager securityIndexManager ;
32+ private final NativeUsersStore nativeUsersStore ;
33+ private final SecurityIndexManager securityIndexManager ;
3134
3235 public GenerateInitialBuiltinUsersPasswordListener (NativeUsersStore nativeUsersStore , SecurityIndexManager securityIndexManager ) {
3336 this .nativeUsersStore = nativeUsersStore ;
@@ -36,6 +39,13 @@ public GenerateInitialBuiltinUsersPasswordListener(NativeUsersStore nativeUsersS
3639
3740 @ Override
3841 public void accept (SecurityIndexManager .State previousState , SecurityIndexManager .State currentState ) {
42+ final PrintStream out = BootstrapInfo .getOriginalStandardOut ();
43+ // Check if it has been closed, try to write something so that we trigger PrintStream#ensureOpen
44+ out .println ();
45+ if (out .checkError ()) {
46+ outputOnError (null );
47+ return ;
48+ }
3949 if (previousState .equals (SecurityIndexManager .State .UNRECOVERED_STATE )
4050 && currentState .equals (SecurityIndexManager .State .UNRECOVERED_STATE ) == false
4151 && securityIndexManager .indexExists () == false ) {
@@ -57,7 +67,7 @@ public void accept(SecurityIndexManager.State previousState, SecurityIndexManage
5767 WriteRequest .RefreshPolicy .IMMEDIATE ,
5868 ActionListener .wrap (
5969 r -> {
60- outputOnSuccess (elasticPassword , kibanaSystemPassword );
70+ outputOnSuccess (elasticPassword , kibanaSystemPassword , out );
6171 }, this ::outputOnError
6272 )
6373 );
@@ -66,55 +76,46 @@ public void accept(SecurityIndexManager.State previousState, SecurityIndexManage
6676 }
6777 }
6878
69- private void outputOnSuccess (SecureString elasticPassword , SecureString kibanaSystemPassword ) {
70- LOGGER .info ("" );
71- LOGGER .info ("-----------------------------------------------------------------" );
72- LOGGER .info ("" );
73- LOGGER .info ("" );
74- LOGGER .info ("" );
75- LOGGER .info ("Password for the elastic user is: " + elasticPassword );
76- LOGGER .info ("" );
77- LOGGER .info ("" );
78- LOGGER .info ("" );
79- LOGGER .info ("Password for the kibana_system user is: " + kibanaSystemPassword );
80- LOGGER .info ("" );
81- LOGGER .info ("" );
82- LOGGER .info ("Please note these down as they will not be shown again." );
83- LOGGER .info ("" );
84- LOGGER .info ("You can use 'bin/elasticsearch-reset-elastic-password' at any time" );
85- LOGGER .info ("in order to reset the password for the elastic user." );
86- LOGGER .info ("" );
87- LOGGER .info ("" );
88- LOGGER .info ("You can use 'bin/elasticsearch-reset-kibana-system-password' at any time" );
89- LOGGER .info ("in order to reset the password for the kibana_system user." );
90- LOGGER .info ("" );
91- LOGGER .info ("" );
92- LOGGER .info ("" );
93- LOGGER .info ("-----------------------------------------------------------------" );
94- LOGGER .info ("" );
79+ private void outputOnSuccess (SecureString elasticPassword , SecureString kibanaSystemPassword , PrintStream out ) {
80+ out .println ();
81+ out .println ("-----------------------------------------------------------------" );
82+ out .println ();
83+ out .println ("Password for the elastic user is: " + elasticPassword );
84+ out .println ();
85+ out .println ("Password for the kibana_system user is: " + kibanaSystemPassword );
86+ out .println ();
87+ out .println ("Please note these down as they will not be shown again." );
88+ out .println ();
89+ out .println ();
90+ out .println ("You can use 'bin/elasticsearch-reset-elastic-password' at any time" );
91+ out .println ("in order to reset the password for the elastic user." );
92+ out .println ();
93+ out .println ("You can use 'bin/elasticsearch-reset-kibana-system-password' at any time" );
94+ out .println ("in order to reset the password for the kibana_system user." );
95+ out .println ();
96+ out .println ("-----------------------------------------------------------------" );
97+ out .println ();
9598 }
9699
97- private void outputOnError (Exception e ) {
100+ private void outputOnError (@ Nullable Exception e ) {
98101 if (e instanceof VersionConflictEngineException == false ) {
99102 LOGGER .info ("" );
100103 LOGGER .info ("-----------------------------------------------------------------" );
101104 LOGGER .info ("" );
102- LOGGER .info ("" );
103- LOGGER .info ("" );
104- LOGGER .info ("Failed to set the password for the elastic and kibana-system users " );
105- LOGGER .info ("automatically" );
105+ LOGGER .info ("Unable set the password for the elastic and kibana_system users " );
106+ LOGGER .info ("automatically." );
106107 LOGGER .info ("" );
107108 LOGGER .info ("You can use 'bin/elasticsearch-reset-elastic-password'" );
108109 LOGGER .info ("in order to set the password for the elastic user." );
109110 LOGGER .info ("" );
110- LOGGER .info ("" );
111111 LOGGER .info ("You can use 'bin/elasticsearch-reset-kibana-system-password'" );
112112 LOGGER .info ("in order to set the password for the kibana_system user." );
113113 LOGGER .info ("" );
114- LOGGER .info ("" );
115- LOGGER .info ("" );
116114 LOGGER .info ("-----------------------------------------------------------------" );
117115 LOGGER .info ("" );
118116 }
117+ if (null != e ) {
118+ LOGGER .warn ("Error initializing passwords for elastic and kibana_system users" , e );
119+ }
119120 }
120121}
0 commit comments