|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.catalog.authorizer.ranger; |
| 19 | + |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | +import org.apache.ranger.plugin.service.RangerBasePlugin; |
| 23 | +import org.apache.ranger.plugin.util.ServicePolicies; |
| 24 | + |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +/** |
| 28 | + * What the two Ranger plugins Doris embeds have in common: they are built with their service's policies or |
| 29 | + * not at all, and the requests built over them carry the groups Ranger keeps for a user. |
| 30 | + * |
| 31 | + * <p><b>Built with the policies, or not at all.</b> {@code RangerBasePlugin.init()} downloads the service's |
| 32 | + * roles and policies - and, with the enricher below on them, its user store - before it returns, and it |
| 33 | + * survives a Ranger admin it cannot reach: it answers out of the local policy cache |
| 34 | + * ({@code ranger.plugin.<type>.policy.cache.dir}) when there is one, and out of no policies at all when there |
| 35 | + * is not, refusing every check until a later poll succeeds. That is the right thing for a plugin embedded in |
| 36 | + * a service that has to keep running, and the wrong thing at the moment Doris binds one: an FE whose instance |
| 37 | + * scope {@code access_controller_type=ranger-doris} governs, and which has no policies, is an FE nobody can |
| 38 | + * use - no account bypasses the source - and a catalog bound to a {@code ranger-hive} source in that state |
| 39 | + * refuses every statement against it, in both cases with nothing but a line in fe.log to say why. So |
| 40 | + * {@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. |
| 45 | + * |
| 46 | + * <p>The user store is not part of that. A load that found the policies but no user store is logged and |
| 47 | + * accepted - policy items written against a group do not apply until the store arrives, which the enricher |
| 48 | + * keeps asking for - because "no store has arrived" cannot be told apart from "this admin serves none", and |
| 49 | + * an admin from before the user store download existed, or one that fails that download while serving the |
| 50 | + * policies, ran every deployment before groups were attached at all; see {@link RangerUserStoreGroups}. |
| 51 | + * |
| 52 | + * <p><b>Requests carry Ranger's own groups.</b> Doris has none to offer, and a policy item written against a |
| 53 | + * group matches nothing without them; the store they are read from is asked for with the policies, see |
| 54 | + * {@link #setPolicies} and {@link RangerUserStoreGroups}. |
| 55 | + */ |
| 56 | +public abstract class LoadedRangerPlugin extends RangerBasePlugin { |
| 57 | + private static final Logger LOG = LogManager.getLogger(LoadedRangerPlugin.class); |
| 58 | + |
| 59 | + protected LoadedRangerPlugin(String serviceType, String serviceName, String appId) { |
| 60 | + super(serviceType, serviceName, appId); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Loads the plugin - {@code RangerBasePlugin.init()}: roles, policies and user store, on this thread - |
| 65 | + * and refuses to leave it without policies; see the class comment. |
| 66 | + * |
| 67 | + * @throws IllegalStateException when the load ended with no policies from either the admin or the cache |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public void init() { |
| 71 | + LOG.info(RangerUserStoreGroups.describe(getConfig())); |
| 72 | + long startedAtNanos = System.nanoTime(); |
| 73 | + try { |
| 74 | + super.init(); |
| 75 | + if (getPoliciesVersion() < 0) { |
| 76 | + throw new IllegalStateException(describeNoPolicies()); |
| 77 | + } |
| 78 | + } catch (RuntimeException | Error e) { |
| 79 | + // Stops what the load had started before it failed - the policy refresher and its download |
| 80 | + // timer, and the engine if one was built - so that a plugin nobody will hold does not go on |
| 81 | + // polling the admin. A configuration the load could not use fails inside RangerBasePlugin.init() |
| 82 | + // before any of that is started, and stopping nothing is a no-op. |
| 83 | + try { |
| 84 | + cleanup(); |
| 85 | + } catch (RuntimeException | Error stopFailure) { |
| 86 | + e.addSuppressed(stopFailure); |
| 87 | + } |
| 88 | + throw e; |
| 89 | + } |
| 90 | + LOG.info("Ranger service {} loaded in {} ms: policies version {}, roles version {}, user store" |
| 91 | + + " version {}", getServiceName(), |
| 92 | + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAtNanos), getPoliciesVersion(), |
| 93 | + getRolesVersion(), getUserStoreVersion()); |
| 94 | + if (RangerUserStoreGroups.enabledFor(getConfig()) && getUserStoreVersion() < 0) { |
| 95 | + // Policies to answer out of, but no user store to read groups from: the admin could not be reached |
| 96 | + // for it and none was cached. Requests carry no groups until one arrives - the enricher keeps |
| 97 | + // asking - which is what this source sent before groups were attached at all, and which a policy |
| 98 | + // item written against a group, a deny included, does not match. |
| 99 | + LOG.warn("Ranger service {} loaded its policies but no user store: until one arrives, requests" |
| 100 | + + " carry no groups and policy items written against a group do not apply", getServiceName()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** Why the plugin was refused: what was asked for the policies, and where they could have been cached. */ |
| 105 | + private String describeNoPolicies() { |
| 106 | + String prefix = getConfig().getPropertyPrefix(); |
| 107 | + String adminUrl = getConfig().get(prefix + ".policy.rest.url"); |
| 108 | + String cacheDir = getConfig().get(prefix + ".policy.cache.dir"); |
| 109 | + return "Ranger service " + getServiceName() + " (type " + getServiceType() + ") has no policies to" |
| 110 | + + " authorize against: Ranger Admin at " + adminUrl + " could not be reached or does not know" |
| 111 | + + " the service, and " |
| 112 | + + (cacheDir == null |
| 113 | + ? "no policy cache directory is configured (" + prefix + ".policy.cache.dir)" |
| 114 | + : "no policy cache was found under " + cacheDir) |
| 115 | + + ". Fix ranger-" + getServiceType() + "-security.xml in fe/conf, or bring Ranger Admin back," |
| 116 | + + " and try again"; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Takes the policies Ranger downloaded, and asks for the user store with them, so that the requests the |
| 121 | + * source over this plugin builds can carry the groups Ranger keeps for a user; see |
| 122 | + * {@link RangerUserStoreGroups}. Done on the way in rather than left to Ranger, because Ranger only does |
| 123 | + * it on its own from 2.5 on, and behind a property. |
| 124 | + */ |
| 125 | + @Override |
| 126 | + public void setPolicies(ServicePolicies policies) { |
| 127 | + RangerUserStoreGroups.addUserStoreEnricher(getConfig(), policies); |
| 128 | + super.setPolicies(policies); |
| 129 | + } |
| 130 | +} |
0 commit comments