[
https://issues.apache.org/jira/browse/ARTEMIS-33?focusedWorklogId=548691&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-548691
]
ASF GitHub Bot logged work on ARTEMIS-33:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 05/Feb/21 15:10
Start Date: 05/Feb/21 15:10
Worklog Time Spent: 10m
Work Description: gemmellr commented on a change in pull request #3432:
URL: https://github.com/apache/activemq-artemis/pull/3432#discussion_r571025540
##########
File path:
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/MechanismFinder.java
##########
@@ -17,31 +17,48 @@
package org.apache.activemq.artemis.protocol.amqp.sasl;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
-import java.util.stream.Stream;
public class MechanismFinder {
- private static final String[] DEFAULT_MECHANISMS = new String[]
{PlainSASL.NAME, AnonymousServerSASL.NAME};
-
- private static final Map<String, ServerSASLFactory> FACTORY_MAP = new
HashMap<>();
+ private static final Map<String, List<ServerSASLFactory>> FACTORY_MAP = new
HashMap<>();
+ private static final Comparator<? super ServerSASLFactory>
PRECEDENCE_COMPARATOR =
+ (f1, f2) -> f1.getPrecedence() - f2.getPrecedence();
static {
ServiceLoader<ServerSASLFactory> serviceLoader =
ServiceLoader.load(ServerSASLFactory.class,
MechanismFinder.class.getClassLoader());
for (ServerSASLFactory factory : serviceLoader) {
- FACTORY_MAP.put(factory.getMechanism(), factory);
+ List<ServerSASLFactory> list =
FACTORY_MAP.computeIfAbsent(factory.getMechanism(), k -> new ArrayList<>());
+ list.add(factory);
+ }
+ for (List<ServerSASLFactory> factories : FACTORY_MAP.values()) {
+ Collections.sort(factories, PRECEDENCE_COMPARATOR);
Review comment:
Maintaining a map of lists seems unecessarily complicated. Cant you jsut
merge the seen factory objects and keep the one with the higher precedence?
This would simplify everything done afterwards too.
Note I am not seeing use of the 'isDefaultPerimitted' bits from the factory,
which presumably means this might currently be advertising GSSAPI and EXTERNAL
by default when it definitely shouldnt.
##########
File path:
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/MechanismFinder.java
##########
@@ -17,31 +17,48 @@
package org.apache.activemq.artemis.protocol.amqp.sasl;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
-import java.util.stream.Stream;
public class MechanismFinder {
- private static final String[] DEFAULT_MECHANISMS = new String[]
{PlainSASL.NAME, AnonymousServerSASL.NAME};
-
- private static final Map<String, ServerSASLFactory> FACTORY_MAP = new
HashMap<>();
+ private static final Map<String, List<ServerSASLFactory>> FACTORY_MAP = new
HashMap<>();
+ private static final Comparator<? super ServerSASLFactory>
PRECEDENCE_COMPARATOR =
+ (f1, f2) -> f1.getPrecedence() - f2.getPrecedence();
static {
ServiceLoader<ServerSASLFactory> serviceLoader =
ServiceLoader.load(ServerSASLFactory.class,
MechanismFinder.class.getClassLoader());
for (ServerSASLFactory factory : serviceLoader) {
- FACTORY_MAP.put(factory.getMechanism(), factory);
+ List<ServerSASLFactory> list =
FACTORY_MAP.computeIfAbsent(factory.getMechanism(), k -> new ArrayList<>());
+ list.add(factory);
+ }
+ for (List<ServerSASLFactory> factories : FACTORY_MAP.values()) {
+ Collections.sort(factories, PRECEDENCE_COMPARATOR);
}
}
- public static String[] getKnownMechanisms() {
- return Stream.concat(Arrays.stream(DEFAULT_MECHANISMS),
FACTORY_MAP.keySet().stream()).toArray(String[]::new);
+ public static String[] getDefaultMechanisms() {
+ return FACTORY_MAP.values()
+ .stream()
+ .flatMap(List<ServerSASLFactory>::stream)
+ .sorted(PRECEDENCE_COMPARATOR)
+ .map(ServerSASLFactory::getMechanism)
+ .distinct()
+ .toArray(String[]::new);
}
public static ServerSASLFactory getFactory(String mechanism) {
- return FACTORY_MAP.get(mechanism);
+ List<ServerSASLFactory> list = FACTORY_MAP.get(mechanism);
+ if (list == null || list.isEmpty()) {
+ return null;
+ }
+ // return mechanism with highest precedence if multiple are registered
+ return list.get(0);
Review comment:
This could be reduced to a simple map lookup after prior comments.
##########
File path:
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/MechanismFinder.java
##########
@@ -17,31 +17,48 @@
package org.apache.activemq.artemis.protocol.amqp.sasl;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
-import java.util.stream.Stream;
public class MechanismFinder {
- private static final String[] DEFAULT_MECHANISMS = new String[]
{PlainSASL.NAME, AnonymousServerSASL.NAME};
-
- private static final Map<String, ServerSASLFactory> FACTORY_MAP = new
HashMap<>();
+ private static final Map<String, List<ServerSASLFactory>> FACTORY_MAP = new
HashMap<>();
+ private static final Comparator<? super ServerSASLFactory>
PRECEDENCE_COMPARATOR =
+ (f1, f2) -> f1.getPrecedence() - f2.getPrecedence();
static {
ServiceLoader<ServerSASLFactory> serviceLoader =
ServiceLoader.load(ServerSASLFactory.class,
MechanismFinder.class.getClassLoader());
for (ServerSASLFactory factory : serviceLoader) {
- FACTORY_MAP.put(factory.getMechanism(), factory);
+ List<ServerSASLFactory> list =
FACTORY_MAP.computeIfAbsent(factory.getMechanism(), k -> new ArrayList<>());
+ list.add(factory);
+ }
+ for (List<ServerSASLFactory> factories : FACTORY_MAP.values()) {
+ Collections.sort(factories, PRECEDENCE_COMPARATOR);
}
}
- public static String[] getKnownMechanisms() {
- return Stream.concat(Arrays.stream(DEFAULT_MECHANISMS),
FACTORY_MAP.keySet().stream()).toArray(String[]::new);
+ public static String[] getDefaultMechanisms() {
+ return FACTORY_MAP.values()
+ .stream()
+ .flatMap(List<ServerSASLFactory>::stream)
+ .sorted(PRECEDENCE_COMPARATOR)
+ .map(ServerSASLFactory::getMechanism)
+ .distinct()
+ .toArray(String[]::new);
Review comment:
Reworking the bit above and then computing the default mechanisms there
at the start as loaded, rather than here, would simplify this and avoid
repeating the same evaluation over and over for every connection created.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 548691)
Time Spent: 1h 50m (was: 1h 40m)
> Generic integration with SASL Frameworks
> ----------------------------------------
>
> Key: ARTEMIS-33
> URL: https://issues.apache.org/jira/browse/ARTEMIS-33
> Project: ActiveMQ Artemis
> Issue Type: New Feature
> Affects Versions: 1.0.0
> Reporter: Clebert Suconic
> Priority: Critical
> Fix For: unscheduled
>
> Time Spent: 1h 50m
> Remaining Estimate: 0h
>
> Right now we are bound to User/Password or anonymous on SASL.
> We should use some framework that would allow SASL integration with a bigger
> number of possibilities.
> We should investigate options from the JDK for this... or if there is any
> other framework available.
> I believe this only affects AMQP, but as part of this issue we should
> investigate if there is any interest extending SASL into any other protocol.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)