Skip to content

Commit

Permalink
Migration for 3918 (#3939)
Browse files Browse the repository at this point in the history
* Revert "Pages shield (#3918)"

This reverts commit 53f94a9.

* Migration: Shield page rules
  • Loading branch information
aerosol committed Mar 25, 2024
1 parent 53f94a9 commit 9989ce6
Show file tree
Hide file tree
Showing 22 changed files with 291 additions and 1,395 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
All notable changes to this project will be documented in this file.

### Added
- Pages Block List in Site Settings
- Add `conversion_rate` to Stats API Timeseries and on the main graph
- Add `total_conversions` and `conversion_rate` to `visitors.csv` in a goal-filtered CSV export
- Ability to display total conversions (with a goal filter) on the main graph
- Add `conversion_rate` to Stats API Timeseries and on the main graph
Expand Down
15 changes: 0 additions & 15 deletions lib/plausible/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ defmodule Plausible.Application do
interval: :timer.seconds(35),
warmer_fn: :refresh_updated_recently
]},
{Plausible.Shield.PageRuleCache, ttl_check_interval: false, ets_options: [:bag]},
{Plausible.Cache.Warmer,
[
child_name: Plausible.Shield.PageRuleCache.All,
cache_impl: Plausible.Shield.PageRuleCache,
interval: :timer.minutes(3) + Enum.random(1..:timer.seconds(10)),
warmer_fn: :refresh_all
]},
{Plausible.Cache.Warmer,
[
child_name: Plausible.Shield.PageRuleCache.RecentlyUpdated,
cache_impl: Plausible.Shield.PageRuleCache,
interval: :timer.seconds(35),
warmer_fn: :refresh_updated_recently
]},
{Plausible.Auth.TOTP.Vault, key: totp_vault_key()},
PlausibleWeb.Endpoint,
{Oban, Application.get_env(:plausible, Oban)},
Expand Down
31 changes: 14 additions & 17 deletions lib/plausible/ingestion/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ defmodule Plausible.Ingestion.Event do
| :dc_ip
| :site_ip_blocklist
| :site_country_blocklist
| :site_page_blocklist

