use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class DataStoreTest method testAddDomainToCacheNewDomain.
@Test
public void testAddDomainToCacheNewDomain() {
ChangeLogStore clogStore = new MockZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", pkey, "0");
DataStore store = new DataStore(clogStore, null);
DataCache dataCache = new DataCache();
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);
List<Role> roles = new ArrayList<>();
roles.add(role);
DomainData domainData = new DomainData();
domainData.setRoles(roles);
dataCache.setDomainData(domainData);
store.addDomainToCache("coretech", dataCache);
DomainData domain = store.getDomainData("coretech");
assertNotNull(domain);
assertEquals(domain.getRoles().size(), 1);
assertEquals(domain.getRoles().get(0).getName(), "coretech:role.admin");
assertEquals(domain.getRoles().get(0).getRoleMembers().size(), 1);
assertEquals(domain.getRoles().get(0).getRoleMembers().get(0).getMemberName(), "user_domain.user");
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class DataStoreTest method testAddDomainToCacheUpdatedDomain.
@Test
public void testAddDomainToCacheUpdatedDomain() {
ChangeLogStore clogStore = new MockZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", pkey, "0");
DataStore store = new DataStore(clogStore, null);
DataCache dataCache = new DataCache();
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);
List<Role> roles = new ArrayList<>();
roles.add(role);
DomainData domainData = new DomainData();
domainData.setRoles(roles);
dataCache.setDomainData(domainData);
store.addDomainToCache("coretech", dataCache);
/* update member list */
role = new Role();
role.setName("coretech:role.admin");
members = new ArrayList<>();
members.add(new RoleMember().setMemberName("user_domain.user1"));
members.add(new RoleMember().setMemberName("user_domain.user2"));
role.setRoleMembers(members);
roles = new ArrayList<>();
roles.add(role);
dataCache = new DataCache();
domainData = new DomainData();
domainData.setRoles(roles);
dataCache.setDomainData(domainData);
store.addDomainToCache("coretech", dataCache);
DomainData domain = store.getDomainData("coretech");
assertNotNull(domain);
assertEquals(domain.getRoles().size(), 1);
assertEquals(domain.getRoles().get(0).getName(), "coretech:role.admin");
assertEquals(domain.getRoles().get(0).getRoleMembers().size(), 2);
boolean user1 = false;
boolean user2 = false;
for (RoleMember member : domain.getRoles().get(0).getRoleMembers()) {
switch(member.getMemberName()) {
case "user_domain.user1":
user1 = true;
break;
case "user_domain.user2":
user2 = true;
break;
}
}
assertTrue(user1);
assertTrue(user2);
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class FileConnection method updateRole.
@Override
public boolean updateRole(String domainName, Role role) {
DomainStruct domainStruct = getDomainStruct(domainName);
if (domainStruct == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "updateRole");
}
if (domainStruct.getRoles() == null) {
domainStruct.setRoles(new HashMap<String, Role>());
}
HashMap<String, Role> roles = domainStruct.getRoles();
String roleName = extractRoleName(domainName, role.getName());
if (roleName == null) {
throw ZMSUtils.error(ResourceException.BAD_REQUEST, "invalid role name", "updateRole");
}
// here we only need to update the main attrs and not
// the members
Role originalRole = getRoleObject(domainStruct, roleName);
List<RoleMember> members = role.getRoleMembers();
if (originalRole != null) {
role.setRoleMembers(originalRole.getRoleMembers());
} else {
role.setRoleMembers(null);
}
role.setModified(Timestamp.fromCurrentTime());
roles.put(roleName, role);
putDomainStruct(domainName, domainStruct);
role.setRoleMembers(members);
return true;
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class FileConnection method deleteRoleMember.
@Override
public boolean deleteRoleMember(String domainName, String roleName, String principal, String admin, String auditRef) {
DomainStruct domainStruct = getDomainStruct(domainName);
if (domainStruct == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "deleteRoleMember");
}
Role role = getRoleObject(domainStruct, roleName);
if (role == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "role not found", "deleteRoleMember");
}
List<RoleMember> roleMembers = role.getRoleMembers();
if (roleMembers != null) {
for (int idx = 0; idx < roleMembers.size(); idx++) {
if (roleMembers.get(idx).getMemberName().equalsIgnoreCase(principal)) {
roleMembers.remove(idx);
break;
}
}
}
putDomainStruct(domainName, domainStruct);
return true;
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class FileConnection method getRoleMember.
@Override
public Membership getRoleMember(String domainName, String roleName, String principal) {
DomainStruct domainStruct = getDomainStruct(domainName);
if (domainStruct == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "getRoleMember");
}
Role role = getRoleObject(domainStruct, roleName);
if (role == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "role not found", "getRoleMember");
}
Membership membership = new Membership().setMemberName(principal).setRoleName(ZMSUtils.roleResourceName(domainName, roleName)).setIsMember(false);
if (role.getRoleMembers() != null) {
Set<RoleMember> members = new HashSet<>(role.getRoleMembers());
for (RoleMember member : members) {
if (member.getMemberName().equalsIgnoreCase(principal)) {
membership.setIsMember(true);
membership.setExpiration(member.getExpiration());
break;
}
}
}
return membership;
}
Aggregations