Skip to content

Commit efe1f8d

Browse files
Merge pull request #263 from getsentry/feat/public-dsn
feat: Allow public DSNs
2 parents b6c9911 + a28e852 commit efe1f8d

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ An example production config might look like this:
105105

106106
```elixir
107107
config :sentry,
108-
dsn: "https://public:[email protected]/1",
108+
dsn: "___PUBLIC_DSN___",
109109
environment_name: :prod,
110110
included_environments: [:prod],
111111
enable_source_code_context: true,
@@ -126,7 +126,7 @@ specific configuration like `config/prod.exs`.
126126
Alternatively, you could use Mix.env in your general configuration file:
127127

128128
```elixir
129-
config :sentry, dsn: "https://public:[email protected]/1",
129+
config :sentry, dsn: "___PUBLIC_DSN___",
130130
included_environments: [:prod],
131131
environment_name: Mix.env
132132
```
@@ -137,7 +137,7 @@ to handle this without adding an additional Mix environment, you can set an
137137
environment variable that determines the release level.
138138

139139
```elixir
140-
config :sentry, dsn: "https://public:[email protected]/1",
140+
config :sentry, dsn: "___PUBLIC_DSN___",
141141
included_environments: ~w(production staging),
142142
environment_name: System.get_env("RELEASE_LEVEL") || "development"
143143
```

docs/config.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Simply add configuration to the ``:sentry`` key in the file ``config/prod.exs``:
88
.. code-block:: elixir
99
1010
config :sentry,
11-
dsn: "https://public:[email protected]/1"
11+
dsn: "___PUBLIC_DSN___"
1212
1313
If using an environment with Plug or Phoenix add the following to your router:
1414

docs/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Setup the application production environment in your ``config/prod.exs``
3535
.. code-block:: elixir
3636
3737
config :sentry,
38-
dsn: "https://public:[email protected]/1",
38+
dsn: "___PUBLIC_DSN___",
3939
environment_name: :prod,
4040
enable_source_code_context: true,
4141
root_source_code_path: File.cwd!,
@@ -55,7 +55,7 @@ An alternative is to use ``Mix.env`` in your general configuration file:
5555

5656
.. code-block:: elixir
5757
58-
config :sentry, dsn: "https://public:[email protected]/1"
58+
config :sentry, dsn: "___PUBLIC_DSN___"
5959
included_environments: [:prod],
6060
environment_name: Mix.env
6161
@@ -70,7 +70,7 @@ environment variable that determines the release level.
7070

7171
.. code-block:: elixir
7272
73-
config :sentry, dsn: "https://public:[email protected]/1"
73+
config :sentry, dsn: "___PUBLIC_DSN___"
7474
included_environments: ~w(production staging),
7575
environment_name: System.get_env("RELEASE_LEVEL") || "development"
7676

docs/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Otherwise we provide a simple way to capture exceptions:
1111

1212
.. code-block:: elixir
1313
14-
do
14+
try do
1515
ThisWillError.reall()
1616
rescue
1717
my_exception ->

lib/sentry/client.ex

Lines changed: 12 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] <- keys_from_userinfo(userinfo),
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
@@ -304,6 +305,14 @@ defmodule Sentry.Client do
304305
end
305306
end
306307

308+
defp keys_from_userinfo(userinfo) do
309+
case String.split(userinfo, ":", parts: 2) do
310+
[public, secret] -> [public, secret]
311+
[public] -> [public, nil]
312+
_ -> :error
313+
end
314+
end
315+
307316
defp get_headers_and_endpoint do
308317
case get_dsn() do
309318
{endpoint, public_key, secret_key} ->

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)