Skip to content

Commit

Permalink
feat: Allow public DSNs
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-auer committed Apr 5, 2018
1 parent b6c9911 commit cadfda1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/sentry/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ defmodule Sentry.Client do

query =
data
|> Enum.filter(fn {_, value} -> value != nil end)
|> Enum.map(fn {name, value} -> "#{name}=#{value}" end)
|> Enum.join(", ")

Expand All @@ -203,15 +204,15 @@ defmodule Sentry.Client do
@doc """
Get a Sentry DSN which is simply a URI.
{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}
{PROTOCOL}://{PUBLIC_KEY}[:{SECRET_KEY}]@{HOST}/{PATH}{PROJECT_ID}
"""
@spec get_dsn :: dsn
def get_dsn do
dsn = Config.dsn()

with %URI{userinfo: userinfo, host: host, port: port, path: path, scheme: protocol}
when is_binary(path) <- URI.parse(dsn),
[public_key, secret_key] <- String.split(userinfo, ":", parts: 2),
when is_binary(path) and is_binary(userinfo) <- URI.parse(dsn),
[public_key, secret_key | _] <- String.split(userinfo, ":", parts: 2) ++ [nil],
[_, binary_project_id] <- String.split(path, "/"),
{project_id, ""} <- Integer.parse(binary_project_id),
endpoint <- "#{protocol}://#{host}:#{port}/api/#{project_id}/store/" do
Expand Down
14 changes: 12 additions & 2 deletions test/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ defmodule Sentry.ClientTest do
}, sentry_timestamp=\d{10}, sentry_key=public, sentry_secret=secret$/
end

test "authorization without secret" do
modify_env(:sentry, dsn: "https://public@app.getsentry.com/1")
{_endpoint, public_key, private_key} = Client.get_dsn()

assert Client.authorization_header(public_key, private_key) =~
~r/^Sentry sentry_version=5, sentry_client=sentry-elixir\/#{
Application.spec(:sentry, :vsn)
}, sentry_timestamp=\d{10}, sentry_key=public$/
end

test "get dsn with default config" do
modify_env(:sentry, dsn: "https://public:secret@app.getsentry.com/1")

Expand All @@ -31,8 +41,8 @@ defmodule Sentry.ClientTest do
Sentry.Client.get_dsn()
end

test "errors on bad public/secret keys" do
modify_env(:sentry, dsn: "https://public@app.getsentry.com/1")
test "errors on bad public keys" do
modify_env(:sentry, dsn: "https://app.getsentry.com/1")

capture_log(fn ->
assert :error = Sentry.Client.get_dsn()
Expand Down

0 comments on commit cadfda1

Please sign in to comment.