Search in sources :

Example 6 with RolePrincipalGrant

use of org.apache.hadoop.hive.metastore.api.RolePrincipalGrant in project hive by apache.

the class SQLStdHiveAccessController method getAllRoleAncestors.

/**
 * Add role names of parentRoles and its parents to processedRolesMap
 *
 * @param processedRolesMap
 * @param roleGrants
 * @throws TException
 * @throws HiveAuthzPluginException
 * @throws MetaException
 */
private void getAllRoleAncestors(Map<String, HiveRoleGrant> processedRolesMap, List<RolePrincipalGrant> roleGrants) throws MetaException, HiveAuthzPluginException, TException {
    for (RolePrincipalGrant parentRoleGrant : roleGrants) {
        String parentRoleName = parentRoleGrant.getRoleName();
        if (processedRolesMap.get(parentRoleName) == null) {
            // unprocessed role: get its parents, add it to processed, and call this
            // function recursively
            List<RolePrincipalGrant> nextParentRoles = getRoleGrants(parentRoleName, PrincipalType.ROLE);
            processedRolesMap.put(parentRoleName, new HiveRoleGrant(parentRoleGrant));
            getAllRoleAncestors(processedRolesMap, nextParentRoles);
        }
    }
}
Also used : RolePrincipalGrant(org.apache.hadoop.hive.metastore.api.RolePrincipalGrant) HiveRoleGrant(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveRoleGrant)

Example 7 with RolePrincipalGrant

use of org.apache.hadoop.hive.metastore.api.RolePrincipalGrant in project hive by apache.

the class HiveV1Authorizer method getRoleGrantInfoForPrincipal.

@Override
public List<HiveRoleGrant> getRoleGrantInfoForPrincipal(HivePrincipal principal) throws HiveAuthzPluginException, HiveAccessControlException {
    PrincipalType type = AuthorizationUtils.getThriftPrincipalType(principal.getType());
    try {
        List<HiveRoleGrant> grants = new ArrayList<HiveRoleGrant>();
        Hive hive = Hive.getWithFastCheck(this.conf);
        for (RolePrincipalGrant grant : hive.getRoleGrantInfoForPrincipal(principal.getName(), type)) {
            grants.add(new HiveRoleGrant(grant));
        }
        return grants;
    } catch (HiveException e) {
        throw new HiveAuthzPluginException(e);
    }
}
Also used : Hive(org.apache.hadoop.hive.ql.metadata.Hive) RolePrincipalGrant(org.apache.hadoop.hive.metastore.api.RolePrincipalGrant) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) ArrayList(java.util.ArrayList) PrincipalType(org.apache.hadoop.hive.metastore.api.PrincipalType)

Example 8 with RolePrincipalGrant

use of org.apache.hadoop.hive.metastore.api.RolePrincipalGrant in project hive by apache.

the class SQLStdHiveAccessController method getRolesFromMS.

private List<HiveRoleGrant> getRolesFromMS() throws HiveAuthzPluginException {
    try {
        List<RolePrincipalGrant> roles = getRoleGrants(currentUserName, PrincipalType.USER);
        Map<String, HiveRoleGrant> name2Rolesmap = new HashMap<String, HiveRoleGrant>();
        getAllRoleAncestors(name2Rolesmap, roles);
        List<HiveRoleGrant> currentRoles = new ArrayList<HiveRoleGrant>(roles.size());
        for (HiveRoleGrant role : name2Rolesmap.values()) {
            if (!HiveMetaStore.ADMIN.equalsIgnoreCase(role.getRoleName())) {
                currentRoles.add(role);
            } else {
                this.adminRole = role;
            }
        }
        return currentRoles;
    } catch (Exception e) {
        throw SQLAuthorizationUtils.getPluginException("Failed to retrieve roles for " + currentUserName, e);
    }
}
Also used : RolePrincipalGrant(org.apache.hadoop.hive.metastore.api.RolePrincipalGrant) HashMap(java.util.HashMap) HiveRoleGrant(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveRoleGrant) ArrayList(java.util.ArrayList) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) HiveAccessControlException(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException) HiveAuthzPluginException(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzPluginException) TException(org.apache.thrift.TException)

