Permalink
Browse files

Add additional specs.

  • Loading branch information...
1 parent 5c88f54 commit 9ff6c2234cf84f4b550f26867136c6d817f3782a @cmeiklejohn cmeiklejohn committed Jun 10, 2013
View
@@ -34,21 +34,27 @@
mime_decode/1,
mime_decode_to_string/1]).
+-spec decode(iolist()) -> binary().
decode(Base64url) ->
base64:decode(urldecode(Base64url)).
+-spec decode_to_string(iolist()) -> string().
decode_to_string(Base64url) ->
base64:decode_to_string(urldecode(Base64url)).
+-spec mime_decode(iolist()) -> binary().
mime_decode(Base64url) ->
base64:mime_decode(urldecode(Base64url)).
+-spec mime_decode_to_string(iolist()) -> string().
mime_decode_to_string(Base64url) ->
base64:mime_decode_to_string(urldecode(Base64url)).
+-spec encode(binary()) -> binary().
encode(Data) ->
urlencode(base64:encode(Data)).
+-spec encode_to_string(iolist()) -> string().
encode_to_string(Data) ->
urlencode(base64:encode_to_string(Data)).
View
@@ -1,4 +1,4 @@
-%% -------------------------------------------------------------------
+%% ------------------------------------------------------------------
%%
%% Copyright (c) 2011 Basho Technologies, Inc.
%%
@@ -16,16 +16,14 @@
%% specific language governing permissions and limitations
%% under the License.
%%
-%% -------------------------------------------------------------------
+%% ------------------------------------------------------------------
%%
%% @doc Utilities needed by many pieces of the riak_control app.
%%
-module(riak_control).
--export([
- is_app_up/1,
- priv_dir/0
- ]).
+-export([is_app_up/1,
+ priv_dir/0]).
%% @doc Find out if any nodes are running a given Riak app.
-spec is_app_up(atom()) -> boolean().
View
@@ -1,4 +1,4 @@
-%% -------------------------------------------------------------------
+%% ------------------------------------------------------------------
%%
%% Copyright (c) 2011 Basho Technologies, Inc.
%%
@@ -16,7 +16,7 @@
%% specific language governing permissions and limitations
%% under the License.
%%
-%% -------------------------------------------------------------------
+%% ------------------------------------------------------------------
%%
%% @doc Application.
@@ -25,11 +25,12 @@
-behaviour(application).
%% Application callbacks
--export([start/2, stop/1]).
+-export([start/2,
+ stop/1]).
-%% ===================================================================
+%% ==================================================================
%% Application callbacks
-%% ===================================================================
+%% ==================================================================
start(_StartType, _StartArgs) ->
riak_control_sup:start_link().
@@ -1,67 +0,0 @@
-%% -------------------------------------------------------------------
-%%
-%% Copyright (c) 2011 Basho Technologies, Inc.
-%%
-%% This file is provided to you 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.
-%%
-%% -------------------------------------------------------------------
-%%
-%% @doc Helper functions for formatting.
-
--module(riak_control_formatting).
-
--export([action_result/3,
- node_ring_details/2]).
-
--include("riak_control.hrl").
-
-%% all actions return the same format
-action_result(Error={badrpc,_},Req,C) ->
- Body = mochijson2:encode({struct,[Error]}),
- {false, wrq:set_resp_body(Body, Req), C};
-action_result(Error={error,_},Req,C) ->
- Body = mochijson2:encode({struct,[Error]}),
- {false, wrq:set_resp_body(Body, Req), C};
-action_result(_,Req,C) ->
- Body = mochijson2:encode({struct,[{result,ok}]}),
- {true, wrq:set_resp_body(Body, Req), C}.
-
-%% return a proplist of details for a given index
-node_ring_details (P=#partition_info{index=Index,vnodes=Vnodes},Nodes) ->
- case lists:keyfind(P#partition_info.owner,2,Nodes) of
- #member_info{node=Node,status=Status,reachable=Reachable} ->
- Handoffs = P#partition_info.handoffs,
- VnodeStatuses = [{atom_to_list(VnodeName) ++
- "_vnode_status", vnode_status(VnodeName, VnodeStatus, Handoffs)}
- || {VnodeName, VnodeStatus} <- Vnodes],
- NodeDetails = [{index,list_to_binary(integer_to_list(Index))},
- {i,P#partition_info.partition},
- {node,Node},
- {status,Status},
- {reachable,Reachable}
- ],
- NodeDetails ++ VnodeStatuses;
- false -> []
- end.
-
-vnode_status(Service, Status, Handoffs) ->
- Vnodes = riak_core:vnode_modules(),
- Worker = proplists:get_value(Service, Vnodes),
- case proplists:get_value(Worker, Handoffs) of
- undefined ->
- Status;
- _ ->
- handoff
- end.
View
@@ -58,29 +58,38 @@ status(Ring, NVal, Unavailable) ->
status(Ring, NVal, Quorum, Unavailable).
%% @doc Return list of nodes, available partition and quorum.
+-spec status(riak_core:ring(), number(), number(), list(node())) ->
+ list({number(), number(), number()}).
status(Ring, NVal, Quorum, Unavailable) ->
Preflists = riak_core_ring:all_preflists(Ring, NVal),
{Status, _, _, _} = lists:foldr(fun fold_preflist_proplist/2,
{[], NVal, Quorum, Unavailable},
Preflists),
lists:usort(fun sort_preflist_proplist/2, Status).
-%% @doc Perform a binary conversion of the index to be returned to the
+%% @doc Perform a binary conversion of the index to be returned to th
%% user.
+-spec pretty_index(number()) -> binary().
pretty_index(Index) ->
list_to_binary(integer_to_list(Index)).
%% @doc Sort the preference-as-proplist data structures by index.
+-spec sort_preflist_proplist(list(), list()) -> boolean().
sort_preflist_proplist(A, B) ->
proplists:get_value(index, A) < proplists:get_value(index, B).
%% @doc Fold over the preference lists and generate a data structure
%% representing partitions in the cluster along with primary
%% replicas.
-fold_preflist_proplist(Preflist, {Status0, NVal, Quorum, Unavailable}) ->
+-spec fold_preflist_proplist(list(),
+ {list(), integer(), integer(), list(node())}) ->
+ {list(), integer(), integer(), list(node())}.
+fold_preflist_proplist(Preflist,
+ {Status0, NVal, Quorum, Unavailable}) ->
[{Index, _}|_] = Preflist,
All = [Node || {_, Node} <- Preflist],
+
{Down, Up} = lists:partition(fun(Node) ->
lists:member(Node, Unavailable) end, All),
@@ -30,14 +30,14 @@
is_null_origin/1,
is_protected/2]).
+-include_lib("riak_control/include/riak_control.hrl").
+
-type context() :: term() | undefined.
-type csrf_token() :: list() | undefined.
--include("riak_control.hrl").
-
-%% if riak_control has an auth scheme selected, then we enforce
-%% use of HTTPS and will redirect the user to the HTTPS version
-%% of the page requested
+%% @doc Enforce use of HTTPS only when a valid auth scheme is enabled.
+-spec scheme_is_available(wrq:reqdata(), context()) ->
+ {boolean(), wrq:reqdata(), context()}.
scheme_is_available(RD, Ctx) ->
case app_helper:get_env(riak_control, auth, none) of
none ->
@@ -51,7 +51,8 @@ scheme_is_available(RD, Ctx) ->
end
end.
-%% get the https location to redirect to (callable w/o a request)
+%% @doc Generate redirect location for https.
+-spec https_redirect_loc(list()) -> undefined | {ok, list()}.
https_redirect_loc(Path) ->
case app_helper:get_env(riak_control, enabled, false) of
true ->
@@ -65,7 +66,9 @@ https_redirect_loc(Path) ->
undefined
end.
-%% set the redirect header and where to go with it
+%% @doc Perform http redirect to ssl.
+-spec https_redirect(wrq:reqdata(), context()) ->
+ {{halt, 303}, wrq:reqdata(), context()}.
https_redirect(RD,Ctx) ->
Path=wrq:raw_path(RD),
Loc=case https_redirect_loc(Path) of
@@ -78,8 +81,8 @@ https_redirect(RD,Ctx) ->
{{halt,303},wrq:set_resp_header("Location",Loc,RD),Ctx}.
%% @doc Intended to be called from a webmachine resource's
-%% is_authorized function. The return value is a valid resource
-%% return value (`{Result, ReqData, Context}').
+%% is_authorized function. The return value is a valid resource
+%% return value (`{Result, ReqData, Context}').
%%
%% This function checks for valid authentication in the request. If
%% the authentication is valid, `true' is returned. If it is invalid,
@@ -93,6 +96,8 @@ https_redirect(RD,Ctx) ->
%%
%% - `none' :: No authentication.
%%
+-spec enforce_auth(wrq:reqdata(), context()) ->
+ {boolean(), wrq:reqdata(), context()}.
enforce_auth(RD, Ctx) ->
case app_helper:get_env(riak_control,auth,none) of
none ->
@@ -106,6 +111,9 @@ enforce_auth(RD, Ctx) ->
end
end.
+%% @doc Enforce basic auth.
+-spec enforce_basic_auth(wrq:reqdata(), context(), term(), atom()) ->
+ {boolean(), wrq:reqdata(), context()}.
enforce_basic_auth(RD, Ctx, Base64, Auth) ->
Str = base64:mime_decode_to_string(Base64),
case string:tokens(Str, ":") of
@@ -115,19 +123,25 @@ enforce_basic_auth(RD, Ctx, Base64, Auth) ->
{?ADMIN_AUTH_HEAD, RD, Ctx}
end.
+%% @doc Enforce user and password match.
+-spec enforce_user_pass(wrq:reqdata(), context(), nonempty_string(),
+ nonempty_string(), atom()) ->
+ {boolean(), wrq:reqdata(), context()}.
enforce_user_pass(RD, Ctx, User, Pass, Auth) ->
case valid_userpass(User, Pass, Auth) of
true ->
{true, RD, Ctx};
- false ->
+ _ ->
{?ADMIN_AUTH_HEAD, RD, Ctx}
end.
-%% validate the username and password with the given auth style
+%% @doc Validate username and password given a particular auth style.
+-spec valid_userpass(nonempty_string(), nonempty_string(),
+ atom()) -> boolean().
valid_userpass(_User, _Pass, none) ->
true;
valid_userpass(User, Pass, userlist) ->
- Users=app_helper:get_env(riak_control, userlist, []),
+ Users = app_helper:get_env(riak_control, userlist, []),
proplists:get_value(User, Users) == Pass;
valid_userpass(_User, _Pass, _Auth) ->
error_logger:warning_msg("Unknown auth type '~p'", [_Auth]),
@@ -169,7 +183,7 @@ is_protected(ReqData, Context) ->
is_protected_method(ReqData).
%% @doc Check if the Origin header is "null". This is useful to look for
-%% attempts at CSRF, but is not a complete answer to the problem.
+%% attempts at CSRF, but is not a complete answer to the problem.
-spec is_null_origin(wrq:reqdata()) -> boolean().
is_null_origin(ReqData) ->
case wrq:get_req_header("Origin", ReqData) of
@@ -119,7 +119,7 @@ get_file(Req) ->
%% respond to an index request
-spec to_resource(wrq:reqdata(), context()) ->
- {iolist(), wrq:reqdata(), context()}.
+ {binary(), wrq:reqdata(), context()}.
to_resource(Req,Ctx=index) ->
Token = riak_control_security:csrf_token(Req, Ctx),
{ok, Content} = index_dtl:render([{csrf_token, Token}]),

0 comments on commit 9ff6c22

Please sign in to comment.