Search in sources :

Example 1 with TestAuditLogger

use of com.yahoo.elide.core.audit.TestAuditLogger in project elide by yahoo.

the class PersistentResourceTest method testClassLevelAudit.

@Test
public void testClassLevelAudit() throws Exception {
    Child child = newChild(5);
    Parent parent = newParent(7);
    TestAuditLogger logger = new TestAuditLogger();
    RequestScope requestScope = getUserScope(goodUser, logger);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, requestScope.getUUIDFor(parent), requestScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, parentResource, "children", requestScope.getUUIDFor(child), requestScope);
    childResource.auditClass(Audit.Action.CREATE, new ChangeSpec(childResource, null, null, childResource.getObject()));
    assertEquals(1, logger.getMessages().size(), "One message should be logged");
    LogMessage message = logger.getMessages().get(0);
    assertEquals("CREATE Child 5 Parent 7", message.getMessage(), "Logging template should match");
    assertEquals(0, message.getOperationCode(), "Operation code should match");
    // tidy up this thread's messages
    logger.clear();
}
Also used : ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Parent(example.Parent) LogMessage(com.yahoo.elide.core.audit.LogMessage) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 2 with TestAuditLogger

use of com.yahoo.elide.core.audit.TestAuditLogger in project elide by yahoo.

the class PersistentResourceTest method testFieldLevelAudit.

@Test
public void testFieldLevelAudit() throws Exception {
    Child child = newChild(5);
    Parent parent = newParent(7);
    TestAuditLogger logger = new TestAuditLogger();
    RequestScope requestScope = getUserScope(goodUser, logger);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, requestScope.getUUIDFor(parent), requestScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, parentResource, "children", requestScope.getUUIDFor(child), requestScope);
    childResource.auditField(new ChangeSpec(childResource, "name", parent, null));
    assertEquals(1, logger.getMessages().size(), "One message should be logged");
    LogMessage message = logger.getMessages().get(0);
    assertEquals("UPDATE Child 5 Parent 7", message.getMessage(), "Logging template should match");
    assertEquals(1, message.getOperationCode(), "Operation code should match");
    // tidy up this thread's messages
    logger.clear();
}
Also used : ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Parent(example.Parent) LogMessage(com.yahoo.elide.core.audit.LogMessage) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 3 with TestAuditLogger

use of com.yahoo.elide.core.audit.TestAuditLogger in project elide by yahoo.

the class TableExportIT method tableExportModelAdminReadPermissions.

/**
 * Tests Read Permissions on TableExport Model for Admin Role.
 * @throws IOException IOException
 */
@Test
public void tableExportModelAdminReadPermissions() throws IOException {
    ElideResponse response = null;
    String id = "edc4a871-dff2-4054-804e-d80075c08959";
    String query = "test-query";
    com.yahoo.elide.async.models.TableExport queryObj = new com.yahoo.elide.async.models.TableExport();
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setResultType(ResultType.CSV);
    queryObj.setQueryType(QueryType.JSONAPI_V1_0);
    queryObj.setPrincipalName("owner-user");
    EntityDictionary dictionary = EntityDictionary.builder().checks(AsyncIntegrationTestApplicationResourceConfig.MAPPINGS).build();
    dataStore.populateEntityDictionary(dictionary);
    DataStoreTransaction tx = dataStore.beginTransaction();
    tx.createObject(queryObj, null);
    tx.commit(null);
    tx.close();
    Elide elide = new Elide(new ElideSettingsBuilder(dataStore).withEntityDictionary(dictionary).withAuditLogger(new TestAuditLogger()).build());
    User ownerUser = new User(() -> "owner-user");
    SecurityContextUser securityContextAdminUser = new SecurityContextUser(new SecurityContext() {

        @Override
        public Principal getUserPrincipal() {
            return () -> "1";
        }

        @Override
        public boolean isUserInRole(String s) {
            return true;
        }

        @Override
        public boolean isSecure() {
            return false;
        }

        @Override
        public String getAuthenticationScheme() {
            return null;
        }
    });
    SecurityContextUser securityContextNonAdminUser = new SecurityContextUser(new SecurityContext() {

        @Override
        public Principal getUserPrincipal() {
            return () -> "2";
        }

        @Override
        public boolean isUserInRole(String s) {
            return false;
        }

        @Override
        public boolean isSecure() {
            return false;
        }

        @Override
        public String getAuthenticationScheme() {
            return null;
        }
    });
    String baseUrl = "/";
    // Principal is Owner
    response = elide.get(baseUrl, "/tableExport/" + id, new MultivaluedHashMap<>(), ownerUser, NO_VERSION);
    assertEquals(HttpStatus.SC_OK, response.getResponseCode());
    // Principal has Admin Role
    response = elide.get(baseUrl, "/tableExport/" + id, new MultivaluedHashMap<>(), securityContextAdminUser, NO_VERSION);
    assertEquals(HttpStatus.SC_OK, response.getResponseCode());
    // Principal without Admin Role
    response = elide.get(baseUrl, "/tableExport/" + id, new MultivaluedHashMap<>(), securityContextNonAdminUser, NO_VERSION);
    assertEquals(HttpStatus.SC_NOT_FOUND, response.getResponseCode());
}
Also used : User(com.yahoo.elide.core.security.User) SecurityContextUser(com.yahoo.elide.jsonapi.resources.SecurityContextUser) SecurityContextUser(com.yahoo.elide.jsonapi.resources.SecurityContextUser) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) ElideResponse(com.yahoo.elide.ElideResponse) SecurityContext(javax.ws.rs.core.SecurityContext) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) Elide(com.yahoo.elide.Elide) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Example 4 with TestAuditLogger

