diff --git a/src/Config.php b/src/Config.php index 25474b5a..e79a0a2f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -291,13 +291,14 @@ protected function updateConfig(array $config): void $this->setLogPayloadLogger($config); $this->setVerbose($config); $this->setVerboseLogger($config); + // The sender must be set before the access token, so we know if it is required. + $this->setSender($config); $this->setAccessToken($config); $this->setDataBuilder($config); $this->setTransformer($config); $this->setMinimumLevel($config); $this->setReportSuppressed($config); $this->setFilters($config); - $this->setSender($config); $this->setScrubber($config); $this->setBatched($config); $this->setBatchSize($config); @@ -337,8 +338,14 @@ private function setAccessToken(array $config): void if (isset($_ENV['ROLLBAR_ACCESS_TOKEN']) && !isset($config['access_token'])) { $config['access_token'] = $_ENV['ROLLBAR_ACCESS_TOKEN']; } - $this->utilities()->validateString($config['access_token'], "config['access_token']", 32, false); - $this->accessToken = $config['access_token']; + + $this->utilities()->validateString( + $config['access_token'], + "config['access_token']", + 32, + !$this->sender->requireAccessToken(), + ); + $this->accessToken = $config['access_token'] ?? ''; } private function setEnabled(array $config): void diff --git a/src/Senders/AgentSender.php b/src/Senders/AgentSender.php index 06ef775d..285c742b 100644 --- a/src/Senders/AgentSender.php +++ b/src/Senders/AgentSender.php @@ -60,9 +60,23 @@ public function wait(string $accessToken, int $max): void return; } + /** + * Returns true if the access token is required by the sender to send the payload. The agent can be configured to + * provide its own access token. But may not have its own, so we are requiring it for now. See + * {@link https://github.com/rollbar/rollbar-php/issues/405} for more details. + * + * @since 4.0.0 + * + * @return bool + */ + public function requireAccessToken(): bool + { + return true; + } + private function loadAgentFile() { - $filename = $this->agentLogLocation . '/rollbar-relay.' . getmypid() . '.' . microtime(true) . '.rollbar'; + $filename = $this->agentLogLocation . '/rollbar-relay.' . getmypid() . '.' . microtime(true) . '.rollbar'; $this->agentLog = fopen($filename, 'a'); } } diff --git a/src/Senders/CurlSender.php b/src/Senders/CurlSender.php index 0df74309..4994827a 100644 --- a/src/Senders/CurlSender.php +++ b/src/Senders/CurlSender.php @@ -110,6 +110,18 @@ public function wait(string $accessToken, int $max = 0): void } } + /** + * Returns true if the access token is required by the sender to send the payload. The curl sender requires the + * access token since it is sending directly to the Rollbar service. + * + * @return bool + * @since 4.0.0 + */ + public function requireAccessToken(): bool + { + return false; + } + private function maybeSendMoreBatchRequests(string $accessToken) { $max = $this->maxBatchRequests - count($this->inflightRequests); diff --git a/src/Senders/FluentSender.php b/src/Senders/FluentSender.php index ddae224c..11f9612c 100644 --- a/src/Senders/FluentSender.php +++ b/src/Senders/FluentSender.php @@ -101,6 +101,18 @@ public function wait(string $accessToken, int $max): void return; } + /** + * Returns true if the access token is required by the sender to send the payload. The Fluentd service can provide + * its own access token. + * + * @return bool + * @since 4.0.0 + */ + public function requireAccessToken(): bool + { + return false; + } + /** * Loads the fluent logger */ diff --git a/src/Senders/SenderInterface.php b/src/Senders/SenderInterface.php index a705633f..395ffc60 100644 --- a/src/Senders/SenderInterface.php +++ b/src/Senders/SenderInterface.php @@ -44,4 +44,13 @@ public function sendBatch(array $batch, string $accessToken): void; * @return void */ public function wait(string $accessToken, int $max): void; + + /** + * Returns true if the access token is required by the sender to send the payload. In cases where an intermediary + * sender is being used like Fluentd. + * + * @return bool + * @since 4.0.0 + */ + public function requireAccessToken(): bool; } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index b7b8e0b5..3e158286 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -428,11 +428,14 @@ public function testFilter(): void public function testSender(): void { $p = m::mock(EncodedPayload::class); - $sender = m::mock(SenderInterface::class) - ->shouldReceive("send") + $sender = m::mock(SenderInterface::class); + $sender->shouldReceive("send") ->with($p, $this->getTestAccessToken()) ->once() ->mock(); + $sender->shouldReceive('requireAccessToken') + ->once() + ->mock(); $c = new Config(array( "access_token" => $this->getTestAccessToken(), "environment" => $this->env, diff --git a/tests/FluentTest.php b/tests/FluentTest.php index 8438ca35..7272ac33 100644 --- a/tests/FluentTest.php +++ b/tests/FluentTest.php @@ -28,7 +28,7 @@ public function testFluent(): void 'fluent_port' => $port, 'handler' => 'fluent' )); - $this->assertEquals(200, $logger->log(Level::INFO, 'this is a test', array())->getStatus()); + $this->assertEquals(200, $logger->report(Level::INFO, 'this is a test', array())->getStatus()); socket_close($socket); }