nickva commented on code in PR #6069:
URL: https://github.com/apache/couchdb/pull/6069#discussion_r3786050658


##########
src/couch_replicator/src/couch_replicator_auth_ibm.erl:
##########
@@ -0,0 +1,673 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+% This module allows a replication source or target to use an IAM api key for 
authentication.
+%
+% Features;
+%
+% Automatic refreshing of time-limited token before expiration
+% Deduplication - only one token will be acquired for each distinct IAM api key
+%
+% Implementation details
+%
+% As api keys are sensitive, the only copy of api keys is held in a private 
ETS table
+% owned by this module's gen_server. An opaque reference is returned to 
clients (this is
+% a message authentication code where the key is a non-persisted value 
generated by the
+% gen_server)
+
+-module(couch_replicator_auth_ibm).
+
+-behaviour(couch_replicator_auth).
+-behaviour(gen_server).
+-behaviour(config_listener).
+
+-export([
+    sup_initialize/0,
+    sup_cleanup/1,
+    initialize/1,
+    update_headers/2,
+    handle_response/3,
+    cleanup/1
+]).
+
+%% gen_server callbacks
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2
+]).
+
+% config_listener callbacks
+-export([
+    handle_config_change/5,
+    handle_config_terminate/3
+]).
+
+-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").
+
+-define(EARLY_REFRESH_MS, 300000).
+-define(MIN_REFRESH_MS, 10000).
+-define(PUBLIC, couch_replicator_auth_ibm_public).
+-define(PRIVATE, couch_replicator_auth_ibm_private).
+
+-record(state, {
+    token_uri_map,
+    mac_key,
+    gun_pid,
+    gun_mref
+}).
+
+-record(public_entry, {
+    api_key_mac,
+    token
+}).
+
+-record(private_entry, {
+    api_key,
+    api_key_uuid,
+    api_key_mac,
+    gun_stream_ref,
+    gun_status_code,
+    gun_body = [],
+    refresh_ref,
+    expires_ref,
+    waiters = []
+}).
+
+%% callbacks
+
+sup_initialize() ->
+    application:ensure_all_started(gun),
+    {ok, _} = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+sup_cleanup(_) ->
+    ok = gen_server:stop(?MODULE).
+
+initialize(#httpdb{} = HttpDb) ->
+    case extract_api_key(HttpDb) of
+        {ok, APIKey} ->
+            case gen_server:call(?MODULE, {register, APIKey}) of
+                {ok, APIKeyMAC} ->
+                    {ok, HttpDb, APIKeyMAC};
+                {error, Reason} ->
+                    {error, Reason}
+            end;
+        {error, _} ->
+            ignore
+    end.
+
+update_headers(APIKeyMAC, Headers) when is_list(Headers) ->
+    case get_token(APIKeyMAC) of
+        {ok, Token} ->
+            {[{~"Authorization", <<"Bearer ", Token/binary>>} | Headers], 
APIKeyMAC};
+        {error, Reason} ->
+            couch_log:warning("Error when retrieving token: ~p", [Reason]),
+            {Headers, APIKeyMAC}
+    end.
+
+handle_response(APIKeyMAC, _StatusCode, _Headers) ->
+    {continue, APIKeyMAC}.
+
+cleanup(_APIKeyMAC) ->
+    ok.
+
+get_token(APIKeyMAC) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [#public_entry{token = Token}] when is_binary(Token) ->
+            {ok, Token};
+        [#public_entry{}] ->
+            gen_server:call(?MODULE, {get_token, APIKeyMAC}, token_timeout());
+        [] ->
+            {error, no_token}
+    end.
+
+%% gen_server callbacks.
+
+init(_) ->
+    case token_uri_map() of
+        {ok, TokenURIMap} ->
+            ?PUBLIC = ets:new(?PUBLIC, [protected, {keypos, 
#public_entry.api_key_mac}, named_table]),
+            ?PRIVATE = ets:new(?PRIVATE, [
+                private, {keypos, #private_entry.api_key_mac}, named_table
+            ]),
+            ok = config:listen_for_changes(?MODULE, nil),
+            start_gun(#state{
+                mac_key = crypto:strong_rand_bytes(32),
+                token_uri_map = TokenURIMap
+            });
+        {error, Reason} ->
+            {error, Reason}
+    end.
+
+handle_call({register, APIKey}, _From, State) ->
+    case ets:match_object(?PRIVATE, #private_entry{api_key = APIKey, _ = '_'}) 
of
+        [#private_entry{} = Entry] ->
+            {reply, {ok, Entry#private_entry.api_key_mac}, State};
+        [] ->
+            GunStreamRef = acquire_token(APIKey, State),
+            APIKeyMAC = mac(State#state.mac_key, APIKey),
+            true = ets:insert_new(?PUBLIC, #public_entry{
+                api_key_mac = APIKeyMAC
+            }),
+            true = ets:insert_new(?PRIVATE, #private_entry{
+                api_key = APIKey,
+                api_key_mac = APIKeyMAC,
+                gun_stream_ref = GunStreamRef
+            }),
+            {reply, {ok, APIKeyMAC}, State}
+    end;
+handle_call({get_token, APIKeyMAC}, From, State) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [] ->
+            {reply, {error, no_such_api_key}, State};
+        [#public_entry{token = Token}] when Token /= undefined ->
+            {reply, {ok, Token}, State};
+        [#public_entry{}] ->
+            [#private_entry{} = Entry] = ets:lookup(?PRIVATE, APIKeyMAC),
+            ets:insert(?PRIVATE, Entry#private_entry{waiters = [From | 
Entry#private_entry.waiters]}),
+            case Entry of
+                #private_entry{gun_stream_ref = GunStreamRef} = Entry when
+                    GunStreamRef /= undefined
+                ->
+                    ok;
+                #private_entry{} = Entry ->
+                    self() ! {refresh_token, APIKeyMAC}
+            end,
+            {noreply, State}
+    end;
+handle_call(_Msg, _From, State) ->
+    {reply, {error, unexpected_msg}, State}.
+
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+handle_info({refresh_token, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{gun_stream_ref = undefined} = Entry] ->
+            couch_log:notice("~p: refreshing api key ~s", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            GunStreamRef = acquire_token(Entry#private_entry.api_key, State),
+            ets:insert(?PRIVATE, Entry#private_entry{gun_stream_ref = 
GunStreamRef});
+        [#private_entry{}] ->
+            ok
+    end,
+    {noreply, State};
+handle_info({expire_api_key_entry, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{} = Entry] ->
+            couch_log:warning("~p: api key entry ~s passed expiration time", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            ets:delete(?PUBLIC, APIKeyMAC),
+            ets:delete(?PRIVATE, APIKeyMAC),
+            cancel_timer(Entry#private_entry.refresh_ref),
+            cancel_timer(Entry#private_entry.expires_ref),
+            reply_all(Entry, {error, expired_api_key_entry})
+    end,
+    {noreply, State};
+handle_info(
+    {gun_response, GunPid, GunStreamRef, nofin, StatusCode, _Headers},

Review Comment:
   Is there a gun_response with `fin` to handle? Got headers but no body, maybe 
some redirect or 500 case?



##########
src/couch_replicator/src/couch_replicator_auth_ibm.erl:
##########
@@ -0,0 +1,673 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+% This module allows a replication source or target to use an IAM api key for 
authentication.
+%
+% Features;
+%
+% Automatic refreshing of time-limited token before expiration
+% Deduplication - only one token will be acquired for each distinct IAM api key
+%
+% Implementation details
+%
+% As api keys are sensitive, the only copy of api keys is held in a private 
ETS table
+% owned by this module's gen_server. An opaque reference is returned to 
clients (this is
+% a message authentication code where the key is a non-persisted value 
generated by the
+% gen_server)
+
+-module(couch_replicator_auth_ibm).
+
+-behaviour(couch_replicator_auth).
+-behaviour(gen_server).
+-behaviour(config_listener).
+
+-export([
+    sup_initialize/0,
+    sup_cleanup/1,
+    initialize/1,
+    update_headers/2,
+    handle_response/3,
+    cleanup/1
+]).
+
+%% gen_server callbacks
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2
+]).
+
+% config_listener callbacks
+-export([
+    handle_config_change/5,
+    handle_config_terminate/3
+]).
+
+-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").
+
+-define(EARLY_REFRESH_MS, 300000).
+-define(MIN_REFRESH_MS, 10000).
+-define(PUBLIC, couch_replicator_auth_ibm_public).
+-define(PRIVATE, couch_replicator_auth_ibm_private).
+
+-record(state, {
+    token_uri_map,
+    mac_key,
+    gun_pid,
+    gun_mref
+}).
+
+-record(public_entry, {
+    api_key_mac,
+    token
+}).
+
+-record(private_entry, {
+    api_key,
+    api_key_uuid,
+    api_key_mac,
+    gun_stream_ref,
+    gun_status_code,
+    gun_body = [],
+    refresh_ref,
+    expires_ref,
+    waiters = []
+}).
+
+%% callbacks
+
+sup_initialize() ->
+    application:ensure_all_started(gun),
+    {ok, _} = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+sup_cleanup(_) ->
+    ok = gen_server:stop(?MODULE).
+
+initialize(#httpdb{} = HttpDb) ->
+    case extract_api_key(HttpDb) of
+        {ok, APIKey} ->
+            case gen_server:call(?MODULE, {register, APIKey}) of
+                {ok, APIKeyMAC} ->
+                    {ok, HttpDb, APIKeyMAC};
+                {error, Reason} ->
+                    {error, Reason}
+            end;
+        {error, _} ->
+            ignore
+    end.
+
+update_headers(APIKeyMAC, Headers) when is_list(Headers) ->
+    case get_token(APIKeyMAC) of
+        {ok, Token} ->
+            {[{~"Authorization", <<"Bearer ", Token/binary>>} | Headers], 
APIKeyMAC};
+        {error, Reason} ->
+            couch_log:warning("Error when retrieving token: ~p", [Reason]),
+            {Headers, APIKeyMAC}
+    end.
+
+handle_response(APIKeyMAC, _StatusCode, _Headers) ->
+    {continue, APIKeyMAC}.
+
+cleanup(_APIKeyMAC) ->
+    ok.
+
+get_token(APIKeyMAC) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [#public_entry{token = Token}] when is_binary(Token) ->
+            {ok, Token};
+        [#public_entry{}] ->
+            gen_server:call(?MODULE, {get_token, APIKeyMAC}, token_timeout());
+        [] ->
+            {error, no_token}
+    end.
+
+%% gen_server callbacks.
+
+init(_) ->
+    case token_uri_map() of
+        {ok, TokenURIMap} ->
+            ?PUBLIC = ets:new(?PUBLIC, [protected, {keypos, 
#public_entry.api_key_mac}, named_table]),
+            ?PRIVATE = ets:new(?PRIVATE, [
+                private, {keypos, #private_entry.api_key_mac}, named_table
+            ]),
+            ok = config:listen_for_changes(?MODULE, nil),
+            start_gun(#state{
+                mac_key = crypto:strong_rand_bytes(32),
+                token_uri_map = TokenURIMap
+            });
+        {error, Reason} ->
+            {error, Reason}
+    end.
+
+handle_call({register, APIKey}, _From, State) ->
+    case ets:match_object(?PRIVATE, #private_entry{api_key = APIKey, _ = '_'}) 
of
+        [#private_entry{} = Entry] ->
+            {reply, {ok, Entry#private_entry.api_key_mac}, State};
+        [] ->
+            GunStreamRef = acquire_token(APIKey, State),
+            APIKeyMAC = mac(State#state.mac_key, APIKey),
+            true = ets:insert_new(?PUBLIC, #public_entry{
+                api_key_mac = APIKeyMAC
+            }),
+            true = ets:insert_new(?PRIVATE, #private_entry{
+                api_key = APIKey,
+                api_key_mac = APIKeyMAC,
+                gun_stream_ref = GunStreamRef
+            }),
+            {reply, {ok, APIKeyMAC}, State}
+    end;
+handle_call({get_token, APIKeyMAC}, From, State) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [] ->
+            {reply, {error, no_such_api_key}, State};
+        [#public_entry{token = Token}] when Token /= undefined ->
+            {reply, {ok, Token}, State};
+        [#public_entry{}] ->
+            [#private_entry{} = Entry] = ets:lookup(?PRIVATE, APIKeyMAC),
+            ets:insert(?PRIVATE, Entry#private_entry{waiters = [From | 
Entry#private_entry.waiters]}),
+            case Entry of
+                #private_entry{gun_stream_ref = GunStreamRef} = Entry when
+                    GunStreamRef /= undefined
+                ->
+                    ok;
+                #private_entry{} = Entry ->
+                    self() ! {refresh_token, APIKeyMAC}
+            end,
+            {noreply, State}
+    end;
+handle_call(_Msg, _From, State) ->
+    {reply, {error, unexpected_msg}, State}.
+
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+handle_info({refresh_token, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{gun_stream_ref = undefined} = Entry] ->
+            couch_log:notice("~p: refreshing api key ~s", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            GunStreamRef = acquire_token(Entry#private_entry.api_key, State),
+            ets:insert(?PRIVATE, Entry#private_entry{gun_stream_ref = 
GunStreamRef});
+        [#private_entry{}] ->
+            ok
+    end,
+    {noreply, State};
+handle_info({expire_api_key_entry, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{} = Entry] ->
+            couch_log:warning("~p: api key entry ~s passed expiration time", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            ets:delete(?PUBLIC, APIKeyMAC),
+            ets:delete(?PRIVATE, APIKeyMAC),
+            cancel_timer(Entry#private_entry.refresh_ref),
+            cancel_timer(Entry#private_entry.expires_ref),
+            reply_all(Entry, {error, expired_api_key_entry})
+    end,
+    {noreply, State};
+handle_info(
+    {gun_response, GunPid, GunStreamRef, nofin, StatusCode, _Headers},
+    #state{gun_pid = GunPid} = State
+) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ets:insert(?PRIVATE, Entry#private_entry{gun_status_code = 
StatusCode});
+        _ ->
+            ok
+    end,
+    {noreply, State};
+handle_info({gun_data, GunPid, GunStreamRef, nofin, Data}, #state{gun_pid = 
GunPid} = State) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ets:insert(?PRIVATE, Entry#private_entry{
+                gun_body = [Data | Entry#private_entry.gun_body]
+            });
+        _ ->
+            ok
+    end,
+    {noreply, State};
+handle_info({gun_data, GunPid, GunStreamRef, fin, Data}, #state{gun_pid = 
GunPid} = State) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ResponseBody = lists:reverse([Data | 
Entry#private_entry.gun_body]),
+            case Entry#private_entry.gun_status_code of
+                200 ->
+                    case decode_iam_response(ResponseBody) of
+                        {ok, Token, ExpiresInMs} ->
+                            UUID = api_key_uuid(Token),
+                            couch_log:notice("~p: refreshed api key ~s", [
+                                ?MODULE, UUID
+                            ]),
+                            cancel_timer(Entry#private_entry.refresh_ref),
+                            cancel_timer(Entry#private_entry.expires_ref),
+                            RefreshRef = erlang:send_after(
+                                max(?MIN_REFRESH_MS, ExpiresInMs - 
?EARLY_REFRESH_MS),

Review Comment:
   In some places dealing with periodic execution (replication jobs starts) we 
usually add a bit of jitter to avoid a thundering herd problem wonder if we 
should add a bit of that here as well.



##########
src/couch_replicator/src/couch_replicator_auth_ibm.erl:
##########
@@ -0,0 +1,673 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+% This module allows a replication source or target to use an IAM api key for 
authentication.
+%
+% Features;
+%
+% Automatic refreshing of time-limited token before expiration
+% Deduplication - only one token will be acquired for each distinct IAM api key
+%
+% Implementation details
+%
+% As api keys are sensitive, the only copy of api keys is held in a private 
ETS table
+% owned by this module's gen_server. An opaque reference is returned to 
clients (this is
+% a message authentication code where the key is a non-persisted value 
generated by the
+% gen_server)
+
+-module(couch_replicator_auth_ibm).
+
+-behaviour(couch_replicator_auth).
+-behaviour(gen_server).
+-behaviour(config_listener).
+
+-export([
+    sup_initialize/0,
+    sup_cleanup/1,
+    initialize/1,
+    update_headers/2,
+    handle_response/3,
+    cleanup/1
+]).
+
+%% gen_server callbacks
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2
+]).
+
+% config_listener callbacks
+-export([
+    handle_config_change/5,
+    handle_config_terminate/3
+]).
+
+-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").
+
+-define(EARLY_REFRESH_MS, 300000).
+-define(MIN_REFRESH_MS, 10000).
+-define(PUBLIC, couch_replicator_auth_ibm_public).
+-define(PRIVATE, couch_replicator_auth_ibm_private).
+
+-record(state, {
+    token_uri_map,
+    mac_key,
+    gun_pid,
+    gun_mref
+}).
+
+-record(public_entry, {
+    api_key_mac,
+    token
+}).
+
+-record(private_entry, {
+    api_key,
+    api_key_uuid,
+    api_key_mac,
+    gun_stream_ref,
+    gun_status_code,
+    gun_body = [],
+    refresh_ref,
+    expires_ref,
+    waiters = []
+}).
+
+%% callbacks
+
+sup_initialize() ->
+    application:ensure_all_started(gun),
+    {ok, _} = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+sup_cleanup(_) ->
+    ok = gen_server:stop(?MODULE).
+
+initialize(#httpdb{} = HttpDb) ->
+    case extract_api_key(HttpDb) of
+        {ok, APIKey} ->
+            case gen_server:call(?MODULE, {register, APIKey}) of
+                {ok, APIKeyMAC} ->
+                    {ok, HttpDb, APIKeyMAC};
+                {error, Reason} ->
+                    {error, Reason}
+            end;
+        {error, _} ->
+            ignore
+    end.
+
+update_headers(APIKeyMAC, Headers) when is_list(Headers) ->
+    case get_token(APIKeyMAC) of
+        {ok, Token} ->
+            {[{~"Authorization", <<"Bearer ", Token/binary>>} | Headers], 
APIKeyMAC};
+        {error, Reason} ->
+            couch_log:warning("Error when retrieving token: ~p", [Reason]),
+            {Headers, APIKeyMAC}
+    end.
+
+handle_response(APIKeyMAC, _StatusCode, _Headers) ->
+    {continue, APIKeyMAC}.
+
+cleanup(_APIKeyMAC) ->
+    ok.
+
+get_token(APIKeyMAC) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [#public_entry{token = Token}] when is_binary(Token) ->
+            {ok, Token};
+        [#public_entry{}] ->
+            gen_server:call(?MODULE, {get_token, APIKeyMAC}, token_timeout());
+        [] ->
+            {error, no_token}
+    end.
+
+%% gen_server callbacks.
+
+init(_) ->
+    case token_uri_map() of
+        {ok, TokenURIMap} ->
+            ?PUBLIC = ets:new(?PUBLIC, [protected, {keypos, 
#public_entry.api_key_mac}, named_table]),
+            ?PRIVATE = ets:new(?PRIVATE, [
+                private, {keypos, #private_entry.api_key_mac}, named_table
+            ]),
+            ok = config:listen_for_changes(?MODULE, nil),
+            start_gun(#state{
+                mac_key = crypto:strong_rand_bytes(32),
+                token_uri_map = TokenURIMap
+            });
+        {error, Reason} ->
+            {error, Reason}
+    end.
+
+handle_call({register, APIKey}, _From, State) ->
+    case ets:match_object(?PRIVATE, #private_entry{api_key = APIKey, _ = '_'}) 
of
+        [#private_entry{} = Entry] ->
+            {reply, {ok, Entry#private_entry.api_key_mac}, State};
+        [] ->
+            GunStreamRef = acquire_token(APIKey, State),
+            APIKeyMAC = mac(State#state.mac_key, APIKey),
+            true = ets:insert_new(?PUBLIC, #public_entry{
+                api_key_mac = APIKeyMAC
+            }),
+            true = ets:insert_new(?PRIVATE, #private_entry{
+                api_key = APIKey,
+                api_key_mac = APIKeyMAC,
+                gun_stream_ref = GunStreamRef
+            }),
+            {reply, {ok, APIKeyMAC}, State}
+    end;
+handle_call({get_token, APIKeyMAC}, From, State) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [] ->
+            {reply, {error, no_such_api_key}, State};
+        [#public_entry{token = Token}] when Token /= undefined ->
+            {reply, {ok, Token}, State};
+        [#public_entry{}] ->
+            [#private_entry{} = Entry] = ets:lookup(?PRIVATE, APIKeyMAC),
+            ets:insert(?PRIVATE, Entry#private_entry{waiters = [From | 
Entry#private_entry.waiters]}),
+            case Entry of
+                #private_entry{gun_stream_ref = GunStreamRef} = Entry when
+                    GunStreamRef /= undefined
+                ->
+                    ok;
+                #private_entry{} = Entry ->
+                    self() ! {refresh_token, APIKeyMAC}
+            end,
+            {noreply, State}
+    end;
+handle_call(_Msg, _From, State) ->
+    {reply, {error, unexpected_msg}, State}.
+
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+handle_info({refresh_token, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{gun_stream_ref = undefined} = Entry] ->
+            couch_log:notice("~p: refreshing api key ~s", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            GunStreamRef = acquire_token(Entry#private_entry.api_key, State),
+            ets:insert(?PRIVATE, Entry#private_entry{gun_stream_ref = 
GunStreamRef});
+        [#private_entry{}] ->
+            ok
+    end,
+    {noreply, State};
+handle_info({expire_api_key_entry, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{} = Entry] ->
+            couch_log:warning("~p: api key entry ~s passed expiration time", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            ets:delete(?PUBLIC, APIKeyMAC),
+            ets:delete(?PRIVATE, APIKeyMAC),
+            cancel_timer(Entry#private_entry.refresh_ref),
+            cancel_timer(Entry#private_entry.expires_ref),
+            reply_all(Entry, {error, expired_api_key_entry})
+    end,
+    {noreply, State};
+handle_info(
+    {gun_response, GunPid, GunStreamRef, nofin, StatusCode, _Headers},
+    #state{gun_pid = GunPid} = State
+) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ets:insert(?PRIVATE, Entry#private_entry{gun_status_code = 
StatusCode});
+        _ ->
+            ok
+    end,
+    {noreply, State};
+handle_info({gun_data, GunPid, GunStreamRef, nofin, Data}, #state{gun_pid = 
GunPid} = State) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ets:insert(?PRIVATE, Entry#private_entry{
+                gun_body = [Data | Entry#private_entry.gun_body]
+            });
+        _ ->
+            ok
+    end,
+    {noreply, State};
+handle_info({gun_data, GunPid, GunStreamRef, fin, Data}, #state{gun_pid = 
GunPid} = State) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ResponseBody = lists:reverse([Data | 
Entry#private_entry.gun_body]),
+            case Entry#private_entry.gun_status_code of
+                200 ->
+                    case decode_iam_response(ResponseBody) of
+                        {ok, Token, ExpiresInMs} ->
+                            UUID = api_key_uuid(Token),
+                            couch_log:notice("~p: refreshed api key ~s", [
+                                ?MODULE, UUID
+                            ]),
+                            cancel_timer(Entry#private_entry.refresh_ref),
+                            cancel_timer(Entry#private_entry.expires_ref),
+                            RefreshRef = erlang:send_after(
+                                max(?MIN_REFRESH_MS, ExpiresInMs - 
?EARLY_REFRESH_MS),
+                                self(),
+                                {refresh_token, 
Entry#private_entry.api_key_mac}
+                            ),
+                            ExpiresRef = erlang:send_after(
+                                ExpiresInMs,
+                                self(),
+                                {expire_api_key_entry, 
Entry#private_entry.api_key_mac}
+                            ),
+                            true = ets:insert(?PUBLIC, #public_entry{
+                                api_key_mac = Entry#private_entry.api_key_mac,
+                                token = Token
+                            }),
+                            reply_and_reset(
+                                Entry#private_entry{
+                                    api_key_uuid = UUID,
+                                    refresh_ref = RefreshRef,
+                                    expires_ref = ExpiresRef
+                                },
+                                {ok, Token}
+                            );
+                        {error, Reason} ->
+                            couch_log:notice("~p: failed to refresh api key 
~s: ~p", [
+                                ?MODULE, Entry#private_entry.api_key_uuid, 
Reason
+                            ]),
+                            reply_and_reset(Entry, {error, Reason})
+                    end;
+                StatusCode ->
+                    ErrorMessage = extract_error_message(ResponseBody),
+                    couch_log:notice("~p: failed to refresh api key ~s: ~p", [
+                        ?MODULE, Entry#private_entry.api_key_uuid, ErrorMessage
+                    ]),
+                    case StatusCode of
+                        500 ->
+                            erlang:send_after(
+                                ?MIN_REFRESH_MS,
+                                self(),
+                                {refresh_token, 
Entry#private_entry.api_key_mac}
+                            );
+                        _ ->
+                            ok
+                    end,
+                    reply_and_reset(Entry, {error, ErrorMessage})
+            end;
+        [] ->
+            ok
+    end,
+    {noreply, State};
+handle_info(
+    {'DOWN', GunMRef, process, GunPid, Reason}, #state{gun_pid = GunPid, 
gun_mref = GunMRef} = State
+) ->
+    couch_log:warning("~p: gun process crashed for reason: ~p", [?MODULE, 
Reason]),
+    handle_info(restart_gun, State#state{gun_pid = undefined, gun_mref = 
undefined});
+handle_info({gun_up, GunPid, _Protocol}, #state{gun_pid = GunPid} = State) ->
+    {noreply, State};
+handle_info({gun_down, GunPid, _Protocol, closed, []}, #state{gun_pid = 
GunPid} = State) ->
+    {noreply, State};
+handle_info({gun_down, GunPid, _Protocol, Reason, KilledStreams}, 
#state{gun_pid = GunPid} = State) ->

Review Comment:
   Gun docs indicated there are also `gun_error` messages send and we don't 
have clauses for them



##########
src/couch_replicator/src/couch_replicator_auth_ibm.erl:
##########
@@ -0,0 +1,673 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+% This module allows a replication source or target to use an IAM api key for 
authentication.
+%
+% Features;
+%
+% Automatic refreshing of time-limited token before expiration
+% Deduplication - only one token will be acquired for each distinct IAM api key
+%
+% Implementation details
+%
+% As api keys are sensitive, the only copy of api keys is held in a private 
ETS table
+% owned by this module's gen_server. An opaque reference is returned to 
clients (this is
+% a message authentication code where the key is a non-persisted value 
generated by the
+% gen_server)
+
+-module(couch_replicator_auth_ibm).
+
+-behaviour(couch_replicator_auth).
+-behaviour(gen_server).
+-behaviour(config_listener).
+
+-export([
+    sup_initialize/0,
+    sup_cleanup/1,
+    initialize/1,
+    update_headers/2,
+    handle_response/3,
+    cleanup/1
+]).
+
+%% gen_server callbacks
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2
+]).
+
+% config_listener callbacks
+-export([
+    handle_config_change/5,
+    handle_config_terminate/3
+]).
+
+-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").
+
+-define(EARLY_REFRESH_MS, 300000).
+-define(MIN_REFRESH_MS, 10000).
+-define(PUBLIC, couch_replicator_auth_ibm_public).
+-define(PRIVATE, couch_replicator_auth_ibm_private).
+
+-record(state, {
+    token_uri_map,
+    mac_key,
+    gun_pid,
+    gun_mref
+}).
+
+-record(public_entry, {
+    api_key_mac,
+    token
+}).
+
+-record(private_entry, {
+    api_key,
+    api_key_uuid,
+    api_key_mac,
+    gun_stream_ref,
+    gun_status_code,
+    gun_body = [],
+    refresh_ref,
+    expires_ref,
+    waiters = []
+}).
+
+%% callbacks
+
+sup_initialize() ->
+    application:ensure_all_started(gun),
+    {ok, _} = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+sup_cleanup(_) ->
+    ok = gen_server:stop(?MODULE).
+
+initialize(#httpdb{} = HttpDb) ->
+    case extract_api_key(HttpDb) of
+        {ok, APIKey} ->
+            case gen_server:call(?MODULE, {register, APIKey}) of
+                {ok, APIKeyMAC} ->
+                    {ok, HttpDb, APIKeyMAC};
+                {error, Reason} ->
+                    {error, Reason}
+            end;
+        {error, _} ->
+            ignore
+    end.
+
+update_headers(APIKeyMAC, Headers) when is_list(Headers) ->
+    case get_token(APIKeyMAC) of
+        {ok, Token} ->
+            {[{~"Authorization", <<"Bearer ", Token/binary>>} | Headers], 
APIKeyMAC};
+        {error, Reason} ->
+            couch_log:warning("Error when retrieving token: ~p", [Reason]),
+            {Headers, APIKeyMAC}
+    end.
+
+handle_response(APIKeyMAC, _StatusCode, _Headers) ->
+    {continue, APIKeyMAC}.
+
+cleanup(_APIKeyMAC) ->
+    ok.
+
+get_token(APIKeyMAC) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [#public_entry{token = Token}] when is_binary(Token) ->
+            {ok, Token};
+        [#public_entry{}] ->
+            gen_server:call(?MODULE, {get_token, APIKeyMAC}, token_timeout());
+        [] ->
+            {error, no_token}
+    end.
+
+%% gen_server callbacks.
+
+init(_) ->
+    case token_uri_map() of
+        {ok, TokenURIMap} ->
+            ?PUBLIC = ets:new(?PUBLIC, [protected, {keypos, 
#public_entry.api_key_mac}, named_table]),
+            ?PRIVATE = ets:new(?PRIVATE, [
+                private, {keypos, #private_entry.api_key_mac}, named_table
+            ]),
+            ok = config:listen_for_changes(?MODULE, nil),
+            start_gun(#state{
+                mac_key = crypto:strong_rand_bytes(32),
+                token_uri_map = TokenURIMap
+            });
+        {error, Reason} ->
+            {error, Reason}
+    end.
+
+handle_call({register, APIKey}, _From, State) ->
+    case ets:match_object(?PRIVATE, #private_entry{api_key = APIKey, _ = '_'}) 
of
+        [#private_entry{} = Entry] ->
+            {reply, {ok, Entry#private_entry.api_key_mac}, State};
+        [] ->
+            GunStreamRef = acquire_token(APIKey, State),
+            APIKeyMAC = mac(State#state.mac_key, APIKey),
+            true = ets:insert_new(?PUBLIC, #public_entry{
+                api_key_mac = APIKeyMAC
+            }),
+            true = ets:insert_new(?PRIVATE, #private_entry{
+                api_key = APIKey,
+                api_key_mac = APIKeyMAC,
+                gun_stream_ref = GunStreamRef
+            }),
+            {reply, {ok, APIKeyMAC}, State}
+    end;
+handle_call({get_token, APIKeyMAC}, From, State) ->
+    case ets:lookup(?PUBLIC, APIKeyMAC) of
+        [] ->
+            {reply, {error, no_such_api_key}, State};
+        [#public_entry{token = Token}] when Token /= undefined ->
+            {reply, {ok, Token}, State};
+        [#public_entry{}] ->
+            [#private_entry{} = Entry] = ets:lookup(?PRIVATE, APIKeyMAC),
+            ets:insert(?PRIVATE, Entry#private_entry{waiters = [From | 
Entry#private_entry.waiters]}),
+            case Entry of
+                #private_entry{gun_stream_ref = GunStreamRef} = Entry when
+                    GunStreamRef /= undefined
+                ->
+                    ok;
+                #private_entry{} = Entry ->
+                    self() ! {refresh_token, APIKeyMAC}
+            end,
+            {noreply, State}
+    end;
+handle_call(_Msg, _From, State) ->
+    {reply, {error, unexpected_msg}, State}.
+
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+handle_info({refresh_token, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{gun_stream_ref = undefined} = Entry] ->
+            couch_log:notice("~p: refreshing api key ~s", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            GunStreamRef = acquire_token(Entry#private_entry.api_key, State),
+            ets:insert(?PRIVATE, Entry#private_entry{gun_stream_ref = 
GunStreamRef});
+        [#private_entry{}] ->
+            ok
+    end,
+    {noreply, State};
+handle_info({expire_api_key_entry, APIKeyMAC}, State) ->
+    case ets:lookup(?PRIVATE, APIKeyMAC) of
+        [] ->
+            ok;
+        [#private_entry{} = Entry] ->
+            couch_log:warning("~p: api key entry ~s passed expiration time", [
+                ?MODULE, Entry#private_entry.api_key_uuid
+            ]),
+            ets:delete(?PUBLIC, APIKeyMAC),
+            ets:delete(?PRIVATE, APIKeyMAC),
+            cancel_timer(Entry#private_entry.refresh_ref),
+            cancel_timer(Entry#private_entry.expires_ref),
+            reply_all(Entry, {error, expired_api_key_entry})
+    end,
+    {noreply, State};
+handle_info(
+    {gun_response, GunPid, GunStreamRef, nofin, StatusCode, _Headers},
+    #state{gun_pid = GunPid} = State
+) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ets:insert(?PRIVATE, Entry#private_entry{gun_status_code = 
StatusCode});
+        _ ->
+            ok
+    end,
+    {noreply, State};
+handle_info({gun_data, GunPid, GunStreamRef, nofin, Data}, #state{gun_pid = 
GunPid} = State) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ets:insert(?PRIVATE, Entry#private_entry{
+                gun_body = [Data | Entry#private_entry.gun_body]
+            });
+        _ ->
+            ok
+    end,
+    {noreply, State};
+handle_info({gun_data, GunPid, GunStreamRef, fin, Data}, #state{gun_pid = 
GunPid} = State) ->
+    case match_on_gun_stream_ref(GunStreamRef) of
+        [#private_entry{} = Entry] ->
+            ResponseBody = lists:reverse([Data | 
Entry#private_entry.gun_body]),
+            case Entry#private_entry.gun_status_code of
+                200 ->
+                    case decode_iam_response(ResponseBody) of
+                        {ok, Token, ExpiresInMs} ->
+                            UUID = api_key_uuid(Token),
+                            couch_log:notice("~p: refreshed api key ~s", [
+                                ?MODULE, UUID
+                            ]),
+                            cancel_timer(Entry#private_entry.refresh_ref),
+                            cancel_timer(Entry#private_entry.expires_ref),
+                            RefreshRef = erlang:send_after(
+                                max(?MIN_REFRESH_MS, ExpiresInMs - 
?EARLY_REFRESH_MS),
+                                self(),
+                                {refresh_token, 
Entry#private_entry.api_key_mac}
+                            ),
+                            ExpiresRef = erlang:send_after(
+                                ExpiresInMs,
+                                self(),
+                                {expire_api_key_entry, 
Entry#private_entry.api_key_mac}
+                            ),
+                            true = ets:insert(?PUBLIC, #public_entry{
+                                api_key_mac = Entry#private_entry.api_key_mac,
+                                token = Token
+                            }),
+                            reply_and_reset(
+                                Entry#private_entry{
+                                    api_key_uuid = UUID,
+                                    refresh_ref = RefreshRef,
+                                    expires_ref = ExpiresRef
+                                },
+                                {ok, Token}
+                            );
+                        {error, Reason} ->
+                            couch_log:notice("~p: failed to refresh api key 
~s: ~p", [
+                                ?MODULE, Entry#private_entry.api_key_uuid, 
Reason
+                            ]),
+                            reply_and_reset(Entry, {error, Reason})
+                    end;
+                StatusCode ->
+                    ErrorMessage = extract_error_message(ResponseBody),
+                    couch_log:notice("~p: failed to refresh api key ~s: ~p", [
+                        ?MODULE, Entry#private_entry.api_key_uuid, ErrorMessage
+                    ]),
+                    case StatusCode of
+                        500 ->
+                            erlang:send_after(
+                                ?MIN_REFRESH_MS,
+                                self(),
+                                {refresh_token, 
Entry#private_entry.api_key_mac}
+                            );
+                        _ ->
+                            ok
+                    end,
+                    reply_and_reset(Entry, {error, ErrorMessage})
+            end;
+        [] ->
+            ok
+    end,
+    {noreply, State};
+handle_info(
+    {'DOWN', GunMRef, process, GunPid, Reason}, #state{gun_pid = GunPid, 
gun_mref = GunMRef} = State
+) ->
+    couch_log:warning("~p: gun process crashed for reason: ~p", [?MODULE, 
Reason]),
+    handle_info(restart_gun, State#state{gun_pid = undefined, gun_mref = 
undefined});
+handle_info({gun_up, GunPid, _Protocol}, #state{gun_pid = GunPid} = State) ->
+    {noreply, State};
+handle_info({gun_down, GunPid, _Protocol, closed, []}, #state{gun_pid = 
GunPid} = State) ->
+    {noreply, State};
+handle_info({gun_down, GunPid, _Protocol, Reason, KilledStreams}, 
#state{gun_pid = GunPid} = State) ->
+    couch_log:warning("~p: gun connection down for reason: ~p", [?MODULE, 
Reason]),
+    lists:foreach(
+        fun(GunStreamRef) ->
+            case match_on_gun_stream_ref(GunStreamRef) of
+                [#private_entry{} = Entry] ->
+                    reply_and_reset(Entry, {error, Reason});
+                [] ->
+                    ok
+            end
+        end,
+        KilledStreams
+    ),
+    {noreply, State};
+handle_info(restart_gun, State) ->
+    case start_gun(State) of
+        {ok, NewState} ->
+            {noreply, NewState};
+        {error, Reason} ->
+            couch_log:warning("~p: gun restart failed for reason: ~p", 
[?MODULE, Reason]),
+            erlang:send_after(5000, self(), restart_gun),
+            {noreply, State}
+    end;
+handle_info(restart_config_listener, State) ->
+    ok = config:listen_for_changes(?MODULE, nil),
+    {noreply, State};
+handle_info(token_url_change, State) ->
+    case token_uri_map() of
+        {ok, TokenURIMap} ->
+            {noreply, State#state{token_uri_map = TokenURIMap}};

Review Comment:
   Wonder if we should clear connections or reset anything with the old url here



##########
src/couch_replicator/src/couch_replicator_auth_ibm.erl:
##########
@@ -0,0 +1,673 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+% This module allows a replication source or target to use an IAM api key for 
authentication.
+%
+% Features;
+%
+% Automatic refreshing of time-limited token before expiration
+% Deduplication - only one token will be acquired for each distinct IAM api key
+%
+% Implementation details
+%
+% As api keys are sensitive, the only copy of api keys is held in a private 
ETS table
+% owned by this module's gen_server. An opaque reference is returned to 
clients (this is
+% a message authentication code where the key is a non-persisted value 
generated by the
+% gen_server)
+
+-module(couch_replicator_auth_ibm).
+
+-behaviour(couch_replicator_auth).
+-behaviour(gen_server).
+-behaviour(config_listener).
+
+-export([
+    sup_initialize/0,
+    sup_cleanup/1,
+    initialize/1,
+    update_headers/2,
+    handle_response/3,
+    cleanup/1
+]).
+
+%% gen_server callbacks
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2
+]).
+
+% config_listener callbacks
+-export([
+    handle_config_change/5,
+    handle_config_terminate/3
+]).
+
+-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").
+
+-define(EARLY_REFRESH_MS, 300000).
+-define(MIN_REFRESH_MS, 10000).
+-define(PUBLIC, couch_replicator_auth_ibm_public).
+-define(PRIVATE, couch_replicator_auth_ibm_private).
+
+-record(state, {
+    token_uri_map,
+    mac_key,
+    gun_pid,
+    gun_mref
+}).
+
+-record(public_entry, {
+    api_key_mac,
+    token
+}).
+
+-record(private_entry, {
+    api_key,
+    api_key_uuid,
+    api_key_mac,
+    gun_stream_ref,
+    gun_status_code,
+    gun_body = [],
+    refresh_ref,
+    expires_ref,
+    waiters = []
+}).
+
+%% callbacks
+
+sup_initialize() ->
+    application:ensure_all_started(gun),
+    {ok, _} = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+sup_cleanup(_) ->
+    ok = gen_server:stop(?MODULE).
+
+initialize(#httpdb{} = HttpDb) ->
+    case extract_api_key(HttpDb) of
+        {ok, APIKey} ->
+            case gen_server:call(?MODULE, {register, APIKey}) of
+                {ok, APIKeyMAC} ->
+                    {ok, HttpDb, APIKeyMAC};
+                {error, Reason} ->
+                    {error, Reason}
+            end;
+        {error, _} ->
+            ignore
+    end.
+
+update_headers(APIKeyMAC, Headers) when is_list(Headers) ->
+    case get_token(APIKeyMAC) of
+        {ok, Token} ->
+            {[{~"Authorization", <<"Bearer ", Token/binary>>} | Headers], 
APIKeyMAC};
+        {error, Reason} ->
+            couch_log:warning("Error when retrieving token: ~p", [Reason]),
+            {Headers, APIKeyMAC}
+    end.
+
+handle_response(APIKeyMAC, _StatusCode, _Headers) ->
+    {continue, APIKeyMAC}.
+
+cleanup(_APIKeyMAC) ->
+    ok.

Review Comment:
   If we don't do cleanup does it mean we'd end up refreshing a once used API 
key forever even if there are no more jobs using it?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to