diff --git a/README.md b/README.md index 96461bdb..7deb10a0 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ An example production config might look like this: ```elixir config :sentry, - dsn: "https://public:secret@app.getsentry.com/1", + dsn: "___PUBLIC_DSN___", environment_name: :prod, included_environments: [:prod], enable_source_code_context: true, @@ -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:secret@app.getsentry.com/1", +config :sentry, dsn: "___PUBLIC_DSN___", included_environments: [:prod], environment_name: Mix.env ``` @@ -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:secret@app.getsentry.com/1", +config :sentry, dsn: "___PUBLIC_DSN___", included_environments: ~w(production staging), environment_name: System.get_env("RELEASE_LEVEL") || "development" ``` diff --git a/docs/config.rst b/docs/config.rst index 27821390..7c2d7153 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -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:secret@app.getsentry.com/1" + dsn: "___PUBLIC_DSN___" If using an environment with Plug or Phoenix add the following to your router: diff --git a/docs/index.rst b/docs/index.rst index 6edccca4..8068315f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -35,7 +35,7 @@ Setup the application production environment in your ``config/prod.exs`` .. code-block:: elixir config :sentry, - dsn: "https://public:secret@app.getsentry.com/1", + dsn: "___PUBLIC_DSN___", environment_name: :prod, enable_source_code_context: true, root_source_code_path: File.cwd!, @@ -55,7 +55,7 @@ An alternative is to use ``Mix.env`` in your general configuration file: .. code-block:: elixir - config :sentry, dsn: "https://public:secret@app.getsentry.com/1" + config :sentry, dsn: "___PUBLIC_DSN___" included_environments: [:prod], environment_name: Mix.env @@ -70,7 +70,7 @@ environment variable that determines the release level. .. code-block:: elixir - config :sentry, dsn: "https://public:secret@app.getsentry.com/1" + config :sentry, dsn: "___PUBLIC_DSN___" included_environments: ~w(production staging), environment_name: System.get_env("RELEASE_LEVEL") || "development" diff --git a/docs/usage.rst b/docs/usage.rst index bcbb84eb..74229abc 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -11,7 +11,7 @@ Otherwise we provide a simple way to capture exceptions: .. code-block:: elixir - do + try do ThisWillError.reall() rescue my_exception -> diff --git a/lib/sentry/client.ex b/lib/sentry/client.ex index c4a360f1..3602bb67 100644 --- a/lib/sentry/client.ex +++ b/lib/sentry/client.ex @@ -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(", ") @@ -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 @@ -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} -> diff --git a/test/client_test.exs b/test/client_test.exs index 47f18f79..a1aaf800 100644 --- a/test/client_test.exs +++ b/test/client_test.exs @@ -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") @@ -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()