All Projects → lorenzo → Audit Stash

lorenzo / Audit Stash

Licence: mit
Flexible and rock solid audit log tracking for CakePHP 3

Projects that are alternatives of or similar to Audit Stash

Bodybuilder
An elasticsearch query body builder 💪
Stars: ✭ 1,093 (+1556.06%)
Mutual labels:  elasticsearch
Aws Config To Elasticsearch
Generates an AWS Config Snapshot and ingests it into ElasticSearch for further analysis using Kibana
Stars: ✭ 62 (-6.06%)
Mutual labels:  elasticsearch
Anomaly Detection
A machine learning plugin in Open Distro for Elasticsearch for real time anomaly detection on streaming data.
Stars: ✭ 65 (-1.52%)
Mutual labels:  elasticsearch
Wp Rest Api Log
WordPress plugin for logging REST API requests and responses
Stars: ✭ 58 (-12.12%)
Mutual labels:  elasticsearch
Aclmanager
Plugin to manage Acl for CakePHP 2.x
Stars: ✭ 59 (-10.61%)
Mutual labels:  cakephp
Terraform Modules
Reusable Terraform modules
Stars: ✭ 63 (-4.55%)
Mutual labels:  elasticsearch
Booksearch
一个基于Elasticsearch本地文档搜索引擎
Stars: ✭ 56 (-15.15%)
Mutual labels:  elasticsearch
Mall Learning
mall学习教程,架构、业务、技术要点全方位解析。mall项目(40k+star)是一套电商系统,使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5.0、MongoDB 4.2.5、Mysql5.7等技术,采用Docker容器化部署。
Stars: ✭ 10,236 (+15409.09%)
Mutual labels:  elasticsearch
Drydock
drydock provides a flexible way of assessing the security of your Docker daemon configuration and containers using editable audit templates
Stars: ✭ 60 (-9.09%)
Mutual labels:  audit
Docker Magento Mutagen
Mage2click Docker-based development environment for Magento with mutagen.io sync for files on macOS
Stars: ✭ 64 (-3.03%)
Mutual labels:  elasticsearch
Redmine elasticsearch
Full text searching plugin for Redmine
Stars: ✭ 58 (-12.12%)
Mutual labels:  elasticsearch
Publisher Ads Lighthouse Plugin
Publisher Ads Lighthouse Plugin is a tool to improve ad speed and overall quality through a series of automated audits. This tool will aid in resolving discovered problems, providing a tool to be used to evaluate effectiveness of iterative changes while suggesting actionable feedback.
Stars: ✭ 58 (-12.12%)
Mutual labels:  audit
Webterminal
ssh rdp vnc telnet sftp bastion/jump web putty xshell terminal jumpserver audit realtime monitor rz/sz 堡垒机 云桌面 linux devops sftp websocket file management rz/sz otp 自动化运维 审计 录像 文件管理 sftp上传 实时监控 录像回放 网页版rz/sz上传下载/动态口令 django
Stars: ✭ 1,124 (+1603.03%)
Mutual labels:  audit
Pivot Kibana
Flexmonster Pivot plugin for Kibana
Stars: ✭ 58 (-12.12%)
Mutual labels:  elasticsearch
Elasticsearch Spring Boot Spring Data
🏆 Starter example for using Elasticsearch repository with Springboot
Stars: ✭ 65 (-1.52%)
Mutual labels:  elasticsearch
Prometheus Es Adapter
Prometheus remote storage adapter for Elasticsearch
Stars: ✭ 57 (-13.64%)
Mutual labels:  elasticsearch
Match
🔮 Scalable reverse image search built on Kubernetes and Elasticsearch
Stars: ✭ 1,121 (+1598.48%)
Mutual labels:  elasticsearch
Cakephp
CakePHP: The Rapid Development Framework for PHP - Official Repository
Stars: ✭ 8,453 (+12707.58%)
Mutual labels:  cakephp
Foselasticabundle
Elasticsearch PHP integration for your Symfony project using Elastica.
Stars: ✭ 1,142 (+1630.3%)
Mutual labels:  elasticsearch
Kubernetes Learning
《从Docker到Kubernetes进阶课程》在线文档
Stars: ✭ 1,128 (+1609.09%)
Mutual labels:  elasticsearch

AuditStash Plugin For CakePHP

Build Status Coverage Status Total Downloads License

This plugin implements an "audit trail" for any of your Table classes in your application, that is, the ability of recording any creation, modification or delete of the entities of any particular table.

