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();
}
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;
}
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());
}
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;
}
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)));
}
Aggregations