Skip to content

Commit f628495

Browse files
bizybotYogesh Gaikwad
authored and
Yogesh Gaikwad
committed
[Kerberos] Remove Kerberos bootstrap checks (#32451)
This commit removes Kerberos bootstrap checks as they were more validation checks and better done in Kerberos realm constructor than as bootstrap checks. This also moves the check for one Kerberos realm per node to where we initialize realms. This commit adds few validations which were missing earlier like missing read permissions on keytab file or if it is directory to throw exception with error message.
1 parent 7bc61f8 commit f628495

File tree

7 files changed

+106
-199
lines changed

7 files changed

+106
-199
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@
178178
import org.elasticsearch.xpack.security.authc.TokenService;
179179
import org.elasticsearch.xpack.security.authc.esnative.NativeUsersStore;
180180
import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm;
181-
import org.elasticsearch.xpack.security.authc.kerberos.KerberosRealmBootstrapCheck;
182181
import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore;
183182
import org.elasticsearch.xpack.security.authz.AuthorizationService;
184183
import org.elasticsearch.xpack.security.authz.SecuritySearchOperationListener;
@@ -315,8 +314,7 @@ public Security(Settings settings, final Path configPath) {
315314
new PasswordHashingAlgorithmBootstrapCheck(),
316315
new FIPS140SecureSettingsBootstrapCheck(settings, env),
317316
new FIPS140JKSKeystoreBootstrapCheck(settings),
318-
new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings),
319-
new KerberosRealmBootstrapCheck(env)));
317+
new FIPS140PasswordHashingAlgorithmBootstrapCheck(settings)));
320318
checks.addAll(InternalRealms.getBootstrapChecks(settings, env));
321319
this.bootstrapChecks = Collections.unmodifiableList(checks);
322320
Automatons.updateMaxDeterminizedStates(settings);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.xpack.core.security.authc.esnative.NativeRealmSettings;
3636
import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm;
3737
import org.elasticsearch.xpack.core.security.authc.file.FileRealmSettings;
38+
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings;
3839

3940

4041
/**
@@ -152,6 +153,7 @@ protected List<Realm> initRealms() throws Exception {
152153
Settings realmsSettings = RealmSettings.get(settings);
153154
Set<String> internalTypes = new HashSet<>();
154155
List<Realm> realms = new ArrayList<>();
156+
List<String> kerberosRealmNames = new ArrayList<>();
155157
for (String name : realmsSettings.names()) {
156158
Settings realmSettings = realmsSettings.getAsSettings(name);
157159
String type = realmSettings.get("type");
@@ -178,6 +180,13 @@ protected List<Realm> initRealms() throws Exception {
178180
}
179181
internalTypes.add(type);
180182
}
183+
if (KerberosRealmSettings.TYPE.equals(type)) {
184+
kerberosRealmNames.add(name);
185+
if (kerberosRealmNames.size() > 1) {
186+
throw new IllegalArgumentException("multiple realms " + kerberosRealmNames.toString() + " configured of type [" + type
187+
+ "], [" + type + "] can only have one such realm configured");
188+
}
189+
}
181190
realms.add(factory.create(config));
182191
}
183192

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealm.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore;
2626
import org.ietf.jgss.GSSException;
2727

28+
import java.nio.file.Files;
2829
import java.nio.file.Path;
2930
import java.util.Collections;
3031
import java.util.List;
@@ -87,6 +88,16 @@ public KerberosRealm(final RealmConfig config, final NativeRoleMappingStore nati
8788
this.kerberosTicketValidator = kerberosTicketValidator;
8889
this.threadPool = threadPool;
8990
this.keytabPath = config.env().configFile().resolve(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.get(config.settings()));
91+
92+
if (Files.exists(keytabPath) == false) {
93+
throw new IllegalArgumentException("configured service key tab file [" + keytabPath + "] does not exist");
94+
}
95+
if (Files.isDirectory(keytabPath)) {
96+
throw new IllegalArgumentException("configured service key tab file [" + keytabPath + "] is a directory");
97+
}
98+
if (Files.isReadable(keytabPath) == false) {
99+
throw new IllegalArgumentException("configured service key tab file [" + keytabPath + "] must have read permission");
100+
}
90101
this.enableKerberosDebug = KerberosRealmSettings.SETTING_KRB_DEBUG_ENABLE.get(config.settings());
91102
this.removeRealmName = KerberosRealmSettings.SETTING_REMOVE_REALM_NAME.get(config.settings());
92103
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmBootstrapCheck.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm;
2828
import org.junit.Before;
2929

30+
import java.io.IOException;
3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import 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) {

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmBootstrapCheckTests.java

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)