use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class JDBCConnectionTest method testInsertRoleMemberUpdate.
@Test
public void testInsertRoleMemberUpdate() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
5).thenReturn(// role id
7).thenReturn(// principal id
9);
Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
true).thenReturn(// this one is for role id
true).thenReturn(// validate principle domain
true).thenReturn(// principal id
true).thenReturn(// member exists
true);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
RoleMember roleMember = new RoleMember().setMemberName("user.user1");
Timestamp expiration = Timestamp.fromCurrentTime();
roleMember.setExpiration(expiration);
java.sql.Timestamp javaExpiration = new java.sql.Timestamp(expiration.toDate().getTime());
boolean requestSuccess = jdbcConn.insertRoleMember("my-domain", "role1", roleMember, "user.admin", "audit-ref");
// this is combined for all operations above
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.user1");
// we need additional operation for the audit log
Mockito.verify(mockPrepStmt, times(2)).setInt(1, 7);
Mockito.verify(mockPrepStmt, times(1)).setInt(2, 9);
// update operation
Mockito.verify(mockPrepStmt, times(1)).setTimestamp(1, javaExpiration);
Mockito.verify(mockPrepStmt, times(1)).setInt(2, 7);
Mockito.verify(mockPrepStmt, times(1)).setInt(3, 9);
// the rest of the audit log details
Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.admin");
Mockito.verify(mockPrepStmt, times(1)).setString(3, "user.user1");
Mockito.verify(mockPrepStmt, times(1)).setString(4, "UPDATE");
Mockito.verify(mockPrepStmt, times(1)).setString(5, "audit-ref");
assertTrue(requestSuccess);
jdbcConn.close();
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class JDBCConnectionTest method testInsertRoleMemberNewPrincipal.
@Test
public void testInsertRoleMemberNewPrincipal() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
5).thenReturn(// role id
7).thenReturn(// principal domain id
8).thenReturn(// principal id
9);
Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
true).thenReturn(// this one is for role id
true).thenReturn(// this one is for valid principal domain
true).thenReturn(// principal does not exist
false).thenReturn(// get last id (for new principal)
true).thenReturn(// role member exists
false);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
boolean requestSuccess = jdbcConn.insertRoleMember("my-domain", "role1", new RoleMember().setMemberName("user.user1"), "user.admin", "audit-ref");
// this is combined for all operations above
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
Mockito.verify(mockPrepStmt, times(1)).setString(1, "user");
// we're going to have 2 sets of operations for principal name
Mockito.verify(mockPrepStmt, times(2)).setString(1, "user.user1");
// we need additional operation for the audit log
// additional operation to check for roleMember exist using roleID and principal ID.
Mockito.verify(mockPrepStmt, times(3)).setInt(1, 7);
Mockito.verify(mockPrepStmt, times(2)).setInt(2, 9);
// the rest of the audit log details
Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.admin");
Mockito.verify(mockPrepStmt, times(1)).setString(3, "user.user1");
Mockito.verify(mockPrepStmt, times(1)).setString(4, "ADD");
Mockito.verify(mockPrepStmt, times(1)).setString(5, "audit-ref");
assertTrue(requestSuccess);
jdbcConn.close();
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class JDBCConnectionTest method testInsertRoleMemberNewPrincipalFailure.
@Test
public void testInsertRoleMemberNewPrincipalFailure() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
5).thenReturn(// role id
7).thenReturn(// principal domain id
8).thenReturn(// principal id
9);
Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
true).thenReturn(// this one is for role id
true).thenReturn(// this one is for valid principal domain
true).thenReturn(// principal does not exist
false);
// principal add returns 0
Mockito.doReturn(0).when(mockPrepStmt).executeUpdate();
try {
jdbcConn.insertRoleMember("my-domain", "role1", new RoleMember().setMemberName("user.user1"), "user.admin", "audit-ref");
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 500);
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class DataStoreTest method createTenantSignedDomain.
private SignedDomain createTenantSignedDomain(String domainName, String providerDomain) {
SignedDomain signedDomain = new SignedDomain();
List<Role> roles = new ArrayList<>();
Role role = new Role();
role.setName(domainName + ":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(domainName + ":role.readers");
members = new ArrayList<>();
members.add(new RoleMember().setMemberName("user_domain.user100"));
members.add(new RoleMember().setMemberName("user_domain.user101"));
role.setRoleMembers(members);
roles.add(role);
List<com.yahoo.athenz.zms.Policy> policies = new ArrayList<>();
com.yahoo.athenz.zms.Policy policy = new com.yahoo.athenz.zms.Policy();
com.yahoo.athenz.zms.Assertion assertion = new com.yahoo.athenz.zms.Assertion();
assertion.setResource(providerDomain + ":role.tenant.readers");
assertion.setAction("assume_role");
assertion.setRole(domainName + ":role.readers");
List<com.yahoo.athenz.zms.Assertion> assertions = new ArrayList<>();
assertions.add(assertion);
policy.setAssertions(assertions);
policy.setName(domainName + ":policy.tenancy.readers");
policies.add(policy);
ServiceIdentity service = new ServiceIdentity();
service.setName(domainName + ".storage");
setServicePublicKey(service, "0", "abcdefgh");
List<String> hosts = new ArrayList<>();
hosts.add("host1");
service.setHosts(hosts);
List<ServiceIdentity> services = new ArrayList<>();
services.add(service);
com.yahoo.athenz.zms.DomainPolicies domainPolicies = new com.yahoo.athenz.zms.DomainPolicies();
domainPolicies.setDomain(domainName);
domainPolicies.setPolicies(policies);
com.yahoo.athenz.zms.SignedPolicies signedPolicies = new com.yahoo.athenz.zms.SignedPolicies();
signedPolicies.setContents(domainPolicies);
signedPolicies.setSignature(Crypto.sign(SignUtils.asCanonicalString(domainPolicies), pkey));
signedPolicies.setKeyId("0");
DomainData domain = new DomainData();
domain.setName(domainName);
domain.setRoles(roles);
domain.setServices(services);
domain.setPolicies(signedPolicies);
signedDomain.setDomain(domain);
signedDomain.setSignature(Crypto.sign(SignUtils.asCanonicalString(domain), pkey));
signedDomain.setKeyId("0");
return signedDomain;
}
use of com.yahoo.athenz.zms.RoleMember in project athenz by yahoo.
the class DataStoreTest method createSignedDomainWildCardMembers.
private SignedDomain createSignedDomainWildCardMembers(String domainName, String tenantDomain) {
SignedDomain signedDomain = new SignedDomain();
List<Role> roles = new ArrayList<>();
Role role = new Role();
role.setName(domainName + ":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(domainName + ":role.writers");
members = new ArrayList<>();
members.add(new RoleMember().setMemberName("user_domain.user*"));
members.add(new RoleMember().setMemberName("user_domain.user1"));
role.setRoleMembers(members);
roles.add(role);
role = new Role();
role.setName(domainName + ":role.readers");
members = new ArrayList<>();
members.add(new RoleMember().setMemberName("user_domain.user3"));
members.add(new RoleMember().setMemberName("user_domain.user4"));
role.setRoleMembers(members);
roles.add(role);
role = new Role();
role.setName(domainName + ":role.all");
members = new ArrayList<>();
members.add(new RoleMember().setMemberName("*"));
role.setRoleMembers(members);
roles.add(role);
role = new Role();
role.setName(domainName + ":role.tenant.readers");
role.setTrust(tenantDomain);
roles.add(role);
List<com.yahoo.athenz.zms.Policy> policies = new ArrayList<>();
com.yahoo.athenz.zms.Policy policy = new com.yahoo.athenz.zms.Policy();
com.yahoo.athenz.zms.Assertion assertion = new com.yahoo.athenz.zms.Assertion();
assertion.setResource(domainName + ":tenant.weather.*");
assertion.setAction("read");
assertion.setRole(domainName + ":role.tenant.readers");
List<com.yahoo.athenz.zms.Assertion> assertions = new ArrayList<>();
assertions.add(assertion);
policy.setAssertions(assertions);
policy.setName(domainName + ":policy.tenant.reader");
policies.add(policy);
ServiceIdentity service = new ServiceIdentity();
service.setName(domainName + ".storage");
setServicePublicKey(service, "0", "abcdefgh");
List<String> hosts = new ArrayList<>();
hosts.add("host1");
service.setHosts(hosts);
List<ServiceIdentity> services = new ArrayList<>();
services.add(service);
com.yahoo.athenz.zms.DomainPolicies domainPolicies = new com.yahoo.athenz.zms.DomainPolicies();
domainPolicies.setDomain(domainName);
domainPolicies.setPolicies(policies);
com.yahoo.athenz.zms.SignedPolicies signedPolicies = new com.yahoo.athenz.zms.SignedPolicies();
signedPolicies.setContents(domainPolicies);
signedPolicies.setSignature(Crypto.sign(SignUtils.asCanonicalString(domainPolicies), pkey));
signedPolicies.setKeyId("0");
DomainData domain = new DomainData();
domain.setName(domainName);
domain.setRoles(roles);
domain.setServices(services);
domain.setPolicies(signedPolicies);
signedDomain.setDomain(domain);
signedDomain.setSignature(Crypto.sign(SignUtils.asCanonicalString(domain), pkey));
signedDomain.setKeyId("0");
return signedDomain;
}
Aggregations