From 3fc2d4f2815267ff5d3e35d21577d716ea26f377 Mon Sep 17 00:00:00 2001 From: Wilker Mesquita Date: Fri, 23 Aug 2024 10:30:43 -0300 Subject: [PATCH 1/5] [KDG] bump composer versions Signed-off-by: Wilker Mesquita --- composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index a50bb14..5eab77e 100644 --- a/composer.json +++ b/composer.json @@ -24,24 +24,24 @@ { "packagist.org": false, "type": "vcs", - "url": "https://github.com/rankarpan/laravel-auditing" + "url": "https://github.com/owen-it/laravel-auditing" } ], "license": "MIT", "support": { - "issues": "https://github.com/iconscout/laravel-auditing-elasticsearch/issues", - "source": "https://github.com/iconscout/laravel-auditing-elasticsearch" + "issues": "https://github.com/kirschbaum-development/laravel-auditing-elasticsearch/issues", + "source": "https://github.com/kirschbaum-development/laravel-auditing-elasticsearch" }, "authors": [ { - "name": "Arpan Rank", - "email": "arpan@iconscout.com" + "name": "Kirschbaum Development", + "email": "wilker@kirschbaumdevelopment.com" } ], "require": { - "php": ">=7.0", - "owen-it/laravel-auditing": "dev-master", - "elasticsearch/elasticsearch": "6.*" + "php": ">=8.0", + "owen-it/laravel-auditing": "^13.6", + "elasticsearch/elasticsearch": "^8.*" }, "autoload": { "psr-4": { From a1d6465254dbbcaae78bce6686aa0da4f1851773 Mon Sep 17 00:00:00 2001 From: Wilker Mesquita Date: Fri, 23 Aug 2024 10:31:03 -0300 Subject: [PATCH 2/5] [KDG] cast Uuid::uuid4() to string Signed-off-by: Wilker Mesquita --- composer.json | 4 ++-- src/Drivers/ElasticSearch.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5eab77e..24d9dde 100644 --- a/composer.json +++ b/composer.json @@ -39,9 +39,9 @@ } ], "require": { - "php": ">=8.0", + "php": "^8.2", "owen-it/laravel-auditing": "^13.6", - "elasticsearch/elasticsearch": "^8.*" + "elasticsearch/elasticsearch": "^8.0" }, "autoload": { "psr-4": { diff --git a/src/Drivers/ElasticSearch.php b/src/Drivers/ElasticSearch.php index c8dfcc6..617fb4c 100644 --- a/src/Drivers/ElasticSearch.php +++ b/src/Drivers/ElasticSearch.php @@ -146,7 +146,7 @@ public function indexAuditDocument($model) $params = [ 'index' => $this->index, 'type' => $this->type, - 'id' => Uuid::uuid4(), + 'id' => (string) Uuid::uuid4(), 'body' => $model ]; From 036f03a265f96b1b8390d7629a11a2cfac44d990 Mon Sep 17 00:00:00 2001 From: Wilker Mesquita Date: Mon, 26 Aug 2024 15:22:54 -0300 Subject: [PATCH 3/5] [KDG] Updates to handle es v8.15 Signed-off-by: Wilker Mesquita --- src/Drivers/ElasticSearch.php | 46 ++++++++++----------------- src/Traits/ElasticSearchAuditable.php | 8 ++--- src/config/audit.php | 1 - 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/Drivers/ElasticSearch.php b/src/Drivers/ElasticSearch.php index 617fb4c..cca1006 100644 --- a/src/Drivers/ElasticSearch.php +++ b/src/Drivers/ElasticSearch.php @@ -14,7 +14,7 @@ namespace Iconscout\Auditing\Drivers; use Carbon\Carbon; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\Facades\Config; use Iconscout\Auditing\Jobs\AuditIndexQueuedModels; use Iconscout\Auditing\Jobs\AuditDeleteQueuedModels; @@ -36,11 +36,6 @@ class ElasticSearch implements AuditDriver */ protected $index = null; - /** - * @var string - */ - protected $type = null; - /** * ElasticSearch constructor. */ @@ -48,7 +43,6 @@ public function __construct() { $this->client = ClientBuilder::create()->setHosts(Config::get('audit.drivers.es.client.hosts', ['localhost:9200']))->build(); $this->index = Config::get('audit.drivers.es.index', 'laravel_auditing'); - $this->type = Config::get('audit.drivers.es.type', 'audits'); } /** @@ -60,10 +54,11 @@ public function __construct() */ public function audit(Auditable $model): Audit { + $implementation = Config::get('audit.implementation', AuditModel::class); - + $this->storeAudit($model->toAudit()); - + return new $implementation; } @@ -90,7 +85,7 @@ public function storeAudit($model) if (Config::get('audit.queue', false)) { return $this->indexQueueAuditDocument($model); } - + return $this->indexAuditDocument($model); } @@ -108,7 +103,7 @@ public function destroyAudit($model) if (Config::get('audit.queue', false)) { return $this->deleteQueueAuditDocument($model); } - + return $this->deleteAuditDocument($model); } @@ -145,7 +140,6 @@ public function indexAuditDocument($model) { $params = [ 'index' => $this->index, - 'type' => $this->type, 'id' => (string) Uuid::uuid4(), 'body' => $model ]; @@ -161,7 +155,6 @@ public function searchAuditDocument($model) $params = [ 'index' => $this->index, - 'type' => $this->type, 'size' => 10000 - $skip, 'from' => $skip, 'body' => [ @@ -199,18 +192,17 @@ public function deleteAuditDocument($model) if (count($audits)) { $audit_ids = array_column($audits, '_id'); - + foreach ($audit_ids as $audit_id) { $params['body'][] = [ 'delete' => [ '_index' => $this->index, - '_type' => $this->type, '_id' => $audit_id ] ]; } - + return (bool) $this->client->bulk($params); } @@ -254,7 +246,7 @@ public function deleteIndex() 'index' => $this->index ]; - return $this->client->indices()->delete($deleteParams); + return $this->client->indices()->delete($deleteParams)->asBool(); } public function existsIndex() @@ -263,39 +255,33 @@ public function existsIndex() 'index' => $this->index ]; - return $this->client->indices()->exists($params); + return $this->client->indices()->exists($params)->asBool(); } public function putMapping() { $params = [ 'index' => $this->index, - 'type' => $this->type, 'body' => [ - $this->type => [ + 'mappings' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'event' => [ - 'type' => 'string', - 'index' => 'not_analyzed' + 'type' => 'keyword' ], 'auditable_type' => [ - 'type' => 'string', - 'index' => 'not_analyzed' + 'type' => 'keyword' ], 'ip_address' => [ - 'type' => 'string', - 'index' => 'not_analyzed' + 'type' => 'keyword' ], 'url' => [ - 'type' => 'string', - 'index' => 'not_analyzed' + 'type' => 'keyword' ], 'user_agent' => [ - 'type' => 'string', - 'index' => 'not_analyzed' + 'type' => 'keyword' ], 'created_at' => [ 'type' => 'date', diff --git a/src/Traits/ElasticSearchAuditable.php b/src/Traits/ElasticSearchAuditable.php index 8dc0c9a..b989677 100644 --- a/src/Traits/ElasticSearchAuditable.php +++ b/src/Traits/ElasticSearchAuditable.php @@ -13,7 +13,7 @@ namespace Iconscout\Auditing\Traits; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\Facades\Config; use Illuminate\Database\Eloquent\Collection; @@ -23,14 +23,12 @@ public function esAudits($page = 1, $perPage = 10, $sort = 'latest') { $client = ClientBuilder::create()->setHosts(Config::get('audit.drivers.es.client.hosts', ['localhost:9200']))->build(); $index = Config::get('audit.drivers.es.index', 'laravel_auditing'); - $type = Config::get('audit.drivers.es.type', 'audits'); - + $from = ($page - 1) * $perPage; $order = $sort === 'latest' ? 'desc' : 'asc'; $params = [ 'index' => $index, - 'type' => $type, 'size' => $perPage, 'from' => $from, 'body' => [ @@ -83,4 +81,4 @@ public function getEsAuditsAttribute() { return $this->esAudits(); } -} \ No newline at end of file +} diff --git a/src/config/audit.php b/src/config/audit.php index 4691d16..a253e63 100644 --- a/src/config/audit.php +++ b/src/config/audit.php @@ -149,7 +149,6 @@ ] ], 'index' => env('AUDIT_INDEX', 'laravel_auditing'), - 'type' => env('AUDIT_TYPE', 'audits') ] ], From 628fd3a6ee21ee989f64bb69ea84e4a8dfda9896 Mon Sep 17 00:00:00 2001 From: Wilker Mesquita Date: Mon, 26 Aug 2024 15:32:46 -0300 Subject: [PATCH 4/5] [KDG] Update README.md Signed-off-by: Wilker Mesquita --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a0bf65a..031a4d6 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,6 @@ The `drivers` key of the config file should look like so: ] ], 'index' => env('AUDIT_INDEX', 'laravel_auditing'), - 'type' => env('AUDIT_TYPE', 'audits') ], ], ... From 1e09b60ff34f4849b45b0ceee0f42095f41ec6ca Mon Sep 17 00:00:00 2001 From: Wilker Mesquita Date: Mon, 26 Aug 2024 17:04:14 -0300 Subject: [PATCH 5/5] [KDG] Update ElasticSearch::putMapping to v8.15.0 Signed-off-by: Wilker Mesquita --- src/Drivers/ElasticSearch.php | 116 +++++++++++++++++----------------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/src/Drivers/ElasticSearch.php b/src/Drivers/ElasticSearch.php index cca1006..276a804 100644 --- a/src/Drivers/ElasticSearch.php +++ b/src/Drivers/ElasticSearch.php @@ -92,8 +92,8 @@ public function storeAudit($model) public function indexQueueAuditDocument($model) { dispatch((new AuditIndexQueuedModels($model)) - ->onQueue($this->syncWithSearchUsingQueue()) - ->onConnection($this->syncWithSearchUsing())); + ->onQueue($this->syncWithSearchUsingQueue()) + ->onConnection($this->syncWithSearchUsing())); return true; } @@ -110,8 +110,8 @@ public function destroyAudit($model) public function deleteQueueAuditDocument($model) { dispatch((new AuditDeleteQueuedModels($model)) - ->onQueue($this->syncWithSearchUsingQueue()) - ->onConnection($this->syncWithSearchUsing())); + ->onQueue($this->syncWithSearchUsingQueue()) + ->onConnection($this->syncWithSearchUsing())); return true; } @@ -146,7 +146,8 @@ public function indexAuditDocument($model) try { return $this->client->index($params); - } catch (\Exception $e) {} + } catch (\Exception $e) { + } } public function searchAuditDocument($model) @@ -200,7 +201,6 @@ public function deleteAuditDocument($model) '_id' => $audit_id ] ]; - } return (bool) $this->client->bulk($params); @@ -231,7 +231,7 @@ public function updateAliases() [ 'add' => [ 'index' => $this->index, - 'alias' => $this->index.'_write' + 'alias' => $this->index . '_write' ] ] ] @@ -263,60 +263,58 @@ public function putMapping() $params = [ 'index' => $this->index, 'body' => [ - 'mappings' => [ - '_source' => [ - 'enabled' => true + '_source' => [ + 'enabled' => true + ], + 'properties' => [ + 'event' => [ + 'type' => 'keyword' ], - 'properties' => [ - 'event' => [ - 'type' => 'keyword' - ], - 'auditable_type' => [ - 'type' => 'keyword' - ], - 'ip_address' => [ - 'type' => 'keyword' - ], - 'url' => [ - 'type' => 'keyword' - ], - 'user_agent' => [ - 'type' => 'keyword' - ], - 'created_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'new_values' => [ - 'properties' => [ - 'created_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'updated_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'deleted_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ] + 'auditable_type' => [ + 'type' => 'keyword' + ], + 'ip_address' => [ + 'type' => 'keyword' + ], + 'url' => [ + 'type' => 'keyword' + ], + 'user_agent' => [ + 'type' => 'keyword' + ], + 'created_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'new_values' => [ + 'properties' => [ + 'created_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'updated_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'deleted_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' ] - ], - 'old_values' => [ - 'properties' => [ - 'created_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'updated_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'deleted_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ] + ] + ], + 'old_values' => [ + 'properties' => [ + 'created_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'updated_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'deleted_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' ] ] ]