Skip to content

Commit cadfda1

Browse files
committed
feat: Allow public DSNs
1 parent b6c9911 commit cadfda1

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/sentry/client.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ defmodule Sentry.Client do
187187

188188
query =
189189
data
190+
|> Enum.filter(fn {_, value} -> value != nil end)
190191
|> Enum.map(fn {name, value} -> "#{name}=#{value}" end)
191192
|> Enum.join(", ")
192193

@@ -203,15 +204,15 @@ defmodule Sentry.Client do
203204
@doc """
204205
Get a Sentry DSN which is simply a URI.
205206
206-
{PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}
207+
{PROTOCOL}://{PUBLIC_KEY}[:{SECRET_KEY}]@{HOST}/{PATH}{PROJECT_ID}
207208
"""
208209
@spec get_dsn :: dsn
209210
def get_dsn do
210211
dsn = Config.dsn()
211212

212213
with %URI{userinfo: userinfo, host: host, port: port, path: path, scheme: protocol}
213-
when is_binary(path) <- URI.parse(dsn),
214-
[public_key, secret_key] <- String.split(userinfo, ":", parts: 2),
214+
when is_binary(path) and is_binary(userinfo) <- URI.parse(dsn),
215+
[public_key, secret_key | _] <- String.split(userinfo, ":", parts: 2) ++ [nil],
215216
[_, binary_project_id] <- String.split(path, "/"),
216217
{project_id, ""} <- Integer.parse(binary_project_id),
217218
endpoint <- "#{protocol}://#{host}:#{port}/api/#{project_id}/store/" do

test/client_test.exs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ defmodule Sentry.ClientTest do
1616
}, sentry_timestamp=\d{10}, sentry_key=public, sentry_secret=secret$/
1717
end
1818

19+
test "authorization without secret" do
20+
modify_env(:sentry, dsn: "https://[email protected]/1")
21+
{_endpoint, public_key, private_key} = Client.get_dsn()
22+
23+
assert Client.authorization_header(public_key, private_key) =~
24+
~r/^Sentry sentry_version=5, sentry_client=sentry-elixir\/#{
25+
Application.spec(:sentry, :vsn)
26+
}, sentry_timestamp=\d{10}, sentry_key=public$/
27+
end
28+
1929
test "get dsn with default config" do
2030
modify_env(:sentry, dsn: "https://public:[email protected]/1")
2131

@@ -31,8 +41,8 @@ defmodule Sentry.ClientTest do
3141
Sentry.Client.get_dsn()
3242
end
3343

34-
test "errors on bad public/secret keys" do
35-
modify_env(:sentry, dsn: "https://public@app.getsentry.com/1")
44+
test "errors on bad public keys" do
45+
modify_env(:sentry, dsn: "https://app.getsentry.com/1")
3646

3747
capture_log(fn ->
3848
assert :error = Sentry.Client.get_dsn()

0 commit comments

Comments
 (0)