Static global API key

pull/5080/head
Viktor Baranov 3 years ago
parent 71c5238645
commit 5a24940032
  1. 2
      CHANGELOG.md
  2. 20
      apps/block_scout_web/config/config.exs
  3. 6
      apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex
  4. 29
      apps/block_scout_web/lib/block_scout_web/views/access_helpers.ex
  5. 1
      apps/explorer/test/explorer/etherscan_test.exs
  6. 6
      docker/Makefile

@ -3,8 +3,8 @@
### Features
### Fixes
- [#5066](https://github.com/blockscout/blockscout/pull/5066) - Fix read contract page bug
- [#5071](https://github.com/blockscout/blockscout/pull/5071) - Fix write page contract tuple input
- [#5066](https://github.com/blockscout/blockscout/pull/5066) - Fix read contract page bug
- [#5034](https://github.com/blockscout/blockscout/pull/5034) - Fix broken functions input at transation page
- [#5025](https://github.com/blockscout/blockscout/pull/5025) - Add standard input JSON files validation
- [#5051](https://github.com/blockscout/blockscout/pull/5051) - Fix 500 response when ABI method was parsed as nil

@ -54,16 +54,28 @@ config :block_scout_web,
re_captcha_secret_key: System.get_env("RE_CAPTCHA_SECRET_KEY", nil),
re_captcha_client_key: System.get_env("RE_CAPTCHA_CLIENT_KEY", nil)
api_rate_limit_value =
global_api_rate_limit_value =
"API_RATE_LIMIT"
|> System.get_env("30")
|> System.get_env("50")
|> Integer.parse()
|> case do
{integer, ""} -> integer
_ -> 30
_ -> 50
end
config :block_scout_web, api_rate_limit: api_rate_limit_value
api_rate_limit_by_key_value =
"API_RATE_LIMIT_BY_KEY"
|> System.get_env("50")
|> Integer.parse()
|> case do
{integer, ""} -> integer
_ -> 50
end
config :block_scout_web,
global_api_rate_limit: global_api_rate_limit_value,
api_rate_limit_by_key: api_rate_limit_by_key_value,
static_api_key: System.get_env("STATIC_API_KEY", nil)
config :block_scout_web, BlockScoutWeb.Counters.BlocksIndexedCounter, enabled: true

@ -25,7 +25,11 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do
alias Plug.Conn
APILogger.message(
"Current API rate limit #{inspect(Application.get_env(:block_scout_web, :api_rate_limit))} reqs/sec"
"Current global API rate limit #{inspect(Application.get_env(:block_scout_web, :global_api_rate_limit))} reqs/sec"
)
APILogger.message(
"Current API rate limit by key #{inspect(Application.get_env(:block_scout_web, :api_rate_limit_by_key))} reqs/sec"
)
def init(opts), do: opts

@ -74,16 +74,31 @@ defmodule BlockScoutWeb.AccessHelpers do
|> Conn.halt()
end
def check_rate_limit(_conn) do
def check_rate_limit(conn) do
if Mix.env() == :test do
:ok
else
case Hammer.check_rate("api", 1_000, Application.get_env(:block_scout_web, :api_rate_limit)) do
{:allow, _count} ->
:ok
{:deny, _limit} ->
:rate_limit_reached
global_api_rate_limit = Application.get_env(:block_scout_web, :global_api_rate_limit)
api_rate_limit_by_key = Application.get_env(:block_scout_web, :api_rate_limit_by_key)
static_api_key = Application.get_env(:block_scout_web, :static_api_key)
if conn.query_params && Map.has_key?(conn.query_params, "apikey") &&
Map.get(conn.query_params, "apikey") == static_api_key do
case Hammer.check_rate("api-#{static_api_key}", 1_000, api_rate_limit_by_key) do
{:allow, _count} ->
:ok
{:deny, _limit} ->
:rate_limit_reached
end
else
case Hammer.check_rate("api", 1_000, global_api_rate_limit) do
{:allow, _count} ->
:ok
{:deny, _limit} ->
:rate_limit_reached
end
end
end
end

@ -1248,7 +1248,6 @@ defmodule Explorer.EtherscanTest do
options = %{order_by_direction: :desc}
found_token_transfers = Etherscan.list_token_transfers(address.hash, nil, options)
IO.inspect("Gimme found_token_transfers #{inspect(found_token_transfers)}")
block_numbers_order = Enum.map(found_token_transfers, & &1.block_number)

@ -356,6 +356,12 @@ endif
ifdef API_RATE_LIMIT
BLOCKSCOUT_CONTAINER_PARAMS += -e 'API_RATE_LIMIT=$(API_RATE_LIMIT)'
endif
ifdef API_RATE_LIMIT_BY_KEY
BLOCKSCOUT_CONTAINER_PARAMS += -e 'API_RATE_LIMIT_BY_KEY=$(API_RATE_LIMIT_BY_KEY)'
endif
ifdef STATIC_API_KEY
BLOCKSCOUT_CONTAINER_PARAMS += -e 'STATIC_API_KEY=$(STATIC_API_KEY)'
endif
HAS_BLOCKSCOUT_IMAGE := $(shell docker images | grep -sw ${DOCKER_IMAGE})
build:

Loading…
Cancel
Save