Skip to content

Commit 708c0d9

Browse files
MB-61292: Test keys on "Save" if they are in use
We already test keys before assigning them to encrypt data. This change ensures that if we are modifying a key that is already in use, we will test it before saving. Change-Id: I11075f716f0dc0ed6da7b2c098f90664b4df7125 Reviewed-on: https://review.couchbase.org/c/ns_server/+/232629 Tested-by: Timofey Barmin <[email protected]> Well-Formed: Build Bot <[email protected]> Reviewed-by: Navdeep S Boparai <[email protected]>
1 parent 707a455 commit 708c0d9

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

apps/ns_server/src/cb_cluster_secrets.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
node_supports_encryption_at_rest/1,
8585
max_dek_num/1,
8686
fetch_snapshot_in_txn/1,
87-
recalculate_deks_info/0]).
87+
recalculate_deks_info/0,
88+
is_secret_used/2]).
8889

8990
%% gen_server callbacks
9091
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -849,6 +850,18 @@ max_dek_num(Kind) ->
849850
end,
850851
?get_param({max_dek_num, Kind}, Default).
851852

853+
-spec is_secret_used(secret_id(), chronicle_snapshot()) -> boolean().
854+
is_secret_used(Id, Snapshot) ->
855+
case get_secret(Id, Snapshot) of
856+
{ok, SecretProps} ->
857+
case can_delete_secret(SecretProps, Snapshot) of
858+
ok -> false;
859+
{error, {used_by, _}} -> true
860+
end;
861+
{error, not_found} ->
862+
false
863+
end.
864+
852865
%%%===================================================================
853866
%%% gen_server callbacks
854867
%%%===================================================================

apps/ns_server/src/menelaus_web_secrets.erl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ handle_post_secret(Req) ->
6464
menelaus_util:assert_is_enterprise(),
6565
assert_is_79(),
6666
with_validated_secret(
67-
fun (ToAdd, _) ->
67+
fun (ToAdd, _, _) ->
6868
maybe
6969
{ok, Res} ?= cb_cluster_secrets:add_new_secret(ToAdd),
7070
Formatted = export_secret(Res),
@@ -79,8 +79,20 @@ handle_put_secret(IdStr, Req) ->
7979
assert_is_79(),
8080
Id = parse_id(IdStr),
8181
with_validated_secret(
82-
fun (Props, _) ->
82+
fun (Props, CurProps, Snapshot) ->
8383
maybe
84+
%% If the secret is already in use, we need to test it before
85+
%% saving it (because we test key when we are assigning it for
86+
%% encryption). Strictly speaking, the fact that it works now
87+
%% doesn't mean that it will work in the future, but it's better
88+
%% than nothing, and hopefully it will help avoiding most of the
89+
%% issues. Not doing it inside a save transaction because
90+
%% (1) testing is very slow and (2) it will still give us no
91+
%% guarantee that it will work in the future.
92+
ok ?= case cb_cluster_secrets:is_secret_used(Id, Snapshot) of
93+
true -> cb_cluster_secrets:test(Props, CurProps);
94+
false -> ok
95+
end,
8496
IsSecretWritableMFA = {?MODULE, is_writable_remote,
8597
[?HIDE(Req), node()]},
8698
%% replace_secret will check "old usages" inside txn
@@ -97,7 +109,7 @@ handle_test_post_secret(Req) ->
97109
menelaus_util:assert_is_enterprise(),
98110
assert_is_79(),
99111
with_validated_secret(
100-
fun (Params, _) ->
112+
fun (Params, _, _) ->
101113
maybe
102114
ok ?= cb_cluster_secrets:test(Params, undefined),
103115
menelaus_util:reply(Req, 200),
@@ -135,7 +147,7 @@ handle_test_put_secret(IdStr, Req) ->
135147
assert_is_79(),
136148
Id = parse_id(IdStr),
137149
with_validated_secret(
138-
fun (Params, CurProps) ->
150+
fun (Params, CurProps, _) ->
139151
maybe
140152
ok ?= cb_cluster_secrets:test(Params, CurProps),
141153
menelaus_util:reply(Req, 200),
@@ -173,7 +185,7 @@ with_validated_secret(Fun, ExistingId, Req) ->
173185
%% Checking "new usages" here:
174186
true ?= is_writable(Props, Req),
175187
%% Fun is responsible for checking "old usages" inside txn
176-
ok ?= Fun(Props, CurProps)
188+
ok ?= Fun(Props, CurProps, Snapshot)
177189
else
178190
false ->
179191
menelaus_util:web_exception(403,

0 commit comments

Comments
 (0)