@type t() :: %__MODULE__{
domain: String.t() | nil,
Expand Down Expand Up @@ -121,7 +120,6 @@ defmodule Plausible.Ingestion.Event do
[
&drop_datacenter_ip/1,
&drop_shield_rule_ip/1,
&drop_shield_rule_page/1,
&put_geolocation/1,
&drop_shield_rule_country/1,
&put_user_agent/1,
Expand Down Expand Up @@ -184,18 +182,15 @@ defmodule Plausible.Ingestion.Event do
end

defp drop_shield_rule_ip(%__MODULE__{} = event) do
if Plausible.Shields.ip_blocked?(event.domain, event.request.remote_ip) do
drop(event, :site_ip_blocklist)
else
event
end
end
domain = event.domain
address = event.request.remote_ip

defp drop_shield_rule_page(%__MODULE__{} = event) do
if Plausible.Shields.page_blocked?(event.domain, event.request.pathname) do
drop(event, :site_page_blocklist)
else
event
case Plausible.Shield.IPRuleCache.get({domain, address}) do
%Plausible.Shield.IPRule{action: :deny} ->
drop(event, :site_ip_blocklist)

_ ->
event
end
end

Expand Down Expand Up @@ -268,10 +263,12 @@ defmodule Plausible.Ingestion.Event do
%__MODULE__{domain: domain, clickhouse_session_attrs: %{country_code: cc}} = event
)
when is_binary(domain) and is_binary(cc) do
if Plausible.Shields.country_blocked?(domain, cc) do
drop(event, :site_country_blocklist)
else
event
case Plausible.Shield.CountryRuleCache.get({domain, String.upcase(cc)}) do
%Plausible.Shield.CountryRule{action: :deny} ->
drop(event, :site_country_blocklist)

_ ->
event
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/shield/country_rule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Plausible.Shield.CountryRule do
|> cast(attrs, [:site_id, :country_code])
|> validate_required([:site_id, :country_code])
|> validate_length(:country_code, is: 2)
|> validate_change(:country_code, fn :country_code, cc ->
|> Ecto.Changeset.validate_change(:country_code, fn :country_code, cc ->
if cc in Enum.map(Location.Country.all(), & &1.alpha_2) do
[]
else
Expand Down
85 changes: 0 additions & 85 deletions lib/plausible/shield/page_rule.ex

This file was deleted.

65 changes: 0 additions & 65 deletions lib/plausible/shield/page_rule_cache.ex

This file was deleted.

74 changes: 0 additions & 74 deletions lib/plausible/shields.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,11 @@ defmodule Plausible.Shields do
@maximum_country_rules 30
def maximum_country_rules(), do: @maximum_country_rules

@maximum_page_rules 30
def maximum_page_rules(), do: @maximum_page_rules

@spec list_ip_rules(Site.t() | non_neg_integer()) :: [Shield.IPRule.t()]
def list_ip_rules(site_or_id) do
list(Shield.IPRule, site_or_id)
end

@spec ip_blocked?(Site.t() | String.t(), String.t()) :: boolean()
def ip_blocked?(%Site{domain: domain}, address) do
ip_blocked?(domain, address)
end

def ip_blocked?(domain, address) when is_binary(domain) and is_binary(address) do
case Shield.IPRuleCache.get({domain, address}) do
%Shield.IPRule{action: :deny} ->
true

_ ->
false
end
end

@spec page_blocked?(Site.t() | String.t(), String.t()) :: boolean()
def page_blocked?(%Site{domain: domain}, address) do
page_blocked?(domain, address)
end

def page_blocked?(domain, pathname) when is_binary(domain) and is_binary(pathname) do
page_rules = Shield.PageRuleCache.get(domain)

if page_rules do
page_rules
|> List.wrap()
|> Enum.find_value(false, fn rule ->
rule.action == :deny and Regex.match?(rule.page_path_pattern, pathname)
end)
else
false
end
end

@spec country_blocked?(Site.t() | String.t(), String.t()) :: boolean()
def country_blocked?(%Site{domain: domain}, country_code) do
country_blocked?(domain, country_code)
end

def country_blocked?(domain, country_code) when is_binary(domain) and is_binary(country_code) do
case Shield.CountryRuleCache.get({domain, String.upcase(country_code)}) do
%Shield.CountryRule{action: :deny} ->
true

_ ->
false
end
end

@spec add_ip_rule(Site.t() | non_neg_integer(), map(), Keyword.t()) ::
{:ok, Shield.IPRule.t()} | {:error, Ecto.Changeset.t()}
def add_ip_rule(site_or_id, params, opts \\ []) do
Expand Down Expand Up @@ -111,28 +59,6 @@ defmodule Plausible.Shields do
count(Shield.CountryRule, site_or_id)
end

@spec list_page_rules(Site.t() | non_neg_integer()) :: [Shield.PageRule.t()]
def list_page_rules(site_or_id) do
list(Shield.PageRule, site_or_id)
end

@spec add_page_rule(Site.t() | non_neg_integer(), map(), Keyword.t()) ::
{:ok, Shield.PageRule.t()} | {:error, Ecto.Changeset.t()}
def add_page_rule(site_or_id, params, opts \\ []) do
opts = Keyword.put(opts, :limit, {:page_path, @maximum_page_rules})
add(Shield.PageRule, site_or_id, params, opts)
end

@spec remove_page_rule(Site.t() | non_neg_integer(), String.t()) :: :ok
def remove_page_rule(site_or_id, rule_id) do
remove(Shield.PageRule, site_or_id, rule_id)
end

@spec count_page_rules(Site.t() | non_neg_integer()) :: non_neg_integer()
def count_page_rules(site_or_id) do
count(Shield.PageRule, site_or_id)
end

defp list(schema, %Site{id: id}) do
list(schema, id)
end
Expand Down

0 comments on commit 9989ce6

Please sign in to comment.