Example 9 with RolePrincipalGrant

use of org.apache.hadoop.hive.metastore.api.RolePrincipalGrant in project hive by apache.

the class HBaseStore method listRoleMembers.

@Override
public List<RolePrincipalGrant> listRoleMembers(String roleName) {
    boolean commit = false;
    openTransaction();
    try {
        HbaseMetastoreProto.RoleGrantInfoList gil = getHBase().getRolePrincipals(roleName);
        List<RolePrincipalGrant> roleMaps = new ArrayList<RolePrincipalGrant>(gil.getGrantInfoList().size());
        for (HbaseMetastoreProto.RoleGrantInfo giw : gil.getGrantInfoList()) {
            roleMaps.add(new RolePrincipalGrant(roleName, giw.getPrincipalName(), HBaseUtils.convertPrincipalTypes(giw.getPrincipalType()), giw.getGrantOption(), (int) giw.getAddTime(), giw.getGrantor(), HBaseUtils.convertPrincipalTypes(giw.getGrantorType())));
        }
        commit = true;
        return roleMaps;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        commitOrRoleBack(commit);
    }
}
Also used : RolePrincipalGrant(org.apache.hadoop.hive.metastore.api.RolePrincipalGrant) ArrayList(java.util.ArrayList) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) InvalidInputException(org.apache.hadoop.hive.metastore.api.InvalidInputException) InvalidPartitionException(org.apache.hadoop.hive.metastore.api.InvalidPartitionException) UnknownDBException(org.apache.hadoop.hive.metastore.api.UnknownDBException) UnknownTableException(org.apache.hadoop.hive.metastore.api.UnknownTableException) TException(org.apache.thrift.TException) UnknownPartitionException(org.apache.hadoop.hive.metastore.api.UnknownPartitionException) IOException(java.io.IOException) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException)

Example 10 with RolePrincipalGrant

use of org.apache.hadoop.hive.metastore.api.RolePrincipalGrant in project hive by apache.

the class TestHBaseStoreIntegration method grantRevokeRoles.

