mneethiraj commented on code in PR #1227:
URL: https://github.com/apache/ranger/pull/1227#discussion_r4007396381
##########
agents-common/src/main/java/org/apache/ranger/plugin/policyengine/RangerPluginContext.java:
##########
@@ -196,4 +198,61 @@ void cleanResourceMatchers() {
LOG.debug("<== cleanResourceMatchers()");
}
+
+ private static Supplier<String> getTokenSupplier(String propertyPrefix,
RangerPluginConfig config) {
+ String providerProp = propertyPrefix +
DefaultTokenSupplier.JWT_SUPPLIER;
+ String clzName = config.get(providerProp);
+ Supplier<String> ret = null;
+
+ if (StringUtils.isNotBlank(clzName) &&
!DefaultTokenSupplier.class.getName().equals(clzName)) {
+ ret = getCustomTokenSupplier(clzName, config, providerProp);
+ }
+
+ /* DefaultTokenSupplier is used only when configured explicitly or
when a JWT source is set: otherwise no
+ * token supplier is created, so the plugin keeps the authentication
it had before (basic-auth or none). */
+ if (ret == null &&
(DefaultTokenSupplier.class.getName().equals(clzName) ||
isJwtSourceConfigured(propertyPrefix, config))) {
+ ret = new DefaultTokenSupplier(propertyPrefix, config);
+ }
+
+ if (ret != null) {
+ LOG.info("Using Token supplier [{}], config: [{}]",
ret.getClass().getName(), providerProp);
+ } else {
+ LOG.debug("No token supplier configured, config: [{}]",
providerProp);
+ }
+
+ return ret;
+ }
+
+ private static boolean isJwtSourceConfigured(String propertyPrefix,
RangerPluginConfig config) {
+ return StringUtils.isNotBlank(config.get(propertyPrefix +
DefaultTokenSupplier.JWT_SOURCE));
+ }
+
+ @SuppressWarnings("unchecked")
+ private static Supplier<String> getCustomTokenSupplier(String clzName,
RangerPluginConfig config, String providerProp) {
+ Supplier<String> ret = null;
+
+ /* a misconfigured supplier must not prevent plugin initialization:
this returns null and the caller decides
+ * the fallback. Throwable covers LinkageError/NoClassDefFoundError
from a class on an incomplete classpath. */
+ try {
+ Class<?> clz = Class.forName(clzName);
+
+ if (Supplier.class.isAssignableFrom(clz)) {
+ try {
+ /* prefer a constructor that accepts the Ranger
Configuration */
+ Constructor<?> ctor =
clz.getDeclaredConstructor(Configuration.class);
+
+ ret = (Supplier<String>) ctor.newInstance(config);
+ } catch (NoSuchMethodException excp) {
+ /* fall back to the no-argument constructor */
+ ret = (Supplier<String>)
clz.getDeclaredConstructor().newInstance();
+ }
+ } else {
+ LOG.error("{}={}: class does not implement {}. Requests may be
sent without a bearer token", providerProp, clzName, Supplier.class.getName());
Review Comment:
Instead of continuing after this error, I suggest throwing
`IllegalArgumentException` - as this is a misconfiguration.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]