use of com.yahoo.elide.core.audit.TestAuditLogger in project elide by yahoo.

the class ResourceIT method elideSecurityEnabled.

@Test
public void elideSecurityEnabled() {
    Elide elide = new Elide(new ElideSettingsBuilder(dataStore).withEntityDictionary(EntityDictionary.builder().checks(TestCheckMappings.MAPPINGS).build()).withAuditLogger(new TestAuditLogger()).build());
    elide.doScans();
    com.yahoo.elide.core.security.User user = new com.yahoo.elide.core.security.User(() -> "-1");
    ElideResponse response = elide.get(baseUrl, "parent/1/children", new MultivaluedHashMap<>(), user, NO_VERSION);
    assertEquals(HttpStatus.SC_OK, response.getResponseCode());
    assertEquals("{\"data\":[]}", response.getBody());
}
Also used : ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) User(example.User) ElideResponse(com.yahoo.elide.ElideResponse) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) Elide(com.yahoo.elide.Elide) Test(org.junit.jupiter.api.Test) IntegrationTest(com.yahoo.elide.initialization.IntegrationTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with TestAuditLogger

use of com.yahoo.elide.core.audit.TestAuditLogger in project elide by yahoo.

the class PermissionAnnotationTest method setup.

@BeforeAll
public static void setup() {
    dictionary.bindEntity(FunWithPermissions.class);
    FunWithPermissions fun = new FunWithPermissions();
    fun.setId(1);
    AuditLogger testLogger = new TestAuditLogger();
    ElideSettings elideSettings = new ElideSettingsBuilder(null).withDefaultPageSize(10).withDefaultMaxPageSize(10).withAuditLogger(testLogger).withEntityDictionary(dictionary).build();
    RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, null, GOOD_USER, null, null, UUID.randomUUID(), elideSettings);
    funRecord = new PersistentResource<>(fun, goodScope.getUUIDFor(fun), goodScope);
    RequestScope badScope = new RequestScope(null, null, NO_VERSION, null, null, BAD_USER, null, null, UUID.randomUUID(), elideSettings);
    badRecord = new PersistentResource<>(fun, badScope.getUUIDFor(fun), badScope);
}
Also used : ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) AuditLogger(com.yahoo.elide.core.audit.AuditLogger) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) ElideSettings(com.yahoo.elide.ElideSettings) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) FunWithPermissions(example.FunWithPermissions) RequestScope(com.yahoo.elide.core.RequestScope) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

TestAuditLogger (com.yahoo.elide.core.audit.TestAuditLogger)6 Test (org.junit.jupiter.api.Test)5 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)4 Elide (com.yahoo.elide.Elide)3 ElideResponse (com.yahoo.elide.ElideResponse)3 LogMessage (com.yahoo.elide.core.audit.LogMessage)2 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)2 ChangeSpec (com.yahoo.elide.core.security.ChangeSpec)2 User (com.yahoo.elide.core.security.User)2 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)2 SecurityContextUser (com.yahoo.elide.jsonapi.resources.SecurityContextUser)2 Child (example.Child)2 Parent (example.Parent)2 Principal (java.security.Principal)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 SecurityContext (javax.ws.rs.core.SecurityContext)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ElideSettings (com.yahoo.elide.ElideSettings)1 RequestScope (com.yahoo.elide.core.RequestScope)1 AuditLogger (com.yahoo.elide.core.audit.AuditLogger)1