Skip to content

Commit

Permalink
Cleanup the DSN (#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric authored Oct 30, 2023
1 parent 0ed7fd9 commit 9ed638f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 90 deletions.
50 changes: 9 additions & 41 deletions src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ final class Dsn implements \Stringable
*/
private $publicKey;

/**
* @var string|null The secret key to authenticate the SDK
*/
private $secretKey;

/**
* @var string The ID of the resource to access
*/
Expand All @@ -50,21 +45,19 @@ final class Dsn implements \Stringable
/**
* Class constructor.
*
* @param string $scheme The protocol to be used to access the resource
* @param string $host The host that holds the resource
* @param int $port The port on which the resource is exposed
* @param string $projectId The ID of the resource to access
* @param string $path The specific resource that the web client wants to access
* @param string $publicKey The public key to authenticate the SDK
* @param string|null $secretKey The secret key to authenticate the SDK
*/
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey, ?string $secretKey)
* @param string $scheme The protocol to be used to access the resource
* @param string $host The host that holds the resource
* @param int $port The port on which the resource is exposed
* @param string $projectId The ID of the resource to access
* @param string $path The specific resource that the web client wants to access
* @param string $publicKey The public key to authenticate the SDK
*/
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey)
{
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->publicKey = $publicKey;
$this->secretKey = $secretKey;
$this->path = $path;
$this->projectId = $projectId;
}
Expand All @@ -88,10 +81,6 @@ public static function createFromString(string $value): self
}
}

if (isset($parsedDsn['pass']) && empty($parsedDsn['pass'])) {
throw new \InvalidArgumentException(sprintf('The "%s" DSN must contain a valid secret key.', $value));
}

if (!\in_array($parsedDsn['scheme'], ['http', 'https'], true)) {
throw new \InvalidArgumentException(sprintf('The scheme of the "%s" DSN must be either "http" or "https".', $value));
}
Expand All @@ -111,8 +100,7 @@ public static function createFromString(string $value): self
$parsedDsn['port'] ?? ($parsedDsn['scheme'] === 'http' ? 80 : 443),
$projectId,
$path,
$parsedDsn['user'],
$parsedDsn['pass'] ?? null
$parsedDsn['user']
);
}

Expand Down Expand Up @@ -164,22 +152,6 @@ public function getPublicKey(): string
return $this->publicKey;
}

/**
* Gets the secret key to authenticate the SDK.
*/
public function getSecretKey(): ?string
{
return $this->secretKey;
}

/**
* Returns the URL of the API for the store endpoint.
*/
public function getStoreApiEndpointUrl(): string
{
return $this->getBaseEndpointUrl() . '/store/';
}

