Skip to content

Commit 22b57bf

Browse files
authored
fix(secrethub): align cache headers for OIDC well-known endpoints (#705)
## 📝 Description We now set a 15‑minute cache window for the JWKS well‑known endpoints and mark their responses as publicly cacheable, so clients can reuse the key set instead of re-fetching on every request. Related [task](renderedtext/tasks#8900). ## ✅ Checklist - [x] I have tested this change - [x] ~This change requires documentation update~ N/A
1 parent defded4 commit 22b57bf

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

secrethub/lib/secrethub/open_id_connect/http_server.ex

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ defmodule Secrethub.OpenIDConnect.HTTPServer do
5555
configuration = openid_configuration(issuer, jwks_uri, org_id)
5656

5757
conn
58-
|> put_resp_header(
59-
"cache-control",
60-
"public, max-age=#{@openid_configuration_cache_max_age}, must-revalidate"
61-
)
58+
|> put_well_known_cache_control_header()
6259
|> json(200, configuration)
6360
end)
6461
end
@@ -89,15 +86,8 @@ defmodule Secrethub.OpenIDConnect.HTTPServer do
8986
public_keys = Secrethub.OpenIDConnect.KeyManager.public_keys(:openid_keys)
9087
Secrethub.OpenIDConnect.Utilization.submit_usage(conn.host)
9188

92-
if Secrethub.on_prem?() do
93-
max_age = Secrethub.OpenIDConnect.KeyManager.cache_max_age_in_seconds()
94-
cache_control_header = "max-age=#{max_age}, private, must-revalidate"
95-
96-
conn
97-
|> put_resp_header("cache-control", cache_control_header)
98-
else
99-
conn
100-
end
89+
conn
90+
|> put_well_known_cache_control_header()
10191
|> json(200, %{"keys" => public_keys})
10292
end
10393

@@ -119,4 +109,16 @@ defmodule Secrethub.OpenIDConnect.HTTPServer do
119109

120110
assign(conn, :org_username, username)
121111
end
112+
113+
defp put_well_known_cache_control_header(conn) do
114+
put_resp_header(conn, "cache-control", well_known_cache_control_header())
115+
end
116+
117+
defp well_known_cache_control_header do
118+
max_age =
119+
Secrethub.OpenIDConnect.KeyManager.cache_max_age_in_seconds()
120+
|> max(@openid_configuration_cache_max_age)
121+
122+
"max-age=#{max_age}, public, must-revalidate"
123+
end
122124
end

secrethub/test/secrethub/open_id_connect/http_server_test.exs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ defmodule Secrethub.OpenIDConnect.HTTPServerTest do
3434
{:ok, response} = request("/.well-known/openid-configuration")
3535

3636
assert response.status_code == 200
37-
assert {"cache-control", "public, max-age=900, must-revalidate"} in response.headers
37+
38+
assert {"cache-control", "max-age=900, public, must-revalidate"} in response.headers
3839

3940
{:ok, body} = Poison.decode(response.body)
4041

@@ -49,6 +50,8 @@ defmodule Secrethub.OpenIDConnect.HTTPServerTest do
4950

5051
assert response.status_code == 200
5152

53+
assert {"cache-control", "max-age=900, public, must-revalidate"} in response.headers
54+
5255
{:ok, body} = Poison.decode(response.body)
5356

5457
assert body == %{"keys" => Secrethub.OpenIDConnect.KeyManager.public_keys(:openid_keys)}
@@ -59,6 +62,8 @@ defmodule Secrethub.OpenIDConnect.HTTPServerTest do
5962

6063
assert response.status_code == 200
6164

65+
assert {"cache-control", "max-age=900, public, must-revalidate"} in response.headers
66+
6267
{:ok, body} = Poison.decode(response.body)
6368

6469
assert body == %{"keys" => Secrethub.OpenIDConnect.KeyManager.public_keys(:openid_keys)}
@@ -76,14 +81,24 @@ defmodule Secrethub.OpenIDConnect.HTTPServerTest do
7681
end)
7782
end
7883

84+
test "GET /.well-known/openid-configuration" do
85+
{:ok, response} = request("/.well-known/openid-configuration")
86+
87+
assert response.status_code == 200
88+
89+
cache_max_age = Application.fetch_env!(:secrethub, :openid_keys_cache_max_age_in_s)
90+
91+
assert {"cache-control", "max-age=#{cache_max_age}, public, must-revalidate"} in response.headers
92+
end
93+
7994
test "GET /.well-known/jwks" do
8095
{:ok, response} = HTTPoison.get("#{@host}/.well-known/jwks")
8196

8297
assert response.status_code == 200
8398

8499
cache_max_age = Application.fetch_env!(:secrethub, :openid_keys_cache_max_age_in_s)
85100

86-
assert {"cache-control", "max-age=#{cache_max_age}, private, must-revalidate"} in response.headers
101+
assert {"cache-control", "max-age=#{cache_max_age}, public, must-revalidate"} in response.headers
87102

88103
{:ok, body} = Poison.decode(response.body)
89104

@@ -97,7 +112,7 @@ defmodule Secrethub.OpenIDConnect.HTTPServerTest do
97112

98113
cache_max_age = Application.fetch_env!(:secrethub, :openid_keys_cache_max_age_in_s)
99114

100-
assert {"cache-control", "max-age=#{cache_max_age}, private, must-revalidate"} in response.headers
115+
assert {"cache-control", "max-age=#{cache_max_age}, public, must-revalidate"} in response.headers
101116

102117
{:ok, body} = Poison.decode(response.body)
103118

0 commit comments

Comments
 (0)