Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #469 Added requireAccessToken() method to senders #595

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/Senders/AgentSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
12 changes: 12 additions & 0 deletions src/Senders/CurlSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/Senders/FluentSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Senders/SenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/FluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down