15 package com.cloudera.impala.util;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.TimeUnit;
22 import org.apache.log4j.Logger;
23 import org.apache.sentry.provider.db.service.thrift.TSentryGroup;
24 import org.apache.sentry.provider.db.service.thrift.TSentryPrivilege;
25 import org.apache.sentry.provider.db.service.thrift.TSentryRole;
36 import com.cloudera.impala.thrift.TPrivilege;
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.Sets;
53 private static final Logger
LOG = Logger.getLogger(SentryProxy.class);
58 Executors.newScheduledThreadPool(1);
71 Preconditions.checkNotNull(catalog);
72 Preconditions.checkNotNull(sentryConfig);
77 policyReader_.scheduleAtFixedRate(
new PolicyReader(), 0, 60,
97 synchronized (SentryProxy.this) {
100 Set<String> rolesToRemove = catalog_.getAuthPolicy().getAllRoleNames();
103 for (TSentryRole sentryRole:
107 rolesToRemove.remove(sentryRole.getRoleName().toLowerCase());
109 Set<String> grantGroups = Sets.newHashSet();
110 for (TSentryGroup group: sentryRole.getGroups()) {
111 grantGroups.add(group.getGroupName());
114 catalog_.getAuthPolicy().getRole(sentryRole.getRoleName());
117 if (existingRole != null &&
121 role = catalog_.addRole(sentryRole.getRoleName(), grantGroups);
127 Set<String> privilegesToRemove = role.getPrivilegeNames();
130 for (TSentryPrivilege sentryPriv:
132 TPrivilege thriftPriv =
133 SentryPolicyService.sentryPrivilegeToTPrivilege(sentryPriv);
134 thriftPriv.setRole_id(role.getId());
135 privilegesToRemove.remove(thriftPriv.getPrivilege_name().toLowerCase());
138 role.getPrivilege(thriftPriv.getPrivilege_name());
140 if (existingPriv != null &&
141 existingPriv.getCreateTimeMs() == sentryPriv.getCreateTime()) {
144 catalog_.addRolePrivilege(role.getName(), thriftPriv);
148 for (String privilegeName: privilegesToRemove) {
149 TPrivilege privilege =
new TPrivilege();
150 privilege.setPrivilege_name(privilegeName);
151 catalog_.removeRolePrivilege(role.getName(), privilege);
154 }
catch (Exception e) {
155 LOG.error(
"Error refreshing Sentry policy: ", e);
161 for (String roleName: rolesToRemove) {
162 catalog_.removeRole(roleName);
181 sentryPolicyService_.listAllRoles(requestingUser);
184 "privileges to access the requested policy metadata or Sentry Service is " +
185 "unavailable.", requestingUser.getName()));
206 sentryPolicyService_.createRole(user, roleName,
false);
208 role = catalog_.addRole(roleName, Sets.<String>newHashSet());
220 sentryPolicyService_.dropRole(user, roleName,
false);
221 return catalog_.removeRole(roleName);
233 sentryPolicyService_.grantRoleToGroup(user, roleName, groupName);
234 return catalog_.addRoleGrantGroup(roleName, groupName);
246 sentryPolicyService_.revokeRoleFromGroup(user, roleName, groupName);
247 return catalog_.removeRoleGrantGroup(roleName, groupName);
259 sentryPolicyService_.grantRolePrivilege(user, roleName, privilege);
260 return catalog_.addRolePrivilege(roleName, privilege);
273 if (!privilege.isHas_grant_opt()) {
274 sentryPolicyService_.revokeRolePrivilege(user, roleName, privilege);
275 return catalog_.removeRolePrivilege(roleName, privilege);
279 RolePrivilege existingPriv = catalog_.getRolePrivilege(roleName, privilege);
280 if (existingPriv == null || !existingPriv.
toThrift().isHas_grant_opt()) {
287 sentryPolicyService_.revokeRolePrivilege(user, roleName, privilege);
288 TPrivilege updatedPriv = existingPriv.toThrift();
289 updatedPriv.setHas_grant_opt(
false);
290 sentryPolicyService_.grantRolePrivilege(user, roleName, updatedPriv);
291 return catalog_.addRolePrivilege(roleName, updatedPriv);
303 }
catch (Exception e) {
306 throw new ImpalaRuntimeException(
"Error refreshing authorization policy, " +
307 "current policy state may be inconsistent. Running 'invalidate metadata' " +
308 "may resolve this problem: ", e);
synchronized Role grantRoleGroup(User user, String roleName, String groupName)
final SentryPolicyService sentryPolicyService_
void checkUserSentryAdmin(User requestingUser)
List< TSentryRole > listAllRoles(User requestingUser)
List< TSentryPrivilege > listRolePrivileges(User requestingUser, String roleName)
final ScheduledExecutorService policyReader_
final CatalogServiceCatalog catalog_
Set< String > getGrantGroups()
AuthorizationPolicy getAuthPolicy()
SentryProxy(SentryConfig sentryConfig, CatalogServiceCatalog catalog)
synchronized Role createRole(User user, String roleName)
synchronized Role dropRole(User user, String roleName)
synchronized RolePrivilege revokeRolePrivilege(User user, String roleName, TPrivilege privilege)
synchronized Role revokeRoleGroup(User user, String roleName, String groupName)
synchronized RolePrivilege grantRolePrivilege(User user, String roleName, TPrivilege privilege)