By default, this plugin stores the audit logs into Elasticsearch, as we have found that it is a fantastic storage engine for append-only streams of data and provides really powerful features for finding changes in the historic data.

Even though we suggest storing the logs in Elasticsearch, this plugin is generic enough so you can implement your own persisting strategies, if so you wish.

Installation

You can install this plugin into your CakePHP application using composer and executing the following lines in the root of your application.

composer require lorenzo/audit-stash
bin/cake plugin load AuditStash

For using the default storage engine (ElasticSearch) you need to install the official elastic-search plugin, by executing the following lines:

composer require cakephp/elastic-search
bin/cake plugin load Cake/ElasticSearch

Configuration

Elastic Search

You now need to add the datasource configuration to your config/app.php file:

'Datasources' => [
    'auditlog_elastic' => [
        'className' => 'Cake\ElasticSearch\Datasource\Connection',
        'driver' => 'Cake\ElasticSearch\Datasource\Connection',
        'host' => '127.0.0.1', // server where elasticsearch is running
        'port' => 9200
    ],
    ...
]

Tables / Regular Databases

If you want to use a regular database, respectively an engine that can be used via the CakePHP ORM API, then you can use the table persister that ships with this plugin.

To do so you need to configure the AuditStash.persister option accordingly. In your config/app.php file add the following configuration:

'AuditStash' => [
    'persister' => 'AuditStash\Persister\TablePersister'
]

The plugin will then by default try to store the logs in a table named audit_logs, via a table class with the alias AuditLogs, which you could create/overwrite in your application if you need.

You can find a migration in the config/migration folder of this plugin which you can apply to your database, this will add a table named audit_logs with all the default columns - alternatively create the table manually. After that you can bake the corresponding table class.

bin/cake migrations migrate -p AuditStash -t 20171018185609
bin/cake bake model AuditLogs

Table Persister Configuration

The table persister supports various configuration options, please refer to its API documentation for further information. Generally configuration can be applied via its config() (or setConfig()) method:

$this->addBehavior('AuditStash.AuditLog');
$this->behaviors()->get('AuditLog')->persister()->config([
    'extractMetaFields' => [
        'user.id' => 'user_id'
    ]
]);

Using AuditStash

Enabling the Audit Log in any of your table classes is as simple as adding a behavior in the initialize() function:

class ArticlesTable extends Table
{
    public function initialize(array $config = [])
    {
        ...
        $this->addBehavior('AuditStash.AuditLog');
    }
}

When using the Elasticserch persister, it is recommended that you tell Elasticsearch about the schema of your table. You can do this automatically by executing the following command:

bin/cake elastic_mapping Articles

If you are using one index per day, save yourself some time and add the --use-templates option. This will create a schema template so any new index will inherit this configuration:

bin/cake elastic_mapping Articles --use-templates

Remember to execute the command line each time you change the schema of your table!

Configuring The Behavior

The AuditLog behavior can be configured to ignore certain fields of your table, by default it ignores the created and modified fields:

class ArticlesTable extends Table
{
    public function initialize(array $config = [])
    {
        ...
        $this->addBehavior('AuditStash.AuditLog', [
            'blacklist' => ['created', 'modified', 'another_field_name']
        ]);
    }
}

If you prefer, you can use a whitelist instead. This means that only the fields listed in that array will be tracked by the behavior:

public function initialize(array $config = [])
{
    ...
    $this->addBehavior('AuditStash.AuditLog', [
        'whitelist' => ['title', 'description', 'author_id']
    ]);
}

Storing The Logged In User

It is often useful to store the identifier of the user that is triggering the changes in a certain table. For this purpose, AuditStash provides the RequestMetadata listener class, that is capable of storing the current URL, IP and logged in user. You need to add this listener to your application in the AppController::beforeFilter() method:

use AuditStash\Meta\RequestMetadata;
...

class AppController extends Controller
{
    public function beforeFilter(Event $event)
    {
        ...
        $eventManager = $this->loadModel()->eventManager();
        $eventManager->on(new RequestMetadata($this->request, $this->Auth->user('id')));
    }
}

The above code assumes that you will trigger the table operations from the controller, using the default Table class for the controller. If you plan to use other Table classes for saving or deleting inside the same controller, it is advised that you attach the listener globally:

use AuditStash\Meta\RequestMetadata;
use Cake\Event\EventManager;
...

