Search in sources :

Example 36 with Policy

use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.

the class ZTSImplTest method testGetPolicyList.

@Test
public void testGetPolicyList() {
    List<com.yahoo.athenz.zms.Policy> policies = new ArrayList<>();
    List<com.yahoo.athenz.zms.Assertion> assertions = 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("coretech:tenant.weather.*");
    assertion.setAction("read");
    assertion.setRole("coretech:role.readers");
    assertions.add(assertion);
    policy.setAssertions(assertions);
    policy.setName("coretech:policy.reader");
    policies.add(policy);
    policy = new com.yahoo.athenz.zms.Policy();
    assertion = new com.yahoo.athenz.zms.Assertion();
    assertion.setResource("coretech:tenant.weather.*");
    assertion.setAction("write");
    assertion.setRole("coretech:role.writers");
    assertions.add(assertion);
    policy.setAssertions(assertions);
    policy.setName("coretech:policy.writer");
    policies.add(policy);
    com.yahoo.athenz.zms.DomainPolicies domainPolicies = new com.yahoo.athenz.zms.DomainPolicies();
    domainPolicies.setDomain("coretech");
    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), privateKey));
    signedPolicies.setKeyId("0");
    DomainData domain = new DomainData();
    domain.setName("coretech");
    domain.setPolicies(signedPolicies);
    domain.setModified(Timestamp.fromCurrentTime());
    List<com.yahoo.athenz.zts.Policy> policyList = zts.getPolicyList(domain);
    assertEquals(policyList.size(), 2);
    assertEquals(policyList.get(0).getName(), "coretech:policy.reader");
    assertEquals(policyList.get(1).getName(), "coretech:policy.writer");
}
Also used : Policy(com.yahoo.athenz.zms.Policy) Policy(com.yahoo.athenz.zms.Policy) ArrayList(java.util.ArrayList) Assertion(com.yahoo.athenz.zms.Assertion) DomainData(com.yahoo.athenz.zms.DomainData) Assertion(com.yahoo.athenz.zms.Assertion) Test(org.testng.annotations.Test)

Example 37 with Policy

use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.

the class ZTSImplTest method createPolicyObject.

private Policy createPolicyObject(String domainName, String policyName, String roleName, boolean generateRoleName, String action, String resource, AssertionEffect effect) {
    Policy policy = new Policy();
    policy.setName(domainName + ":policy." + policyName);
    Assertion assertion = new Assertion();
    assertion.setAction(action);
    assertion.setEffect(effect);
    assertion.setResource(resource);
    if (generateRoleName) {
        assertion.setRole(domainName + ":role." + roleName);
    } else {
        assertion.setRole(roleName);
    }
    List<Assertion> assertList = new ArrayList<Assertion>();
    assertList.add(assertion);
    policy.setAssertions(assertList);
    return policy;
}
Also used : Policy(com.yahoo.athenz.zms.Policy) Assertion(com.yahoo.athenz.zms.Assertion) ArrayList(java.util.ArrayList)

Example 38 with Policy

use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.

the class JDBCConnectionTest method testUpdatePolicy.

@Test
public void testUpdatePolicy() 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.when(mockResultSet.getInt(1)).thenReturn(5).thenReturn(// policy id
    4);
    boolean requestSuccess = jdbcConn.updatePolicy("my-domain", policy);
    assertTrue(requestSuccess);
    // get domain id
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    // get policy id
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "policy1");
    // update policy
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "policy1");
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 4);
    jdbcConn.close();
}
Also used : Policy(com.yahoo.athenz.zms.Policy) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 39 with Policy

use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.

the class JDBCConnectionTest method testUpdatePolicyInvalidName.

@Test
public void testUpdatePolicyInvalidName() 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.updatePolicy("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 40 with Policy

use of com.yahoo.athenz.zms.Policy in project athenz by yahoo.

the class JDBCConnectionTest method testGetPolicy.

@Test
public void testGetPolicy() throws Exception {
    Mockito.when(mockResultSet.next()).thenReturn(true);
    Mockito.doReturn("policy1").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_NAME);
    Mockito.doReturn(new java.sql.Timestamp(1454358916)).when(mockResultSet).getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED);
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Policy policy = jdbcConn.getPolicy("my-domain", "policy1");
    assertNotNull(policy);
    assertEquals("my-domain:policy.policy1", policy.getName());
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "policy1");
    jdbcConn.close();
}
Also used : Policy(com.yahoo.athenz.zms.Policy) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) 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