Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Private key is not longer required for DSN #232

Merged
merged 3 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The only thing you have to do is provide a DSN, either by registering an instanc

protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
container.Register(new Dsn("http://public:secret@example.com/project-id"));
container.Register(new Dsn("http://public@example.com/project-id"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You could use ___PUBLIC_DSN___ here to show an interactive DSN chooser in docs and the setup wizard:

container.Register(new Dsn("___PUBLIC_DSN___"));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}

or through configuration:
Expand All @@ -118,7 +118,7 @@ or through configuration:
<section name="sharpRaven" type="SharpRaven.Nancy.NancyConfiguration, SharpRaven.Nancy" />
</configSections>
<sharpRaven>
<dsn value="http://public:secret@example.com/project-id" />
<dsn value="http://public@example.com/project-id" />
</sharpRaven>
</configuration>

Expand Down
10 changes: 6 additions & 4 deletions src/app/SharpRaven/Dsn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Dsn(string dsn)
{
this.uri = new Uri(dsn);
this.privateKey = GetPrivateKey(this.uri);
this.publicKey = GetPublicKey(this.uri);
this.publicKey = GetPublicKey(this.uri) ?? throw new ArgumentException("A publicKey is required.", nameof(dsn));
this.port = this.uri.Port;
this.projectID = GetProjectID(this.uri);
this.path = GetPath(this.uri);
Expand Down Expand Up @@ -176,13 +176,14 @@ private static string GetPath(Uri uri)


/// <summary>
/// Get a private key from a Dsn uri.
/// Get a private key or null if no private key defined.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private static string GetPrivateKey(Uri uri)
{
return uri.UserInfo.Split(':')[1];
var parts = uri.UserInfo.Split(':');
return parts.Length == 2 ? parts[1] : null;
}


Expand All @@ -205,7 +206,8 @@ private static string GetProjectID(Uri uri)
/// <returns></returns>
private static string GetPublicKey(Uri uri)
{
return uri.UserInfo.Split(':')[0];
var publicKey = uri.UserInfo.Split(':')[0];
return publicKey != string.Empty ? publicKey : null;
}
}
}
4 changes: 2 additions & 2 deletions src/app/SharpRaven/Utilities/PacketBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public static string CreateAuthenticationHeader(Dsn dsn)
+ ", sentry_client={1}"
+ ", sentry_timestamp={2}"
+ ", sentry_key={3}"
+ ", sentry_secret={4}",
+ "{4}",
SentryVersion,
UserAgent,
(long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds,
dsn.PublicKey,
dsn.PrivateKey);
dsn.PrivateKey != null ? ", sentry_secret=" + dsn.PrivateKey : null);
}
}
}
28 changes: 28 additions & 0 deletions src/tests/SharpRaven.UnitTests/DsnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public void Constructor_ValidHttpsUri_SentryUriHasHttpsScheme()
}


[Test]
public void Constructor_ValidDsnWithoutPrivateKey_PrivateKeyGetterReturnsNull()
{
var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey);

Assert.That(dsn.PrivateKey, Is.Null);
}


[Test]
public void Constructor_ValidDsnWithoutPrivateKey_PublicKeyIsParsed()
{
var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey);

Assert.AreEqual("7d6466e66155431495bdb4036ba9a04b", dsn.PublicKey);
}


[Test]
public void Constructor_ValidHttpsUri_UriIsEqualToDsn()
{
Expand All @@ -83,6 +101,16 @@ public void Constructor_ValidHttpsUri_UriIsEqualToDsn()
}


[Test]
public void Constructor_ValidDsnWithoutPrivateKey_UriIsEqualToDsn()
{
var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey);

Assert.That(dsn.Uri, Is.Not.Null);
Assert.That(dsn.Uri.ToString(), Is.EqualTo(TestHelper.DsnUriWithoutPrivateKey));
}


[Test]
public void Constructor_ValidHttpUri_SentryUriHasHttpScheme()
{
Expand Down
16 changes: 15 additions & 1 deletion src/tests/SharpRaven.UnitTests/Utilities/PacketBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace SharpRaven.UnitTests.Utilities
public class PacketBuilderTests
{
[Test]
public void CreateAuthenticationHeader_ReturnsCorrectAuthenticationHeader()
public void CreateAuthenticationHeader_DsnWithSecret_ReturnsCorrectAuthenticationHeader()
{
const string expected =
@"^Sentry sentry_version=[\d], sentry_client=SharpRaven/[\d\.]+, sentry_timestamp=\d+, sentry_key=7d6466e66155431495bdb4036ba9a04b, sentry_secret=4c1cfeab7ebd4c1cb9e18008173a3630$";
Expand All @@ -50,5 +50,19 @@ public void CreateAuthenticationHeader_ReturnsCorrectAuthenticationHeader()

Assert.That(authenticationHeader, Is.StringMatching(expected));
}

[Test]
public void CreateAuthenticationHeader_DsnWithoutSecret_ReturnsCorrectAuthenticationHeader()
{
const string expected =
@"^Sentry sentry_version=[\d], sentry_client=SharpRaven/[\d\.]+, sentry_timestamp=\d+, sentry_key=7d6466e66155431495bdb4036ba9a04b$";

var dsn = new Dsn(
"https://7d6466e66155431495bdb4036ba9a04b@app.getsentry.com/3739");

var authenticationHeader = PacketBuilder.CreateAuthenticationHeader(dsn);

Assert.That(authenticationHeader, Is.StringMatching(expected));
}
}
}
3 changes: 3 additions & 0 deletions src/tests/SharpRaven.UnitTests/Utilities/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static class TestHelper
public const string DsnUri =
"https://7d6466e66155431495bdb4036ba9a04b:4c1cfeab7ebd4c1cb9e18008173a3630@app.getsentry.com/3739";

public const string DsnUriWithoutPrivateKey =
"https://7d6466e66155431495bdb4036ba9a04b@app.getsentry.com/3739";


public static Exception GetException()
{
Expand Down