Skip to content

Commit 81374fa

Browse files
authored
Do not stash environment in security (#54372)
Today the security plugin stashes a copy of the environment in its constructor, and uses the stashed copy to construct its components even though it is provided with an environment to create these components. What is more, the environment it creates in its constructor is not fully initialized, as it does not have the final copy of the settings, but the environment passed in while creating components does. This commit removes that stashed copy of the environment.
1 parent 3595195 commit 81374fa

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ public class Security extends Plugin implements SystemIndexPlugin, IngestPlugin,
283283
private static final Logger logger = LogManager.getLogger(Security.class);
284284

285285
private final Settings settings;
286-
private final Environment env;
287286
private final boolean enabled;
288287
/* what a PITA that we need an extra indirection to initialize this. Yet, once we got rid of guice we can thing about how
289288
* to fix this or make it simpler. Today we need several service that are created in createComponents but we need to register
@@ -311,7 +310,6 @@ public Security(Settings settings, final Path configPath) {
311310
// TODO This is wrong. Settings can change after this. We should use the settings from createComponents
312311
this.settings = settings;
313312
// TODO this is wrong, we should only use the environment that is provided to createComponents
314-
this.env = new Environment(settings, configPath);
315313
this.enabled = XPackSettings.SECURITY_ENABLED.get(settings);
316314
if (enabled) {
317315
runStartupChecks(settings);
@@ -348,7 +346,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
348346
IndexNameExpressionResolver expressionResolver) {
349347
try {
350348
return createComponents(client, threadPool, clusterService, resourceWatcherService, scriptService, xContentRegistry,
351-
expressionResolver);
349+
environment, expressionResolver);
352350
} catch (final Exception e) {
353351
throw new IllegalStateException("security initialization failed", e);
354352
}
@@ -357,7 +355,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
357355
// pkg private for testing - tests want to pass in their set of extensions hence we are not using the extension service directly
358356
Collection<Object> createComponents(Client client, ThreadPool threadPool, ClusterService clusterService,
359357
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
360-
NamedXContentRegistry xContentRegistry,
358+
NamedXContentRegistry xContentRegistry, Environment environment,
361359
IndexNameExpressionResolver expressionResolver) throws Exception {
362360
if (enabled == false) {
363361
return Collections.singletonList(new SecurityUsageServices(null, null, null, null));
@@ -371,7 +369,7 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste
371369
new TokenSSLBootstrapCheck(),
372370
new PkiRealmBootstrapCheck(getSslService()),
373371
new TLSLicenseBootstrapCheck()));
374-
checks.addAll(InternalRealms.getBootstrapChecks(settings, env));
372+
checks.addAll(InternalRealms.getBootstrapChecks(settings, environment));
375373
this.bootstrapChecks.set(Collections.unmodifiableList(checks));
376374

377375
threadContext.set(threadPool.getThreadContext());
@@ -399,9 +397,9 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste
399397
final NativeRoleMappingStore nativeRoleMappingStore = new NativeRoleMappingStore(settings, client, securityIndex.get(),
400398
scriptService);
401399
final AnonymousUser anonymousUser = new AnonymousUser(settings);
402-
final ReservedRealm reservedRealm = new ReservedRealm(env, settings, nativeUsersStore,
400+
final ReservedRealm reservedRealm = new ReservedRealm(environment, settings, nativeUsersStore,
403401
anonymousUser, securityIndex.get(), threadPool);
404-
final SecurityExtension.SecurityComponents extensionComponents = new ExtensionComponents(env, client, clusterService,
402+
final SecurityExtension.SecurityComponents extensionComponents = new ExtensionComponents(environment, client, clusterService,
405403
resourceWatcherService, nativeRoleMappingStore);
406404
Map<String, Realm.Factory> realmFactories = new HashMap<>(InternalRealms.getFactories(threadPool, resourceWatcherService,
407405
getSslService(), nativeUsersStore, nativeRoleMappingStore, securityIndex.get()));
@@ -413,7 +411,8 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste
413411
}
414412
}
415413
}
416-
final Realms realms = new Realms(settings, env, realmFactories, getLicenseState(), threadPool.getThreadContext(), reservedRealm);
414+
final Realms realms =
415+
new Realms(settings, environment, realmFactories, getLicenseState(), threadPool.getThreadContext(), reservedRealm);
417416
components.add(nativeUsersStore);
418417
components.add(nativeRoleMappingStore);
419418
components.add(realms);
@@ -426,7 +425,7 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste
426425

427426
dlsBitsetCache.set(new DocumentSubsetBitsetCache(settings, threadPool));
428427
final FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(settings);
429-
final FileRolesStore fileRolesStore = new FileRolesStore(settings, env, resourceWatcherService, getLicenseState(),
428+
final FileRolesStore fileRolesStore = new FileRolesStore(settings, environment, resourceWatcherService, getLicenseState(),
430429
xContentRegistry);
431430
final NativeRolesStore nativeRolesStore = new NativeRolesStore(settings, client, getLicenseState(), securityIndex.get());
432431
final ReservedRolesStore reservedRolesStore = new ReservedRolesStore();

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected SSLService getSslService() {
130130
when(client.threadPool()).thenReturn(threadPool);
131131
when(client.settings()).thenReturn(settings);
132132
return security.createComponents(client, threadPool, clusterService, mock(ResourceWatcherService.class), mock(ScriptService.class),
133-
xContentRegistry(), new IndexNameExpressionResolver());
133+
xContentRegistry(), env, new IndexNameExpressionResolver());
134134
}
135135

136136
private static <T> T findComponent(Class<T> type, Collection<Object> components) {

0 commit comments

Comments
 (0)