class AppController extends Controller
{
    public function beforeFilter(Event $event)
    {
        ...
        EventManager::instance()->on(new RequestMetadata($this->request, $this->Auth->user('id')));
    }
}

Storing Extra Information In Logs

AuditStash is also capable of storing arbitrary data for each of the logged events. You can use the ApplicationMetadata listener or create your own. If you choose to use ApplicationMetadata, your logs will contain the app_name key stored and any extra information your may have provided. You can configure this listener anywhere in your application, such as the bootstrap.php file or, again, directly in your AppController.

use AuditStash\Meta\ApplicationMetadata;
use Cake\Event\EventManager;

EventManager::instance()->on(new ApplicationMetadata('my_blog_app', [
    'server' => $theServerID,
    'extra' => $somExtraInformation,
    'moon_phase' => $currentMoonPhase
]));

Implementing your own metadata listeners is as simple as attaching the listener to the AuditStash.beforeLog event. For example:

EventManager::instance()->on('AuditStash.beforeLog', function ($event, array $logs) {
    foreach ($logs as $log) {
        $log->setMetaInfo($log->getMetaInfo() + ['extra' => 'This is extra data to be stored']);
    }
});

Implementing Your Own Persister Strategies

There are valid reasons for wanting to use a different persist engine for your audit logs. Luckily, this plugin allows you to implement your own storage engines. It is as simple as implementing the PersisterInterface interface:

use AuditStash\PersisterInterface;

class MyPersister implements PersisterInterface
{
    public function logEvents(array $auditLogs)
    {
        foreach ($auditLogs as $log) {
            $eventType = $log->getEventType();
            $data = [
                'timestamp' => $log->getTimestamp(),
                'transaction' => $log->getTransactionId(),
                'type' => $log->getEventType(),
                'primary_key' => $log->getId(),
                'source' => $log->getSourceName(),
                'parent_source' => $log->getParentSourceName(),
                'original' => json_encode($log->getOriginal()),
                'changed' => $eventType === 'delete' ? null : json_encode($log->getChanged()),
                'meta' => json_encode($log->getMetaInfo())
            ];
            $storage = new MyStorage();
            $storage->save($data);
        }
    }
}

Finally, you need to configure AuditStash to use your new persister. In the config/app.php file add the following lines:

'AuditStash' => [
    'persister' => 'App\Namespace\For\Your\Persister'
]

or if you are using as standalone via

\Cake\Core\Configure::write('AuditStash.persister', 'App\Namespace\For\Your\DatabasePersister');

The configuration contains the fully namespaced class name of your persister.

Working With Transactional Queries

Occasionally, you may want to wrap a number of database changes in a transaction, so that it can be rolled back if one part of the process fails. In order to create audit logs during a transaction, some additional setup is required. First create the file src/Model/Audit/AuditTrail.php with the following:

<?php
namespace App\Model\Audit;

use Cake\Utility\Text;
use SplObjectStorage;

class AuditTrail
{
    protected $_auditQueue;
    protected $_auditTransaction;

    public function __construct()
    {
        $this->_auditQueue = new SplObjectStorage;
        $this->_auditTransaction = Text::uuid();
    }

    public function toSaveOptions()
    {
        return [
            '_auditQueue' => $this->_auditQueue,
            '_auditTransaction' => $this->_auditTransaction
        ];
    }
}

Anywhere you wish to use Connection::transactional(), you will need to first include the following at the top of the file:

use App\Model\Audit\AuditTrail;
use Cake\Event\Event;

Your transaction should then look similar to this example of a BookmarksController:

$trail = new AuditTrail();
$success = $this->Bookmarks->connection()->transactional(function () use ($trail) {
    $bookmark = $this->Bookmarks->newEntity();
    $bookmark1->save($data1, $trail->toSaveOptions());
    $bookmark2 = $this->Bookmarks->newEntity();
    $bookmark2->save($data2, $trail->toSaveOptions());
    ...
    $bookmarkN = $this->Bookmarks->newEntity();
    $bookmarkN->save($dataN, $trail->toSaveOptions());

    return true;
});

if ($success) {
    $event = new Event('Model.afterCommit', $this->Bookmarks);
    $table->behaviors()->get('AuditLog')->afterCommit($event, $result, $auditTrail->toSaveOptions());
}

This will save all audit info for your objects, as well as audits for any associated data. Please note, $result must be an instance of an Object. Do not change the text "Model.afterCommit".

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].