Skip to content

Commit

Permalink
Fix dialyzer claims
Browse files Browse the repository at this point in the history
  • Loading branch information
kuenishi committed Dec 15, 2015
1 parent c4d5dff commit 83d2dd1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 119 deletions.
2 changes: 1 addition & 1 deletion src/riak_cs_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fetch_and_cache_admin_creds(Key) ->
%% Do we count this into stats?; This is a startup query and
%% system latency is expected to be low. So get timeout can be
%% low like 10% of configuration value.
case riak_cs_pbc:get_sans_stats(MasterPbc, ?USER_BUCKET, Key,
case riak_cs_pbc:get_sans_stats(MasterPbc, ?USER_BUCKET, iolist_to_binary(Key),
[{notfound_ok, false}],
riak_cs_config:get_user_timeout() div 10) of
{ok, Obj} ->
Expand Down
57 changes: 27 additions & 30 deletions src/riak_cs_bucket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ bucket_exists(Buckets, CheckBucket) ->
-spec bucket_fun(bucket_operation(),
binary(),
bag_id(),
acl(),
[] | policy() | acl(),
string(),
{string(), string()},
{string(), pos_integer(), boolean()}) -> function().
Expand Down Expand Up @@ -682,36 +682,33 @@ serialized_bucket_op(Bucket, ACL, User, UserObj, BucketOp, StatKey, RcPid) ->
serialized_bucket_op(Bucket, BagId, ACL, User, UserObj, BucketOp, StatsKey, RcPid) ->
StartTime = os:timestamp(),
_ = riak_cs_stats:inflow(StatsKey),
case riak_cs_config:admin_creds() of
{ok, AdminCreds} ->
BucketFun = bucket_fun(BucketOp,
Bucket,
BagId,
ACL,
User?RCS_USER.key_id,
AdminCreds,
riak_cs_utils:stanchion_data()),
%% Make a call to the request serialization service.
OpResult = BucketFun(),
_ = riak_cs_stats:update_with_start(StatsKey, StartTime, OpResult),
case OpResult of
ok ->
BucketRecord = bucket_record(Bucket, BucketOp),
case update_user_buckets(User, BucketRecord) of
{ok, ignore} when BucketOp == update_acl ->
OpResult;
{ok, ignore} ->
OpResult;
{ok, UpdUser} ->
riak_cs_user:save_user(UpdUser, UserObj, RcPid)
end;
{error, {error_status, Status, _, ErrorDoc}} ->
handle_stanchion_response(Status, ErrorDoc, BucketOp, Bucket);
{error, _} ->
OpResult
{ok, AdminCreds} = riak_cs_config:admin_creds(),

BucketFun = bucket_fun(BucketOp,
Bucket,
BagId,
ACL,
User?RCS_USER.key_id,
AdminCreds,
riak_cs_utils:stanchion_data()),
%% Make a call to the request serialization service.
OpResult = BucketFun(),
_ = riak_cs_stats:update_with_start(StatsKey, StartTime, OpResult),
case OpResult of
ok ->
BucketRecord = bucket_record(Bucket, BucketOp),
case update_user_buckets(User, BucketRecord) of
{ok, ignore} when BucketOp == update_acl ->
OpResult;
{ok, ignore} ->
OpResult;
{ok, UpdUser} ->
riak_cs_user:save_user(UpdUser, UserObj, RcPid)
end;
{error, Reason1} ->
{error, Reason1}
{error, {error_status, Status, _, ErrorDoc}} ->
handle_stanchion_response(Status, ErrorDoc, BucketOp, Bucket);
{error, _} ->
OpResult
end.

%% @doc needs retry for delete op. 409 assumes
Expand Down
2 changes: 1 addition & 1 deletion src/riak_cs_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ anonymous_user_creation() ->
get_env(riak_cs, anonymous_user_creation, false).

%% @doc Return the credentials of the admin user
-spec admin_creds() -> {ok, {string(), string()}} | {error, term()}.
-spec admin_creds() -> {ok, {string()|undefined, string()|undefined}}.
admin_creds() ->
{ok, {get_env(riak_cs, admin_key, undefined),
get_env(riak_cs, admin_secret, undefined)}}.
Expand Down
40 changes: 16 additions & 24 deletions src/riak_cs_user.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ create_user(Name, Email, KeyId, Secret) ->
Error
end.

-spec create_credentialed_user({error, term()}, rcs_user()) ->
{error, term()};
({ok, {term(), term()}}, rcs_user()) ->
{ok, rcs_user()} | {error, term()}.
create_credentialed_user({error, _}=Error, _User) ->
Error;
-spec create_credentialed_user({ok, {term(), term()}}, rcs_user()) ->
{ok, rcs_user()} | {error, term()}.
create_credentialed_user({ok, AdminCreds}, User) ->
{StIp, StPort, StSSL} = riak_cs_utils:stanchion_data(),
%% Make a call to the user request serialization service.
Expand Down Expand Up @@ -118,24 +114,20 @@ handle_update_user({error, _}=Error, _User, _, _) ->
{ok, rcs_user()} | {error, term()}.
update_user(User, UserObj, RcPid) ->
{StIp, StPort, StSSL} = riak_cs_utils:stanchion_data(),
case riak_cs_config:admin_creds() of
{ok, AdminCreds} ->
Options = [{ssl, StSSL}, {auth_creds, AdminCreds}],
StatsKey = [velvet, update_user],
_ = riak_cs_stats:inflow(StatsKey),
StartTime = os:timestamp(),
%% Make a call to the user request serialization service.
Result = velvet:update_user(StIp,
StPort,
"application/json",
User?RCS_USER.key_id,
binary_to_list(riak_cs_json:to_json(User)),
Options),
_ = riak_cs_stats:update_with_start(StatsKey, StartTime, Result),
handle_update_user(Result, User, UserObj, RcPid);
{error, _}=Error ->
Error
end.
{ok, AdminCreds} = riak_cs_config:admin_creds(),
Options = [{ssl, StSSL}, {auth_creds, AdminCreds}],
StatsKey = [velvet, update_user],
_ = riak_cs_stats:inflow(StatsKey),
StartTime = os:timestamp(),
%% Make a call to the user request serialization service.
Result = velvet:update_user(StIp,
StPort,
"application/json",
User?RCS_USER.key_id,
binary_to_list(riak_cs_json:to_json(User)),
Options),
_ = riak_cs_stats:update_with_start(StatsKey, StartTime, Result),
handle_update_user(Result, User, UserObj, RcPid).

%% @doc Retrieve a Riak CS user's information based on their id string.
-spec get_user('undefined' | list(), riak_client()) -> {ok, {rcs_user(), riakc_obj:riakc_obj()}} | {error, term()}.
Expand Down
64 changes: 1 addition & 63 deletions src/velvet_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,12 @@
-include_lib("eunit/include/eunit.hrl").
-endif.

-export([authenticate/2,
request_signature/4]).
-export([request_signature/4]).

%% ===================================================================
%% Public API
%% ===================================================================

-spec authenticate(term(), [string()]) -> ok | {error, atom()}.
authenticate(RD, [KeyId, Signature]) ->
case riak_cs_config:admin_creds() of
{ok, {AdminKeyId, AdminSecret}} ->
CalculatedSignature = signature(AdminSecret, RD),
_ = lager:debug("Presented Signature: ~p~nCalculated Signature: ~p~n",
[Signature, CalculatedSignature]),
case KeyId == AdminKeyId andalso
check_auth(Signature, CalculatedSignature) of
true ->
ok;
_ ->
{error, invalid_authentication}
end;
_ ->
{error, invalid_authentication}
end.

%% Calculate a signature for inclusion in a client request.
-type http_verb() :: 'GET' | 'HEAD' | 'PUT' | 'POST' | 'DELETE'.
-spec request_signature(http_verb(),
Expand Down Expand Up @@ -91,49 +72,6 @@ request_signature(HttpVerb, RawHeaders, Path, KeyData) ->
base64:encode_to_string(
riak_cs_utils:sha_mac(KeyData, STS)).

%% ===================================================================
%% Internal functions
%% ===================================================================

signature(KeyData, RD) ->
Headers = normalize_headers(get_request_headers(RD)),
BashoHeaders = extract_basho_headers(Headers),
Resource = wrq:path(RD),
case proplists:is_defined("x-basho-date", Headers) of
true ->
Date = "\n";
false ->
Date = [wrq:get_req_header("date", RD), "\n"]
end,
case wrq:get_req_header("content-md5", RD) of
undefined ->
CMD5 = [];
CMD5 ->
ok
end,
case wrq:get_req_header("content-type", RD) of
undefined ->
ContentType = [];
ContentType ->
ok
end,
STS = [atom_to_list(wrq:method(RD)), "\n",
CMD5,
"\n",
ContentType,
"\n",
Date,
BashoHeaders,
Resource],
base64:encode_to_string(
riak_cs_utils:sha_mac(KeyData, STS)).

check_auth(PresentedSignature, CalculatedSignature) ->
PresentedSignature == CalculatedSignature.

get_request_headers(RD) ->
mochiweb_headers:to_list(wrq:req_headers(RD)).

normalize_headers(Headers) ->
FilterFun =
fun({K, V}, Acc) ->
Expand Down

0 comments on commit 83d2dd1

Please sign in to comment.