Allow domain change (#2803)
* Migration (PR: https://github.com/plausible/analytics/pull/2802) * Implement Site.Domain interface allowing change and expiry * Fixup seeds so they work with V2_MIGRATION_DONE=1 * Update Sites.Cache so it's capable of multi-keyed lookups * Implement worker handling domain change expiration * Implement domain change UI * Implement transition period for public APIs * Exclude v2 tests in primary test run * Update lib/plausible_web/controllers/site_controller.ex Co-authored-by: Vini Brasil <vini@hey.com> * Update lib/plausible_web/controllers/site_controller.ex Co-authored-by: Vini Brasil <vini@hey.com> * Update moduledoc * Update changelog * Remove remnant from previous implementation attempt * !fixup * !fixup * Implement domain change via Sites API cc @ukutaht * Update CHANGELOG * Credo * !fixup commit missing tests * Allow continuous domain change within the same site --------- Co-authored-by: Vini Brasil <vini@hey.com>pull/2819/head r2023.04.04-93
parent
5ca53a70be
commit
1d01328287
@ -0,0 +1,62 @@
|
||||
defmodule Plausible.Site.Domain do
|
||||
@expire_threshold_hours 72
|
||||
|
||||
@moduledoc """
|
||||
Basic interface for domain changes.
|
||||
|
||||
Once `Plausible.DataMigration.NumericIDs` schema migration is ready,
|
||||
domain change operation will be enabled, accessible to the users.
|
||||
|
||||
We will set a transition period of #{@expire_threshold_hours} hours
|
||||
during which, both old and new domains, will be accepted as traffic
|
||||
identifiers to the same site.
|
||||
|
||||
A periodic worker will call the `expire/0` function to end it where applicable.
|
||||
See: `Plausible.Workers.ExpireDomainChangeTransitions`.
|
||||
|
||||
The underlying changeset for domain change (see: `Plausible.Site`) relies
|
||||
on database trigger installed via `Plausible.Repo.Migrations.AllowDomainChange`
|
||||
Postgres migration. The trigger checks if either `domain` or `domain_changed_from`
|
||||
exist to ensure unicity.
|
||||
"""
|
||||
|
||||
alias Plausible.Site
|
||||
alias Plausible.Repo
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
@spec expire_change_transitions(integer()) :: {:ok, non_neg_integer()}
|
||||
def expire_change_transitions(expire_threshold_hours \\ @expire_threshold_hours) do
|
||||
{updated, _} =
|
||||
Repo.update_all(
|
||||
from(s in Site,
|
||||
where: s.domain_changed_at < ago(^expire_threshold_hours, "hour")
|
||||
),
|
||||
set: [
|
||||
domain_changed_from: nil,
|
||||
domain_changed_at: nil
|
||||
]
|
||||
)
|
||||
|
||||
{:ok, updated}
|
||||
end
|
||||
|
||||
@spec change(Site.t(), String.t(), Keyword.t()) ::
|
||||
{:ok, Site.t()} | {:error, Ecto.Changeset.t()}
|
||||
def change(%Site{} = site, new_domain, opts \\ []) do
|
||||
changeset = Site.update_changeset(site, %{domain: new_domain}, opts)
|
||||
|
||||
changeset =
|
||||
if Enum.empty?(changeset.changes) and is_nil(changeset.errors[:domain]) do
|
||||
Ecto.Changeset.add_error(
|
||||
changeset,
|
||||
:domain,
|
||||
"New domain must be different than the current one"
|
||||
)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
Repo.update(changeset)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,29 @@
|
||||
<div class="w-full max-w-3xl mt-4 mx-auto flex">
|
||||
<%= form_for @changeset, Routes.site_path(@conn, :change_domain_submit, @site.domain), [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-lg rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Change your website domain</h2>
|
||||
|
||||
<div class="my-6">
|
||||
<%= label f, :domain, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<p class="text-gray-500 dark:text-gray-400 text-xs mt-1">Just the naked domain or subdomain without 'www'</p>
|
||||
<div class="mt-2 flex rounded-md shadow-sm">
|
||||
<span class="inline-flex items-center px-3 rounded-l-md border border-r-0 border-gray-300 dark:border-gray-500 bg-gray-50 dark:bg-gray-850 text-gray-500 dark:text-gray-400 sm:text-sm">
|
||||
https://
|
||||
</span>
|
||||
<%= text_input f, :domain, class: "focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-800 flex-1 block w-full px-3 py-2 rounded-none rounded-r-md sm:text-sm border-gray-300 dark:border-gray-500 dark:bg-gray-900 dark:text-gray-300", placeholder: "example.com" %>
|
||||
</div>
|
||||
<%= error_tag f, :domain %>
|
||||
</div>
|
||||
|
||||
<p class="text-sm sm:text-sm text-gray-700 dark:text-gray-300">
|
||||
<span class="font-bold dark:text-gray-100">Once you change your domain, you must update the JavaScript snippet on your site within 72 hours to guarantee continuous tracking</span>. If you're using the API, please also make sure to update your API credentials.</p>
|
||||
<p class="text-sm sm:text-sm text-gray-700 dark:text-gray-300 mt-4">
|
||||
Visit our <a target="_blank" href="https://plausible.io/docs/change-domain-name/" class="text-indigo-500">documentation</a> for details.
|
||||
</p>
|
||||
|
||||
<%= submit "Change domain and add new snippet →", class: "button mt-4 w-full" %>
|
||||
|
||||
<div class="text-center mt-8">
|
||||
<%= link "Back to site settings", to: Routes.site_path(@conn, :settings_general, @site.domain), class: "text-indigo-500 w-full text-center" %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
@ -0,0 +1,23 @@
|
||||
<div class="w-full max-w-3xl mt-4 mx-auto flex">
|
||||
<%= form_for @conn, "/", [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<h2 class="text-xl font-bold dark:text-gray-100">Change JavaScript snippet</h2>
|
||||
<div class="my-4">
|
||||
<p class="dark:text-gray-100">Replace your snippet in the <code><head></code> of your website.</p>
|
||||
|
||||
<div class="relative">
|
||||
<%= textarea f, :domain, id: "snippet_code", class: "transition overflow-hidden bg-gray-100 dark:bg-gray-900 appearance-none border border-transparent rounded w-full p-2 pr-6 text-gray-700 dark:text-gray-300 leading-normal appearance-none focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-400 dark:focus:border-gray-500 text-xs mt-4 resize-none", value: snippet(@site), rows: 3, readonly: "readonly" %>
|
||||
<a onclick="var textarea = document.getElementById('snippet_code'); textarea.focus(); textarea.select(); document.execCommand('copy');" href="javascript:void(0)" class="no-underline text-indigo-500 text-sm hover:underline">
|
||||
<svg class="absolute text-indigo-500" style="top: 24px; right: 12px;" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="text-sm sm:text-sm text-gray-700 dark:text-gray-300">
|
||||
<span class="font-bold dark:text-gray-100">Your domain has been changed. You must update the JavaScript snippet on your site within 72 hours to guarantee continuous tracking</span>. If you're using the API, please also make sure to update your API credentials.</p>
|
||||
<p class="text-sm sm:text-sm text-gray-700 dark:text-gray-300 mt-4">
|
||||
Visit our <a target="_blank" href="https://plausible.io/docs/change-domain-name/" class="text-indigo-500">documentation</a> for details.
|
||||
</p>
|
||||
|
||||
<%= link("I understand, I'll change my snippet →", class: "button mt-4 w-full", to: "/#{URI.encode_www_form(@site.domain)}") %>
|
||||
<% end %>
|
||||
</div>
|
||||
@ -0,0 +1,22 @@
|
||||
defmodule Plausible.Workers.ExpireDomainChangeTransitions do
|
||||
@moduledoc """
|
||||
Periodic worker that expires domain change transition period.
|
||||
Old domains are frozen for a given time, so users can still access them
|
||||
before redeploying their scripts and integrations.
|
||||
"""
|
||||
use Plausible.Repo
|
||||
use Oban.Worker, queue: :domain_change_transition
|
||||
|
||||
require Logger
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_job) do
|
||||
{:ok, n} = Plausible.Site.Domain.expire_change_transitions()
|
||||
|
||||
if n > 0 do
|
||||
Logger.warning("Expired #{n} from the domain change transition period.")
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,96 @@
|
||||
defmodule Plausible.Site.DomainTest do
|
||||
alias Plausible.Site
|
||||
alias Plausible.Site.Domain
|
||||
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
@moduletag :v2_only
|
||||
|
||||
test "successful change" do
|
||||
site = insert(:site)
|
||||
assert {:ok, updated} = Domain.change(site, "new-domain.example.com")
|
||||
assert updated.domain_changed_from == site.domain
|
||||
assert updated.domain == "new-domain.example.com"
|
||||
assert updated.domain_changed_at
|
||||
end
|
||||
|
||||
test "domain_changed_from is kept unique, so no double change is possible" do
|
||||
site1 = insert(:site)
|
||||
assert {:ok, _} = Domain.change(site1, "new-domain.example.com")
|
||||
|
||||
site2 = insert(:site)
|
||||
assert {:error, changeset} = Domain.change(site2, "new-domain.example.com")
|
||||
assert {error_message, _} = changeset.errors[:domain]
|
||||
assert error_message =~ "This domain cannot be registered"
|
||||
end
|
||||
|
||||
test "domain is also guaranteed unique against existing domain_changed_from entries" do
|
||||
site1 =
|
||||
insert(:site, domain: "site1.example.com", domain_changed_from: "oldsite1.example.com")
|
||||
|
||||
site2 = insert(:site, domain: "site2.example.com")
|
||||
|
||||
assert {:error, %{errors: [{:domain, {error, _}}]}} = Domain.change(site2, site1.domain)
|
||||
|
||||
assert {:error, %{errors: [{:domain, {^error, _}}]}} =
|
||||
Domain.change(site2, site1.domain_changed_from)
|
||||
|
||||
assert error =~ "This domain cannot be registered"
|
||||
end
|
||||
|
||||
test "a single site's domain can be changed back and forth" do
|
||||
site1 = insert(:site, domain: "foo.example.com")
|
||||
site2 = insert(:site, domain: "baz.example.com")
|
||||
|
||||
assert {:ok, _} = Domain.change(site1, "bar.example.com")
|
||||
|
||||
assert {:error, _} = Domain.change(site2, "bar.example.com")
|
||||
assert {:error, _} = Domain.change(site2, "foo.example.com")
|
||||
|
||||
assert {:ok, _} = Domain.change(Repo.reload!(site1), "foo.example.com")
|
||||
assert {:ok, _} = Domain.change(Repo.reload!(site1), "bar.example.com")
|
||||
end
|
||||
|
||||
test "change info is cleared when the grace period expires" do
|
||||
site = insert(:site)
|
||||
|
||||
assert {:ok, site} = Domain.change(site, "new-domain.example.com")
|
||||
assert site.domain_changed_from
|
||||
assert site.domain_changed_at
|
||||
|
||||
assert {:ok, _} = Domain.expire_change_transitions(-1)
|
||||
refute Repo.reload!(site).domain_changed_from
|
||||
refute Repo.reload!(site).domain_changed_at
|
||||
end
|
||||
|
||||
test "expire changes overdue" do
|
||||
now = NaiveDateTime.utc_now()
|
||||
yesterday = now |> NaiveDateTime.add(-60 * 60 * 24, :second)
|
||||
three_days_ago = now |> NaiveDateTime.add(-60 * 60 * 72, :second)
|
||||
|
||||
{:ok, s1} = insert(:site) |> Domain.change("new-domain1.example.com")
|
||||
{:ok, s2} = insert(:site) |> Domain.change("new-domain2.example.com", at: yesterday)
|
||||
|
||||
{:ok, s3} = insert(:site) |> Domain.change("new-domain3.example.com", at: three_days_ago)
|
||||
|
||||
assert {:ok, 1} = Domain.expire_change_transitions()
|
||||
|
||||
assert is_nil(Repo.reload!(s3).domain_changed_from)
|
||||
assert is_nil(Repo.reload!(s3).domain_changed_at)
|
||||
|
||||
assert {:ok, 1} = Domain.expire_change_transitions(24)
|
||||
assert is_nil(Repo.reload!(s2).domain_changed_at)
|
||||
|
||||
assert {:ok, 0} = Domain.expire_change_transitions()
|
||||
assert Repo.reload!(s1).domain_changed_at
|
||||
end
|
||||
|
||||
test "new domain gets validated" do
|
||||
site = build(:site)
|
||||
changeset = Site.update_changeset(site, %{domain: " "})
|
||||
assert {"can't be blank", _} = changeset.errors[:domain]
|
||||
|
||||
changeset = Site.update_changeset(site, %{domain: "?#[]"})
|
||||
assert {"must not contain URI reserved characters" <> _, _} = changeset.errors[:domain]
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,43 @@
|
||||
defmodule Plausible.Workers.ExpireDomainChangeTransitionsTest do
|
||||
use Plausible.DataCase, async: true
|
||||
alias Plausible.Workers.ExpireDomainChangeTransitions
|
||||
alias Plausible.Site
|
||||
alias Plausible.Sites
|
||||
|
||||
import ExUnit.CaptureLog
|
||||
|
||||
@moduletag :v2_only
|
||||
|
||||
test "doesn't log when there is nothing to do" do
|
||||
log =
|
||||
capture_log(fn ->
|
||||
assert :ok = ExpireDomainChangeTransitions.perform(nil)
|
||||
end)
|
||||
|
||||
assert log == ""
|
||||
end
|
||||
|
||||
test "expires domains selectively after change and logs the result" do
|
||||
now = NaiveDateTime.utc_now()
|
||||
yesterday = now |> NaiveDateTime.add(-60 * 60 * 24, :second)
|
||||
three_days_ago = now |> NaiveDateTime.add(-60 * 60 * 72, :second)
|
||||
long_time_ago = now |> NaiveDateTime.add(-60 * 60 * 24 * 365, :second)
|
||||
|
||||
insert(:site) |> Site.Domain.change("site1.example.com")
|
||||
insert(:site) |> Site.Domain.change("site2.example.com", at: yesterday)
|
||||
insert(:site) |> Site.Domain.change("site3.example.com", at: three_days_ago)
|
||||
insert(:site) |> Site.Domain.change("site4.example.com", at: long_time_ago)
|
||||
|
||||
log =
|
||||
capture_log(fn ->
|
||||
assert :ok = ExpireDomainChangeTransitions.perform(nil)
|
||||
end)
|
||||
|
||||
assert log =~ "Expired 2 from the domain change transition period"
|
||||
|
||||
assert Sites.get_by_domain("site1.example.com").domain_changed_from
|
||||
assert Sites.get_by_domain("site2.example.com").domain_changed_from
|
||||
refute Sites.get_by_domain("site3.example.com").domain_changed_from
|
||||
refute Sites.get_by_domain("site4.example.com").domain_changed_from
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue