Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.ranger.audit.model.AuditEventBase;
import org.apache.ranger.audit.model.AuthzAuditEvent;
import org.apache.ranger.audit.provider.MiscUtil;
import org.apache.ranger.plugin.authn.DefaultJwtProvider;
import org.apache.ranger.plugin.authn.DefaultTokenSupplier;
import org.apache.ranger.plugin.util.PluginHeaderAuthConfig;
import org.apache.ranger.plugin.util.RangerRESTClient;
import org.slf4j.Logger;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void init(Properties props, String propPrefix) {
this.restClient = new RangerRESTClient(url, sslConfigFileName, config);

if (AUTH_TYPE_JWT.equalsIgnoreCase(authType)) {
this.restClient.setJwtProvider(new DefaultJwtProvider("ranger.plugin.policy.rest.client", config));
this.restClient.setTokenSupplier(new DefaultTokenSupplier("ranger.plugin.policy.rest.client", config));
}

this.restClient.setRestClientConnTimeOutMs(connTimeoutMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.ranger.audit.provider.MiscUtil;
import org.apache.ranger.authorization.hadoop.config.RangerPluginConfig;
import org.apache.ranger.authorization.utils.StringUtil;
import org.apache.ranger.plugin.authn.JwtProvider;
import org.apache.ranger.plugin.model.RangerRole;
import org.apache.ranger.plugin.util.GrantRevokeRequest;
import org.apache.ranger.plugin.util.GrantRevokeRoleRequest;
Expand Down Expand Up @@ -55,6 +54,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class RangerAdminRESTClient extends AbstractRangerAdminClient {
private static final Logger LOG = LoggerFactory.getLogger(RangerAdminRESTClient.class);
Expand All @@ -67,7 +67,7 @@ public class RangerAdminRESTClient extends AbstractRangerAdminClient {
private String serviceNameUrlParam;
private String pluginId;
private String clusterName;
private JwtProvider jwtProvider;
private Supplier<String> tokenSupplier;
private RangerRESTClient restClient;
private boolean supportsPolicyDeltas;
private boolean supportsTagDeltas;
Expand Down Expand Up @@ -1021,20 +1021,20 @@ public boolean isAuthenticationEnabled() {
return (restClient != null && restClient.isAuthFilterPresent()) || super.isAuthenticationEnabled();
}

public void setJwtProvider(JwtProvider jwtProvider) {
this.jwtProvider = jwtProvider;
public void setTokenSupplier(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;

if (restClient != null) {
restClient.setJwtProvider(jwtProvider);
restClient.setTokenSupplier(tokenSupplier);
}
}

private void init(String url, String sslConfigFileName, int restClientConnTimeOutMs, int restClientReadTimeOutMs, int restClientMaxRetryAttempts, int restClientRetryIntervalMs, Configuration config) {
LOG.debug("==> RangerAdminRESTClient.init({}, {})", url, sslConfigFileName);

restClient = new RangerRESTClient(url, sslConfigFileName, config);
if (jwtProvider != null) {
restClient.setJwtProvider(jwtProvider);
if (tokenSupplier != null) {
restClient.setTokenSupplier(tokenSupplier);
}

restClient.setRestClientConnTimeOutMs(restClientConnTimeOutMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.function.Supplier;

public class DefaultJwtProvider implements JwtProvider {
private static final Logger LOG = LoggerFactory.getLogger(DefaultJwtProvider.class);
public class DefaultTokenSupplier implements Supplier<String> {
private static final Logger LOG = LoggerFactory.getLogger(DefaultTokenSupplier.class);

public static final String JWT_SOURCE = ".jwt.source";
public static final String JWT_ENV = ".jwt.env";
public static final String JWT_FILE = ".jwt.file";
public static final String JWT_CRED_FILE = ".jwt.cred.file";
public static final String JWT_CRED_ALIAS = ".jwt.cred.alias";

// class name of a Supplier<String> implementation; defaults to DefaultTokenSupplier if JWT_SOURCE is configured
public static final String JWT_SUPPLIER = ".jwt.supplier";

private final String jwtEnvVar;
private final String jwtFilePath;
private final String jwtCredFilePath;
Expand All @@ -48,7 +52,7 @@ public class DefaultJwtProvider implements JwtProvider {

private volatile String jwt;

public DefaultJwtProvider(String propertyPrefix, Configuration config) {
public DefaultTokenSupplier(String propertyPrefix, Configuration config) {
String jwtSrc = config.get(propertyPrefix + JWT_SOURCE);

if (jwtSrc == null) {
Expand Down Expand Up @@ -87,7 +91,7 @@ public DefaultJwtProvider(String propertyPrefix, Configuration config) {
}

@Override
public String getJwt() {
public String get() {
if (StringUtils.isNotEmpty(jwtEnvVar)) {
jwt = System.getenv(jwtEnvVar);
} else if (StringUtils.isNotEmpty(jwtFilePath)) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,38 @@
package org.apache.ranger.plugin.policyengine;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.ranger.admin.client.RangerAdminClient;
import org.apache.ranger.admin.client.RangerAdminRESTClient;
import org.apache.ranger.authorization.hadoop.config.RangerPluginConfig;
import org.apache.ranger.plugin.authn.DefaultJwtProvider;
import org.apache.ranger.plugin.authn.JwtProvider;
import org.apache.ranger.plugin.authn.DefaultTokenSupplier;
import org.apache.ranger.plugin.model.RangerPolicy;
import org.apache.ranger.plugin.resourcematcher.RangerResourceMatcher;
import org.apache.ranger.plugin.service.RangerAuthContext;
import org.apache.ranger.plugin.service.RangerAuthContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;

public class RangerPluginContext {
private static final Logger LOG = LoggerFactory.getLogger(RangerPluginContext.class);

private final RangerPluginConfig config;
private final Map<String, Map<RangerPolicy.RangerPolicyResource, RangerResourceMatcher>> resourceMatchers = new HashMap<>();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); // fair lock
private JwtProvider jwtProvider;
private Supplier<String> tokenSupplier;
private RangerAuthContext authContext;
private RangerAuthContextListener authContextListener;
private RangerAdminClient adminClient;

public RangerPluginContext(RangerPluginConfig config) {
this.config = config;
this.jwtProvider = new DefaultJwtProvider(config.getPropertyPrefix() + ".policy.rest.client", config);
this.config = config;
this.tokenSupplier = getTokenSupplier(config.getPropertyPrefix() + ".policy.rest.client", config);
}

public RangerPluginConfig getConfig() {
Expand Down Expand Up @@ -155,8 +157,8 @@ public RangerAdminClient createAdminClient(RangerPluginConfig pluginConfig) {

if (ret == null) {
ret = new RangerAdminRESTClient();
if (jwtProvider != null) {
((RangerAdminRESTClient) ret).setJwtProvider(jwtProvider);
if (tokenSupplier != null) {
((RangerAdminRESTClient) ret).setTokenSupplier(tokenSupplier);
}
}

Expand All @@ -170,17 +172,17 @@ public RangerAdminClient createAdminClient(RangerPluginConfig pluginConfig) {
return ret;
}

public void registerJWTProvider(JwtProvider jwtProvider) {
this.jwtProvider = jwtProvider;
public void registerTokenSupplier(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;

RangerAdminRESTClient restClient = (adminClient instanceof RangerAdminRESTClient) ? (RangerAdminRESTClient) adminClient : null;
if (restClient != null) {
restClient.setJwtProvider(jwtProvider);
restClient.setTokenSupplier(tokenSupplier);
}
}

public JwtProvider getJwtProvider() {
return jwtProvider;
public Supplier<String> getTokenSupplier() {
return tokenSupplier;
}

void cleanResourceMatchers() {
Expand All @@ -196,4 +198,62 @@ 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);
final Supplier<String> ret;

if (StringUtils.isNotBlank(clzName)) {
LOG.info("Using Token supplier [{}], config: [{}]", clzName, providerProp);

if (DefaultTokenSupplier.class.getName().equals(clzName)) {
ret = new DefaultTokenSupplier(propertyPrefix, config);
} else {
ret = getCustomTokenSupplier(clzName, config, providerProp);
}
} else if (isJwtSourceConfigured(propertyPrefix, config)) {
ret = new DefaultTokenSupplier(propertyPrefix, config);
} else {
LOG.debug("No token supplier configured, config: [{}]", providerProp);

ret = null;
}

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) {
final Class<?> clz;

/* a misconfigured supplier is rejected: Throwable covers LinkageError/NoClassDefFoundError from an incomplete classpath */
try {
clz = Class.forName(clzName);
} catch (Throwable excp) {
throw new IllegalArgumentException(providerProp + "=" + clzName + ": failed to load token supplier class", excp);
}

if (!Supplier.class.isAssignableFrom(clz)) {
throw new IllegalArgumentException(providerProp + "=" + clzName + ": class does not implement " + Supplier.class.getName());
}

try {
try {
/* prefer a constructor that accepts the Ranger Configuration */
Constructor<?> ctor = clz.getDeclaredConstructor(Configuration.class);

return (Supplier<String>) ctor.newInstance(config);
} catch (NoSuchMethodException excp) {
/* fall back to the no-argument constructor */
return (Supplier<String>) clz.getDeclaredConstructor().newInstance();
}
} catch (Throwable excp) {
throw new IllegalArgumentException(providerProp + "=" + clzName + ": failed to instantiate token supplier", excp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.ranger.authorization.hadoop.config.RangerAuditConfig;
import org.apache.ranger.authorization.hadoop.config.RangerPluginConfig;
import org.apache.ranger.authorization.utils.StringUtil;
import org.apache.ranger.plugin.authn.JwtProvider;
import org.apache.ranger.plugin.contextenricher.RangerAdminGdsInfoRetriever;
import org.apache.ranger.plugin.contextenricher.RangerAdminUserStoreRetriever;
import org.apache.ranger.plugin.contextenricher.RangerContextEnricher;
Expand Down Expand Up @@ -87,6 +86,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;

public class RangerBasePlugin {
private static final Logger LOG = LoggerFactory.getLogger(RangerBasePlugin.class);
Expand Down Expand Up @@ -303,8 +303,8 @@ public static RangerResourceACLs getMergedResourceACLs(RangerResourceACLs baseAC
return baseACLs;
}

public void registerJwtProvider(JwtProvider jwtProvider) {
pluginContext.registerJWTProvider(jwtProvider);
public void registerTokenSupplier(Supplier<String> tokenSupplier) {
pluginContext.registerTokenSupplier(tokenSupplier);
}

public String getServiceType() {
Expand Down
Loading