15 package com.cloudera.impala.util;
17 import java.util.List;
19 import org.apache.sentry.SentryUserException;
20 import org.apache.sentry.provider.db.SentryAccessDeniedException;
21 import org.apache.sentry.provider.db.SentryAlreadyExistsException;
22 import org.apache.sentry.provider.db.service.thrift.SentryPolicyServiceClient;
23 import org.apache.sentry.provider.db.service.thrift.TSentryGrantOption;
24 import org.apache.sentry.provider.db.service.thrift.TSentryPrivilege;
25 import org.apache.sentry.provider.db.service.thrift.TSentryRole;
26 import org.apache.sentry.service.thrift.SentryServiceClientFactory;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
36 import com.cloudera.impala.thrift.TPrivilege;
37 import com.cloudera.impala.thrift.TPrivilegeLevel;
38 import com.cloudera.impala.thrift.TPrivilegeScope;
39 import com.google.common.collect.Lists;
45 private final static Logger
LOG = LoggerFactory.getLogger(SentryPolicyService.class);
47 "User '%s' does not have privileges to execute: %s";
56 private final SentryPolicyServiceClient
client_;
68 public SentryPolicyServiceClient
get() {
83 SentryPolicyServiceClient client;
85 client = SentryServiceClientFactory.create(config_.getConfig());
86 }
catch (Exception e) {
87 throw new InternalException(
"Error creating Sentry Service client: ", e);
105 public void dropRole(
User requestingUser, String roleName,
boolean ifExists)
107 LOG.trace(String.format(
"Dropping role: %s on behalf of: %s", roleName,
108 requestingUser.getName()));
112 client.get().dropRoleIfExists(requestingUser.getShortName(), roleName);
114 client.get().
dropRole(requestingUser.getShortName(), roleName);
116 }
catch (SentryAccessDeniedException e) {
118 requestingUser.getName(),
"DROP_ROLE"));
119 }
catch (SentryUserException e) {
134 public void createRole(
User requestingUser, String roleName,
boolean ifNotExists)
136 LOG.trace(String.format(
"Creating role: %s on behalf of: %s", roleName,
137 requestingUser.getName()));
140 client.get().
createRole(requestingUser.getShortName(), roleName);
141 }
catch (SentryAccessDeniedException e) {
143 requestingUser.getName(),
"CREATE_ROLE"));
144 }
catch (SentryAlreadyExistsException e) {
145 if (ifNotExists)
return;
147 }
catch (SentryUserException e) {
164 LOG.trace(String.format(
"Granting role '%s' to group '%s' on behalf of: %s",
165 roleName, groupName, requestingUser.getName()));
168 client.get().
grantRoleToGroup(requestingUser.getShortName(), groupName, roleName);
169 }
catch (SentryAccessDeniedException e) {
171 requestingUser.getName(),
"GRANT_ROLE"));
172 }
catch (SentryUserException e) {
174 "Error making 'grantRoleToGroup' RPC to Sentry Service: ", e);
191 LOG.trace(String.format(
"Revoking role '%s' from group '%s' on behalf of: %s",
192 roleName, groupName, requestingUser.getName()));
196 groupName, roleName);
197 }
catch (SentryAccessDeniedException e) {
199 requestingUser.getName(),
"REVOKE_ROLE"));
200 }
catch (SentryUserException e) {
202 "Error making 'revokeRoleFromGroup' RPC to Sentry Service: ", e);
218 LOG.trace(String.format(
"Granting role '%s' privilege '%s' on '%s' on behalf of: %s",
219 roleName, privilege.toString(), privilege.getScope().toString(),
220 requestingUser.getName()));
223 switch (privilege.getScope()) {
225 client.get().grantServerPrivilege(requestingUser.getShortName(), roleName,
226 privilege.getServer_name(), privilege.isHas_grant_opt());
229 client.get().grantDatabasePrivilege(requestingUser.getShortName(), roleName,
230 privilege.getServer_name(), privilege.getDb_name(),
231 privilege.getPrivilege_level().toString(),
232 privilege.isHas_grant_opt());
235 String tblName = privilege.getTable_name();
236 String dbName = privilege.getDb_name();
237 client.get().grantTablePrivilege(requestingUser.getShortName(), roleName,
238 privilege.getServer_name(), dbName, tblName,
239 privilege.getPrivilege_level().toString(),
240 privilege.isHas_grant_opt());
243 client.get().grantURIPrivilege(requestingUser.getShortName(),
244 roleName, privilege.getServer_name(), privilege.getUri(),
245 privilege.isHas_grant_opt());
248 }
catch (SentryAccessDeniedException e) {
250 requestingUser.getName(),
"GRANT_PRIVILEGE"));
251 }
catch (SentryUserException e) {
253 "Error making 'grantPrivilege*' RPC to Sentry Service: ", e);
269 LOG.trace(String.format(
"Revoking role '%s' privilege '%s' on '%s' on behalf of: %s",
270 roleName, privilege.toString(), privilege.getScope().toString(),
271 requestingUser.getName()));
274 switch (privilege.getScope()) {
276 client.get().revokeServerPrivilege(requestingUser.getShortName(), roleName,
277 privilege.getServer_name(), null);
280 client.get().revokeDatabasePrivilege(requestingUser.getShortName(), roleName,
281 privilege.getServer_name(), privilege.getDb_name(),
282 privilege.getPrivilege_level().toString(), null);
285 String tblName = privilege.getTable_name();
286 String dbName = privilege.getDb_name();
287 client.get().revokeTablePrivilege(requestingUser.getShortName(), roleName,
288 privilege.getServer_name(), dbName, tblName,
289 privilege.getPrivilege_level().toString(),
293 client.get().revokeURIPrivilege(requestingUser.getShortName(),
294 roleName, privilege.getServer_name(), privilege.getUri(),
298 }
catch (SentryAccessDeniedException e) {
300 requestingUser.getName(),
"REVOKE_PRIVILEGE"));
301 }
catch (SentryUserException e) {
303 "Error making 'revokePrivilege*' RPC to Sentry Service: ", e);
317 requestingUser.getShortName()));
318 }
catch (SentryAccessDeniedException e) {
320 requestingUser.getName(),
"LIST_USER_ROLES"));
321 }
catch (SentryUserException e) {
323 "Error making 'listUserRoles' RPC to Sentry Service: ", e);
335 return Lists.newArrayList(client.get().listRoles(requestingUser.getShortName()));
336 }
catch (SentryAccessDeniedException e) {
338 requestingUser.getName(),
"LIST_ROLES"));
339 }
catch (SentryUserException e) {
340 throw new InternalException(
"Error making 'listRoles' RPC to Sentry Service: ", e);
353 return Lists.newArrayList(client.get().listAllPrivilegesByRoleName(
354 requestingUser.getShortName(), roleName));
355 }
catch (SentryAccessDeniedException e) {
357 requestingUser.getName(),
"LIST_ROLE_PRIVILEGES"));
358 }
catch (SentryUserException e) {
359 throw new InternalException(
"Error making 'listAllPrivilegesByRoleName' RPC to " +
360 "Sentry Service: ", e);
370 TPrivilege privilege =
new TPrivilege();
371 privilege.setServer_name(sentryPriv.getServerName());
372 if (sentryPriv.isSetDbName()) privilege.setDb_name(sentryPriv.getDbName());
373 if (sentryPriv.isSetTableName()) privilege.setTable_name(sentryPriv.getTableName());
374 if (sentryPriv.isSetURI()) privilege.setUri(sentryPriv.getURI());
375 privilege.setScope(Enum.valueOf(TPrivilegeScope.class,
376 sentryPriv.getPrivilegeScope().toUpperCase()));
377 if (sentryPriv.getAction().equals(
"*")) {
380 privilege.setPrivilege_level(Enum.valueOf(TPrivilegeLevel.class,
381 sentryPriv.getAction().toUpperCase()));
383 privilege.setPrivilege_name(RolePrivilege.buildRolePrivilegeName(privilege));
384 privilege.setCreate_time_ms(sentryPriv.getCreateTime());
385 if (sentryPriv.isSetGrantOption() &&
386 sentryPriv.getGrantOption() == TSentryGrantOption.TRUE) {
387 privilege.setHas_grant_opt(
true);
389 privilege.setHas_grant_opt(
false);
static TPrivilege sentryPrivilegeToTPrivilege(TSentryPrivilege sentryPriv)
final SentryPolicyServiceClient client_
void dropRole(User requestingUser, String roleName, boolean ifExists)
SentryPolicyServiceClient createClient()
List< TSentryRole > listUserRoles(User requestingUser)
final SentryConfig config_
void grantRoleToGroup(User requestingUser, String roleName, String groupName)
final String ACCESS_DENIED_ERROR_MSG
List< TSentryRole > listAllRoles(User requestingUser)
List< TSentryPrivilege > listRolePrivileges(User requestingUser, String roleName)
void revokeRoleFromGroup(User requestingUser, String roleName, String groupName)
void revokeRolePrivilege(User requestingUser, String roleName, TPrivilege privilege)
void grantRolePrivilege(User requestingUser, String roleName, TPrivilege privilege)
void createRole(User requestingUser, String roleName, boolean ifNotExists)
SentryPolicyService(SentryConfig config)