Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ An example production config might look like this:

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

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

```elixir
config :sentry, dsn: "https://public:[email protected]/1",
config :sentry, dsn: "___PUBLIC_DSN___",
included_environments: ~w(production staging),
environment_name: System.get_env("RELEASE_LEVEL") || "development"
```
Expand Down
2 changes: 1 addition & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Simply add configuration to the ``:sentry`` key in the file ``config/prod.exs``:
.. code-block:: elixir

config :sentry,
dsn: "https://public:[email protected]/1"
dsn: "___PUBLIC_DSN___"

If using an environment with Plug or Phoenix add the following to your router:

Expand Down
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Setup the application production environment in your ``config/prod.exs``
.. code-block:: elixir

config :sentry,
dsn: "https://public:[email protected]/1",
dsn: "___PUBLIC_DSN___",
environment_name: :prod,
enable_source_code_context: true,
root_source_code_path: File.cwd!,
Expand All @@ -55,7 +55,7 @@ An alternative is to use ``Mix.env`` in your general configuration file:

.. code-block:: elixir

config :sentry, dsn: "https://public:[email protected]/1"
config :sentry, dsn: "___PUBLIC_DSN___"
included_environments: [:prod],
environment_name: Mix.env

Expand All @@ -70,7 +70,7 @@ environment variable that determines the release level.

.. code-block:: elixir

config :sentry, dsn: "https://public:[email protected]/1"
config :sentry, dsn: "___PUBLIC_DSN___"
included_environments: ~w(production staging),
environment_name: System.get_env("RELEASE_LEVEL") || "development"

Expand Down
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Otherwise we provide a simple way to capture exceptions:

.. code-block:: elixir

do
try do
ThisWillError.reall()
rescue
my_exception ->
Expand Down
15 changes: 12 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] <- keys_from_userinfo(userinfo),
[_, binary_project_id] <- String.split(path, "/"),
{project_id, ""} <- Integer.parse(binary_project_id),
endpoint <- "#{protocol}://#{host}:#{port}/api/#{project_id}/store/" do
Expand Down Expand Up @@ -304,6 +305,14 @@ defmodule Sentry.Client do
end
end

defp keys_from_userinfo(userinfo) do
case String.split(userinfo, ":", parts: 2) do
[public, secret] -> [public, secret]
[public] -> [public, nil]
_ -> :error
end
end

defp get_headers_and_endpoint do
case get_dsn() do
{endpoint, public_key, secret_key} ->
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://[email protected]/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:[email protected]/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