Search in sources :

Example 76 with Role

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

the class JDBCConnectionTest method testInsertRoleException.

@Test
public void testInsertRoleException() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Role role = new Role().setName("my-domain:role.role1");
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.doReturn(5).when(mockResultSet).getInt(1);
    Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    try {
        jdbcConn.insertRole("my-domain", role);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : Role(com.yahoo.athenz.zms.Role) PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 77 with Role

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

the class JDBCConnection method getRole.

@Override
public Role getRole(String domainName, String roleName) {
    final String caller = "getRole";
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_ROLE)) {
        ps.setString(1, domainName);
        ps.setString(2, roleName);
        try (ResultSet rs = executeQuery(ps, caller)) {
            if (rs.next()) {
                Role role = new Role().setName(ZMSUtils.roleResourceName(domainName, roleName)).setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime())).setTrust(saveValue(rs.getString(ZMSConsts.DB_COLUMN_TRUST)));
                return role;
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return null;
}
Also used : Role(com.yahoo.athenz.zms.Role) PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 78 with Role

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

the class JDBCConnection method getAthenzDomainRoles.

void getAthenzDomainRoles(String domainName, int domainId, AthenzDomain athenzDomain, String caller) {
    Map<String, Role> roleMap = new HashMap<>();
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_ROLES)) {
        ps.setInt(1, domainId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                String roleName = rs.getString(ZMSConsts.DB_COLUMN_NAME);
                Role role = new Role().setName(ZMSUtils.roleResourceName(domainName, roleName)).setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime())).setTrust(saveValue(rs.getString(ZMSConsts.DB_COLUMN_TRUST)));
                roleMap.put(roleName, role);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_ROLE_MEMBERS)) {
        ps.setInt(1, domainId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                String roleName = rs.getString(1);
                Role role = roleMap.get(roleName);
                if (role == null) {
                    continue;
                }
                List<RoleMember> members = role.getRoleMembers();
                if (members == null) {
                    members = new ArrayList<>();
                    role.setRoleMembers(members);
                }
                RoleMember roleMember = new RoleMember();
                roleMember.setMemberName(rs.getString(2));
                java.sql.Timestamp expiration = rs.getTimestamp(3);
                if (expiration != null) {
                    roleMember.setExpiration(Timestamp.fromMillis(expiration.getTime()));
                }
                members.add(roleMember);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    athenzDomain.getRoles().addAll(roleMap.values());
}
Also used : Role(com.yahoo.athenz.zms.Role) PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RoleMember(com.yahoo.athenz.zms.RoleMember)

Example 79 with Role

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

the class ZMSUtils method makeAdminRole.

public static Role makeAdminRole(String domainName, List<String> adminUsers) {
    List<RoleMember> roleMembers = new ArrayList<>();
    for (String admin : adminUsers) {
        RoleMember roleMember = new RoleMember();
        roleMember.setMemberName(admin);
        roleMembers.add(roleMember);
    }
    Role role = new Role().setName(roleResourceName(domainName, ZMSConsts.ADMIN_ROLE_NAME)).setRoleMembers(roleMembers);
    return role;
}
Also used : Role(com.yahoo.athenz.zms.Role) ArrayList(java.util.ArrayList) RoleMember(com.yahoo.athenz.zms.RoleMember)

Example 80 with Role

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

the class DataStoreTest method testProcessDomainRoles.

@Test
public void testProcessDomainRoles() {
    ChangeLogStore clogStore = new MockZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", pkey, "0");
    DataStore store = new DataStore(clogStore, null);
    List<Role> roles = new ArrayList<>();
    Role role = new Role();
    role.setName("coretech:role.admin");
    List<RoleMember> members = new ArrayList<>();
    members.add(new RoleMember().setMemberName("user_domain.user"));
    role.setRoleMembers(members);
    roles.add(role);
    role = new Role();
    role.setName("coretech:role.readers");
    members = new ArrayList<>();
    members.add(new RoleMember().setMemberName("user_domain.user"));
    role.setRoleMembers(members);
    roles.add(role);
    DomainData domainData = new DomainData();
    domainData.setName("coretech");
    domainData.setRoles(roles);
    DataCache dataCache = new DataCache();
    dataCache.setDomainData(domainData);
    store.processDomainRoles(domainData, dataCache);
    assertEquals(dataCache.getMemberRoleSet("user_domain.user").size(), 2);
    assertTrue(dataCache.getMemberRoleSet("user_domain.user").contains(new MemberRole("coretech:role.admin", 0)));
    assertTrue(dataCache.getMemberRoleSet("user_domain.user").contains(new MemberRole("coretech:role.readers", 0)));
}
Also used : Role(com.yahoo.athenz.zms.Role) MemberRole(com.yahoo.athenz.zts.cache.MemberRole) MemberRole(com.yahoo.athenz.zts.cache.MemberRole) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ArrayList(java.util.ArrayList) DomainData(com.yahoo.athenz.zms.DomainData) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) RoleMember(com.yahoo.athenz.zms.RoleMember) DataCache(com.yahoo.athenz.zts.cache.DataCache) Test(org.testng.annotations.Test)

Aggregations

Role (com.yahoo.athenz.zms.Role)94 Test (org.testng.annotations.Test)57 RoleMember (com.yahoo.athenz.zms.RoleMember)47 ArrayList (java.util.ArrayList)47 DomainData (com.yahoo.athenz.zms.DomainData)32 DataCache (com.yahoo.athenz.zts.cache.DataCache)31 PrincipalRole (com.yahoo.athenz.zms.PrincipalRole)27 Policy (com.yahoo.athenz.zms.Policy)22 SignedDomain (com.yahoo.athenz.zms.SignedDomain)22 Assertion (com.yahoo.athenz.zms.Assertion)20 MemberRole (com.yahoo.athenz.zts.cache.MemberRole)19 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)14 MockZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore)13 ZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore)12 ServiceIdentity (com.yahoo.athenz.zms.ServiceIdentity)11 SQLException (java.sql.SQLException)9 HashMap (java.util.HashMap)8 ResourceException (com.yahoo.athenz.zms.ResourceException)7 Domain (com.yahoo.athenz.zms.Domain)6 File (java.io.File)6