Skip to content

Commit 7ca5ef5

Browse files
morningmanclaude
andcommitted
[fix](ranger) Refuse an opt-out or refresh interval the load cannot use before it starts, and name the cause when policies arrive but no engine is built
Review round on 15e1747: the four findings that hold up. ranger.plugin.<type>.use.rangerGroups was read through Hadoop's getBoolean, which takes any value it cannot read as the default: a mistyped opt-out (flase) switched groups on in the deployment that had just decided the opposite, and Ranger reads the same property with the opposite default. It is now read strictly - true, false, or nothing - and anything else is refused before the load, naming the property and the value. userStoreRefresherPollingInterval went to Ranger's enricher unread, which parses it inside the policy engine's construction. A value that is not a number failed there; a non-positive one failed a step later, in Timer.schedule, after the enricher had downloaded the store and started its refresher thread. RangerBasePlugin.setPolicies catches both, leaves the plugin without an engine, and LoadedRangerPlugin then refused it as "Ranger Admin could not be reached and no cache was found" - the wrong repair - with, in the second case, the refresher thread left behind, one per CREATE CATALOG retry, since cleanup() reaches it only through an engine that was never built. The interval is now validated before the load as well. The plugin also tells "no policies" apart from "policies arrived and no engine could be built out of them" - anything the engine's construction throws lands there, a context enricher's init() included - by noting in setPolicies whether a versioned payload left it without an engine, and the refusal then says so and points at the cause RangerBasePlugin.setPolicies logged. The regression suite wrote its access policy first and waited on it: with four Ranger Admin calls and a 5 s policy poll, a poll between two of them brought a generation with the access policy alone, so the row filter, mask and deny assertions that followed could run against a generation without them. The access policy is written last, so answering on it proves the other three are in the same generation. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 15e1747 commit 7ca5ef5

5 files changed

Lines changed: 281 additions & 26 deletions

File tree

