use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.
the class JDBCConnectionTest method testInsertPolicyInvalidName.
@Test
public void testInsertPolicyInvalidName() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Policy policy = new Policy().setName("policy1");
Mockito.when(mockResultSet.next()).thenReturn(true);
// return domain id
Mockito.doReturn(5).when(mockResultSet).getInt(1);
try {
jdbcConn.insertPolicy("my-domain", policy);
fail();
} catch (ResourceException ex) {
assertEquals(400, ex.getCode());
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.
the class JDBCConnectionTest method testInsertPolicy.
@Test
public void testInsertPolicy() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Policy policy = new Policy().setName("my-domain:policy.policy1");
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
Mockito.when(mockResultSet.next()).thenReturn(true);
// return domain id
Mockito.doReturn(5).when(mockResultSet).getInt(1);
boolean requestSuccess = jdbcConn.insertPolicy("my-domain", policy);
assertTrue(requestSuccess);
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setString(1, "policy1");
Mockito.verify(mockPrepStmt, times(1)).setInt(2, 5);
jdbcConn.close();
}
use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.
the class ZMSUtils method makeAdminPolicy.
public static Policy makeAdminPolicy(String domainName, Role adminsRole) {
Policy policy = new Policy().setName(policyResourceName(domainName, ZMSConsts.ADMIN_POLICY_NAME));
addAssertion(policy, domainName + ":*", "*", adminsRole.getName(), AssertionEffect.ALLOW);
return policy;
}
use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.
the class JDBCConnection method getAthenzDomainPolicies.
void getAthenzDomainPolicies(String domainName, int domainId, AthenzDomain athenzDomain, String caller) {
Map<String, Policy> policyMap = new HashMap<>();
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_POLICIES)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String policyName = rs.getString(ZMSConsts.DB_COLUMN_NAME);
Policy policy = new Policy().setName(ZMSUtils.policyResourceName(domainName, policyName)).setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime()));
policyMap.put(policyName, policy);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_POLICY_ASSERTIONS)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String policyName = rs.getString(1);
Policy policy = policyMap.get(policyName);
if (policy == null) {
continue;
}
List<Assertion> assertions = policy.getAssertions();
if (assertions == null) {
assertions = new ArrayList<>();
policy.setAssertions(assertions);
}
Assertion assertion = new Assertion();
assertion.setRole(ZMSUtils.roleResourceName(domainName, rs.getString(ZMSConsts.DB_COLUMN_ROLE)));
assertion.setResource(rs.getString(ZMSConsts.DB_COLUMN_RESOURCE));
assertion.setAction(rs.getString(ZMSConsts.DB_COLUMN_ACTION));
assertion.setEffect(AssertionEffect.valueOf(rs.getString(ZMSConsts.DB_COLUMN_EFFECT)));
assertion.setId((long) rs.getInt(ZMSConsts.DB_COLUMN_ASSERT_ID));
assertions.add(assertion);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
athenzDomain.getPolicies().addAll(policyMap.values());
}
use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.
the class SignUtilsTest method testAsStructPolicy.
@Test
public void testAsStructPolicy() {
List<Policy> policies = new ArrayList<Policy>();
Policy mPolicy = Mockito.mock(Policy.class);
policies.add(mPolicy);
List<Assertion> assertions = new ArrayList<Assertion>();
Assertion mAssertion = Mockito.mock(Assertion.class);
assertions.add(mAssertion);
Mockito.when(mockPolicies.getPolicies()).thenReturn(policies);
Mockito.when(mPolicy.getAssertions()).thenReturn(assertions);
String check = SignUtils.asCanonicalString(mockPolicies);
assertNotNull(check);
assertEquals(check, "{\"policies\":[{\"assertions\":[{}]}]}");
Mockito.when(mPolicy.getAssertions()).thenReturn(null);
check = SignUtils.asCanonicalString(mockPolicies);
assertNotNull(check);
assertEquals(check, "{\"policies\":[{}]}");
}
Aggregations