Skip to content

Commit 6fae14b

Browse files
address review feedback
1 parent b0ad6d1 commit 6fae14b

File tree

2 files changed

+32
-18
lines changed
  • x-pack/plugin

2 files changed

+32
-18
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,7 @@ private void checkConsistencyForInternalAuthenticationType() {
919919

920920
private void checkConsistencyForApiKeyAuthenticationType() {
921921
final RealmRef authenticatingRealm = authenticatingSubject.getRealm();
922-
if (false == authenticatingRealm.isApiKeyRealm()
923-
&& false == authenticatingRealm.isCrossClusterAccessRealm()
924-
&& false == authenticatingRealm.isCloudApiKeyRealm()) {
922+
if (false == authenticatingRealm.usesApiKeys()) {
925923
throw new IllegalArgumentException(
926924
Strings.format("API key authentication cannot have realm type [%s]", authenticatingRealm.type)
927925
);
@@ -1217,6 +1215,10 @@ private boolean isAnonymousRealm() {
12171215
return ANONYMOUS_REALM_NAME.equals(name) && ANONYMOUS_REALM_TYPE.equals(type);
12181216
}
12191217

1218+
private boolean usesApiKeys() {
1219+
return isCloudApiKeyRealm() || isApiKeyRealm() || isCrossClusterAccessRealm();
1220+
}
1221+
12201222
private boolean isCloudApiKeyRealm() {
12211223
return CLOUD_API_KEY_REALM_NAME.equals(name) && CLOUD_API_KEY_REALM_TYPE.equals(type);
12221224
}

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,33 +1254,45 @@ Collection<Object> createComponents(
12541254
}
12551255

12561256
private CustomApiKeyAuthenticator createCustomApiKeyAuthenticator(SecurityExtension.SecurityComponents extensionComponents) {
1257-
final SetOnce<CustomApiKeyAuthenticator> customApiKeyAuthenticatorSetOnce = new SetOnce<>();
1258-
for (var extension : securityExtensions) {
1257+
final Map<String, CustomApiKeyAuthenticator> customApiKeyAuthenticatorByExtension = new HashMap<>();
1258+
for (final SecurityExtension extension : securityExtensions) {
12591259
final CustomApiKeyAuthenticator customApiKeyAuthenticator = extension.getCustomApiKeyAuthenticator(extensionComponents);
12601260
if (customApiKeyAuthenticator != null) {
12611261
if (false == isInternalExtension(extension)) {
12621262
throw new IllegalStateException(
12631263
"The ["
1264-
+ extension.getClass().getName()
1264+
+ extension.extensionName()
12651265
+ "] extension tried to install a custom CustomApiKeyAuthenticator. "
12661266
+ "This functionality is not available to external extensions."
12671267
);
12681268
}
1269-
boolean success = customApiKeyAuthenticatorSetOnce.trySet(customApiKeyAuthenticator);
1270-
if (false == success) {
1271-
throw new IllegalStateException(
1272-
"The ["
1273-
+ extension.getClass().getName()
1274-
+ "] extension tried to install a custom CustomApiKeyAuthenticator, but one has already been installed."
1275-
);
1276-
}
1277-
logger.debug("CustomApiKeyAuthenticator provided by extension [{}]", extension.extensionName());
1269+
customApiKeyAuthenticatorByExtension.put(extension.extensionName(), customApiKeyAuthenticator);
12781270
}
12791271
}
1280-
if (customApiKeyAuthenticatorSetOnce.get() == null) {
1281-
customApiKeyAuthenticatorSetOnce.set(new CustomApiKeyAuthenticator.Noop());
1272+
1273+
if (customApiKeyAuthenticatorByExtension.isEmpty()) {
1274+
logger.debug(
1275+
"No custom implementation for [{}]. Falling-back to noop implementation.",
1276+
CustomApiKeyAuthenticator.class.getCanonicalName()
1277+
);
1278+
return new CustomApiKeyAuthenticator.Noop();
1279+
1280+
} else if (customApiKeyAuthenticatorByExtension.size() > 1) {
1281+
throw new IllegalStateException(
1282+
"Multiple extensions tried to install a custom CustomApiKeyAuthenticator: " + customApiKeyAuthenticatorByExtension.keySet()
1283+
);
1284+
1285+
} else {
1286+
final var authenticatorByExtensionEntry = customApiKeyAuthenticatorByExtension.entrySet().iterator().next();
1287+
final CustomApiKeyAuthenticator customApiKeyAuthenticator = authenticatorByExtensionEntry.getValue();
1288+
final String extensionName = authenticatorByExtensionEntry.getKey();
1289+
logger.debug(
1290+
"CustomApiKeyAuthenticator implementation [{}] provided by extension [{}]",
1291+
customApiKeyAuthenticator.getClass().getCanonicalName(),
1292+
extensionName
1293+
);
1294+
return customApiKeyAuthenticator;
12821295
}
1283-
return customApiKeyAuthenticatorSetOnce.get();
12841296
}
12851297

12861298
private ServiceAccountService createServiceAccountService(

0 commit comments

Comments
 (0)