|
| 1 | +% Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 2 | +% use this file except in compliance with the License. You may obtain a copy of |
| 3 | +% the License at |
| 4 | +% |
| 5 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +% |
| 7 | +% Unless required by applicable law or agreed to in writing, software |
| 8 | +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +% License for the specific language governing permissions and limitations under |
| 11 | +% the License. |
| 12 | + |
| 13 | +-module(couch_secrets). |
| 14 | + |
| 15 | +-behaviour(gen_server). |
| 16 | +-behaviour(config_listener). |
| 17 | + |
| 18 | +-include_lib("couch/include/couch_db.hrl"). |
| 19 | + |
| 20 | +%% public api |
| 21 | +-export([sign/1, sign/2, verify/2, verify/3, secret_is_set/0]). |
| 22 | + |
| 23 | +%% gen_server functions |
| 24 | +-export([ |
| 25 | + start_link/0, |
| 26 | + init/1, |
| 27 | + handle_call/3, |
| 28 | + handle_cast/2, |
| 29 | + handle_continue/2, |
| 30 | + handle_info/2 |
| 31 | +]). |
| 32 | + |
| 33 | +%% config_listener functions |
| 34 | +-export([ |
| 35 | + handle_config_change/5, |
| 36 | + handle_config_terminate/3 |
| 37 | +]). |
| 38 | + |
| 39 | +sign(Message) -> |
| 40 | + sign(Message, <<>>). |
| 41 | + |
| 42 | +sign(Message, ExtraSecret) -> |
| 43 | + [HashAlgorithm | _] = couch_util:get_config_hash_algorithms(), |
| 44 | + case current_secret_from_ets() of |
| 45 | + undefined -> |
| 46 | + throw({internal_server_error, <<"cookie auth secret is not set">>}); |
| 47 | + CurrentSecret -> |
| 48 | + FullSecret = <<CurrentSecret/binary, ExtraSecret/binary>>, |
| 49 | + couch_util:hmac(HashAlgorithm, FullSecret, Message) |
| 50 | + end. |
| 51 | + |
| 52 | +verify(Message, ExpectedMAC) -> |
| 53 | + verify(Message, <<>>, ExpectedMAC). |
| 54 | + |
| 55 | +verify(Message, ExtraSecret, ExpectedMAC) -> |
| 56 | + FullSecrets = [<<Secret/binary, ExtraSecret/binary>> || Secret <- all_secrets_from_ets()], |
| 57 | + AllAlgorithms = couch_util:get_config_hash_algorithms(), |
| 58 | + verify(Message, AllAlgorithms, FullSecrets, ExpectedMAC). |
| 59 | + |
| 60 | +verify(Message, AllAlgorithms, FullSecrets, ExpectedMAC) -> |
| 61 | + Algorithms = lists:filter( |
| 62 | + fun(Algorithm) -> |
| 63 | + #{size := Size} = crypto:hash_info(Algorithm), |
| 64 | + Size == byte_size(ExpectedMAC) |
| 65 | + end, |
| 66 | + AllAlgorithms |
| 67 | + ), |
| 68 | + VerifyFun = fun({Secret, Algorithm}) -> |
| 69 | + ActualMAC = couch_util:hmac(Algorithm, Secret, Message), |
| 70 | + crypto:hash_equals(ExpectedMAC, ActualMAC) |
| 71 | + end, |
| 72 | + lists:any(VerifyFun, [{S, A} || S <- FullSecrets, A <- Algorithms]). |
| 73 | + |
| 74 | +secret_is_set() -> |
| 75 | + current_secret_from_ets() /= undefined. |
| 76 | + |
| 77 | +start_link() -> |
| 78 | + gen_server:start_link({local, ?MODULE}, ?MODULE, nil, []). |
| 79 | + |
| 80 | +init(nil) -> |
| 81 | + ets:new(?MODULE, [named_table, {read_concurrency, true}]), |
| 82 | + true = ets:insert(?MODULE, {{node(), current}, current_secret_from_config()}), |
| 83 | + update_all_secrets(), |
| 84 | + erlang:send_after(5000, self(), cache_cleanup), |
| 85 | + ok = config:listen_for_changes(?MODULE, undefined), |
| 86 | + {ok, nil, {continue, get_secrets}}. |
| 87 | + |
| 88 | +handle_call({insert, {Node, current}, Secret}, _From, State) -> |
| 89 | + case current_secret_from_ets(Node) of |
| 90 | + undefined -> |
| 91 | + ets:insert(?MODULE, [{{Node, current}, Secret}]); |
| 92 | + OldSecret -> |
| 93 | + TimeoutSecs = chttpd_util:get_chttpd_auth_config_integer("timeout", 600), |
| 94 | + ExpiresAt = erlang:system_time(second) + TimeoutSecs, |
| 95 | + ets:insert(?MODULE, [{{Node, current}, Secret}, {{Node, ExpiresAt}, OldSecret}]) |
| 96 | + end, |
| 97 | + update_all_secrets(), |
| 98 | + {reply, ok, State}; |
| 99 | +handle_call({insert, Key, Secret}, _From, State) -> |
| 100 | + ets:insert(?MODULE, {Key, Secret}), |
| 101 | + update_all_secrets(), |
| 102 | + {reply, ok, State}; |
| 103 | +handle_call(get_secrets, _From, State) -> |
| 104 | + Secrets = ets:match_object(?MODULE, {{node(), '_'}, '_'}), |
| 105 | + {reply, Secrets, State}; |
| 106 | +handle_call(flush_cache, _From, State) -> |
| 107 | + %% used from tests to prevent spurious failures due to timing |
| 108 | + MatchSpec = [{{{'_', '$1'}, '_'}, [{is_integer, '$1'}], [true]}], |
| 109 | + NumDeleted = ets:select_delete(?MODULE, MatchSpec), |
| 110 | + if |
| 111 | + NumDeleted > 0 -> update_all_secrets(); |
| 112 | + true -> ok |
| 113 | + end, |
| 114 | + {reply, NumDeleted, State}; |
| 115 | +handle_call(_Msg, _From, State) -> |
| 116 | + {noreply, State}. |
| 117 | + |
| 118 | +handle_cast(_Msg, State) -> |
| 119 | + {noreply, State}. |
| 120 | + |
| 121 | +handle_continue(get_secrets, State) -> |
| 122 | + {Replies, _BadNodes} = gen_server:multi_call(nodes(), ?MODULE, get_secrets), |
| 123 | + {_Nodes, Secrets} = lists:unzip(Replies), |
| 124 | + true = ets:insert(?MODULE, lists:flatten(Secrets)), |
| 125 | + update_all_secrets(), |
| 126 | + {noreply, State}. |
| 127 | + |
| 128 | +handle_info(restart_config_listener, State) -> |
| 129 | + ok = config:listen_for_changes(?MODULE, nil), |
| 130 | + update_current_secret(), |
| 131 | + {noreply, State}; |
| 132 | +handle_info(cache_cleanup, State) -> |
| 133 | + erlang:send_after(5000, self(), cache_cleanup), |
| 134 | + Now = os:system_time(second), |
| 135 | + MatchSpec = [{{{'_', '$1'}, '_'}, [{is_integer, '$1'}, {'<', '$1', Now}], [true]}], |
| 136 | + NumDeleted = ets:select_delete(?MODULE, MatchSpec), |
| 137 | + if |
| 138 | + NumDeleted > 0 -> update_all_secrets(); |
| 139 | + true -> ok |
| 140 | + end, |
| 141 | + {noreply, State}; |
| 142 | +handle_info(_Msg, State) -> |
| 143 | + {noreply, State}. |
| 144 | + |
| 145 | +handle_config_change("chttpd_auth", "secret", _, _, _) -> |
| 146 | + update_current_secret(), |
| 147 | + {ok, undefined}; |
| 148 | +handle_config_change("couch_httpd_auth", "secret", _, _, _) -> |
| 149 | + update_current_secret(), |
| 150 | + {ok, undefined}; |
| 151 | +handle_config_change(_, _, _, _, _) -> |
| 152 | + {ok, undefined}. |
| 153 | + |
| 154 | +handle_config_terminate(_, stop, _) -> |
| 155 | + ok; |
| 156 | +handle_config_terminate(_Server, _Reason, _State) -> |
| 157 | + erlang:send_after(3000, whereis(?MODULE), restart_config_listener). |
| 158 | + |
| 159 | +%% private functions |
| 160 | + |
| 161 | +update_current_secret() -> |
| 162 | + NewSecret = current_secret_from_config(), |
| 163 | + spawn(fun() -> |
| 164 | + gen_server:multi_call(nodes(), ?MODULE, {insert, {node(), current}, NewSecret}), |
| 165 | + gen_server:call(?MODULE, {insert, {node(), current}, NewSecret}) |
| 166 | + end). |
| 167 | + |
| 168 | +update_all_secrets() -> |
| 169 | + AllSecrets = ets:match_object(?MODULE, {{'_', '_'}, '_'}), |
| 170 | + ets:insert(?MODULE, {all_secrets, lists:usort([V || {_K, V} <- AllSecrets, is_binary(V)])}). |
| 171 | + |
| 172 | +current_secret_from_config() -> |
| 173 | + case chttpd_util:get_chttpd_auth_config("secret") of |
| 174 | + undefined -> |
| 175 | + undefined; |
| 176 | + Secret -> |
| 177 | + ?l2b(Secret) |
| 178 | + end. |
| 179 | + |
| 180 | +current_secret_from_ets() -> |
| 181 | + current_secret_from_ets(node()). |
| 182 | + |
| 183 | +current_secret_from_ets(Node) -> |
| 184 | + secret_from_ets({Node, current}). |
| 185 | + |
| 186 | +all_secrets_from_ets() -> |
| 187 | + secret_from_ets(all_secrets). |
| 188 | + |
| 189 | +secret_from_ets(Key) -> |
| 190 | + case ets:lookup(?MODULE, Key) of |
| 191 | + [{Key, Value}] -> Value; |
| 192 | + [] -> undefined |
| 193 | + end. |
0 commit comments