/**
* Returns the URL of the API for the envelope endpoint.
*/
Expand All @@ -203,10 +175,6 @@ public function __toString(): string
{
$url = $this->scheme . '://' . $this->publicKey;

if ($this->secretKey !== null) {
$url .= ':' . $this->secretKey;
}

$url .= '@' . $this->host;

if (($this->scheme === 'http' && $this->port !== 80) || ($this->scheme === 'https' && $this->port !== 443)) {
Expand Down
17 changes: 4 additions & 13 deletions src/Util/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@ final class Http
*/
public static function getRequestHeaders(Dsn $dsn, string $sdkIdentifier, string $sdkVersion): array
{
$data = [
'sentry_version' => Client::PROTOCOL_VERSION,
'sentry_client' => $sdkIdentifier . '/' . $sdkVersion,
'sentry_key' => $dsn->getPublicKey(),
$authHeader = [
'sentry_version=' . Client::PROTOCOL_VERSION,
'sentry_client=' . $sdkIdentifier . '/' . $sdkVersion,
'sentry_key=' . $dsn->getPublicKey(),
];

if ($dsn->getSecretKey() !== null) {
$data['sentry_secret'] = $dsn->getSecretKey();
}

$authHeader = [];
foreach ($data as $headerKey => $headerValue) {
$authHeader[] = $headerKey . '=' . $headerValue;
}

return [
'Content-Type' => 'application/x-sentry-envelope',
'X-Sentry-Auth' => 'Sentry ' . implode(', ', $authHeader),
Expand Down
26 changes: 0 additions & 26 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function testCreateFromString(
string $expectedHost,
int $expectedPort,
string $expectedPublicKey,
?string $expectedSecretKey,
string $expectedProjectId,
string $expectedPath
): void {
Expand All @@ -31,7 +30,6 @@ public function testCreateFromString(
$this->assertSame($expectedHost, $dsn->getHost());
$this->assertSame($expectedPort, $dsn->getPort());
$this->assertSame($expectedPublicKey, $dsn->getPublicKey());
$this->assertSame($expectedSecretKey, $dsn->getSecretKey());
$this->assertSame($expectedProjectId, $dsn->getProjectId(true));
$this->assertSame($expectedPath, $dsn->getPath());
}
Expand All @@ -44,7 +42,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
80,
'public',
null,
'1',
'/sentry',
];
Expand All @@ -55,7 +52,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
80,
'public',
null,
'1',
'',
];
Expand All @@ -66,7 +62,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
80,
'public',
'secret',
'1',
'',
];
Expand All @@ -77,7 +72,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
80,
'public',
null,
'1',
'',
];
Expand All @@ -88,7 +82,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
8080,
'public',
null,
'1',
'',
];
Expand All @@ -99,7 +92,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
443,
'public',
null,
'1',
'',
];
Expand All @@ -110,7 +102,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
443,
'public',
null,
'1',
'',
];
Expand All @@ -121,7 +112,6 @@ public static function createFromStringDataProvider(): \Generator
'example.com',
4343,
'public',
null,
'1',
'',
];
Expand Down Expand Up @@ -155,11 +145,6 @@ public static function createFromStringThrowsExceptionIfValueIsInvalidDataProvid
'The "http://:secret@example.com/sentry/1" DSN must contain a scheme, a host, a user and a path component.',
];

yield 'missing secret key' => [
'http://public:@example.com/sentry/1',
'The "http://public:@example.com/sentry/1" DSN must contain a valid secret key.',
];

yield 'missing host' => [
'/sentry/1',
'The "/sentry/1" DSN must contain a scheme, a host, a user and a path component.',
Expand All @@ -176,16 +161,6 @@ public static function createFromStringThrowsExceptionIfValueIsInvalidDataProvid
];
}

/**
* @dataProvider getStoreApiEndpointUrlDataProvider
*/
public function testGetStoreApiEndpointUrl(string $value, string $expectedUrl): void
{
$dsn = Dsn::createFromString($value);

$this->assertSame($expectedUrl, $dsn->getStoreApiEndpointUrl());
}

public static function getStoreApiEndpointUrlDataProvider(): \Generator
{
yield [
Expand Down Expand Up @@ -264,7 +239,6 @@ public static function toStringDataProvider(): array
{
return [
['http://public@example.com/sentry/1'],
['http://public:secret@example.com/sentry/1'],
['http://public@example.com/1'],
['http://public@example.com:8080/sentry/1'],
['https://public@example.com/sentry/1'],
Expand Down
10 changes: 0 additions & 10 deletions tests/Util/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public static function getRequestHeadersDataProvider(): \Generator
'X-Sentry-Auth' => 'Sentry sentry_version=7, sentry_client=sentry.sdk.identifier/1.2.3, sentry_key=public',
],
];

yield [
Dsn::createFromString('http://public:secret@example.com/1'),
'sentry.sdk.identifier',
'1.2.3',
[
'Content-Type' => 'application/x-sentry-envelope',
'X-Sentry-Auth' => 'Sentry sentry_version=7, sentry_client=sentry.sdk.identifier/1.2.3, sentry_key=public, sentry_secret=secret',
],
];
}

/**
Expand Down

0 comments on commit 9ed638f

Please sign in to comment.