Skip to content

Commit

Permalink
CacheWatcher hide values (#1206)
Browse files Browse the repository at this point in the history
* add ability to hide a cache value

* add tests to confirm hiding

* fix style

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
dododedodonl and taylorotwell authored Mar 25, 2022
1 parent 82eb2c2 commit e8f09dc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/Watchers/CacheWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]));
}

Expand Down Expand Up @@ -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),
]));
}
Expand All @@ -102,6 +102,33 @@ public function recordKeyForgotten(KeyForgotten $event)
]));
}

/**
* 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['hidden'] ?? [],
$event->key
);
}

/**
* @param \Illuminate\Cache\Events\KeyWritten $event
* @return mixed
Expand Down
37 changes: 36 additions & 1 deletion tests/Watchers/CacheWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ protected function getEnvironmentSetUp($app)
parent::getEnvironmentSetUp($app);

$app->get('config')->set('telescope.watchers', [
CacheWatcher::class => true,
CacheWatcher::class => [
'enabled' => true,
'hidden' => [
'my-hidden-value-key',
],
],
]);
}

Expand Down Expand Up @@ -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']);
}
}

0 comments on commit e8f09dc

Please sign in to comment.