From 91efb13e29ddc58b5b6cd26fa9d8bf6f0a12f1ef Mon Sep 17 00:00:00 2001 From: Tom Janssen Date: Mon, 21 Mar 2022 12:00:23 +0100 Subject: [PATCH 1/4] add ability to hide a cache value --- src/Watchers/CacheWatcher.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Watchers/CacheWatcher.php b/src/Watchers/CacheWatcher.php index f5434fbd3..fe1931e5f 100644 --- a/src/Watchers/CacheWatcher.php +++ b/src/Watchers/CacheWatcher.php @@ -42,7 +42,7 @@ public function recordCacheHit(CacheHit $event) Telescope::recordCache(IncomingEntry::make([ 'type' => 'hit', 'key' => $event->key, - 'value' => $event->value, + 'value' => $this->formatValue($event), ])); } @@ -79,7 +79,7 @@ public function recordKeyWritten(KeyWritten $event) Telescope::recordCache(IncomingEntry::make([ 'type' => 'set', 'key' => $event->key, - 'value' => $event->value, + 'value' => $this->formatValue($event), 'expiration' => $this->formatExpiration($event), ])); } @@ -126,4 +126,30 @@ private function shouldIgnore($event) 'telescope:*', ], $event->key); } + + /** + * Determine the value of an event. + * + * @param mixed $event + * @return mixed + */ + private function formatValue($event) + { + return (! $this->shouldHideValue($event)) + ? $event->value : '********'; + } + + /** + * Determine if the event value should be ignored. + * + * @param mixed $event + * @return bool + */ + private function shouldHideValue($event) + { + return Str::is( + $this->options['hideValues'] ?? [], + $event->key + ); + } } From 5652aeeb00a6d1351e7dd9b888380a585001f13c Mon Sep 17 00:00:00 2001 From: Tom Janssen Date: Mon, 21 Mar 2022 12:00:40 +0100 Subject: [PATCH 2/4] add tests to confirm hiding --- tests/Watchers/CacheWatcherTest.php | 37 ++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/Watchers/CacheWatcherTest.php b/tests/Watchers/CacheWatcherTest.php index 18cf1ce25..a7d5ce05c 100644 --- a/tests/Watchers/CacheWatcherTest.php +++ b/tests/Watchers/CacheWatcherTest.php @@ -15,7 +15,12 @@ protected function getEnvironmentSetUp($app) parent::getEnvironmentSetUp($app); $app->get('config')->set('telescope.watchers', [ - CacheWatcher::class => true, + CacheWatcher::class => [ + 'enabled' => true, + 'hideValues' => [ + 'my-hidden-value-key', + ], + ] ]); } @@ -76,4 +81,34 @@ public function test_cache_watcher_registers_forget_entries() $this->assertSame('forget', $entry->content['type']); $this->assertSame('outdated', $entry->content['key']); } + + public function test_cache_watcher_hides_hidden_values_when_set() + { + $this->app->get(Repository::class)->put('my-hidden-value-key', 'laravel', 1); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertSame(EntryType::CACHE, $entry->type); + $this->assertSame('set', $entry->content['type']); + $this->assertSame('my-hidden-value-key', $entry->content['key']); + $this->assertSame('********', $entry->content['value']); + } + + public function test_cache_watcher_hides_hidden_values_when_retrieved() + { + $repository = $this->app->get(Repository::class); + + Telescope::withoutRecording(function () use ($repository) { + $repository->put('my-hidden-value-key', 'laravel', 1); + }); + + $repository->get('my-hidden-value-key'); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertSame(EntryType::CACHE, $entry->type); + $this->assertSame('hit', $entry->content['type']); + $this->assertSame('my-hidden-value-key', $entry->content['key']); + $this->assertSame('********', $entry->content['value']); + } } From 7fae2e9749df4efb2486978d901de2d9c13f835d Mon Sep 17 00:00:00 2001 From: Tom Janssen Date: Mon, 21 Mar 2022 12:05:35 +0100 Subject: [PATCH 3/4] fix style --- tests/Watchers/CacheWatcherTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Watchers/CacheWatcherTest.php b/tests/Watchers/CacheWatcherTest.php index a7d5ce05c..5daf591e6 100644 --- a/tests/Watchers/CacheWatcherTest.php +++ b/tests/Watchers/CacheWatcherTest.php @@ -20,7 +20,7 @@ protected function getEnvironmentSetUp($app) 'hideValues' => [ 'my-hidden-value-key', ], - ] + ], ]); } From 9d6d3e4e2503aaa74371f8f470e9b682abd6d20e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 25 Mar 2022 10:07:11 -0500 Subject: [PATCH 4/4] formatting --- src/Watchers/CacheWatcher.php | 47 +++++++++++++++-------------- tests/Watchers/CacheWatcherTest.php | 2 +- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Watchers/CacheWatcher.php b/src/Watchers/CacheWatcher.php index fe1931e5f..19f03014b 100644 --- a/src/Watchers/CacheWatcher.php +++ b/src/Watchers/CacheWatcher.php @@ -103,53 +103,54 @@ public function recordKeyForgotten(KeyForgotten $event) } /** - * @param \Illuminate\Cache\Events\KeyWritten $event + * Determine the value of an event. + * + * @param mixed $event * @return mixed */ - protected function formatExpiration(KeyWritten $event) + private function formatValue($event) { - return property_exists($event, 'seconds') - ? $event->seconds : $event->minutes * 60; + return (! $this->shouldHideValue($event)) + ? $event->value + : '********'; } /** - * Determine if the event should be ignored. + * Determine if the event value should be ignored. * * @param mixed $event * @return bool */ - private function shouldIgnore($event) + private function shouldHideValue($event) { - return Str::is([ - 'illuminate:queue:restart', - 'framework/schedule*', - 'telescope:*', - ], $event->key); + return Str::is( + $this->options['hidden'] ?? [], + $event->key + ); } /** - * Determine the value of an event. - * - * @param mixed $event + * @param \Illuminate\Cache\Events\KeyWritten $event * @return mixed */ - private function formatValue($event) + protected function formatExpiration(KeyWritten $event) { - return (! $this->shouldHideValue($event)) - ? $event->value : '********'; + return property_exists($event, 'seconds') + ? $event->seconds : $event->minutes * 60; } /** - * Determine if the event value should be ignored. + * Determine if the event should be ignored. * * @param mixed $event * @return bool */ - private function shouldHideValue($event) + private function shouldIgnore($event) { - return Str::is( - $this->options['hideValues'] ?? [], - $event->key - ); + return Str::is([ + 'illuminate:queue:restart', + 'framework/schedule*', + 'telescope:*', + ], $event->key); } } diff --git a/tests/Watchers/CacheWatcherTest.php b/tests/Watchers/CacheWatcherTest.php index 5daf591e6..2580ab633 100644 --- a/tests/Watchers/CacheWatcherTest.php +++ b/tests/Watchers/CacheWatcherTest.php @@ -17,7 +17,7 @@ protected function getEnvironmentSetUp($app) $app->get('config')->set('telescope.watchers', [ CacheWatcher::class => [ 'enabled' => true, - 'hideValues' => [ + 'hidden' => [ 'my-hidden-value-key', ], ],