Search in sources :

Example 41 with Policy

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();
}
Also used : Policy(com.yahoo.athenz.zms.Policy) ResourceException(com.yahoo.athenz.zms.ResourceException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 42 with Policy

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();
}
Also used : Policy(com.yahoo.athenz.zms.Policy) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 43 with Policy

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;
}
Also used : Policy(com.yahoo.athenz.zms.Policy)

Example 44 with 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());
}
Also used : Policy(com.yahoo.athenz.zms.Policy) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Assertion(com.yahoo.athenz.zms.Assertion) PreparedStatement(java.sql.PreparedStatement)

Example 45 with Policy

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\":[{}]}");
}
Also used : Policy(com.yahoo.athenz.zms.Policy) ArrayList(java.util.ArrayList) Assertion(com.yahoo.athenz.zms.Assertion) Test(org.testng.annotations.Test)

Aggregations

Policy (com.yahoo.athenz.zms.Policy)46 Assertion (com.yahoo.athenz.zms.Assertion)24 Test (org.testng.annotations.Test)24 Role (com.yahoo.athenz.zms.Role)22 ArrayList (java.util.ArrayList)18 DomainData (com.yahoo.athenz.zms.DomainData)16 RoleMember (com.yahoo.athenz.zms.RoleMember)13 DataCache (com.yahoo.athenz.zts.cache.DataCache)13 SignedDomain (com.yahoo.athenz.zms.SignedDomain)8 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)7 HashMap (java.util.HashMap)7 Domain (com.yahoo.athenz.zms.Domain)6 ResourceException (com.yahoo.athenz.zms.ResourceException)4 ServiceIdentity (com.yahoo.athenz.zms.ServiceIdentity)4 SQLException (java.sql.SQLException)4 Principal (com.yahoo.athenz.auth.Principal)3 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)3 Array (com.yahoo.rdl.Array)2 Struct (com.yahoo.rdl.Struct)2 PreparedStatement (java.sql.PreparedStatement)2