rnewson commented on code in PR #6069: URL: https://github.com/apache/couchdb/pull/6069#discussion_r3786995418
########## 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: ... yes. the problem is I've no way to know when no replication job refers to a particular apikey. 'register' gets called multiple times per replication, which is why I ripped out my refcounting (which did ref-decrementing in cleanup and deleted entries when that hit 0). perhaps I can add a 'last used' timestamp in the private table and just not renew an entry that hasn't been used for a while -- 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]
