-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Add extensionName() to security extension #79329
Changes from 3 commits
595e8c4
297e860
3187ae3
25cc971
1268467
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
import org.elasticsearch.common.util.concurrent.EsExecutors; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.common.util.set.Sets; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.env.Environment; | ||
|
@@ -560,7 +561,7 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste | |
extensionComponents | ||
); | ||
if (providers != null && providers.isEmpty() == false) { | ||
customRoleProviders.put(extension.toString(), providers); | ||
customRoleProviders.put(extension.extensionName(), providers); | ||
} | ||
} | ||
|
||
|
@@ -669,37 +670,15 @@ auditTrailService, failureHandler, threadPool, anonymousUser, getAuthorizationEn | |
} | ||
|
||
private AuthorizationEngine getAuthorizationEngine() { | ||
AuthorizationEngine authorizationEngine = null; | ||
String extensionName = null; | ||
for (SecurityExtension extension : securityExtensions) { | ||
final AuthorizationEngine extensionEngine = extension.getAuthorizationEngine(settings); | ||
if (extensionEngine != null && authorizationEngine != null) { | ||
throw new IllegalStateException("Extensions [" + extensionName + "] and [" + extension.toString() + "] " | ||
+ "both set an authorization engine"); | ||
} | ||
authorizationEngine = extensionEngine; | ||
extensionName = extension.toString(); | ||
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 old code was buggy.
Because we set |
||
} | ||
|
||
if (authorizationEngine != null) { | ||
logger.debug("Using authorization engine from extension [" + extensionName + "]"); | ||
} | ||
return authorizationEngine; | ||
return findValueFromExtensions("authorization engine", extension -> extension.getAuthorizationEngine(settings)); | ||
} | ||
|
||
private AuthenticationFailureHandler createAuthenticationFailureHandler(final Realms realms, | ||
final SecurityExtension.SecurityComponents components) { | ||
AuthenticationFailureHandler failureHandler = null; | ||
String extensionName = null; | ||
for (SecurityExtension extension : securityExtensions) { | ||
AuthenticationFailureHandler extensionFailureHandler = extension.getAuthenticationFailureHandler(components); | ||
if (extensionFailureHandler != null && failureHandler != null) { | ||
throw new IllegalStateException("Extensions [" + extensionName + "] and [" + extension.toString() + "] " | ||
+ "both set an authentication failure handler"); | ||
} | ||
failureHandler = extensionFailureHandler; | ||
extensionName = extension.toString(); | ||
} | ||
AuthenticationFailureHandler failureHandler = findValueFromExtensions( | ||
"authentication failure handler", | ||
extension -> extension.getAuthenticationFailureHandler(components) | ||
); | ||
if (failureHandler == null) { | ||
logger.debug("Using default authentication failure handler"); | ||
Supplier<Map<String, List<String>>> headersSupplier = () -> { | ||
|
@@ -736,12 +715,49 @@ private AuthenticationFailureHandler createAuthenticationFailureHandler(final Re | |
getLicenseState().addListener(() -> { | ||
finalDefaultFailureHandler.setHeaders(headersSupplier.get()); | ||
}); | ||
} else { | ||
logger.debug("Using authentication failure handler from extension [" + extensionName + "]"); | ||
} | ||
return failureHandler; | ||
} | ||
|
||
/** | ||
* Calls the provided function for each configured extension and return the value that was generated by the extensions. | ||
* If multiple extensions provide a value, throws {@link IllegalStateException}. | ||
* If no extensions provide a value (or if there are no extensions) returns {@code null}. | ||
*/ | ||
@Nullable | ||
private <T> T findValueFromExtensions(String valueType, Function<SecurityExtension, T> method) { | ||
T foundValue = null; | ||
String fromExtension = null; | ||
for (SecurityExtension extension : securityExtensions) { | ||
final T extensionValue = method.apply(extension); | ||
if (extensionValue == null) { | ||
continue; | ||
} | ||
if (foundValue == null) { | ||
foundValue = extensionValue; | ||
fromExtension = extension.extensionName(); | ||
} | ||
else { | ||
tvernum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new IllegalStateException( | ||
"Extensions [" | ||
+ fromExtension | ||
+ "] and [" | ||
+ extension.extensionName() | ||
+ "] " | ||
+ " both attempted to provide a value for [" | ||
+ valueType | ||
+ "]" | ||
); | ||
} | ||
} | ||
if (foundValue == null) { | ||
return null; | ||
} else { | ||
logger.debug("Using [{}] [{}] from extension [{}]", valueType, foundValue, fromExtension); | ||
return foundValue; | ||
} | ||
} | ||
|
||
@Override | ||
public Settings additionalSettings() { | ||
return additionalSettings(settings, enabled); | ||
|
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.
Nit: why not just
name()
since the wordextension
is already part of the class name?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.
Because I didn't think it would be as clear to people implementing security extensions what "name" was supposed to be, and what behaviour it should have.
extensionName
is self describing