2727import org .elasticsearch .xpack .security .authc .esnative .ReservedRealm ;
2828import org .junit .Before ;
2929
30+ import java .io .IOException ;
3031import java .util .ArrayList ;
3132import java .util .Collections ;
3233import java .util .HashMap ;
@@ -51,13 +52,16 @@ public class RealmsTests extends ESTestCase {
5152 private XPackLicenseState licenseState ;
5253 private ThreadContext threadContext ;
5354 private ReservedRealm reservedRealm ;
55+ private int randomRealmTypesCount ;
5456
5557 @ Before
5658 public void init () throws Exception {
5759 factories = new HashMap <>();
5860 factories .put (FileRealmSettings .TYPE , config -> new DummyRealm (FileRealmSettings .TYPE , config ));
5961 factories .put (NativeRealmSettings .TYPE , config -> new DummyRealm (NativeRealmSettings .TYPE , config ));
60- for (int i = 0 ; i < randomIntBetween (1 , 5 ); i ++) {
62+ factories .put (KerberosRealmSettings .TYPE , config -> new DummyRealm (KerberosRealmSettings .TYPE , config ));
63+ randomRealmTypesCount = randomIntBetween (1 , 5 );
64+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
6165 String name = "type_" + i ;
6266 factories .put (name , config -> new DummyRealm (name , config ));
6367 }
@@ -73,13 +77,13 @@ public void init() throws Exception {
7377 public void testWithSettings () throws Exception {
7478 Settings .Builder builder = Settings .builder ()
7579 .put ("path.home" , createTempDir ());
76- List <Integer > orders = new ArrayList <>(factories . size () - 2 );
77- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
80+ List <Integer > orders = new ArrayList <>(randomRealmTypesCount );
81+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
7882 orders .add (i );
7983 }
8084 Collections .shuffle (orders , random ());
8185 Map <Integer , Integer > orderToIndex = new HashMap <>();
82- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
86+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
8387 builder .put ("xpack.security.authc.realms.realm_" + i + ".type" , "type_" + i );
8488 builder .put ("xpack.security.authc.realms.realm_" + i + ".order" , orders .get (i ));
8589 orderToIndex .put (orders .get (i ), i );
@@ -107,14 +111,14 @@ public void testWithSettings() throws Exception {
107111 public void testWithSettingsWhereDifferentRealmsHaveSameOrder () throws Exception {
108112 Settings .Builder builder = Settings .builder ()
109113 .put ("path.home" , createTempDir ());
110- List <Integer > randomSeq = new ArrayList <>(factories . size () - 2 );
111- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
114+ List <Integer > randomSeq = new ArrayList <>(randomRealmTypesCount );
115+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
112116 randomSeq .add (i );
113117 }
114118 Collections .shuffle (randomSeq , random ());
115119
116120 TreeMap <String , Integer > nameToRealmId = new TreeMap <>();
117- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
121+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
118122 int randomizedRealmId = randomSeq .get (i );
119123 String randomizedRealmName = randomAlphaOfLengthBetween (12 ,32 );
120124 nameToRealmId .put ("realm_" + randomizedRealmName , randomizedRealmId );
@@ -181,13 +185,13 @@ public void testWithEmptySettings() throws Exception {
181185 public void testUnlicensedWithOnlyCustomRealms () throws Exception {
182186 Settings .Builder builder = Settings .builder ()
183187 .put ("path.home" , createTempDir ());
184- List <Integer > orders = new ArrayList <>(factories . size () - 2 );
185- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
188+ List <Integer > orders = new ArrayList <>(randomRealmTypesCount );
189+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
186190 orders .add (i );
187191 }
188192 Collections .shuffle (orders , random ());
189193 Map <Integer , Integer > orderToIndex = new HashMap <>();
190- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
194+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
191195 builder .put ("xpack.security.authc.realms.realm_" + i + ".type" , "type_" + i );
192196 builder .put ("xpack.security.authc.realms.realm_" + i + ".order" , orders .get (i ));
193197 orderToIndex .put (orders .get (i ), i );
@@ -384,13 +388,13 @@ public void testUnlicensedWithNonStandardRealms() throws Exception {
384388 public void testDisabledRealmsAreNotAdded () throws Exception {
385389 Settings .Builder builder = Settings .builder ()
386390 .put ("path.home" , createTempDir ());
387- List <Integer > orders = new ArrayList <>(factories . size () - 2 );
388- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
391+ List <Integer > orders = new ArrayList <>(randomRealmTypesCount );
392+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
389393 orders .add (i );
390394 }
391395 Collections .shuffle (orders , random ());
392396 Map <Integer , Integer > orderToIndex = new HashMap <>();
393- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
397+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
394398 builder .put ("xpack.security.authc.realms.realm_" + i + ".type" , "type_" + i );
395399 builder .put ("xpack.security.authc.realms.realm_" + i + ".order" , orders .get (i ));
396400 boolean enabled = randomBoolean ();
@@ -520,6 +524,20 @@ public void testUsageStats() throws Exception {
520524 }
521525 }
522526
527+ public void testInitRealmsFailsForMultipleKerberosRealms () throws IOException {
528+ final Settings .Builder builder = Settings .builder ().put ("path.home" , createTempDir ());
529+ builder .put ("xpack.security.authc.realms.realm_1.type" , "kerberos" );
530+ builder .put ("xpack.security.authc.realms.realm_1.order" , 1 );
531+ builder .put ("xpack.security.authc.realms.realm_2.type" , "kerberos" );
532+ builder .put ("xpack.security.authc.realms.realm_2.order" , 2 );
533+ final Settings settings = builder .build ();
534+ Environment env = TestEnvironment .newEnvironment (settings );
535+ final IllegalArgumentException iae = expectThrows (IllegalArgumentException .class ,
536+ () -> new Realms (settings , env , factories , licenseState , threadContext , reservedRealm ));
537+ assertThat (iae .getMessage (), is (equalTo (
538+ "multiple realms [realm_1, realm_2] configured of type [kerberos], [kerberos] can only have one such realm configured" )));
539+ }
540+
523541 static class DummyRealm extends Realm {
524542
525543 DummyRealm (String type , RealmConfig config ) {
0 commit comments