[GitHub] iilyak commented on a change in pull request #1176: Implement pluggable authentication and session support for replicator

2018-02-19 Thread GitBox
iilyak commented on a change in pull request #1176: Implement pluggable 
authentication and session support for replicator
URL: https://github.com/apache/couchdb/pull/1176#discussion_r169185592
 
 

 ##
 File path: src/couch_replicator/src/couch_replicator_auth_session.erl
 ##
 @@ -0,0 +1,486 @@
+% 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.
+
+-module(couch_replicator_auth_session).
+
+
+-behavior(couch_replicator_auth).
+
+
+-export([
+initialize/1,
+update_headers/2,
+handle_response/4,
+cleanup/1
+]).
+
+-export([
+init/1,
+terminate/2,
+handle_call/3,
+handle_cast/2,
+handle_info/2,
+code_change/3,
+format_status/2
+]).
+
+
+-include_lib("ibrowse/include/ibrowse.hrl").
+-include("couch_replicator_api_wrap.hrl").
+
+
+-type headers() :: [{string(), string()}].
+-type code() :: non_neg_integer().
+-type creds() :: {string() | undefined, string() | undefined}.
+
+
+% Behavior API callbacks
+
+-spec initialize(#httpdb{}) -> {ok, #httpdb{}, term()} | ignore.
+initialize(#httpdb{} = HttpDb) ->
+case remove_creds(HttpDb) of
+{ok, User, Pass, HttpDb1} ->
+case gen_server:start_link(?MODULE, [User, Pass, HttpDb1], []) of
+{ok, Pid} ->
+{ok, HttpDb1, {Pid, 0}};
+ignore ->
+ignore;
+{error, Error} ->
+{error, Error}
+end;
+{error, missing_credentials} ->
+ignore;
+{error, Error} ->
+{error, Error}
+end.
+
+
+-spec update_headers(term(), headers()) -> {headers(), term()}.
+update_headers({Pid, Epoch}, Headers) ->
+Args = {update_headers, Headers, Epoch},
+{Headers1, Epoch1} = gen_server:call(Pid, Args, infinity),
+{Headers1, {Pid, Epoch1}}.
+
+
+-spec handle_response(term(), code(), headers(), term()) ->
+{continue | retry, term()}.
+handle_response({Pid, Epoch}, Code, Headers, Body) ->
+Args =  {handle_response, Code, Headers, Body, Epoch},
+{Retry, Epoch1} = gen_server:call(Pid, Args, infinity),
+{Retry, {Pid, Epoch1}}.
+
+
+-spec cleanup(term()) -> ok.
+cleanup({Pid, _Epoch}) ->
+gen_server:call(Pid, stop, infinity).
+
+
+%% Definitions
+
+-define(MIN_UPDATE_INTERVAL, 5).
+
+
+%% gen_server state
+
+-record(state, {
+epoch = 0 :: non_neg_integer(),
+cookie :: string() | undefined,
+user :: string() | undefined,
+pass :: string() | undefined,
+httpdb_timeout :: integer(),
+httpdb_pool :: pid(),
+httpdb_ibrowse_options = [] :: list(),
+session_url :: string(),
+next_refresh = infinity :: infinity |  non_neg_integer(),
+refresh_tstamp = 0 :: non_neg_integer()
+}).
+
+
+%% gen_server functions
+
+init([User, Pass, HttpDb]) ->
+State = #state{
+user = User,
+pass = Pass,
+session_url = get_session_url(HttpDb#httpdb.url),
+httpdb_pool = HttpDb#httpdb.httpc_pool,
+httpdb_timeout = HttpDb#httpdb.timeout,
+httpdb_ibrowse_options = HttpDb#httpdb.ibrowse_options
+},
+case refresh(State) of
+{ok, UpdatedState} ->
+{ok, UpdatedState};
+{error, {session_not_supported, _, _}} ->
+ignore;
+{error, Error} ->
+{stop, Error}
+end.
+
+
+terminate(_Reason, _State) ->
+ok.
+
+
+handle_call({update_headers, Headers, _Epoch}, _From, State) ->
+case maybe_refresh(State) of
+{ok, State1} ->
+Cookie = "AuthSession=" ++ State1#state.cookie,
 
 Review comment:
   I am a bit concerned with this. Cookie value is not escaped and can be 
manipulated. I followed the code we use for `maybe_update_cookie` and it looks 
like there is a possibility to inject new headers.
   ```
   CookieKVs = 
mochiweb_cookies:parse_cookie("AuthSession=\"see\nInjected=hack\";baz; 
Version=1").
   Cookie = mochiweb_headers:get_value("AuthSession", CaseInsKVs).
   40> "AuthSession=" ++ Cookie.
   "AuthSession=see\nInjected=hack"
   ```


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] iilyak commented on a change in pull request #1176: Implement pluggable authentication and session support for replicator

2018-02-19 Thread GitBox
iilyak commented on a change in pull request #1176: Implement pluggable 
authentication and session support for replicator
URL: https://github.com/apache/couchdb/pull/1176#discussion_r169180999
 
 

 ##
 File path: src/couch_replicator/src/couch_replicator_auth_session.erl
 ##
 @@ -0,0 +1,486 @@
+% 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.
+
+-module(couch_replicator_auth_session).
+
+
+-behavior(couch_replicator_auth).
+
+
+-export([
+initialize/1,
+update_headers/2,
+handle_response/4,
+cleanup/1
+]).
+
+-export([
+init/1,
+terminate/2,
+handle_call/3,
+handle_cast/2,
+handle_info/2,
+code_change/3,
+format_status/2
+]).
+
+
+-include_lib("ibrowse/include/ibrowse.hrl").
+-include("couch_replicator_api_wrap.hrl").
+
+
+-type headers() :: [{string(), string()}].
+-type code() :: non_neg_integer().
+-type creds() :: {string() | undefined, string() | undefined}.
+
+
+% Behavior API callbacks
+
+-spec initialize(#httpdb{}) -> {ok, #httpdb{}, term()} | ignore.
+initialize(#httpdb{} = HttpDb) ->
+case remove_creds(HttpDb) of
+{ok, User, Pass, HttpDb1} ->
+case gen_server:start_link(?MODULE, [User, Pass, HttpDb1], []) of
+{ok, Pid} ->
+{ok, HttpDb1, {Pid, 0}};
+ignore ->
+ignore;
+{error, Error} ->
+{error, Error}
+end;
+{error, missing_credentials} ->
+ignore;
+{error, Error} ->
+{error, Error}
+end.
+
+
+-spec update_headers(term(), headers()) -> {headers(), term()}.
+update_headers({Pid, Epoch}, Headers) ->
+Args = {update_headers, Headers, Epoch},
+{Headers1, Epoch1} = gen_server:call(Pid, Args, infinity),
+{Headers1, {Pid, Epoch1}}.
+
+
+-spec handle_response(term(), code(), headers(), term()) ->
+{continue | retry, term()}.
+handle_response({Pid, Epoch}, Code, Headers, Body) ->
+Args =  {handle_response, Code, Headers, Body, Epoch},
+{Retry, Epoch1} = gen_server:call(Pid, Args, infinity),
+{Retry, {Pid, Epoch1}}.
+
+
+-spec cleanup(term()) -> ok.
+cleanup({Pid, _Epoch}) ->
+gen_server:call(Pid, stop, infinity).
+
+
+%% Definitions
+
+-define(MIN_UPDATE_INTERVAL, 5).
+
+
+%% gen_server state
+
+-record(state, {
+epoch = 0 :: non_neg_integer(),
+cookie :: string() | undefined,
+user :: string() | undefined,
+pass :: string() | undefined,
+httpdb_timeout :: integer(),
+httpdb_pool :: pid(),
+httpdb_ibrowse_options = [] :: list(),
+session_url :: string(),
+next_refresh = infinity :: infinity |  non_neg_integer(),
+refresh_tstamp = 0 :: non_neg_integer()
+}).
+
+
+%% gen_server functions
+
+init([User, Pass, HttpDb]) ->
+State = #state{
+user = User,
+pass = Pass,
+session_url = get_session_url(HttpDb#httpdb.url),
+httpdb_pool = HttpDb#httpdb.httpc_pool,
+httpdb_timeout = HttpDb#httpdb.timeout,
+httpdb_ibrowse_options = HttpDb#httpdb.ibrowse_options
+},
+case refresh(State) of
+{ok, UpdatedState} ->
+{ok, UpdatedState};
+{error, {session_not_supported, _, _}} ->
+ignore;
+{error, Error} ->
+{stop, Error}
+end.
+
+
+terminate(_Reason, _State) ->
+ok.
+
+
+handle_call({update_headers, Headers, _Epoch}, _From, State) ->
+case maybe_refresh(State) of
+{ok, State1} ->
+Cookie = "AuthSession=" ++ State1#state.cookie,
+Headers1 = [{"Cookie", Cookie} | Headers],
+{reply, {Headers1, State1#state.epoch}, State1};
+{error, Error} ->
+LogMsg = "~p: Stopping session auth plugin because of error ~p",
+couch_log:error(LogMsg, [?MODULE, Error]),
+{stop, Error, State}
+end;
+
+handle_call({handle_response, Code, Headers, _, Epoch}, _From, State) ->
+{Retry, State1} = process_response(Code, Headers, Epoch, State),
+{reply, {Retry, State1#state.epoch}, State1};
+
+handle_call(stop, _From, State) ->
+{stop, normal, ok, State}.
+
+
+handle_cast(Msg, State) ->
+couch_log:error("~p: Received un-expected cast ~p", [?MODULE, Msg]),
+{noreply, State}.
+
+
+handle_info(Msg, State) ->
+couch_log:error("~p : Received un-expected message ~p", [?MODULE, Msg]),
+{noreply, State}.
+
+
+code_change(_OldVsn, State, _Extra) ->
+{ok, State}.
+
+
+format_status(_Opt, [_PDict, State]) 

[GitHub] iilyak commented on a change in pull request #1176: Implement pluggable authentication and session support for replicator

2018-02-19 Thread GitBox
iilyak commented on a change in pull request #1176: Implement pluggable 
authentication and session support for replicator
URL: https://github.com/apache/couchdb/pull/1176#discussion_r169178743
 
 

 ##
 File path: src/couch_replicator/src/couch_replicator_utils.erl
 ##
 @@ -174,3 +175,93 @@ filter_state(State, States, Info) ->
 false ->
 skip
 end.
+
+
+remove_basic_auth_from_headers(Headers) ->
+Headers1 = mochiweb_headers:make(Headers),
+case mochiweb_headers:get_value("Authorization", Headers1) of
+undefined ->
+{{undefined, undefined}, Headers};
+Auth when length(Auth) =< 6 ->
 
 Review comment:
   What is the rationale for this approach. It seems rather complex. Here are 
the possible downsides of the code as it is written:
   * use of magic number 6 (I got it is the lenght("Basic ") but I have to stop 
for a minute)
   * unnecessary use of length/1
   * subsequent use of lists:split/2
   * what if we decide to use other types (Bearer or Digest or something else)
   
   If I understand the structure should be always `Authorization:  
`
   This means that the same could be written as:
   ```
   Auth ->
   case lists:splitwith(fun(X) -> X =/= $\s end, Auth) of
   {AType, " " ++ Base64} ->
   maybe_remove_basic_auth(string:to_lower(AType), Base64, Headers1)
   ```

   OR if we use binaries 
   ```
   case binary:split(?l2b(Auth), <<" ">>) of
  {AType, Base64} ->
  maybe_remove_basic_auth(string:to_lower(?b2l(AType)), Base64, 
Headers1);
   ...
   ```


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] iilyak commented on a change in pull request #1176: Implement pluggable authentication and session support for replicator

2018-02-19 Thread GitBox
iilyak commented on a change in pull request #1176: Implement pluggable 
authentication and session support for replicator
URL: https://github.com/apache/couchdb/pull/1176#discussion_r169173128
 
 

 ##
 File path: src/couch_replicator/src/couch_replicator_auth.erl
 ##
 @@ -0,0 +1,103 @@
+% 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.
+
+-module(couch_replicator_auth).
+
+
+-export([
+initialize/1,
+update_headers/2,
+handle_response/4,
+cleanup/1
+]).
+
+
+-include("couch_replicator_api_wrap.hrl").
+
+
+-type headers() :: [{string(), string()}].
+-type code() :: non_neg_integer().
+
+
+-define(PLUGINS, "couch_replicator_auth_session,couch_replicator_auth_basic").
+
+
+% Behavior API
+
+-callback initialize(#httpdb{}) -> {ok, #httpdb{}, term()} | ignore.
+
+-callback update_headers(term(), headers()) -> {headers(), term()}.
+
+-callback handle_response(term(), code(), headers(), term()) ->
+{continue | retry, term()}.
+
+-callback cleanup(term()) -> ok.
+
+
+% Main API
+
+-spec initialize(#httpdb{}) -> {ok, #httpdb{}} | {error, term()}.
+initialize(#httpdb{auth_context = nil} = HttpDb) ->
+case try_initialize(get_plugin_modules(), HttpDb) of
+{ok, Mod, HttpDb1, Context} ->
+{ok, HttpDb1#httpdb{auth_context = {Mod, Context}}};
+{error, Error} ->
+{error, Error}
+end;
+initialize(#httpdb{} = HttpDb) ->
+{ok, HttpDb}.
+
+
+-spec update_headers(#httpdb{}, headers()) -> {headers(), #httpdb{}}.
+update_headers(#httpdb{auth_context = {Mod, Context}} = HttpDb, Headers) ->
 
 Review comment:
   Is it intentional that we don't handle the case `#httpdb.auth_context == 
nil`?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] iilyak commented on a change in pull request #1176: Implement pluggable authentication and session support for replicator

2018-02-19 Thread GitBox
iilyak commented on a change in pull request #1176: Implement pluggable 
authentication and session support for replicator
URL: https://github.com/apache/couchdb/pull/1176#discussion_r169173128
 
 

 ##
 File path: src/couch_replicator/src/couch_replicator_auth.erl
 ##
 @@ -0,0 +1,103 @@
+% 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.
+
+-module(couch_replicator_auth).
+
+
+-export([
+initialize/1,
+update_headers/2,
+handle_response/4,
+cleanup/1
+]).
+
+
+-include("couch_replicator_api_wrap.hrl").
+
+
+-type headers() :: [{string(), string()}].
+-type code() :: non_neg_integer().
+
+
+-define(PLUGINS, "couch_replicator_auth_session,couch_replicator_auth_basic").
+
+
+% Behavior API
+
+-callback initialize(#httpdb{}) -> {ok, #httpdb{}, term()} | ignore.
+
+-callback update_headers(term(), headers()) -> {headers(), term()}.
+
+-callback handle_response(term(), code(), headers(), term()) ->
+{continue | retry, term()}.
+
+-callback cleanup(term()) -> ok.
+
+
+% Main API
+
+-spec initialize(#httpdb{}) -> {ok, #httpdb{}} | {error, term()}.
+initialize(#httpdb{auth_context = nil} = HttpDb) ->
+case try_initialize(get_plugin_modules(), HttpDb) of
+{ok, Mod, HttpDb1, Context} ->
+{ok, HttpDb1#httpdb{auth_context = {Mod, Context}}};
+{error, Error} ->
+{error, Error}
+end;
+initialize(#httpdb{} = HttpDb) ->
+{ok, HttpDb}.
+
+
+-spec update_headers(#httpdb{}, headers()) -> {headers(), #httpdb{}}.
+update_headers(#httpdb{auth_context = {Mod, Context}} = HttpDb, Headers) ->
 
 Review comment:
   Is it intentional that we don't handle the case `#httpdb.auth_context == nil`


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] iilyak commented on a change in pull request #1176: Implement pluggable authentication and session support for replicator

2018-02-19 Thread GitBox
iilyak commented on a change in pull request #1176: Implement pluggable 
authentication and session support for replicator
URL: https://github.com/apache/couchdb/pull/1176#discussion_r169173107
 
 

 ##
 File path: src/couch_replicator/src/couch_replicator_auth.erl
 ##
 @@ -0,0 +1,103 @@
+% 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.
+
+-module(couch_replicator_auth).
+
+
+-export([
+initialize/1,
+update_headers/2,
+handle_response/4,
+cleanup/1
+]).
+
+
+-include("couch_replicator_api_wrap.hrl").
+
+
+-type headers() :: [{string(), string()}].
+-type code() :: non_neg_integer().
+
+
+-define(PLUGINS, "couch_replicator_auth_session,couch_replicator_auth_basic").
+
+
+% Behavior API
+
+-callback initialize(#httpdb{}) -> {ok, #httpdb{}, term()} | ignore.
+
+-callback update_headers(term(), headers()) -> {headers(), term()}.
+
+-callback handle_response(term(), code(), headers(), term()) ->
+{continue | retry, term()}.
+
+-callback cleanup(term()) -> ok.
+
+
+% Main API
+
+-spec initialize(#httpdb{}) -> {ok, #httpdb{}} | {error, term()}.
+initialize(#httpdb{auth_context = nil} = HttpDb) ->
+case try_initialize(get_plugin_modules(), HttpDb) of
+{ok, Mod, HttpDb1, Context} ->
+{ok, HttpDb1#httpdb{auth_context = {Mod, Context}}};
+{error, Error} ->
+{error, Error}
+end;
+initialize(#httpdb{} = HttpDb) ->
+{ok, HttpDb}.
+
+
+-spec update_headers(#httpdb{}, headers()) -> {headers(), #httpdb{}}.
+update_headers(#httpdb{auth_context = {Mod, Context}} = HttpDb, Headers) ->
+{Headers1, Context1} = Mod:update_headers(Context, Headers),
+{Headers1, HttpDb#httpdb{auth_context = {Mod, Context1}}}.
+
+
+-spec handle_response(#httpdb{}, code(), headers(), term()) ->
+{continue | retry, term()}.
+handle_response(#httpdb{} = HttpDb, Code, Headers, Message) ->
 
 Review comment:
   Is it intentional that we don't handle the case `#httpdb.auth_context == 
nil`?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services