Skip to content

Commit 9fc92de

Browse files
committed
Add auth to Fastly purge requests
1 parent 2830910 commit 9fc92de

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

config/config.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ config :changelog, ChangelogWeb.Endpoint,
1919
config :changelog,
2020
buffer_token: SecretOrEnv.get("BUFFER_TOKEN"),
2121
cm_api_token: Base.encode64("#{SecretOrEnv.get("CM_API_TOKEN")}:x"),
22+
fastly_token: SecretOrEnv.get("FASTLY_TOKEN"),
2223
github_api_token: SecretOrEnv.get("GITHUB_API_TOKEN"),
2324
hn_user: SecretOrEnv.get("HN_USER"),
2425
hn_pass: SecretOrEnv.get("HN_PASS"),

lib/changelog/fastly.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ defmodule Changelog.Fastly do
137137
end
138138

139139
def purge(url) do
140+
auth = Application.get_env(:changelog, :fastly_token)
140141
%{path: path, host: host} = URI.parse(url)
141142
Logger.info("Fastly: Purging #{url}")
142-
HTTP.request(:purge, endpoint(path), "", host: host)
143+
HTTP.request(:purge, endpoint(path), "", [{"Host", host} , {"Fastly-Key", auth}])
143144
end
144145
end

lib/changelog/http.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ defmodule Changelog.HTTP do
66

77
@options [ssl: [{:middlebox_comp_mode, false}]]
88

9-
def request(method, body \\ "", headers \\ [], options \\ []) do
10-
case HTTPoison.request(method, body, headers, options) do
11-
{:error, _} -> HTTPoison.request(method, body, headers, with_shared_options(options))
9+
def request(method, url, body \\ "", headers \\ [], options \\ []) do
10+
case HTTPoison.request(method, url, body, headers, options) do
11+
{:error, _} -> HTTPoison.request(method, url, body, headers, with_shared_options(options))
1212
response -> response
1313
end
1414
end

test/changelog/fastly_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ defmodule Changelog.FastlyTest do
77

88
describe "purge/1" do
99
test "when given a url string" do
10-
with_mock(HTTP, request: fn _, _, _, _ -> %{status_code: 200} end) do
10+
with_mock(HTTP, request: fn _, _, _, _-> %{status_code: 200} end) do
1111
url = "https://cdn.changelog.com/uploads/podcast/1/the-changelog-1.mp3"
1212
Changelog.Fastly.purge(url)
13-
assert called(HTTP.request(:purge, "https://api.fastly.com/uploads/podcast/1/the-changelog-1.mp3", "", host: "cdn.changelog.com"))
13+
assert called(HTTP.request(:purge, "https://api.fastly.com/uploads/podcast/1/the-changelog-1.mp3", "", :_))
1414
end
1515
end
1616
end

0 commit comments

Comments
 (0)