@Test
public void grantRevokeRoles() throws Exception {
    int now = (int) (System.currentTimeMillis() / 1000);
    String roleName1 = "role1";
    store.addRole(roleName1, "me");
    String roleName2 = "role2";
    store.addRole(roleName2, "me");
    Role role1 = store.getRole(roleName1);
    Role role2 = store.getRole(roleName2);
    store.grantRole(role1, "fred", PrincipalType.USER, "bob", PrincipalType.USER, false);
    store.grantRole(role2, roleName1, PrincipalType.ROLE, "admin", PrincipalType.ROLE, true);
    store.grantRole(role2, "fred", PrincipalType.USER, "admin", PrincipalType.ROLE, false);
    List<Role> roles = store.listRoles("fred", PrincipalType.USER);
    Assert.assertEquals(3, roles.size());
    boolean sawRole1 = false, sawRole2 = false, sawPublic = false;
    for (Role role : roles) {
        if (role.getRoleName().equals(roleName1)) {
            sawRole1 = true;
        } else if (role.getRoleName().equals(roleName2)) {
            sawRole2 = true;
        } else if (role.getRoleName().equals(HiveMetaStore.PUBLIC)) {
            sawPublic = true;
        } else {
            Assert.fail("Unknown role name " + role.getRoleName());
        }
    }
    Assert.assertTrue(sawRole1 && sawRole2 && sawPublic);
    roles = store.listRoles("fred", PrincipalType.ROLE);
    Assert.assertEquals(0, roles.size());
    roles = store.listRoles(roleName1, PrincipalType.ROLE);
    Assert.assertEquals(1, roles.size());
    Role role = roles.get(0);
    Assert.assertEquals(roleName2, role.getRoleName());
    // Test listing all members in a role
    List<RolePrincipalGrant> grants = store.listRoleMembers(roleName1);
    Assert.assertEquals(1, grants.size());
    Assert.assertEquals("fred", grants.get(0).getPrincipalName());
    Assert.assertEquals(PrincipalType.USER, grants.get(0).getPrincipalType());
    Assert.assertTrue("Expected grant time of " + now + " got " + grants.get(0).getGrantTime(), grants.get(0).getGrantTime() >= now);
    Assert.assertEquals("bob", grants.get(0).getGrantorName());
    Assert.assertEquals(PrincipalType.USER, grants.get(0).getGrantorPrincipalType());
    Assert.assertFalse(grants.get(0).isGrantOption());
    grants = store.listRoleMembers(roleName2);
    Assert.assertEquals(2, grants.size());
    boolean sawFred = false;
    sawRole1 = false;
    for (RolePrincipalGrant m : grants) {
        if ("fred".equals(m.getPrincipalName()))
            sawFred = true;
        else if (roleName1.equals(m.getPrincipalName()))
            sawRole1 = true;
        else
            Assert.fail("Unexpected principal " + m.getPrincipalName());
    }
    Assert.assertTrue(sawFred && sawRole1);
    // Revoke a role with grant option, make sure it just goes to no grant option
    store.revokeRole(role2, roleName1, PrincipalType.ROLE, true);
    roles = store.listRoles(roleName1, PrincipalType.ROLE);
    Assert.assertEquals(1, roles.size());
    Assert.assertEquals(roleName2, roles.get(0).getRoleName());
    grants = store.listRoleMembers(roleName1);
    Assert.assertFalse(grants.get(0).isGrantOption());
    // Drop a role, make sure it is properly removed from the map
    store.removeRole(roleName1);
    roles = store.listRoles("fred", PrincipalType.USER);
    Assert.assertEquals(2, roles.size());
    sawRole2 = sawPublic = false;
    for (Role m : roles) {
        if (m.getRoleName().equals(roleName2))
            sawRole2 = true;
        else if (m.getRoleName().equals(HiveMetaStore.PUBLIC))
            sawPublic = true;
        else
            Assert.fail("Unknown role " + m.getRoleName());
    }
    Assert.assertTrue(sawRole2 && sawPublic);
    roles = store.listRoles(roleName1, PrincipalType.ROLE);
    Assert.assertEquals(0, roles.size());
    // Revoke a role without grant option, make sure it goes away
    store.revokeRole(role2, "fred", PrincipalType.USER, false);
    roles = store.listRoles("fred", PrincipalType.USER);
    Assert.assertEquals(1, roles.size());
    Assert.assertEquals(HiveMetaStore.PUBLIC, roles.get(0).getRoleName());
}
Also used : Role(org.apache.hadoop.hive.metastore.api.Role) RolePrincipalGrant(org.apache.hadoop.hive.metastore.api.RolePrincipalGrant) Test(org.junit.Test)

Aggregations

RolePrincipalGrant (org.apache.hadoop.hive.metastore.api.RolePrincipalGrant)11 ArrayList (java.util.ArrayList)8 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)4 HiveRoleGrant (org.apache.hadoop.hive.ql.security.authorization.plugin.HiveRoleGrant)4 TException (org.apache.thrift.TException)4 IOException (java.io.IOException)2 InvalidInputException (org.apache.hadoop.hive.metastore.api.InvalidInputException)2 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)2 InvalidPartitionException (org.apache.hadoop.hive.metastore.api.InvalidPartitionException)2 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)2 Role (org.apache.hadoop.hive.metastore.api.Role)2 UnknownDBException (org.apache.hadoop.hive.metastore.api.UnknownDBException)2 UnknownPartitionException (org.apache.hadoop.hive.metastore.api.UnknownPartitionException)2 UnknownTableException (org.apache.hadoop.hive.metastore.api.UnknownTableException)2 MRoleMap (org.apache.hadoop.hive.metastore.model.MRoleMap)2 HiveAccessControlException (org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException)2 HiveAuthzPluginException (org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzPluginException)2 HashMap (java.util.HashMap)1 GetPrincipalsInRoleRequest (org.apache.hadoop.hive.metastore.api.GetPrincipalsInRoleRequest)1 GetPrincipalsInRoleResponse (org.apache.hadoop.hive.metastore.api.GetPrincipalsInRoleResponse)1