-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
[Kerberos] Add bootstrap checks for kerberos realm #31548
Merged
bizybot
merged 4 commits into
elastic:feature/kerberos
from
bizybot:kerberos/bootstrapcheck
Jun 27, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
69 changes: 69 additions & 0 deletions
69
...ain/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmBootstrapCheck.java
This file contains 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,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security.authc.kerberos; | ||
|
||
import org.elasticsearch.bootstrap.BootstrapCheck; | ||
import org.elasticsearch.bootstrap.BootstrapContext; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.xpack.core.security.authc.RealmSettings; | ||
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
/** | ||
* This class is used to perform bootstrap checks for kerberos realm. | ||
* <p> | ||
* We use service keytabs for validating incoming kerberos tickets and is a | ||
* required configuration. Due to JVM wide system properties for Kerberos we | ||
* cannot support multiple Kerberos realms. This class adds checks for node to | ||
* fail if service keytab does not exist or multiple kerberos realms have been | ||
* configured. | ||
*/ | ||
public class KerberosRealmBootstrapCheck implements BootstrapCheck { | ||
private final Environment env; | ||
|
||
public KerberosRealmBootstrapCheck(final Environment env) { | ||
this.env = env; | ||
} | ||
|
||
@Override | ||
public BootstrapCheckResult check(final BootstrapContext context) { | ||
final Map<String, Settings> realmsSettings = RealmSettings.getRealmSettings(context.settings); | ||
boolean isKerberosRealmConfigured = false; | ||
for (final Entry<String, Settings> entry : realmsSettings.entrySet()) { | ||
final String name = entry.getKey(); | ||
final Settings realmSettings = entry.getValue(); | ||
final String type = realmSettings.get("type"); | ||
if (Strings.hasText(type) == false) { | ||
return BootstrapCheckResult.failure("missing realm type for [" + name + "] realm"); | ||
} | ||
if (KerberosRealmSettings.TYPE.equals(type)) { | ||
if (isKerberosRealmConfigured) { | ||
return BootstrapCheckResult.failure( | ||
"multiple [" + type + "] realms are configured. [" + type + "] can only have one such realm configured"); | ||
} | ||
isKerberosRealmConfigured = true; | ||
|
||
final Path keytabPath = env.configFile().resolve(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.get(realmSettings)); | ||
if (Files.exists(keytabPath) == false) { | ||
return BootstrapCheckResult.failure("configured service key tab file [" + keytabPath + "] does not exist"); | ||
} | ||
} | ||
} | ||
return BootstrapCheckResult.success(); | ||
} | ||
|
||
@Override | ||
public boolean alwaysEnforce() { | ||
return true; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...ava/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmBootstrapCheckTests.java
This file contains 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,115 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security.authc.kerberos; | ||
|
||
import org.elasticsearch.bootstrap.BootstrapCheck; | ||
import org.elasticsearch.bootstrap.BootstrapContext; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.env.TestEnvironment; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.core.security.authc.RealmSettings; | ||
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings; | ||
import org.elasticsearch.xpack.core.security.authc.pki.PkiRealmSettings; | ||
import org.elasticsearch.xpack.security.authc.kerberos.support.KerberosTestCase; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
public class KerberosRealmBootstrapCheckTests extends ESTestCase { | ||
|
||
public void testBootstrapCheckFailsForMultipleKerberosRealms() throws IOException { | ||
final Path tempDir = createTempDir(); | ||
final Settings settings1 = buildKerberosRealmSettings("kerb1", false, tempDir); | ||
final Settings settings2 = buildKerberosRealmSettings("kerb2", false, tempDir); | ||
final Settings settings3 = realm("pki1", PkiRealmSettings.TYPE, Settings.builder()).build(); | ||
final Settings settings = | ||
Settings.builder().put("path.home", tempDir).put(settings1).put(settings2).put(settings3).build(); | ||
final BootstrapContext context = new BootstrapContext(settings, null); | ||
final KerberosRealmBootstrapCheck kerbRealmBootstrapCheck = | ||
new KerberosRealmBootstrapCheck(TestEnvironment.newEnvironment(settings)); | ||
final BootstrapCheck.BootstrapCheckResult result = kerbRealmBootstrapCheck.check(context); | ||
assertThat(result, is(notNullValue())); | ||
assertThat(result.isFailure(), is(true)); | ||
assertThat(result.getMessage(), equalTo("multiple [" + KerberosRealmSettings.TYPE + "] realms are configured. [" | ||
+ KerberosRealmSettings.TYPE + "] can only have one such realm configured")); | ||
} | ||
|
||
public void testBootstrapCheckFailsForMissingKeytabFile() throws IOException { | ||
final Path tempDir = createTempDir(); | ||
final Settings settings = | ||
Settings.builder().put("path.home", tempDir).put(buildKerberosRealmSettings("kerb1", true, tempDir)).build(); | ||
final BootstrapContext context = new BootstrapContext(settings, null); | ||
final KerberosRealmBootstrapCheck kerbRealmBootstrapCheck = | ||
new KerberosRealmBootstrapCheck(TestEnvironment.newEnvironment(settings)); | ||
final BootstrapCheck.BootstrapCheckResult result = kerbRealmBootstrapCheck.check(context); | ||
assertThat(result, is(notNullValue())); | ||
assertThat(result.isFailure(), is(true)); | ||
assertThat(result.getMessage(), | ||
equalTo("configured service key tab file [" + tempDir.resolve("kerb1.keytab").toString() + "] does not exist")); | ||
} | ||
|
||
public void testBootstrapCheckFailsForMissingRealmType() throws IOException { | ||
final Path tempDir = createTempDir(); | ||
final String name = "kerb1"; | ||
final Settings settings1 = buildKerberosRealmSettings("kerb1", false, tempDir); | ||
final Settings settings2 = realm(name, randomFrom("", " "), Settings.builder()).build(); | ||
final Settings settings = | ||
Settings.builder().put("path.home", tempDir).put(settings1).put(settings2).build(); | ||
final BootstrapContext context = new BootstrapContext(settings, null); | ||
final KerberosRealmBootstrapCheck kerbRealmBootstrapCheck = | ||
new KerberosRealmBootstrapCheck(TestEnvironment.newEnvironment(settings)); | ||
final BootstrapCheck.BootstrapCheckResult result = kerbRealmBootstrapCheck.check(context); | ||
assertThat(result, is(notNullValue())); | ||
assertThat(result.isFailure(), is(true)); | ||
assertThat(result.getMessage(), equalTo("missing realm type for [" + name + "] realm")); | ||
} | ||
|
||
public void testBootstrapCheckSucceedsForCorrectConfiguration() throws IOException { | ||
final Path tempDir = createTempDir(); | ||
final Settings finalSettings = | ||
Settings.builder().put("path.home", tempDir).put(buildKerberosRealmSettings("kerb1", false, tempDir)).build(); | ||
final BootstrapContext context = new BootstrapContext(finalSettings, null); | ||
final KerberosRealmBootstrapCheck kerbRealmBootstrapCheck = | ||
new KerberosRealmBootstrapCheck(TestEnvironment.newEnvironment(finalSettings)); | ||
final BootstrapCheck.BootstrapCheckResult result = kerbRealmBootstrapCheck.check(context); | ||
assertThat(result, is(notNullValue())); | ||
assertThat(result.isSuccess(), is(true)); | ||
} | ||
|
||
public void testBootstrapCheckSucceedsForNoKerberosRealms() throws IOException { | ||
final Path tempDir = createTempDir(); | ||
final Settings finalSettings = Settings.builder().put("path.home", tempDir).build(); | ||
final BootstrapContext context = new BootstrapContext(finalSettings, null); | ||
final KerberosRealmBootstrapCheck kerbRealmBootstrapCheck = | ||
new KerberosRealmBootstrapCheck(TestEnvironment.newEnvironment(finalSettings)); | ||
final BootstrapCheck.BootstrapCheckResult result = kerbRealmBootstrapCheck.check(context); | ||
assertThat(result, is(notNullValue())); | ||
assertThat(result.isSuccess(), is(true)); | ||
} | ||
|
||
private Settings buildKerberosRealmSettings(final String name, final boolean missingKeytab, final Path tempDir) throws IOException { | ||
final Settings.Builder builder = Settings.builder(); | ||
if (missingKeytab == false) { | ||
KerberosTestCase.writeKeyTab(tempDir.resolve(name + ".keytab"), null); | ||
} | ||
builder.put(KerberosTestCase.buildKerberosRealmSettings(tempDir.resolve(name + ".keytab").toString())); | ||
return realm(name, KerberosRealmSettings.TYPE, builder).build(); | ||
} | ||
|
||
private Settings.Builder realm(final String name, final String type, final Settings.Builder settings) { | ||
final String prefix = RealmSettings.PREFIX + name + "."; | ||
if (type != null) { | ||
settings.put("type", type); | ||
} | ||
final Settings.Builder builder = Settings.builder().put(settings.normalizePrefix(prefix).build(), false); | ||
return builder; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you be more detailed here about the checks and why we have these checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, addressed the javadocs.