SentryPolicyServiceClientDefaultImpl implements SentryPolicyServiceClient, SentryConnection { @Override public Set<TSentryPrivilege> listPrivilegesByUserName(String requestorUserName, String userName, List<? extends Authorizable> authorizable) throws SentryUserException { TListSentryPrivilegesRequest request = new TListSentryPrivilegesRequest(); request.setProtocol_version(ThriftConstants.TSENTRY_SERVICE_VERSION_CURRENT); request.setRequestorUserName(requestorUserName); request.setRoleName(""); request.setPrincipalName(userName); if (authorizable != null && !authorizable.isEmpty()) { TSentryAuthorizable tSentryAuthorizable = setupSentryAuthorizable(authorizable); request.setAuthorizableHierarchy(tSentryAuthorizable); } TListSentryPrivilegesResponse response; try { response = client.list_sentry_privileges_by_user(request); if (response == null) { throw new SentryUserException("The Sentry server has returned a NULL response. " + "See the Sentry server logs for more information about the error."); } Status.throwIfNotOk(response.getStatus()); return response.getPrivileges(); } catch (TException e) { throw new SentryUserException(THRIFT_EXCEPTION_MESSAGE, e); } } SentryPolicyServiceClientDefaultImpl(Configuration conf,
                                              SentryTransportPool transportPool); @Override void connect(); @Override boolean isAdmin(String userName); @Override void createRole(String requestorUserName, String roleName); @Override void dropRole(String requestorUserName,
                                    String roleName); @Override void dropRoleIfExists(String requestorUserName,
                                            String roleName); @Override Set<TSentryRole> listRolesByGroupName(
    String requestorUserName,
    String groupName); @Override Set<TSentryRole> listRolesByUserName(String requestorUserName, String userName); @Override Set<TSentryPrivilege> listAllPrivilegesByRoleName(String requestorUserName,
                                                                        String roleName); @Override Set<TSentryPrivilege> listPrivilegesByRoleName(String requestorUserName,
                                                                     String roleName, List<? extends Authorizable> authorizable); @Override Set<TSentryPrivilege> listAllPrivilegesByUserName(String requestorUserName,
                                                                    String userName); @Override Set<TSentryPrivilege> listPrivilegesByUserName(String requestorUserName, String userName,
                                                                  List<? extends Authorizable> authorizable); @Override Set<TSentryRole> listAllRoles(String requestorUserName); @Override Set<TSentryRole> listUserRoles(String requestorUserName); @Override TSentryPrivilege grantURIPrivilege(String requestorUserName,
                                                         String roleName, String server, String uri); @Override TSentryPrivilege grantURIPrivilege(String requestorUserName,
                                                         String roleName, String server, String uri, Boolean grantOption); @Override void grantServerPrivilege(String requestorUserName,
                                                String roleName, String server, String action); @Deprecated /*** * Should use grantServerPrivilege(String requestorUserName, * String roleName, String server, String action, Boolean grantOption) */ TSentryPrivilege grantServerPrivilege(String requestorUserName,
                                                            String roleName, String server, Boolean grantOption); @Override TSentryPrivilege grantServerPrivilege(String requestorUserName,
                                                            String roleName, String server, String action, Boolean grantOption); @Override TSentryPrivilege grantDatabasePrivilege(String requestorUserName,
                                                              String roleName, String server, String db, String action); @Override TSentryPrivilege grantDatabasePrivilege(String requestorUserName,
                                                              String roleName, String server, String db, String action, Boolean grantOption); @Override TSentryPrivilege grantTablePrivilege(String requestorUserName,
                                                           String roleName, String server, String db, String table, String action); @Override TSentryPrivilege grantTablePrivilege(String requestorUserName,
                                                           String roleName, String server, String db, String table, String action, Boolean grantOption); @Override TSentryPrivilege grantColumnPrivilege(String requestorUserName,
                                                            String roleName, String server, String db, String table, String columnName, String action); @Override TSentryPrivilege grantColumnPrivilege(String requestorUserName,
                                                            String roleName, String server, String db, String table, String columnName, String action, Boolean grantOption); @Override Set<TSentryPrivilege> grantColumnsPrivileges(String requestorUserName,
                                                                   String roleName, String server, String db, String table, List<String> columnNames, String action); @Override Set<TSentryPrivilege> grantColumnsPrivileges(String requestorUserName,
                                                                   String roleName, String server, String db, String table, List<String> columnNames, String action, Boolean grantOption); @Override Set<TSentryPrivilege> grantPrivileges(
    String requestorUserName, String roleName,
    Set<TSentryPrivilege> privileges); @Override TSentryPrivilege grantPrivilege(String requestorUserName, String roleName,
                                                      TSentryPrivilege privilege); @VisibleForTesting static TSentryAuthorizable setupSentryAuthorizable(
    List<? extends Authorizable> authorizable); @Override void revokePrivileges(String requestorUserName, String roleName, Set<TSentryPrivilege> privileges); @Override void revokePrivilege(String requestorUserName, String roleName, TSentryPrivilege privilege); @Override void revokeURIPrivilege(String requestorUserName,
                                              String roleName, String server, String uri); @Override void revokeURIPrivilege(String requestorUserName,
                                              String roleName, String server, String uri, Boolean grantOption); @Override void revokeServerPrivilege(String requestorUserName,
                                                 String roleName, String server, String action); void revokeServerPrivilege(String requestorUserName,
                                                 String roleName, String server, String action, Boolean grantOption); @Deprecated /*** * Should use revokeServerPrivilege(String requestorUserName, * String roleName, String server, String action, Boolean grantOption) */ @Override void revokeServerPrivilege(String requestorUserName,
                                                 String roleName, String server, boolean grantOption); @Override void revokeDatabasePrivilege(String requestorUserName,
                                                   String roleName, String server, String db, String action); @Override void revokeDatabasePrivilege(String requestorUserName,
                                                   String roleName, String server, String db, String action, Boolean grantOption); @Override void revokeTablePrivilege(String requestorUserName,
                                                String roleName, String server, String db, String table, String action); @Override void revokeTablePrivilege(String requestorUserName,
                                                String roleName, String server, String db, String table, String action, Boolean grantOption); @Override void revokeColumnPrivilege(String requestorUserName, String roleName,
                                                 String server, String db, String table, String columnName, String action); @Override void revokeColumnPrivilege(String requestorUserName, String roleName,
                                                 String server, String db, String table, String columnName, String action, Boolean grantOption); @Override void revokeColumnsPrivilege(String requestorUserName, String roleName,
                                                  String server, String db, String table, List<String> columns, String action); @Override void revokeColumnsPrivilege(String requestorUserName, String roleName,
                                                  String server, String db, String table, List<String> columns, String action, Boolean grantOption); @Override Set<String> listPrivilegesForProvider(Set<String> groups, Set<String> users,
     ActiveRoleSet roleSet, Authorizable... authorizable); @Override void grantRoleToGroup(String requestorUserName,
                                            String groupName, String roleName); @Override void revokeRoleFromGroup(String requestorUserName,
                                               String groupName, String roleName); @Override void grantRoleToGroups(String requestorUserName,
                                             String roleName, Set<String> groups); @Override void revokeRoleFromGroups(String requestorUserName,
                                                String roleName, Set<String> groups); @Override void grantRoleToUser(String requestorUserName, String userName,
                                           String roleName); @Override void revokeRoleFromUser(String requestorUserName, String userName,
                                              String roleName); @Override void grantRoleToUsers(String requestorUserName, String roleName,
                                            Set<String> users); @Override void revokeRoleFromUsers(String requestorUserName, String roleName,
                                               Set<String> users); @Override void dropPrivileges(String requestorUserName,
                                          List<? extends Authorizable> authorizableObjects); @Override void renamePrivileges(String requestorUserName,
                                            List<? extends Authorizable> oldAuthorizables,
                                            List<? extends Authorizable> newAuthorizables); @Override Map<TSentryAuthorizable, TSentryPrivilegeMap> listPrivilegesbyAuthorizable(
    String requestorUserName, Set<List<? extends Authorizable>> authorizables, Set<String> groups,
    ActiveRoleSet roleSet); @Override Map<TSentryAuthorizable, TSentryPrivilegeMap> listPrivilegesbyAuthorizable(
      String requestorUserName,
      Set<List<? extends Authorizable>> authorizables, Set<String> groups,
      Set<String> users, ActiveRoleSet roleSet); @Override SentryObjectPrivileges getAllPrivilegesbyAuthorizable(
    String requestorUserName,
    Set<List<? extends Authorizable>> authorizables, Set<String> groups,
    Set<String> users, ActiveRoleSet roleSet); TListSentryPrivilegesByAuthResponse getSentryPrivilegeByAuthResponse(String requestorUserName,
      Set<List<? extends Authorizable>> authorizables, Set<String> groups, Set<String> users,
      ActiveRoleSet roleSet); @Override String getConfigValue(String propertyName, String defaultValue); @Override void importPolicy(Map<String, Map<String, Set<String>>> policyFileMappingData,
   String requestorUserName, boolean isOverwriteRole); @Override Map<String, Map<String, Set<String>>> exportPolicy(String requestorUserName,
      String objects); @Override void close(); @Override void done(); @Override void invalidate(); long syncNotifications(long id); long notifyHmsEvent(String requestorUserName, long eventId, String eventType,
    TSentryPrincipalType ownerType, String ownerName, TSentryAuthorizable authorizable); @Override Map<String, Set<TSentryPrivilege>> listAllRolesPrivileges(String requestorUserName); @Override Map<String, Set<TSentryPrivilege>> listAllUsersPrivileges(String requestorUserName);  }
@Test public void testListPrivilegesByUserName() throws SentryUserException, TException { Set<TSentryPrivilege> privileges; Mockito.when(mockClient.list_sentry_privileges_by_user( listSentryPrivilegesRequest("admin", "user1", Arrays.asList(new Table("t1"))))) .thenReturn(listSentryPrivilegesResponse( Sets.newHashSet( newSentryPrivilege("database", "db1", "t1", "select") ))); privileges = sentryClient.listPrivilegesByUserName("admin", "user1", Arrays.asList(new Table("t1"))); assertEquals(1, privileges.size()); assertTrue(privileges.contains(newSentryPrivilege("database", "db1", "t1", "select"))); }