Skip to content

Commit

Permalink
Ensure redis healthcheck key is unique
Browse files Browse the repository at this point in the history
At the moment we use the same key for our healthcheck. This poses a
potential race condition problem as we share the Redis database across
all our applications.

Instead we can generate a random key for each request which should
prevent this problem from happening.
  • Loading branch information
thomasleese committed Jan 18, 2021
1 parent c9d0e9b commit cfb60c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/govuk_app_config/govuk_healthcheck/redis.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "securerandom"

module GovukHealthcheck
class Redis
def name
Expand All @@ -7,9 +9,11 @@ def name
def status
client = ::Redis.new

client.set("healthcheck", "val")
client.get("healthcheck")
client.del("healthcheck")
key = "healthcheck-#{SecureRandom.hex}"

client.set(key, "val")
client.get(key)
client.del(key)

client.close

Expand Down
5 changes: 3 additions & 2 deletions spec/lib/govuk_healthcheck/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
before do
stub_const("Redis", redis)
allow(redis).to receive(:new).and_return(redis_client)
allow(SecureRandom).to receive(:hex).and_return("abc")
end

context "when the database is connected" do
before do
allow(redis_client)
.to receive(:set).with("healthcheck", anything)
.to receive(:set).with("healthcheck-abc", anything)
end

it_behaves_like "a healthcheck"
Expand All @@ -27,7 +28,7 @@
context "when the database is not connected" do
before do
allow(redis_client)
.to receive(:set).with("healthcheck", anything)
.to receive(:set).with("healthcheck-abc", anything)
.and_raise("error")
end

Expand Down

0 comments on commit cfb60c6

Please sign in to comment.