‎fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-common/src/main/java/org/apache/doris/catalog/authorizer/ranger/LoadedRangerPlugin.java‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
* use - no account bypasses the source - and a catalog bound to a {@code ranger-hive} source in that state
3939
* refuses every statement against it, in both cases with nothing but a line in fe.log to say why. So
4040
* {@link #init()} refuses that state instead: a load that ended with no policies, from the admin or the
41-
* cache, stops what it started and throws, with the cause. That fails the FE start, the {@code CREATE
42-
* CATALOG} (whose dry run builds the plugin), or the statement binding a catalog to its source again, which
43-
* is where an operator sees it. Once built, an outage of the admin is Ranger's to survive, as it always
44-
* was: the refresher keeps the last policies it downloaded and keeps polling.
41+
* cache - or with policies Ranger could build no engine out of, which it also survives, by logging - stops
42+
* what it started and throws, with the cause. That fails the FE start, the {@code CREATE CATALOG} (whose
43+
* dry run builds the plugin), or the statement binding a catalog to its source again, which is where an
44+
* operator sees it. A configuration the load could not use is refused before it starts, for the same
45+
* reader; see {@link RangerUserStoreGroups#validate}. Once built, an outage of the admin is Ranger's to
46+
* survive, as it always was: the refresher keeps the last policies it downloaded and keeps polling.
4547
*
4648
* <p>The user store is not part of that. A load that found the policies but no user store is logged and
4749
* accepted - policy items written against a group do not apply until the store arrives, which the enricher
@@ -56,6 +58,15 @@
5658
public abstract class LoadedRangerPlugin extends RangerBasePlugin {
5759
private static final Logger LOG = LogManager.getLogger(LoadedRangerPlugin.class);
5860

61+
/**
62+
* The version of the policies the last {@link #setPolicies} was handed and could not build an engine
63+
* out of; null when it built one, or was handed none. {@code RangerBasePlugin.setPolicies} catches
64+
* whatever the engine's construction throws and leaves the plugin without an engine, which is the state
65+
* "no policies" leaves it in as well, and not the same cause: {@link #init} reads this to tell the two
66+
* apart when it refuses the plugin.
67+
*/
68+
private volatile Long policiesWithoutEngine;
69+
5970
protected LoadedRangerPlugin(String serviceType, String serviceName, String appId) {
6071
super(serviceType, serviceName, appId);
6172
}
@@ -64,16 +75,22 @@ protected LoadedRangerPlugin(String serviceType, String serviceName, String appI
6475
* Loads the plugin - {@code RangerBasePlugin.init()}: roles, policies and user store, on this thread -
6576
* and refuses to leave it without policies; see the class comment.
6677
*
67-
* @throws IllegalStateException when the load ended with no policies from either the admin or the cache
78+
* @throws IllegalArgumentException for a configuration the load could not use, before it starts; see
79+
* {@link RangerUserStoreGroups#validate}
80+
* @throws IllegalStateException when the load ended with no policies from either the admin or the
81+
* cache, or with policies no engine could be built out of
6882
*/
6983
@Override
7084
public void init() {
85+
RangerUserStoreGroups.validate(getConfig());
7186
LOG.info(RangerUserStoreGroups.describe(getConfig()));
7287
long startedAtNanos = System.nanoTime();
7388
try {
7489
super.init();
7590
if (getPoliciesVersion() < 0) {
76-
throw new IllegalStateException(describeNoPolicies());
91+
Long refusedVersion = policiesWithoutEngine;
92+
throw new IllegalStateException(refusedVersion == null
93+
? describeNoPolicies() : describeNoEngine(refusedVersion));
7794
}
7895
} catch (RuntimeException | Error e) {
7996
// Stops what the load had started before it failed - the policy refresher and its download
@@ -116,15 +133,27 @@ private String describeNoPolicies() {
116133
+ " and try again";
117134
}
118135

136+
/** Why the plugin was refused when its policies did arrive: nothing could be built out of them. */
137+
private String describeNoEngine(long policiesVersion) {
138+
return "Ranger service " + getServiceName() + " (type " + getServiceType() + ") has no policies to"
139+
+ " authorize against: its policies (version " + policiesVersion + ") were downloaded, but no"
140+
+ " policy engine could be built out of them; RangerBasePlugin.setPolicies logged the cause"
141+
+ " just before this. Fix what it names and try again";
142+
}
143+
119144
/**
120145
* Takes the policies Ranger downloaded, and asks for the user store with them, so that the requests the
121146
* source over this plugin builds can carry the groups Ranger keeps for a user; see
122147
* {@link RangerUserStoreGroups}. Done on the way in rather than left to Ranger, because Ranger only does
123148
* it on its own from 2.5 on, and behind a property.
149+
*
150+
* <p>Notes, for {@link #init}, whether the policies handed over left the plugin without an engine.
124151
*/
125152
@Override
126153
public void setPolicies(ServicePolicies policies) {
127154
RangerUserStoreGroups.addUserStoreEnricher(getConfig(), policies);
128155
super.setPolicies(policies);
156+
Long version = policies == null ? null : policies.getPolicyVersion();
157+
policiesWithoutEngine = version != null && getPoliciesVersion() < 0 ? version : null;
129158
}
130159
}

‎fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-common/src/main/java/org/apache/doris/catalog/authorizer/ranger/RangerUserStoreGroups.java‎

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,86 @@ public final class RangerUserStoreGroups {
6666
public static final String USE_RANGER_GROUPS = ".use.rangerGroups";
6767

6868
/** How often the plugin asks Ranger Admin for a newer user store, when nothing configures it. */
69-
private static final String DEFAULT_REFRESH_INTERVAL_MS = Integer.toString(60 * 1000);
69+
private static final long DEFAULT_REFRESH_INTERVAL_MS = 60 * 1000L;
7070

7171
private RangerUserStoreGroups() {
7272
}
7373

74-
/** Whether the plugin configured by {@code config} attaches user store groups; on unless switched off. */
74+
/**
75+
* Whether the plugin configured by {@code config} attaches user store groups; on unless switched off.
76+
*
77+
* <p>Read strictly - {@code true}, {@code false}, or nothing - and not through Hadoop's {@code getBoolean},
78+
* which takes any value it cannot read as the default: a mistyped opt-out ({@code flase}) would switch this
79+
* on, in a deployment that has just decided the opposite, and Ranger reads the same property with the
80+
* opposite default, so that the two would disagree about what a single setting says.
81+
*
82+
* @throws IllegalArgumentException for a value that is neither; {@link #validate} raises it before the
83+
* load, so that a request never meets it
84+
*/
7585
public static boolean enabledFor(RangerPluginConfig config) {
7686
// No configuration at all - a plugin stubbed out in a test - has not switched anything off.
77-
return config == null || config.getBoolean(config.getPropertyPrefix() + USE_RANGER_GROUPS, true);
87+
if (config == null) {
88+
return true;
89+
}
90+
String property = config.getPropertyPrefix() + USE_RANGER_GROUPS;
91+
String value = config.getTrimmed(property);
92+
if (value == null || value.isEmpty() || value.equalsIgnoreCase("true")) {
93+
return true;
94+
}
95+
if (value.equalsIgnoreCase("false")) {
96+
return false;
97+
}
98+
throw new IllegalArgumentException("Ranger service " + config.getServiceName() + ": " + property + "="
99+
+ value + " is neither true nor false; leave it unset or set it to false to switch off the"
100+
+ " groups requests carry");
101+
}
102+
103+
/**
104+
* How often the plugin asks Ranger Admin for a newer user store, in milliseconds: what
105+
* {@code userStoreRefresherPollingInterval} says, or a minute.
106+
*
107+
* <p>Read here rather than left to the enricher, which parses the option inside the policy engine's
108+
* construction: a value that is not a number fails there, and one that is not positive fails a step
109+
* later in {@code Timer.schedule}, after the enricher has downloaded the store and started its refresher
110+
* thread. {@code RangerBasePlugin.setPolicies} catches both and leaves the plugin without an engine, which
111+
* {@link LoadedRangerPlugin#init} would refuse for the wrong reason - and, in the second case, with that
112+
* thread left behind. So both are refused before the load, by {@link #validate}.
113+
*
114+
* @throws IllegalArgumentException for a value that is not a positive number of milliseconds
115+
*/
116+
static long refreshIntervalMsOf(RangerPluginConfig config) {
117+
String property = RangerUserStoreEnricher.USERSTORE_REFRESHER_POLLINGINTERVAL_OPTION;
118+
String value = config.getTrimmed(property);
119+
if (value == null || value.isEmpty()) {
120+
return DEFAULT_REFRESH_INTERVAL_MS;
121+
}
122+
long intervalMs;
123+
try {
124+
intervalMs = Long.parseLong(value);
125+
} catch (NumberFormatException e) {
126+
throw new IllegalArgumentException("Ranger service " + config.getServiceName() + ": " + property + "="
127+
+ value + " is not a number of milliseconds", e);
128+
}
129+
if (intervalMs <= 0) {
130+
throw new IllegalArgumentException("Ranger service " + config.getServiceName() + ": " + property + "="
131+
+ value + " is not a positive number of milliseconds");
132+
}
133+
return intervalMs;
134+
}
135+
136+
/**
137+
* Refuses a configuration this cannot run with, before {@code RangerBasePlugin.init()} starts anything:
138+
* an opt-out that is neither true nor false, and a refresh interval that is not a positive number of
139+
* milliseconds. See {@link #enabledFor} and {@link #refreshIntervalMsOf} for where each would fail
140+
* otherwise, and how much worse.
141+
*
142+
* @throws IllegalArgumentException naming the property and its value
143+
*/
144+
public static void validate(RangerPluginConfig config) {
145+
if (config == null || !enabledFor(config)) {
146+
return;
147+
}
148+
refreshIntervalMsOf(config);
78149
}
79150

80151
/**
@@ -93,8 +164,7 @@ public static void addUserStoreEnricher(RangerPluginConfig config, ServicePolici
93164
}
94165
String retriever = config.get(RangerUserStoreEnricher.USERSTORE_RETRIEVER_CLASSNAME_OPTION,
95166
RangerAdminUserStoreRetriever.class.getCanonicalName());
96-
String refreshIntervalMs = config.get(RangerUserStoreEnricher.USERSTORE_REFRESHER_POLLINGINTERVAL_OPTION,
97-
DEFAULT_REFRESH_INTERVAL_MS);
167+
String refreshIntervalMs = Long.toString(refreshIntervalMsOf(config));
98168
// Ranger logs the addition itself, once per download that needed it; the operator-facing line about
99169
// why the store is downloaded at all is the plugin's, written once when it starts (see describe).
100170
if (ServiceDefUtil.addUserStoreEnricher(policies, retriever, refreshIntervalMs) && LOG.isDebugEnabled()) {

‎fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/test/java/org/apache/doris/catalog/authorizer/ranger/LoadedRangerPluginTest.java‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import org.apache.hadoop.conf.Configuration;
2323
import org.apache.ranger.admin.client.RangerAdminClient;
2424
import org.apache.ranger.authorization.utils.JsonUtils;
25+
import org.apache.ranger.plugin.contextenricher.RangerAbstractContextEnricher;
2526
import org.apache.ranger.plugin.model.RangerRole;
2627
import org.apache.ranger.plugin.model.RangerServiceDef;
28+
import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
2729
import org.apache.ranger.plugin.util.GrantRevokeRequest;
2830
import org.apache.ranger.plugin.util.GrantRevokeRoleRequest;
2931
import org.apache.ranger.plugin.util.RangerRoles;
@@ -165,6 +167,21 @@ public void revokeAccess(GrantRevokeRequest request) {
165167
}
166168
}
167169

170+
/**
171+
* A context enricher the policy engine cannot be built with: Ranger builds the enrichers a service
172+
* definition names while it builds the engine, and does not catch what their init() throws there.
173+
*/
174+
public static final class BrokenEnricher extends RangerAbstractContextEnricher {
175+
@Override
176+
public void init() {
177+
throw new IllegalStateException("broken on purpose");
178+
}
179+
180+
@Override
181+
public void enrich(RangerAccessRequest request) {
182+
}
183+
}
184+
168185
private static final class TestPlugin extends LoadedRangerPlugin {
169186
private TestPlugin(String cacheDir) {
170187
// Service type "test" reads ranger-test-*.xml, none of which exist here: what the load needs is set
@@ -230,6 +247,12 @@ private static boolean refresherRunning() {
230247
.anyMatch(thread -> thread.getName().startsWith("PolicyRefresher(serviceName=test)"));
231248
}
232249

250+
/** The thread the user store enricher starts to download the store, which cleanup() reaches through the engine. */
251+
private static boolean userStoreRefresherRunning() {
252+
return Thread.getAllStackTraces().keySet().stream()
253+
.anyMatch(thread -> thread.getName().startsWith("RangerUserStoreRefresher(serviceName=test)"));
254+
}
255+
233256
/** Built out of what the admin serves, groups included: the user store came with the policies. */
234257
@Test
235258
public void testBuiltWithWhatTheAdminServes() {
@@ -313,6 +336,65 @@ public void testAConfigurationTheLoadCannotUseIsRefusedWithItsCause() {
313336
Assertions.assertFalse(refresherRunning());
314337
}
315338

339+
/**
340+
* A refresh interval the enricher could not schedule is refused before the load starts. Left to the
341+
* enricher, a non-positive interval fails in Timer.schedule after the store is downloaded and the
342+
* refresher thread is up, inside the engine's construction: RangerBasePlugin.setPolicies catches it, the
343+
* plugin is refused as if it had no policies, and cleanup() cannot reach the thread through an engine
344+
* that was never built.
345+
*/
346+
@Test
347+
public void testRefusedBeforeTheLoadForAnIntervalTheEnricherCouldNotSchedule() {
348+
Admin.policies = policies(3L);
349+
Admin.userStore = new RangerUserStore(1L, null, null, ImmutableMap.of("user1", ImmutableSet.of("readers")));
350+
plugin = new TestPlugin(cacheDir.toString());
351+
plugin.getConfig().set("userStoreRefresherPollingInterval", "0");
352+
353+
IllegalArgumentException refused = Assertions.assertThrows(IllegalArgumentException.class, plugin::init);
354+
355+
Assertions.assertTrue(refused.getMessage().contains("userStoreRefresherPollingInterval=0"),
356+
refused.getMessage());
357+
Assertions.assertFalse(plugin.hasEngine(), "an engine was left behind");
358+
Assertions.assertFalse(refresherRunning(), "the policy refresher was started");
359+
Assertions.assertFalse(userStoreRefresherRunning(), "the user store refresher was started");
360+
}
361+
362+
/** An opt-out Hadoop's getBoolean would have read as the default is refused, not switched on. */
363+
@Test
364+
public void testRefusedBeforeTheLoadForASwitchThatIsNeitherTrueNorFalse() {
365+
Admin.policies = policies(3L);
366+
plugin = new TestPlugin(cacheDir.toString());
367+
plugin.getConfig().set(PREFIX + ".use.rangerGroups", "flase");
368+
369+
IllegalArgumentException refused = Assertions.assertThrows(IllegalArgumentException.class, plugin::init);
370+
371+
Assertions.assertTrue(refused.getMessage().contains(PREFIX + ".use.rangerGroups=flase"), refused.getMessage());
372+
Assertions.assertFalse(refresherRunning());
373+
}
374+
375+
/**
376+
* The policies arrived and Ranger could build no engine out of them - here, a context enricher on the
377+
* service definition whose init() throws; RangerBasePlugin.setPolicies logs that and keeps going with
378+
* no engine, the same state as no policies at all. Refused for what happened, not for an admin that
379+
* answered.
380+
*/
381+
@Test
382+
public void testRefusedWithPoliciesNoEngineCouldBeBuiltOutOf() {
383+
ServicePolicies policies = policies(3L);
384+
policies.getServiceDef().setContextEnrichers(Collections.singletonList(
385+
new RangerServiceDef.RangerContextEnricherDef(1L, "broken", BrokenEnricher.class.getName(), null)));
386+
Admin.policies = policies;
387+
plugin = new TestPlugin(cacheDir.toString());
388+
389+
IllegalStateException refused = Assertions.assertThrows(IllegalStateException.class, plugin::init);
390+
391+
Assertions.assertTrue(refused.getMessage().contains("policies (version 3) were downloaded, but no policy"
392+
+ " engine could be built out of them"), refused.getMessage());
393+
Assertions.assertFalse(refused.getMessage().contains("could not be reached"), refused.getMessage());
394+
Assertions.assertFalse(plugin.hasEngine(), "an engine was left behind");
395+
Assertions.assertFalse(refresherRunning());
396+
}
397+
316398
/** The seam the groups hang on: policies are handed on with the user store enricher on their definition. */
317399
@Test
318400
public void testThePoliciesAreHandedOnWithTheUserStoreEnricher() {

0 commit comments

Comments
 (0)