27
27
import org .elasticsearch .xpack .security .authc .esnative .ReservedRealm ;
28
28
import org .junit .Before ;
29
29
30
+ import java .io .IOException ;
30
31
import java .util .ArrayList ;
31
32
import java .util .Collections ;
32
33
import java .util .HashMap ;
@@ -51,13 +52,16 @@ public class RealmsTests extends ESTestCase {
51
52
private XPackLicenseState licenseState ;
52
53
private ThreadContext threadContext ;
53
54
private ReservedRealm reservedRealm ;
55
+ private int randomRealmTypesCount ;
54
56
55
57
@ Before
56
58
public void init () throws Exception {
57
59
factories = new HashMap <>();
58
60
factories .put (FileRealmSettings .TYPE , config -> new DummyRealm (FileRealmSettings .TYPE , config ));
59
61
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 ++) {
61
65
String name = "type_" + i ;
62
66
factories .put (name , config -> new DummyRealm (name , config ));
63
67
}
@@ -73,13 +77,13 @@ public void init() throws Exception {
73
77
public void testWithSettings () throws Exception {
74
78
Settings .Builder builder = Settings .builder ()
75
79
.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 ++) {
78
82
orders .add (i );
79
83
}
80
84
Collections .shuffle (orders , random ());
81
85
Map <Integer , Integer > orderToIndex = new HashMap <>();
82
- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
86
+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
83
87
builder .put ("xpack.security.authc.realms.realm_" + i + ".type" , "type_" + i );
84
88
builder .put ("xpack.security.authc.realms.realm_" + i + ".order" , orders .get (i ));
85
89
orderToIndex .put (orders .get (i ), i );
@@ -107,14 +111,14 @@ public void testWithSettings() throws Exception {
107
111
public void testWithSettingsWhereDifferentRealmsHaveSameOrder () throws Exception {
108
112
Settings .Builder builder = Settings .builder ()
109
113
.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 ++) {
112
116
randomSeq .add (i );
113
117
}
114
118
Collections .shuffle (randomSeq , random ());
115
119
116
120
TreeMap <String , Integer > nameToRealmId = new TreeMap <>();
117
- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
121
+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
118
122
int randomizedRealmId = randomSeq .get (i );
119
123
String randomizedRealmName = randomAlphaOfLengthBetween (12 ,32 );
120
124
nameToRealmId .put ("realm_" + randomizedRealmName , randomizedRealmId );
@@ -181,13 +185,13 @@ public void testWithEmptySettings() throws Exception {
181
185
public void testUnlicensedWithOnlyCustomRealms () throws Exception {
182
186
Settings .Builder builder = Settings .builder ()
183
187
.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 ++) {
186
190
orders .add (i );
187
191
}
188
192
Collections .shuffle (orders , random ());
189
193
Map <Integer , Integer > orderToIndex = new HashMap <>();
190
- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
194
+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
191
195
builder .put ("xpack.security.authc.realms.realm_" + i + ".type" , "type_" + i );
192
196
builder .put ("xpack.security.authc.realms.realm_" + i + ".order" , orders .get (i ));
193
197
orderToIndex .put (orders .get (i ), i );
@@ -384,13 +388,13 @@ public void testUnlicensedWithNonStandardRealms() throws Exception {
384
388
public void testDisabledRealmsAreNotAdded () throws Exception {
385
389
Settings .Builder builder = Settings .builder ()
386
390
.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 ++) {
389
393
orders .add (i );
390
394
}
391
395
Collections .shuffle (orders , random ());
392
396
Map <Integer , Integer > orderToIndex = new HashMap <>();
393
- for (int i = 0 ; i < factories . size () - 2 ; i ++) {
397
+ for (int i = 0 ; i < randomRealmTypesCount ; i ++) {
394
398
builder .put ("xpack.security.authc.realms.realm_" + i + ".type" , "type_" + i );
395
399
builder .put ("xpack.security.authc.realms.realm_" + i + ".order" , orders .get (i ));
396
400
boolean enabled = randomBoolean ();
@@ -520,6 +524,20 @@ public void testUsageStats() throws Exception {
520
524
}
521
525
}
522
526
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
+
523
541
static class DummyRealm extends Realm {
524
542
525
543
DummyRealm (String type , RealmConfig config ) {
0 commit comments