Search in sources :

Example 1 with PrincipalRole

use of com.yahoo.athenz.zms.PrincipalRole in project athenz by yahoo.

the class FileConnection method listPrincipalRoles.

@Override
public List<PrincipalRole> listPrincipalRoles(String principalName) {
    // we're going to go through all domains
    String[] fnames = rootDir.list();
    List<PrincipalRole> roles = new ArrayList<>();
    for (String fname : fnames) {
        File f = new File(rootDir, fname);
        DomainStruct domainStruct = null;
        try {
            Path path = Paths.get(f.toURI());
            domainStruct = JSON.fromBytes(Files.readAllBytes(path), DomainStruct.class);
        } catch (IOException e) {
        }
        if (domainStruct == null) {
            continue;
        }
        for (Role role : domainStruct.getRoles().values()) {
            List<RoleMember> roleMembers = role.getRoleMembers();
            if (roleMembers == null) {
                continue;
            }
            for (int idx = 0; idx < roleMembers.size(); idx++) {
                final String memberName = roleMembers.get(idx).getMemberName();
                if (memberName.equals(principalName)) {
                    PrincipalRole pRole = new PrincipalRole();
                    pRole.setDomainName(fname);
                    pRole.setRoleName(extractRoleName(fname, role.getName()));
                    roles.add(pRole);
                }
            }
        }
    }
    return roles;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) Role(com.yahoo.athenz.zms.Role) PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) File(java.io.File) RoleMember(com.yahoo.athenz.zms.RoleMember)

Example 2 with PrincipalRole

use of com.yahoo.athenz.zms.PrincipalRole in project athenz by yahoo.

the class JDBCConnectionTest method testListPrincipalRoles.

@Test
public void testListPrincipalRoles() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// principal id
    5);
    // principal roles
    Mockito.when(mockResultSet.next()).thenReturn(// get principal id
    true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_NAME)).thenReturn("coretech").thenReturn("sports").thenReturn("sports").thenReturn("weather");
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ROLE_NAME)).thenReturn("admin").thenReturn("reader").thenReturn("writer").thenReturn("reader");
    List<PrincipalRole> roles = jdbcConn.listPrincipalRoles("user.joe");
    assertEquals(4, roles.size());
    // get principal id
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.joe");
    // get role list
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    boolean coretech_admin = false;
    boolean sports_reader = false;
    boolean sports_writer = false;
    boolean weather_reader = false;
    for (PrincipalRole role : roles) {
        if (role.getDomainName().equals("coretech") && role.getRoleName().equals("admin")) {
            coretech_admin = true;
        } else if (role.getDomainName().equals("sports") && role.getRoleName().equals("reader")) {
            sports_reader = true;
        } else if (role.getDomainName().equals("sports") && role.getRoleName().equals("writer")) {
            sports_writer = true;
        } else if (role.getDomainName().equals("weather") && role.getRoleName().equals("reader")) {
            weather_reader = true;
        }
    }
    assertTrue(coretech_admin);
    assertTrue(sports_reader);
    assertTrue(sports_writer);
    assertTrue(weather_reader);
    jdbcConn.close();
}
Also used : PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 3 with PrincipalRole

use of com.yahoo.athenz.zms.PrincipalRole in project athenz by yahoo.

the class JDBCConnection method listPrincipalRoles.

@Override
public List<PrincipalRole> listPrincipalRoles(String principalName) {
    final String caller = "listPrincipalRoles";
    int principalId = getPrincipalId(principalName);
    if (principalId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_PRINCIPAL, principalName);
    }
    List<PrincipalRole> roles = new ArrayList<>();
    try (PreparedStatement ps = con.prepareStatement(SQL_LIST_PRINCIPAL_ROLES)) {
        ps.setInt(1, principalId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                PrincipalRole role = new PrincipalRole();
                role.setDomainName(rs.getString(ZMSConsts.DB_COLUMN_NAME));
                role.setRoleName(rs.getString(ZMSConsts.DB_COLUMN_ROLE_NAME));
                roles.add(role);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return roles;
}
Also used : PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

PrincipalRole (com.yahoo.athenz.zms.PrincipalRole)3 ArrayList (java.util.ArrayList)2 Role (com.yahoo.athenz.zms.Role)1 RoleMember (com.yahoo.athenz.zms.RoleMember)1 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Test (org.testng.annotations.Test)1