You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ei...@apache.org on 2018/08/30 12:40:06 UTC

[couchdb] branch master updated: Check if db exists in /db/_ensure_full_commit call (#1588)

This is an automated email from the ASF dual-hosted git repository.

eiri pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/master by this push:
     new b2b6988  Check if db exists in /db/_ensure_full_commit call (#1588)
b2b6988 is described below

commit b2b6988e65af7273ddd8b9223b2236c3d182c779
Author: Eric Avdey <ei...@eiri.ca>
AuthorDate: Thu Aug 30 09:40:00 2018 -0300

    Check if db exists in /db/_ensure_full_commit call (#1588)
    
    We removed a security call in `do_db_req` to avoid
    a duplicate authorization check and as a result
    there are now no db validation in noop call
    `/db/_ensure_full_commit`. This makes it always
    return a success code, even for missing databases.
    
    This fix places the security check back, directly
    in _ensure_full_commit call and adds eunit tests
    for a good measure.
---
 src/chttpd/src/chttpd_db.erl       |  7 ++++++-
 src/chttpd/test/chttpd_db_test.erl | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index d3655c3..49d7b58 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -375,8 +375,13 @@ db_req(#httpd{method='POST', path_parts=[DbName], user_ctx=Ctx}=Req, Db) ->
 db_req(#httpd{path_parts=[_DbName]}=Req, _Db) ->
     send_method_not_allowed(Req, "DELETE,GET,HEAD,POST");
 
-db_req(#httpd{method='POST',path_parts=[_,<<"_ensure_full_commit">>]}=Req, _Db) ->
+db_req(#httpd{method='POST', path_parts=[DbName, <<"_ensure_full_commit">>],
+        user_ctx=Ctx}=Req, _Db) ->
     chttpd:validate_ctype(Req, "application/json"),
+    %% use fabric call to trigger a database_does_not_exist exception
+    %% for missing databases that'd return error 404 from chttpd
+    %% get_security used to prefer shards on the same node over other nodes
+    fabric:get_security(DbName, [{user_ctx, Ctx}]),
     send_json(Req, 201, {[
         {ok, true},
         {instance_start_time, <<"0">>}
diff --git a/src/chttpd/test/chttpd_db_test.erl b/src/chttpd/test/chttpd_db_test.erl
index 6366037..2708aa0 100644
--- a/src/chttpd/test/chttpd_db_test.erl
+++ b/src/chttpd/test/chttpd_db_test.erl
@@ -61,6 +61,8 @@ all_test_() ->
                 fun setup/0, fun teardown/1,
                 [
                     fun should_return_ok_true_on_bulk_update/1,
+                    fun should_return_ok_true_on_ensure_full_commit/1,
+                    fun should_return_404_for_ensure_full_commit_on_no_db/1,
                     fun should_accept_live_as_an_alias_for_continuous/1,
                     fun should_return_404_for_delete_att_on_notadoc/1,
                     fun should_return_409_for_del_att_without_rev/1,
@@ -100,6 +102,26 @@ should_return_ok_true_on_bulk_update(Url) ->
         end).
 
 
+should_return_ok_true_on_ensure_full_commit(Url0) ->
+    ?_test(begin
+        Url = Url0 ++ "/_ensure_full_commit",
+        {ok, RC, _, Body} = test_request:post(Url, [?CONTENT_JSON, ?AUTH], []),
+        {Json} = ?JSON_DECODE(Body),
+        ?assertEqual(201, RC),
+        ?assert(couch_util:get_value(<<"ok">>, Json))
+    end).
+
+
+should_return_404_for_ensure_full_commit_on_no_db(Url0) ->
+    ?_test(begin
+        Url = Url0 ++ "-missing-db" ++ "/_ensure_full_commit",
+        {ok, RC, _, Body} = test_request:post(Url, [?CONTENT_JSON, ?AUTH], []),
+        {Json} = ?JSON_DECODE(Body),
+        ?assertEqual(404, RC),
+        ?assertEqual(<<"not_found">>, couch_util:get_value(<<"error">>, Json))
+    end).
+
+
 should_accept_live_as_an_alias_for_continuous(Url) ->
     GetLastSeq = fun(Bin) ->
         Parts = binary:split(Bin, <<"\n">>, [global]),