-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix unique realm name check to cover default realms #87999
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
Merged
elasticsearchmachine
merged 5 commits into
elastic:master
from
ywangd:actually-no-duplicate-realm-names
Jun 24, 2022
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
664354f
Fix unique realm names check to cover default realms
ywangd ede3bd0
Update docs/changelog/87999.yaml
ywangd 5f61832
Merge branch 'master' into actually-no-duplicate-realm-names
elasticmachine 71bbf5a
remove obsolete test
ywangd 466583a
Update x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/s…
ywangd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 87999 | ||
| summary: Fix unique realm name check to cover default realms | ||
| area: Authentication | ||
| type: bug | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import org.elasticsearch.common.util.CollectionUtils; | ||
| import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
| import org.elasticsearch.common.util.set.Sets; | ||
| import org.elasticsearch.core.Tuple; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.env.TestEnvironment; | ||
| import org.elasticsearch.license.License; | ||
|
|
@@ -125,16 +126,7 @@ public void init() throws Exception { | |
|
|
||
| threadContext = new ThreadContext(Settings.EMPTY); | ||
|
|
||
| ThreadPool threadPool = mock(ThreadPool.class); | ||
| when(threadPool.getThreadContext()).thenReturn(threadContext); | ||
|
|
||
| reservedRealm = new ReservedRealm( | ||
| mock(Environment.class), | ||
| Settings.EMPTY, | ||
| mock(NativeUsersStore.class), | ||
| new AnonymousUser(Settings.EMPTY), | ||
| threadPool | ||
| ); | ||
| reservedRealm = buildReservedRealm(); | ||
| allowAllRealms(); | ||
| } | ||
|
|
||
|
|
@@ -408,27 +400,6 @@ public void testRealmAssignedToMultipleDomainsNotPermitted() { | |
| ); | ||
| } | ||
|
|
||
| public void testReservedRealmIsAlwaysDomainless() throws Exception { | ||
| int realmId = randomIntBetween(0, randomRealmTypesCount - 1); | ||
| Settings settings = Settings.builder() | ||
| .put("xpack.security.authc.realms.type_" + realmId + ".reserved.order", 2) | ||
| .put("xpack.security.authc.domains.domain_reserved.realms", "reserved") | ||
| .put("xpack.security.authc.realms." + NativeRealmSettings.TYPE + ".disabled_native.enabled", false) | ||
| .put("xpack.security.authc.realms." + FileRealmSettings.TYPE + ".disabled_file.enabled", false) | ||
| .put("path.home", createTempDir()) | ||
| .build(); | ||
| Environment env = TestEnvironment.newEnvironment(settings); | ||
| Realms realms = new Realms(settings, env, factories, licenseState, threadContext, reservedRealm); | ||
| Iterator<Realm> iterator = realms.iterator(); | ||
| assertThat(iterator.hasNext(), is(true)); | ||
| Realm realm = iterator.next(); | ||
| assertThat(realm, is(reservedRealm)); | ||
| realm = iterator.next(); | ||
| assertThat(realm.type(), is("type_" + realmId)); | ||
| assertThat(realm.name(), is("reserved")); | ||
| assertThat(realm.realmRef().getDomain().name(), is("domain_reserved")); | ||
| } | ||
|
|
||
| public void testDomainWithUndefinedRealms() { | ||
| Settings settings = Settings.builder() | ||
| .put("xpack.security.authc.domains.test_domain.realms", "reserved, default_file") | ||
|
|
@@ -1097,6 +1068,147 @@ public void testWarningsForReservedPrefixedRealmNames() throws Exception { | |
| ); | ||
| } | ||
|
|
||
| public void testExplicitlyConfiguredRealmNamesMustNotClashWithDefaultRealmNames() throws Exception { | ||
| final Tuple<String, String> defaultRealm = randomFrom( | ||
| new Tuple<>("reserved", "reserved"), | ||
| new Tuple<>("file", "default_file"), | ||
| new Tuple<>("native", "default_native") | ||
| ); | ||
|
|
||
| final String configuredRealmType = randomValueOtherThan(defaultRealm.v1(), () -> randomAlphaOfLengthBetween(3, 18)); | ||
| factories.put(configuredRealmType, DummyRealm::new); | ||
| final String realmSettingKey = "xpack.security.authc.realms." + configuredRealmType + "." + defaultRealm.v2(); | ||
|
|
||
| // The name of an explicitly configured realm must not be either default_native, default_file or "reserved" | ||
| final Settings settings1 = Settings.builder() | ||
| .put("path.home", createTempDir()) | ||
| .put(realmSettingKey + ".order", randomInt()) | ||
| .build(); | ||
| final IllegalArgumentException e1 = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> new Realms( | ||
| settings1, | ||
| TestEnvironment.newEnvironment(settings1), | ||
| factories, | ||
| licenseState, | ||
| threadContext, | ||
| buildReservedRealm() | ||
| ) | ||
| ); | ||
| assertThat( | ||
| e1.getMessage(), | ||
| containsString( | ||
| "Found realm configured with name clashing with the [" + defaultRealm.v2() + "] realm: [" + realmSettingKey + "]" | ||
| ) | ||
| ); | ||
|
|
||
| // It is OK if the explicitly configured realm is disabled | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and a few other test scenarios below can be seen as a leniency. Ideally these names should not be used at all. We may want to do something about it, e.g. log warnings. But it is a separate issue from what we are trying to fix here. |
||
| final Settings settings2 = Settings.builder() | ||
| .put("path.home", createTempDir()) | ||
| .put(realmSettingKey + ".order", randomInt()) | ||
| .put(realmSettingKey + ".enabled", false) | ||
| .build(); | ||
| final Realms realms2 = new Realms( | ||
| settings2, | ||
| TestEnvironment.newEnvironment(settings2), | ||
| factories, | ||
| licenseState, | ||
| threadContext, | ||
| buildReservedRealm() | ||
| ); | ||
| assertThat(realms2.getActiveRealms().stream().map(Realm::type).toList(), containsInAnyOrder("reserved", "file", "native")); | ||
|
|
||
| // This block of tests do not apply to reserved realm since it is not configurable | ||
| if ("file".equals(defaultRealm.v1()) || "native".equals(defaultRealm.v1())) { | ||
| final String autoEnabledRealmType = "file".equals(defaultRealm.v1()) ? "native" : "file"; | ||
|
|
||
| // It is OK if the default file or native realm is disabled | ||
| final Settings settings3 = Settings.builder() | ||
| .put("path.home", createTempDir()) | ||
| .put(realmSettingKey + ".order", randomInt()) | ||
| .put("xpack.security.authc.realms." + defaultRealm.v1() + "." + randomAlphaOfLengthBetween(3, 18) + ".enabled", false) | ||
| .build(); | ||
| final Realms realms3 = new Realms( | ||
| settings3, | ||
| TestEnvironment.newEnvironment(settings3), | ||
| factories, | ||
| licenseState, | ||
| threadContext, | ||
| buildReservedRealm() | ||
| ); | ||
| assertThat( | ||
| realms3.getActiveRealms().stream().map(Realm::type).toList(), | ||
| containsInAnyOrder("reserved", configuredRealmType, autoEnabledRealmType) | ||
| ); | ||
|
|
||
| // It is OK if file or native realm is named something else | ||
| final Settings settings4 = Settings.builder() | ||
| .put("path.home", createTempDir()) | ||
| .put(realmSettingKey + ".order", randomInt()) | ||
| .put("xpack.security.authc.realms." + defaultRealm.v1() + "." + randomAlphaOfLengthBetween(3, 18) + ".order", randomInt()) | ||
| .build(); | ||
| final Realms realms4 = new Realms( | ||
| settings4, | ||
| TestEnvironment.newEnvironment(settings4), | ||
| factories, | ||
| licenseState, | ||
| threadContext, | ||
| buildReservedRealm() | ||
| ); | ||
| assertThat( | ||
| realms4.getActiveRealms().stream().map(Realm::type).toList(), | ||
| containsInAnyOrder("reserved", configuredRealmType, "file", "native") | ||
| ); | ||
|
|
||
| // All explicitly configured realm names still must be unique | ||
| final Settings settings5 = Settings.builder() | ||
| .put("path.home", createTempDir()) | ||
| .put(realmSettingKey + ".order", randomInt()) | ||
| .put("xpack.security.authc.realms." + defaultRealm.v1() + "." + defaultRealm.v2() + ".order", randomInt()) | ||
| .build(); | ||
| final IllegalArgumentException e5 = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> new Realms( | ||
| settings5, | ||
| TestEnvironment.newEnvironment(settings5), | ||
| factories, | ||
| licenseState, | ||
| threadContext, | ||
| buildReservedRealm() | ||
| ) | ||
| ); | ||
| assertThat(e5.getMessage(), containsString("Found multiple realms configured with the same name")); | ||
| } | ||
|
|
||
| // It is OK to explicitly configure file to be default_file and native to be default_native | ||
| final Settings settings6 = Settings.builder() | ||
| .put("path.home", createTempDir()) | ||
| .put("xpack.security.authc.realms.file.default_file.order", randomInt()) | ||
| .put("xpack.security.authc.realms.native.default_native.order", randomInt()) | ||
| .build(); | ||
| final Realms realms6 = new Realms( | ||
| settings6, | ||
| TestEnvironment.newEnvironment(settings6), | ||
| factories, | ||
| licenseState, | ||
| threadContext, | ||
| buildReservedRealm() | ||
| ); | ||
| assertThat(realms6.getActiveRealms().stream().map(Realm::type).toList(), containsInAnyOrder("reserved", "file", "native")); | ||
| } | ||
|
|
||
| private ReservedRealm buildReservedRealm() { | ||
| final ThreadPool threadPool = mock(ThreadPool.class); | ||
| when(threadPool.getThreadContext()).thenReturn(threadContext); | ||
| return new ReservedRealm( | ||
| mock(Environment.class), | ||
| Settings.EMPTY, | ||
| mock(NativeUsersStore.class), | ||
| new AnonymousUser(Settings.EMPTY), | ||
| threadPool | ||
| ); | ||
| } | ||
|
|
||
| private boolean randomDisableRealm(Settings.Builder builder, String type) { | ||
| final boolean disabled = randomBoolean(); | ||
| if (disabled) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.