55 */
66package org .elasticsearch .xpack .security ;
77
8+ import org .apache .logging .log4j .LogManager ;
89import org .apache .logging .log4j .Logger ;
910import org .apache .lucene .util .SetOnce ;
1011import org .elasticsearch .Version ;
2728import org .elasticsearch .common .inject .Module ;
2829import org .elasticsearch .common .inject .util .Providers ;
2930import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
31+ import org .elasticsearch .common .logging .DeprecationLogger ;
3032import org .elasticsearch .common .logging .LoggerMessageFormat ;
31- import org .elasticsearch .common .logging .Loggers ;
3233import org .elasticsearch .common .network .NetworkModule ;
3334import org .elasticsearch .common .network .NetworkService ;
3435import org .elasticsearch .common .regex .Regex ;
221222import org .joda .time .DateTimeZone ;
222223
223224import java .io .IOException ;
225+ import java .io .InputStream ;
226+ import java .io .UncheckedIOException ;
224227import java .nio .charset .StandardCharsets ;
228+ import java .nio .file .Files ;
225229import java .nio .file .Path ;
226230import java .time .Clock ;
227231import java .util .ArrayList ;
253257public class Security extends Plugin implements ActionPlugin , IngestPlugin , NetworkPlugin , ClusterPlugin , DiscoveryPlugin , MapperPlugin ,
254258 ExtensiblePlugin {
255259
256- private static final Logger logger = Loggers .getLogger (Security .class );
260+ private static final Logger LOGGER = LogManager .getLogger (Security .class );
261+ private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger (LOGGER );
257262
258263 public static final String NAME4 = XPackField .SECURITY + "4" ;
259264 public static final Setting <Optional <String >> USER_SETTING =
@@ -535,7 +540,7 @@ private AuthenticationFailureHandler createAuthenticationFailureHandler(final Re
535540 extensionName = extension .toString ();
536541 }
537542 if (failureHandler == null ) {
538- logger .debug ("Using default authentication failure handler" );
543+ LOGGER .debug ("Using default authentication failure handler" );
539544 final Map <String , List <String >> defaultFailureResponseHeaders = new HashMap <>();
540545 realms .asList ().stream ().forEach ((realm ) -> {
541546 Map <String , List <String >> realmFailureHeaders = realm .getAuthenticationFailureHeaders ();
@@ -556,7 +561,7 @@ private AuthenticationFailureHandler createAuthenticationFailureHandler(final Re
556561 }
557562 failureHandler = new DefaultAuthenticationFailureHandler (defaultFailureResponseHeaders );
558563 } else {
559- logger .debug ("Using authentication failure handler from extension [" + extensionName + "]" );
564+ LOGGER .debug ("Using authentication failure handler from extension [" + extensionName + "]" );
560565 }
561566 return failureHandler ;
562567 }
@@ -949,7 +954,7 @@ static void validateAutoCreateIndex(Settings settings) {
949954 }
950955 }
951956
952- logger .warn ("the [action.auto_create_index] setting is configured to be restrictive [{}]. " +
957+ LOGGER .warn ("the [action.auto_create_index] setting is configured to be restrictive [{}]. " +
953958 " for the next 6 months audit indices are allowed to be created, but please make sure" +
954959 " that any future history indices after 6 months with the pattern " +
955960 "[.security_audit_log*] are allowed to be created" , value );
@@ -1039,7 +1044,7 @@ public UnaryOperator<Map<String, IndexTemplateMetaData>> getIndexTemplateMetaDat
10391044 templates .put (SECURITY_TEMPLATE_NAME , IndexTemplateMetaData .Builder .fromXContent (parser , SECURITY_TEMPLATE_NAME ));
10401045 } catch (IOException e ) {
10411046 // TODO: should we handle this with a thrown exception?
1042- logger .error ("Error loading template [{}] as part of metadata upgrading" , SECURITY_TEMPLATE_NAME );
1047+ LOGGER .error ("Error loading template [{}] as part of metadata upgrading" , SECURITY_TEMPLATE_NAME );
10431048 }
10441049
10451050 final byte [] auditTemplate = TemplateUtils .loadTemplate ("/" + IndexAuditTrail .INDEX_TEMPLATE_NAME + ".json" ,
@@ -1049,12 +1054,12 @@ public UnaryOperator<Map<String, IndexTemplateMetaData>> getIndexTemplateMetaDat
10491054 .createParser (NamedXContentRegistry .EMPTY , LoggingDeprecationHandler .INSTANCE , auditTemplate )) {
10501055 IndexTemplateMetaData auditMetadata = new IndexTemplateMetaData .Builder (
10511056 IndexTemplateMetaData .Builder .fromXContent (parser , IndexAuditTrail .INDEX_TEMPLATE_NAME ))
1052- .settings (IndexAuditTrail .customAuditIndexSettings (settings , logger ))
1057+ .settings (IndexAuditTrail .customAuditIndexSettings (settings , LOGGER ))
10531058 .build ();
10541059 templates .put (IndexAuditTrail .INDEX_TEMPLATE_NAME , auditMetadata );
10551060 } catch (IOException e ) {
10561061 // TODO: should we handle this with a thrown exception?
1057- logger .error ("Error loading template [{}] as part of metadata upgrading" , IndexAuditTrail .INDEX_TEMPLATE_NAME );
1062+ LOGGER .error ("Error loading template [{}] as part of metadata upgrading" , IndexAuditTrail .INDEX_TEMPLATE_NAME );
10581063 }
10591064
10601065 return templates ;
@@ -1167,4 +1172,55 @@ public void accept(DiscoveryNode node, ClusterState state) {
11671172 public void reloadSPI (ClassLoader loader ) {
11681173 securityExtensions .addAll (SecurityExtension .loadExtensions (loader ));
11691174 }
1175+
1176+ public static Path resolveConfigFile (Environment env , String name ) {
1177+ final Path config = env .configFile ().resolve (name );
1178+ final Path legacyConfig = env .configFile ().resolve ("x-pack" ).resolve (name );
1179+ // config and legacy config can be the same path if name is an absolute path
1180+ if (config .equals (legacyConfig ) == false ) {
1181+ final boolean configFileExists = Files .exists (config );
1182+ final boolean legacyConfigExists = Files .exists (legacyConfig );
1183+ if (configFileExists == false ) {
1184+ if (legacyConfigExists ) {
1185+ DEPRECATION_LOGGER .deprecated ("Config file [" + name + "] is in a deprecated location. Move from " +
1186+ legacyConfig .toString () + " to " + config .toString ());
1187+ return legacyConfig ;
1188+ }
1189+ } else if (legacyConfigExists ) {
1190+ // there is a file in both locations
1191+ if (isDefaultFile (name , config )) {
1192+ // use the legacy file as the new file is the default but warn user
1193+ DEPRECATION_LOGGER .deprecated ("Config file [" + name + "] exists in a deprecated location and non-deprecated " +
1194+ "location. The file in the non-deprecated location is the default file. Using file found in the deprecated " +
1195+ "location. Move " + legacyConfig .toString () + " to " + config .toString ());
1196+ return legacyConfig ;
1197+ } else {
1198+ // the regular file has been modified, but the old still exists, warn the user
1199+ DEPRECATION_LOGGER .deprecated ("Config file [" + name + "] exists in a deprecated location and non-deprecated " +
1200+ "location. Using file found in the non-deprecated location [" + config .toString () + "]. Determine which file " +
1201+ "should be kept and move it to " + config .toString () + ", then remove " + legacyConfig .toString ());
1202+ }
1203+ }
1204+ }
1205+ return config ;
1206+ }
1207+
1208+ static boolean isDefaultFile (String name , Path file ) {
1209+ try (InputStream in = XPackPlugin .class .getResourceAsStream ("/config/" + name )) {
1210+ if (in != null ) {
1211+ try (InputStream fin = Files .newInputStream (file )) {
1212+ int inValue = in .read ();
1213+ int finValue = fin .read ();
1214+ while (inValue != -1 && finValue != -1 && inValue == finValue ) {
1215+ inValue = in .read ();
1216+ finValue = fin .read ();
1217+ }
1218+ return inValue == finValue ;
1219+ }
1220+ }
1221+ } catch (IOException e ) {
1222+ throw new UncheckedIOException (e );
1223+ }
1224+ return false ;
1225+ }
11701226}
0 commit comments